/* -*- c -*- */ /* GlossTeX, a tool for the automatic preparation of glossaries. Copyright (C) 1997 Volkan Yavuz This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Volkan Yavuz, yavuzv@rumms.uni-mannheim.de */ /* $Id: database.c,v 1.45 1997/12/13 16:06:54 volkan Exp $ */ #include "glosstex.h" #include "database.h" #include "error.h" #include "list.h" #include "labels.h" #include #include #include #include #include enum e_pass { PASS_1 = 0, PASS_2 = 1 }; static unsigned int lineno = 0; static void process_file (enum e_pass pass, FILE * infile, char *inname); static void process_line (enum e_pass pass, char *inname, char *line); static void write_line (FILE * outfile, char* inname, char *list, char *label, char *item, char *longform, char *line, char *list_mode, char *pageref_mode, char *page); static int glo_parse_item (char *line, char *label, char *item, char *longform, int *ptr); void read_databases (void) { FILE *dbfile; enum e_pass pass = PASS_1; s_node *filename = databases.root; for (pass = PASS_1; pass <= PASS_2; pass++) { while (filename != 0) { if ((dbfile = fopen (filename->ptr, "r")) == NULL) { error ("database %s", (char *) filename->ptr); } else { printlog (PROGRESS, STDOUT, "(%s ", (char *) filename->ptr); process_file (pass, dbfile, (char *) filename->ptr); printlog (PROGRESS, STDOUT, ")"); } filename = filename->next; } filename = databases.root; } } static void process_file (enum e_pass pass, FILE * dbfile, char *inname) { enum e_state { HEADER, BODY }; int status; char buf[LINESIZE]; enum e_state state = HEADER; char *line = 0; while (fgets (buf, LINESIZE, dbfile) != 0) { if (buf[strlen (buf) - 1] == '\n') lineno++; status = strncmp (buf, "@entry{", 7); if ((state == HEADER) && (status != 0)) { ; /* ignore heading garbage */ } else { state = BODY; if (status == 0) { /* begin new entry */ /* process current entry before starting new entry */ if (line != 0) { process_line (pass, inname, line); free (line); } line = (char *) malloc (strlen (buf) + 1); assert (line != 0); strcpy (line, buf); if (line[strlen (line) - 1] == '\n') line[strlen (line) - 1] = ' '; } else if (strncmp (buf, "%", 1) == 0) { ; /* skip comments */ } else { /* add lines to current entry */ size_t len = strlen (line); /* LINT: null is ok here */ line = (char *) realloc (line, len + strlen (buf) + 1); assert (line != 0); strcpy (&line[len], buf); if (line[strlen (line) - 1] == '\n') line[strlen (line) - 1] = ' '; } } } /* process last pending line in file */ if (line != 0) { process_line (pass, inname, line); free (line); } } static void process_line (enum e_pass pass, char *inname, char *line) { char label[LINESIZE]; char item[LINESIZE]; char longform[LINESIZE]; size_t index; int ptr = 0; s_list_iterator iter; s_list_iterator iter2; s_label *node; s_label *node2; iter.root = labels.root; iter.current = labels.root; iter2.root = labels.root; iter2.current = labels.root; if (glo_parse_item (line, label, item, longform, &ptr) != 0) { printlog (PROGRESS, STDOUT, "x"); printlog (WARNING, LOGFILE, "\n%s:%u parse error: %s", inname, lineno, line); count_gdf_parsing++; return; } /* remove all trailing spaces */ index = strlen (&line[ptr]); index--; while (line[ptr + index] == ' ') { line[ptr + index] = '\0'; index--; } switch (pass) { case PASS_1: /* is there a reference to