Add some I/O error checking so that an error when writing the output
file will correctly result in a non-zero exit code.
Currently, if you try to compile a literate Haskell file and /tmp is
full, unlit will write an empty file and exit successfully, leading to
confusing behaviour.
#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 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"
#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* 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
/* 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 */
#define TABPOS 8
/* As getc, but does TAB expansion */
while (c=egetc(istream), !isLineTerm(c)) ;
return SHEBANG;
}
while (c=egetc(istream), !isLineTerm(c)) ;
return SHEBANG;
}
c=c1;
}
if ( leavecpp ) {
c=c1;
}
if ( leavecpp ) {
while (c=egetc(istream), !isLineTerm(c))
while (c=egetc(istream), !isLineTerm(c))
- putc(c,ostream);
- putc('\n',ostream);
+ myputc(c,ostream);
+ myputc('\n',ostream);
return HASH;
}
}
if (c==DEFNCHAR) {
return HASH;
}
}
if (c==DEFNCHAR) {
+/* myputc(' ',ostream);*/
while (c=egetc(istream), !isLineTerm(c))
while (c=egetc(istream), !isLineTerm(c))
- putc(c,ostream);
- putc('\n',ostream);
+ myputc(c,ostream);
+ myputc('\n',ostream);
return DEFN;
}
if (!crunchnl)
return DEFN;
}
if (!crunchnl)
while (isWhitespace(c))
c=egetc(istream);
while (isWhitespace(c))
c=egetc(istream);
}
linesread++;
if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) {
}
linesread++;
if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) {
break;
}
fputs(lineb, ostream);
break;
}
fputs(lineb, ostream);
if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) {
break;
}
if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) {
break;
}
if (strcmp(argv[1], "-")==0)
ostream = stdout;
else
if (strcmp(argv[1], "-")==0)
ostream = stdout;
else
unlit(file, istream, ostream);
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);
}
exit(errors==0 ? 0 : 1);
}