int locfile_read (struct localedef_t *result, const struct charmap_t *charmap) { const char *filename = result->name; const char *repertoire_name = result->repertoire_name; int locale_mask = result->needed & ~result->avail; struct linereader *ldfile; int not_here = ALL_LOCALES; /* If no repertoire name was specified use the global one. */ if (repertoire_name == NULL) repertoire_name = repertoire_global; /* Open the locale definition file. */ ldfile = lr_open (filename, locfile_hash); if (ldfile == NULL) { if (filename != NULL && filename[0] != '/') { char *i18npath = getenv ("I18NPATH"); if (i18npath != NULL && *i18npath != '\0') { const size_t pathlen = strlen (i18npath); char i18npathbuf[pathlen + 1]; char path[strlen (filename) + 1 + pathlen + sizeof ("/locales/") - 1]; char *next; i18npath = memcpy (i18npathbuf, i18npath, pathlen + 1); while (ldfile == NULL && (next = strsep (&i18npath, ":")) != NULL) { stpcpy (stpcpy (stpcpy (path, next), "/locales/"), filename); ldfile = lr_open (path, locfile_hash); if (ldfile == NULL) { stpcpy (stpcpy (path, next), filename); ldfile = lr_open (path, locfile_hash); } } } /* Test in the default directory. */ if (ldfile == NULL) { char path[strlen (filename) + 1 + sizeof (LOCSRCDIR)]; stpcpy (stpcpy (stpcpy (path, LOCSRCDIR), "/"), filename); ldfile = lr_open (path, locfile_hash); } } if (ldfile == NULL) return 1; } /* Parse locale definition file and store result in RESULT. */ while (1) { struct token *now = lr_token (ldfile, charmap, NULL, NULL, verbose); enum token_t nowtok = now->tok; struct token *arg; if (nowtok == tok_eof) break; if (nowtok == tok_eol) /* Ignore empty lines. */ continue; switch (nowtok) { case tok_escape_char: case tok_comment_char: /* We need an argument. */ arg = lr_token (ldfile, charmap, NULL, NULL, verbose); if (arg->tok != tok_ident) { SYNTAX_ERROR (_("bad argument")); continue; } if (arg->val.str.lenmb != 1) { lr_error (ldfile, _("\ argument to `%s' must be a single character"), nowtok == tok_escape_char ? "escape_char" : "comment_char"); lr_ignore_rest (ldfile, 0); continue; } if (nowtok == tok_escape_char) ldfile->escape_char = *arg->val.str.startmb; else ldfile->comment_char = *arg->val.str.startmb; break; case tok_repertoiremap: /* We need an argument. */ arg = lr_token (ldfile, charmap, NULL, NULL, verbose); if (arg->tok != tok_ident) { SYNTAX_ERROR (_("bad argument")); continue; } if (repertoire_name == NULL) { char *newp = alloca (arg->val.str.lenmb + 1); *((char *) mempcpy (newp, arg->val.str.startmb, arg->val.str.lenmb)) = '\0'; repertoire_name = newp; } break; case tok_lc_ctype: ctype_read (ldfile, result, charmap, repertoire_name, (locale_mask & CTYPE_LOCALE) == 0); result->avail |= locale_mask & CTYPE_LOCALE; not_here ^= CTYPE_LOCALE; continue; case tok_lc_collate: collate_read (ldfile, result, charmap, repertoire_name, (locale_mask & COLLATE_LOCALE) == 0); result->avail |= locale_mask & COLLATE_LOCALE; not_here ^= COLLATE_LOCALE; continue; case tok_lc_monetary: monetary_read (ldfile, result, charmap, repertoire_name, (locale_mask & MONETARY_LOCALE) == 0); result->avail |= locale_mask & MONETARY_LOCALE; not_here ^= MONETARY_LOCALE; continue; case tok_lc_numeric: numeric_read (ldfile, result, charmap, repertoire_name, (locale_mask & NUMERIC_LOCALE) == 0); result->avail |= locale_mask & NUMERIC_LOCALE; not_here ^= NUMERIC_LOCALE; continue; case tok_lc_time: time_read (ldfile, result, charmap, repertoire_name, (locale_mask & TIME_LOCALE) == 0); result->avail |= locale_mask & TIME_LOCALE; not_here ^= TIME_LOCALE; continue; case tok_lc_messages: messages_read (ldfile, result, charmap, repertoire_name, (locale_mask & MESSAGES_LOCALE) == 0); result->avail |= locale_mask & MESSAGES_LOCALE; not_here ^= MESSAGES_LOCALE; continue; case tok_lc_paper: paper_read (ldfile, result, charmap, repertoire_name, (locale_mask & PAPER_LOCALE) == 0); result->avail |= locale_mask & PAPER_LOCALE; not_here ^= PAPER_LOCALE; continue; case tok_lc_name: name_read (ldfile, result, charmap, repertoire_name, (locale_mask & NAME_LOCALE) == 0); result->avail |= locale_mask & NAME_LOCALE; not_here ^= NAME_LOCALE; continue; case tok_lc_address: address_read (ldfile, result, charmap, repertoire_name, (locale_mask & ADDRESS_LOCALE) == 0); result->avail |= locale_mask & ADDRESS_LOCALE; not_here ^= ADDRESS_LOCALE; continue; case tok_lc_telephone: telephone_read (ldfile, result, charmap, repertoire_name, (locale_mask & TELEPHONE_LOCALE) == 0); result->avail |= locale_mask & TELEPHONE_LOCALE; not_here ^= TELEPHONE_LOCALE; continue; case tok_lc_measurement: measurement_read (ldfile, result, charmap, repertoire_name, (locale_mask & MEASUREMENT_LOCALE) == 0); result->avail |= locale_mask & MEASUREMENT_LOCALE; not_here ^= MEASUREMENT_LOCALE; continue; case tok_lc_identification: identification_read (ldfile, result, charmap, repertoire_name, (locale_mask & IDENTIFICATION_LOCALE) == 0); result->avail |= locale_mask & IDENTIFICATION_LOCALE; not_here ^= IDENTIFICATION_LOCALE; continue; default: SYNTAX_ERROR (_("\ syntax error: not inside a locale definition section")); continue; } /* The rest of the line must be empty. */ lr_ignore_rest (ldfile, 1); }
int main(int argc, char **argv) { char *inputFilename = NULL; int inputFiles = 0; char *matFilename = NULL; busAssignment_t *busAssignment = busAssignment_create(); int bus = -1; signalFormat_t signalFormat = signalFormat_Name; measurement_t *measurement; int ret = 1; sint32 timeResolution = 10000; parserFunction_t parserFunction = NULL; program_name = argv[0]; /* parse arguments */ while (1) { static struct option long_options[] = { /* These options set a flag. */ {"verbose", no_argument, &verbose_flag, 1}, {"brief", no_argument, &verbose_flag, 0}, {"debug", no_argument, &debug_flag, 1}, /* These options don't set a flag. We distinguish them by their indices. */ {"asc", required_argument, 0, 'a'}, {"bus", required_argument, 0, 'b'}, #ifdef HAVE_CLGREADER_H {"clg", required_argument, 0, 'c'}, #endif {"dbc", required_argument, 0, 'd'}, {"format", required_argument, 0, 'f'}, {"mat", required_argument, 0, 'm'}, {"timeres", required_argument, 0, 't'}, {"vsb", required_argument, 0, 'v'}, {"help", no_argument, NULL, 'h'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; int c; c = getopt_long (argc, argv, "a:b:c:d:f:m:t:v:", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 0: break; case 'a': inputFilename = optarg; parserFunction =ascReader_processFile; inputFiles++; break; #ifdef HAVE_CLGREADER_H case 'c': inputFilename = optarg; parserFunction =clgReader_processFile; inputFiles++; break; #endif case 'b': bus = atoi(optarg); break; case 'd': if(verbose_flag) { if(bus == -1) { fprintf(stderr, "Assigning DBC file %s to all busses\n", optarg); } else { fprintf(stderr, "Assigning DBC file %s to bus %d\n", optarg, bus); } } busAssignment_associate(busAssignment, bus, optarg); /* reset bus specification */ bus = -1; break; case 'm': matFilename = optarg; break; case 'f': if(!strcmp(optarg, "n")) { signalFormat = signalFormat_Name; } else if(!strcmp(optarg, "mn")) { signalFormat = signalFormat_Message | signalFormat_Name; } else if(!strcmp(optarg, "dmn")) { signalFormat = signalFormat_Database | signalFormat_Message | signalFormat_Name; } else { fprintf(stderr, "error: format must be 's', 'ms', or 'dms'\n"); usage_error(); } matFilename = optarg; break; case 't': timeResolution = atoi(optarg); break; case 'v': inputFilename = optarg; parserFunction = vsbReader_processFile; inputFiles++; break; case 'h': help(); exit(0); break; case '?': /* getopt_long already printed an error message. */ usage_error(); break; default: fprintf(stderr, "error: unknown option %c\n", c); busAssignment_free(busAssignment); usage_error(); } } #ifdef YYDEBUG if(debug_flag) { extern int yydebug; yydebug=1; } #endif /* diagnose options */ if(inputFiles != 1) { fprintf(stderr, "error: please specify exactly one input file\n"); busAssignment_free(busAssignment); usage_error(); } if(matFilename == NULL) { fprintf(stderr, "error: MAT output filename not specified\n"); busAssignment_free(busAssignment); usage_error(); } /* parse DBC files */ busAssignment_parseDBC(busAssignment); /* parse input file */ if(verbose_flag) { if(inputFilename != NULL) { fprintf(stderr, "Parsing input file %s\n", inputFilename?inputFilename:"<stdin>"); } } measurement = measurement_read(busAssignment, inputFilename, signalFormat, timeResolution, parserFunction); if(measurement != NULL) { /* write MAT file */ if(verbose_flag) { fprintf(stderr, "Writing MAT file %s\n", matFilename); } matWrite(measurement, matFilename); /* free memory */ measurement_free(measurement); } ret = 0; usage_error: busAssignment_free(busAssignment); return ret; }