From 0136906c9e69b02cd1ffe2704fa5d737d8c4cfaf Mon Sep 17 00:00:00 2001 From: Zejun Wu Date: Tue, 11 Dec 2018 13:18:03 -0500 Subject: Fix uninformative hp2ps error when the cmdline contains double quotes Reapply D5346 with fix incompatible shell quoting in tests. It seems like `$'string'` is not recognized under all test environments, so let's avoid it in tests. Test Plan: ``` hp2ps: "T15904".hp, line 2: integer must follow identifier ``` use new ghc and hp2ps to profile a simple program. Reviewers: simonmar, bgamari, erikd, tdammers Reviewed By: bgamari Subscribers: tdammers, carter, rwbarton GHC Trac Issues: #15904 Differential Revision: https://phabricator.haskell.org/D5388 --- utils/hp2ps/HpFile.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'utils/hp2ps') diff --git a/utils/hp2ps/HpFile.c b/utils/hp2ps/HpFile.c index a64a74a254..bc172e5748 100644 --- a/utils/hp2ps/HpFile.c +++ b/utils/hp2ps/HpFile.c @@ -398,45 +398,42 @@ GetIdent(FILE *infp) /* - * Read a sequence of characters that make up a string and - * assign the result to "thestring". + * Read a sequence of characters that make up a string and assign the result to + * "thestring". A string is surrounded by double quotes, with a double quote + * itself escaped as two contiguous double quotes. */ void GetString(FILE *infp) { - unsigned int i; - char *stringbuffer; - size_t stringbuffersize; - ASSERT(ch == '\"'); - stringbuffersize = 5000; - stringbuffer = xmalloc(stringbuffersize); + size_t stringbuffersize = 5000; + char *stringbuffer = xmalloc(stringbuffersize); ch = getc(infp); /* skip the '\"' that begins the string */ - i = 0; - while (ch != '\"') { + for (size_t i = 0; ; ++i) { if (ch == EOF) { - Error("%s, line %d: EOF when expecting \"", hpfile, linenum, ch); + Error("%s, line %d: EOF when expecting \"", hpfile, linenum, ch); } - else if (i == stringbuffersize - 1) { - stringbuffersize = 2 * stringbuffersize; + if (i == stringbuffersize) { + stringbuffersize *= 2; stringbuffer = xrealloc(stringbuffer, stringbuffersize); } - stringbuffer[ i++ ] = ch; + if (ch == '\"') { + ch = getc(infp); + if (ch != '\"') { + stringbuffer[i] = '\0'; + break; + } + } + stringbuffer[i] = ch; ch = getc(infp); } - stringbuffer[i] = '\0'; thestring = copystring(stringbuffer); - free(stringbuffer); - - ASSERT(ch == '\"'); - - ch = getc(infp); /* skip the '\"' that terminates the string */ } boolish -- cgit v1.2.1