X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fq2c.c;h=20b245986b127b3bc8ddd703048d11def95f7682;hb=f9d47b5bba8416419cf3bcd3aa23c2d40a05fcac;hp=87dcadb1044e4a6ef18624017e162d8a2b6932d9;hpb=cb4033020c8a24d573814e6ac9192046bffdccac;p=pspp diff --git a/src/q2c.c b/src/q2c.c index 87dcadb104..20b245986b 100644 --- a/src/q2c.c +++ b/src/q2c.c @@ -43,8 +43,6 @@ #include "misc/strerror.c" #endif -#undef DEBUGGING -/*#define DEBUGGING 1*/ #include "debug-print.h" /* Max length of an input line. */ @@ -89,7 +87,7 @@ char *tokstr; char nullstr[] = ""; /* Close all open files and delete the output file, on failure. */ -void +static void finish_up (void) { if (!is_open) @@ -101,9 +99,7 @@ finish_up (void) fprintf (stderr, "%s: %s: remove: %s\n", pgmname, ofn, strerror (errno)); } -#if __GNUC__ >= 2 -void hcf (void) __attribute__ ((noreturn)); -#endif +void hcf (void) NO_RETURN; /* Terminate unsuccessfully. */ void @@ -113,10 +109,8 @@ hcf (void) exit (EXIT_FAILURE); } -#if __GNUC__ >= 2 -int fail (const char *, ...) __attribute__ ((format (printf, 1, 2))); -int error (const char *, ...) __attribute__ ((format (printf, 1, 2))); -#endif +int fail (const char *, ...) PRINTF_FORMAT (1, 2); +int error (const char *, ...) PRINTF_FORMAT (1, 2); /* Output an error message and terminate unsuccessfully. */ int @@ -153,7 +147,7 @@ error (const char *format,...) /* Allocate a block of SIZE bytes and return a pointer to its beginning. */ -void * +static void * xmalloc (size_t size) { void *vp; @@ -168,34 +162,9 @@ xmalloc (size_t size) return vp; } -/* Resize the block at PTR to size SIZE and return a pointer to the - beginning of the new block. */ -void * -xrealloc (void *ptr, size_t size) -{ - void *vp; - - if (!size) - { - if (ptr) - free (ptr); - return NULL; - } - - if (ptr) - vp = realloc (ptr, size); - else - vp = malloc (size); - - if (!vp) - fail ("xrealloc(%lu): %s", (unsigned long) size, VME); - - return vp; -} - /* Make a dynamically allocated copy of string S and return a pointer to the first character. */ -char * +static char * xstrdup (const char *s) { size_t size; @@ -214,7 +183,7 @@ xstrdup (const char *s) /* Returns a pointer to one of 8 static buffers. The buffers are used in rotation. */ -char * +static char * get_buffer (void) { static char b[8][256]; @@ -228,7 +197,7 @@ get_buffer (void) /* Copies a string to a static buffer, converting it to lowercase in the process, and returns a pointer to the static buffer. */ -char * +static char * st_lower (const char *s) { char *p, *cp; @@ -243,7 +212,7 @@ st_lower (const char *s) /* Copies a string to a static buffer, converting it to uppercase in the process, and returns a pointer to the static buffer. */ -char * +static char * st_upper (const char *s) { char *p, *cp; @@ -258,7 +227,7 @@ st_upper (const char *s) /* Returns the address of the first non-whitespace character in S, or the address of the null terminator if none. */ -char * +static char * skip_ws (const char *s) { while (isspace ((unsigned char) *s)) @@ -268,7 +237,7 @@ skip_ws (const char *s) /* Read one line from the input file into buf. Lines having special formats are handled specially. */ -int +static int get_line (void) { ln++; @@ -308,7 +277,7 @@ symbol *symtab; in the symbol table, its sequence number is returned and the symbol table is not modified. Otherwise, the symbol is added and the next available sequence number is returned. */ -int +static int add_symbol (const char *name, int unique, int value) { symbol *iter, *sym; @@ -354,7 +323,7 @@ add_symbol (const char *name, int unique, int value) /* Finds the symbol having given sequence number X within the symbol table, and returns the associated symbol structure. */ -symbol * +static symbol * find_symbol (int x) { symbol *iter; @@ -390,7 +359,7 @@ dump_token (void) #endif /* DEBUGGING */ /* Reads a token from the input file. */ -int +static int lex_get (void) { /* Skip whitespace and check for end of file. */ @@ -445,7 +414,7 @@ lex_get (void) } /* Force the current token to be an identifier token. */ -void +static void force_id (void) { if (token != T_ID) @@ -453,7 +422,7 @@ force_id (void) } /* Force the current token to be a string token. */ -void +static void force_string (void) { if (token != T_STRING) @@ -462,7 +431,7 @@ force_string (void) /* Checks whether the current token is the identifier S; if so, skips the token and returns 1; otherwise, returns 0. */ -int +static int match_id (const char *s) { if (token == T_ID && !strcmp (tokstr, s)) @@ -475,7 +444,7 @@ match_id (const char *s) /* Checks whether the current token is T. If so, skips the token and returns 1; otherwise, returns 0. */ -int +static int match_token (int t) { if (token == t) @@ -487,7 +456,7 @@ match_token (int t) } /* Force the current token to be T, and skip it. */ -void +static void skip_token (int t) { if (token != t) @@ -596,7 +565,7 @@ subcommand *def; void parse_subcommands (void); /* Parse an entire specification. */ -void +static void parse (void) { /* Get the command name and prefix. */ @@ -619,7 +588,7 @@ parse (void) /* Parses a single setting into S, given subcommand information SBC and specifier information SPEC. */ -void +static void parse_setting (setting *s, specifier *spec) { s->parent = spec; @@ -690,7 +659,7 @@ parse_setting (setting *s, specifier *spec) /* Parse a single specifier into SPEC, given subcommand information SBC. */ -void +static void parse_specifier (specifier *spec, subcommand *sbc) { spec->index = 0; @@ -739,7 +708,7 @@ parse_specifier (specifier *spec, subcommand *sbc) } /* Parse a list of specifiers for subcommand SBC. */ -void +static void parse_specifiers (subcommand *sbc) { specifier **spec = &sbc->spec; @@ -763,7 +732,7 @@ parse_specifiers (subcommand *sbc) } /* Parse a subcommand into SBC. */ -void +static void parse_subcommand (subcommand *sbc) { if (match_token ('*')) @@ -886,9 +855,7 @@ parse_subcommands (void) /* Size of the indent from the left margin. */ int indent; -#if __GNUC__ >= 2 -void dump (int, const char *, ...) __attribute__ ((format (printf, 2, 3))); -#endif +void dump (int, const char *, ...) PRINTF_FORMAT (2, 3); /* Write line FORMAT to the output file, formatted as with printf, indented `indent' characters from the left margin. If INDENTION is @@ -918,7 +885,7 @@ dump (int indention, const char *format, ...) /* Write the structure members for specifier SPEC to the output file. SBC is the including subcommand. */ -void +static void dump_specifier_vars (const specifier *spec, const subcommand *sbc) { if (spec->varname) @@ -943,7 +910,7 @@ dump_specifier_vars (const specifier *spec, const subcommand *sbc) } /* Returns 1 if string T is a PSPP keyword, 0 otherwise. */ -int +static int is_keyword (const char *t) { static const char *kw[] = @@ -962,7 +929,7 @@ is_keyword (const char *t) /* Transforms a string NAME into a valid C identifier: makes everything lowercase and maps nonalphabetic characters to underscores. Returns a pointer to a static buffer. */ -char * +static char * make_identifier (const char *name) { char *p = get_buffer (); @@ -979,7 +946,7 @@ make_identifier (const char *name) } /* Writes the struct and enum declarations for the parser. */ -void +static void dump_declarations (void) { indent = 0; @@ -1124,6 +1091,10 @@ dump_declarations (void) dump (0, "long n_%s;", st_lower (sbc->name)); break; + case SBC_DBL: + dump (0, "double n_%s;", st_lower (sbc->name)); + break; + default:; /* nothing */ } @@ -1169,7 +1140,7 @@ dump_declarations (void) /* Writes out code to initialize all the variables that need initialization for particular specifier SPEC inside subcommand SBC. */ -void +static void dump_specifier_init (const specifier *spec, const subcommand *sbc) { if (spec->varname) @@ -1203,7 +1174,7 @@ dump_specifier_init (const specifier *spec, const subcommand *sbc) } /* Write code to initialize all variables. */ -void +static void dump_vars_init (void) { /* Loop through all the subcommands. */ @@ -1276,7 +1247,7 @@ dump_vars_init (void) /* Return a pointer to a static buffer containing an expression that will match token T. */ -char * +static char * make_match (const char *t) { char *s; @@ -1304,7 +1275,7 @@ make_match (const char *t) /* Write out the parsing code for specifier SPEC within subcommand SBC. */ -void +static void dump_specifier_parse (const specifier *spec, const subcommand *sbc) { setting *s; @@ -1448,7 +1419,7 @@ dump_specifier_parse (const specifier *spec, const subcommand *sbc) } /* Write out the code to parse subcommand SBC. */ -void +static void dump_subcommand (const subcommand *sbc) { if (sbc->type == SBC_PLAIN || sbc->type == SBC_ARRAY) @@ -1519,7 +1490,7 @@ dump_subcommand (const subcommand *sbc) } else if (sbc->type == SBC_VARLIST) { - dump (1, "if (!parse_variables (NULL, &p->%sv_%s, &p->%sn_%s, " + dump (1, "if (!parse_variables (default_dict, &p->%sv_%s, &p->%sn_%s, " "PV_APPEND%s%s))", st_lower (sbc->prefix), st_lower (sbc->name), st_lower (sbc->prefix), st_lower (sbc->name), @@ -1564,11 +1535,19 @@ dump_subcommand (const subcommand *sbc) if (sbc->restriction) dump (-1, "}"); } + else if (sbc->type == SBC_DBL) + { + dump (1, "if (!lex_force_num ())"); + dump (0, "goto lossage;"); + dump (-1, "p->n_%s = lex_double ();", st_lower (sbc->name)); + dump (0, "lex_get();"); + } else if (sbc->type == SBC_INT) { dump (1, "if (!lex_force_int ())"); dump (0, "goto lossage;"); dump (-1, "p->n_%s = lex_integer ();", st_lower (sbc->name)); + dump (0, "lex_get();"); } else if (sbc->type == SBC_PINT) { @@ -1601,7 +1580,7 @@ dump_subcommand (const subcommand *sbc) } /* Write out entire parser. */ -void +static void dump_parser (void) { int f; @@ -1622,17 +1601,19 @@ dump_parser (void) if (def && (def->type == SBC_VARLIST)) { if (def->type == SBC_VARLIST) - dump (1, "if (token == T_ID && is_varname (tokid) && " - "lex_look_ahead () != '=')"); + dump (1, "if (token == T_ID " + "&& dict_lookup_var (default_dict, tokid) != NULL " + "&& lex_look_ahead () != '=')"); else { - dump (0, "if ((token == T_ID && is_varname (tokid) && " - "lex_look_ahead () != '=')"); + dump (0, "if ((token == T_ID " + "&& dict_lookup_var (default_dict, tokid) " + "&& lex_look_ahead () != '=')"); dump (1, " || token == T_ALL)"); } dump (1, "{"); dump (0, "p->sbc_%s++;", st_lower (def->name)); - dump (1, "if (!parse_variables (NULL, &p->%sv_%s, &p->%sn_%s, " + dump (1, "if (!parse_variables (default_dict, &p->%sv_%s, &p->%sn_%s, " "PV_APPEND))", st_lower (def->prefix), st_lower (def->name), st_lower (def->prefix), st_lower (def->name)); @@ -1711,7 +1692,7 @@ dump_parser (void) } /* Write the output file header. */ -void +static void dump_header (void) { time_t curtime; @@ -1723,7 +1704,7 @@ dump_header (void) loctime = localtime (&curtime); timep = asctime (loctime); timep[strlen (timep) - 1] = 0; - dump (0, "/* %s", ofn); + dump (0, "/* %s\t\t-*- mode: c; buffer-read-only: t -*-", ofn); dump (0, nullstr); dump (0, " Generated by q2c from %s on %s.", ifn, timep); dump (0, " Do not modify!"); @@ -1732,7 +1713,7 @@ dump_header (void) } /* Write out commands to free variable state. */ -void +static void dump_free (void) { subcommand *sbc; @@ -1747,7 +1728,7 @@ dump_free (void) dump (0, "static void"); dump (0, "free_%s (struct cmd_%s *p%s)", make_identifier (cmdname), - make_identifier (cmdname), used ? "" : " unused"); + make_identifier (cmdname), used ? "" : " UNUSED"); dump (1, "{"); for (sbc = subcommands; sbc; sbc = sbc->next) @@ -1759,7 +1740,7 @@ dump_free (void) /* Returns the name of a directive found on the current input line, if any, or a null pointer if none found. */ -const char * +static const char * recognize_directive (void) { static char directive[16];