From a705278de7c661af9b78d956af25e13055cba864 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 13 Jul 2014 17:45:19 -0700 Subject: [PATCH] * make-docfile.c: Simplify a bit, to simplify further refactoring. (outfile): Remove static var. All uses changed to use stdout, since it's always stdout anyway. While we're at it, prefer putchar/puts/fputs to printf when there are no format strings. (main): Use freopen rather than fopen, so that stdout is reused. Move O_BINARY stuff after the freopen, so it affects the reopened file. (write_c_args): Omit first arg, since it's always stdout now. All uses changed. --- lib-src/ChangeLog | 12 ++++ lib-src/make-docfile.c | 123 +++++++++++++++++++---------------------- 2 files changed, 70 insertions(+), 65 deletions(-) diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index 3a7d128b905..0e0dc51f361 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog @@ -1,3 +1,15 @@ +2014-07-13 Paul Eggert + + * make-docfile.c: Simplify a bit, to simplify further refactoring. + (outfile): Remove static var. All uses changed to use stdout, + since it's always stdout anyway. While we're at it, prefer + putchar/puts/fputs to printf when there are no format strings. + (main): Use freopen rather than fopen, so that stdout is reused. + Move O_BINARY stuff after the freopen, so it affects the + reopened file. + (write_c_args): Omit first arg, since it's always stdout now. + All uses changed. + 2014-07-12 Paul Eggert * etags.c (Lisp_functions): Also record cl-defun etc. (Bug#17965) diff --git a/lib-src/make-docfile.c b/lib-src/make-docfile.c index 15ffa138b51..6692a0a6450 100644 --- a/lib-src/make-docfile.c +++ b/lib-src/make-docfile.c @@ -73,9 +73,6 @@ static void write_globals (void); #include -/* Stdio stream for output to the DOC file. */ -FILE *outfile; - /* Name this program was invoked with. */ char *progname; @@ -135,33 +132,24 @@ main (int argc, char **argv) progname = argv[0]; - outfile = stdout; - - /* Don't put CRs in the DOC file. */ -#ifdef MSDOS - _fmode = O_BINARY; -#if 0 /* Suspicion is that this causes hanging. - So instead we require people to use -o on MSDOS. */ - (stdout)->_flag &= ~_IOTEXT; - _setmode (fileno (stdout), O_BINARY); -#endif - outfile = 0; -#endif /* MSDOS */ -#ifdef WINDOWSNT - _fmode = O_BINARY; - _setmode (fileno (stdout), O_BINARY); -#endif /* WINDOWSNT */ - /* If first two args are -o FILE, output to FILE. */ i = 1; if (argc > i + 1 && !strcmp (argv[i], "-o")) { - outfile = fopen (argv[i + 1], "w"); + if (! freopen (argv[i + 1], "w", stdout)) + { + perror (argv[i + 1]); + return EXIT_FAILURE; + } i += 2; } if (argc > i + 1 && !strcmp (argv[i], "-a")) { - outfile = fopen (argv[i + 1], "a"); + if (! freopen (argv[i + 1], "a", stdout)) + { + perror (argv[i + 1]); + return EXIT_FAILURE; + } i += 2; } if (argc > i + 1 && !strcmp (argv[i], "-d")) @@ -179,8 +167,19 @@ main (int argc, char **argv) ++i; } - if (outfile == 0) - fatal ("No output file specified", ""); + /* Don't put CRs in the output file. */ +#ifdef MSDOS + _fmode = O_BINARY; +#if 0 /* Suspicion is that this causes hanging. + So instead we require people to use -o on MSDOS. */ + (stdout)->_flag &= ~_IOTEXT; + _setmode (fileno (stdout), O_BINARY); +#endif +#endif /* MSDOS */ +#ifdef WINDOWSNT + _fmode = O_BINARY; + _setmode (fileno (stdout), O_BINARY); +#endif /* WINDOWSNT */ if (generate_globals) start_globals (); @@ -215,13 +214,11 @@ put_filename (char *filename) filename = tmp + 1; } - putc (037, outfile); - putc ('S', outfile); - fprintf (outfile, "%s\n", filename); + printf ("\037S%s\n", filename); } -/* Read file FILENAME and output its doc strings to outfile. */ -/* Return 1 if file is not found, 0 if it is found. */ +/* Read file FILENAME and output its doc strings to stdout. + Return 1 if file is not found, 0 if it is found. */ static int scan_file (char *filename) @@ -242,9 +239,9 @@ scan_file (char *filename) static void start_globals (void) { - fprintf (outfile, "/* This file was auto-generated by make-docfile. */\n"); - fprintf (outfile, "/* DO NOT EDIT. */\n"); - fprintf (outfile, "struct emacs_globals {\n"); + puts ("/* This file was auto-generated by make-docfile. */"); + puts ("/* DO NOT EDIT. */"); + puts ("struct emacs_globals {"); } static char input_buffer[128]; @@ -373,7 +370,7 @@ scan_keyword_or_put_char (int ch, struct rcsoc_state *state) /* Skip a C string or C-style comment from INFILE, and return the character that follows. COMMENT non-zero means skip a comment. If - PRINTFLAG is positive, output string contents to outfile. If it is + PRINTFLAG is positive, output string contents to stdout. If it is negative, store contents in buf. Convert escape sequences \n and \t to newline and tab; discard \ followed by newline. If SAW_USAGE is non-zero, then any occurrences of the string `usage:' @@ -388,7 +385,7 @@ read_c_string_or_comment (FILE *infile, int printflag, int comment, int *saw_usa state.in_file = infile; state.buf_ptr = (printflag < 0 ? input_buffer : 0); - state.out_file = (printflag > 0 ? outfile : 0); + state.out_file = (printflag > 0 ? stdout : 0); state.pending_spaces = 0; state.pending_newlines = 0; state.keyword = (saw_usage ? "usage:" : 0); @@ -465,18 +462,18 @@ read_c_string_or_comment (FILE *infile, int printflag, int comment, int *saw_usa -/* Write to file OUT the argument names of function FUNC, whose text is in BUF. +/* Write to stdout the argument names of function FUNC, whose text is in BUF. MINARGS and MAXARGS are the minimum and maximum number of arguments. */ static void -write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs) +write_c_args (char *func, char *buf, int minargs, int maxargs) { register char *p; int in_ident = 0; char *ident_start IF_LINT (= NULL); size_t ident_length = 0; - fprintf (out, "(fn"); + fputs ("(fn", stdout); if (*buf == '(') ++buf; @@ -517,10 +514,10 @@ write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs) if (strncmp (ident_start, "void", ident_length) == 0) continue; - putc (' ', out); + putchar (' '); if (minargs == 0 && maxargs > 0) - fprintf (out, "&optional "); + fputs ("&optional ", stdout); minargs--; maxargs--; @@ -528,7 +525,7 @@ write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs) /* In C code, `default' is a reserved word, so we spell it `defalt'; demangle that here. */ if (ident_length == 6 && memcmp (ident_start, "defalt", 6) == 0) - fprintf (out, "DEFAULT"); + fputs ("DEFAULT", stdout); else while (ident_length-- > 0) { @@ -539,12 +536,12 @@ write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs) else if (c == '_') /* Print underscore as hyphen. */ c = '-'; - putc (c, out); + putchar (c); } } } - putc (')', out); + putchar (')'); } /* The types of globals. These are sorted roughly in decreasing alignment @@ -613,8 +610,8 @@ compare_globals (const void *a, const void *b) static void close_emacs_globals (void) { - fprintf (outfile, "};\n"); - fprintf (outfile, "extern struct emacs_globals globals;\n"); + puts ("};"); + puts ("extern struct emacs_globals globals;"); } static void @@ -641,7 +638,7 @@ write_globals (void) if (!seen_defun) { close_emacs_globals (); - fprintf (outfile, "\n"); + putchar ('\n'); seen_defun = 1; } break; @@ -651,9 +648,9 @@ write_globals (void) if (type) { - fprintf (outfile, " %s f_%s;\n", type, globals[i].name); - fprintf (outfile, "#define %s globals.f_%s\n", - globals[i].name, globals[i].name); + printf (" %s f_%s;\n", type, globals[i].name); + printf ("#define %s globals.f_%s\n", + globals[i].name, globals[i].name); } else { @@ -664,16 +661,16 @@ write_globals (void) || strcmp (globals[i].name, "Fkill_emacs") == 0 || strcmp (globals[i].name, "Fexit_recursive_edit") == 0 || strcmp (globals[i].name, "Fabort_recursive_edit") == 0) - fprintf (outfile, "_Noreturn "); + fputs ("_Noreturn ", stdout); - fprintf (outfile, "EXFUN (%s, ", globals[i].name); + printf ("EXFUN (%s, ", globals[i].name); if (globals[i].value == -1) - fprintf (outfile, "MANY"); + fputs ("MANY", stdout); else if (globals[i].value == -2) - fprintf (outfile, "UNEVALLED"); + fputs ("UNEVALLED", stdout); else - fprintf (outfile, "%d", globals[i].value); - fprintf (outfile, ")"); + printf ("%d", globals[i].value); + putchar (')'); /* It would be nice to have a cleaner way to deal with these special hacks, too. */ @@ -681,9 +678,9 @@ write_globals (void) || strcmp (globals[i].name, "Ftool_bar_height") == 0 || strcmp (globals[i].name, "Fmax_char") == 0 || strcmp (globals[i].name, "Fidentity") == 0) - fprintf (outfile, " ATTRIBUTE_CONST"); + fputs (" ATTRIBUTE_CONST", stdout); - fprintf (outfile, ";\n"); + puts (";"); } while (i + 1 < num_globals @@ -952,9 +949,7 @@ scan_c_file (char *filename, const char *mode) int comment = c != '"'; int saw_usage; - putc (037, outfile); - putc (defvarflag ? 'V' : 'F', outfile); - fprintf (outfile, "%s\n", input_buffer); + printf ("\037%c%s\n", defvarflag ? 'V' : 'F', input_buffer); if (comment) getc (infile); /* Skip past `*'. */ @@ -996,8 +991,8 @@ scan_c_file (char *filename, const char *mode) while (c != ')'); *p = '\0'; /* Output them. */ - fprintf (outfile, "\n\n"); - write_c_args (outfile, input_buffer, argbuf, minargs, maxargs); + fputs ("\n\n", stdout); + write_c_args (input_buffer, argbuf, minargs, maxargs); } else if (defunflag && maxargs == -1 && !saw_usage) /* The DOC should provide the usage form. */ @@ -1433,12 +1428,10 @@ scan_lisp_file (const char *filename, const char *mode) In the latter case, the opening quote (and leading backslash-newline) have already been read. */ - putc (037, outfile); - putc (type, outfile); - fprintf (outfile, "%s\n", buffer); + printf ("\037%c%s\n", type, buffer); if (saved_string) { - fputs (saved_string, outfile); + fputs (saved_string, stdout); /* Don't use one dynamic doc string twice. */ free (saved_string); saved_string = 0; -- 2.39.2