*/
#include <stdio.h>
+#include <stdlib.h>
#include <ctype.h>
#define NULLSTR ((char *)0)
#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"
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
}
}
+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 */
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);
}
linesread++;
if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) {
- putc('\n', ostream);
+ myputc('\n', ostream);
break;
}
fputs(lineb, ostream);
exit(1);
}
linesread++;
- putc('\n', ostream);
+ myputc('\n', ostream);
if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) {
break;
}
exit(1);
}
+ ofilename=argv[1];
if (strcmp(argv[1], "-")==0)
ostream = stdout;
else
/* 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);
}