X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Futils%2Funlit%2Funlit.c;h=52e64156a7a4a06ef35a0b6828c55bbc284b78fa;hb=d369f9643ee5a0758c5de1c08e415c20105d75b3;hp=a9645fba1e9b58c3e85465e2998a291bf0cba536;hpb=d98d90047c337e204a86d939a18c57d95f220207;p=ghc-hetmet.git diff --git a/ghc/utils/unlit/unlit.c b/ghc/utils/unlit/unlit.c index a9645fb..52e6415 100644 --- a/ghc/utils/unlit/unlit.c +++ b/ghc/utils/unlit/unlit.c @@ -41,6 +41,7 @@ */ #include +#include #include #define NULLSTR ((char *)0) @@ -49,6 +50,8 @@ #define EMPTYSCRIPT "unlit: No definitions in file (perhaps you forgot the '>'s?)" #define USAGE "usage: unlit [-q] [-n] [-c] file1 file2\n" #define CANNOTOPEN "unlit: cannot open \"%s\"\n" +#define CANNOTWRITE "unlit: error writing \"%s\"\n" +#define CANNOTWRITESTDOUT "unlit: error writing standard output\n" #define DISTINCTNAMES "unlit: input and output filenames must differ\n" #define MISSINGENDCODE "unlit: missing \\end{code}\n" @@ -77,6 +80,8 @@ static int ignore_shebang = 1; /* Leave out shebang (#!) lines */ static char* prefix_str = NULL; /* Prefix output with a string */ +static char *ofilename = NULL; + /* complain(file,line,what) * * print error message `what' for `file' at `line'. The error is suppressed @@ -95,6 +100,24 @@ int lin; { } } +writeerror() +{ + if (!strcmp(ofilename,"-")) { + fprintf(stderr, CANNOTWRITESTDOUT); + } else { + fprintf(stderr, CANNOTWRITE, ofilename); + } + exit(1); +} + +myputc(c, ostream) +char c; +FILE *ostream; { + if (putc(c,ostream) == EOF) { + writeerror(); + } +} + #define TABPOS 8 /* As getc, but does TAB expansion */ @@ -166,28 +189,28 @@ FILE *istream, *ostream; { while (c=egetc(istream), !isLineTerm(c)) ; return SHEBANG; } - putc(c, ostream); + myputc(c, ostream); c=c1; } if ( leavecpp ) { - putc(c, ostream); + myputc(c, ostream); while (c=egetc(istream), !isLineTerm(c)) - putc(c,ostream); - putc('\n',ostream); + myputc(c,ostream); + myputc('\n',ostream); return HASH; } } if (c==DEFNCHAR) { -/* putc(' ',ostream);*/ +/* myputc(' ',ostream);*/ while (c=egetc(istream), !isLineTerm(c)) - putc(c,ostream); - putc('\n',ostream); + myputc(c,ostream); + myputc('\n',ostream); return DEFN; } if (!crunchnl) - putc('\n',ostream); + myputc('\n',ostream); while (isWhitespace(c)) c=egetc(istream); @@ -250,7 +273,7 @@ FILE *ostream; { } linesread++; if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) { - putc('\n', ostream); + myputc('\n', ostream); break; } fputs(lineb, ostream); @@ -266,7 +289,7 @@ FILE *ostream; { exit(1); } linesread++; - putc('\n', ostream); + myputc('\n', ostream); if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) { break; } @@ -339,6 +362,7 @@ char **argv; { exit(1); } + ofilename=argv[1]; if (strcmp(argv[1], "-")==0) ostream = stdout; else @@ -349,13 +373,24 @@ char **argv; { /* Prefix the output with line pragmas */ if (prefix_str) { - fprintf(ostream, "#line 1 \"%s\"\n{-# LINE 1 \"%s\" #-}\n", prefix_str, prefix_str); + /* Both GHC and CPP understand the #line pragma. + * We used to throw in both a #line and a {-# LINE #-} pragma + * here, but CPP doesn't understand {-# LINE #-} so it thought + * the line numbers were off by one. We could put the {-# LINE + * #-} before the #line, but there's no point since GHC + * understands #line anyhow. --SDM 8/2003 + */ + fprintf(ostream, "#line 1 \"%s\"\n", prefix_str, prefix_str); } unlit(file, istream, ostream); - if (istream != stdin) fclose(istream); - if (ostream != stdout) fclose(ostream); + if (istream != stdin) fclose(istream); + if (ostream != stdout) { + if (fclose(ostream) == EOF) { + writeerror(); + } + } exit(errors==0 ? 0 : 1); }