summaryrefslogtreecommitdiff
path: root/indexing/glo+idxtex/globldlab.c
blob: 65a325535584ad14b04f4f8a4e841cd62f714814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* #module    GloBldLab    "2-001"
 ***********************************************************************
 *                                                                     *
 * The software was developed at the Monsanto Company and is provided  *
 * "as-is".  Monsanto Company and the auther disclaim all warranties   *
 * on the software, including without limitation, all implied warran-  *
 * ties of merchantabilitiy and fitness.                               *
 *                                                                     *
 * This software does not contain any technical data or information    *
 * that is proprietary in nature.  It may be copied, modified, and     *
 * distributed on a non-profit basis and with the inclusion of this    *
 * notice.                                                             *
 *                                                                     *
 ***********************************************************************
 */
/*
 * Module Name:	GloBldLab
 *
 * Author:	R L Aurbach	CR&DS MIS Group    18-Aug-1986
 *
 * Function:
 *	Build the list of labels seen in the input file.  Also, if a file
 *	entry is seen, add it to the filelist.
 *
 * Modification History:
 *
 * Version     Initials	   Date		Description
 * ------------------------------------------------------------------------
 * 1-001	RLA	18-Aug-1986	Original Code
 * 1-002	RLA	26-Aug-1986	Fix problem with duplicate label 
 *					  detection.
 * 2-001	F.H.	17-May-1991	converted to portable C
 */
/*
 * Module GloBldLab - Module-Wide Data Description Section
 *
 * Include Files:
 */
#ifdef MSDOS
#include <stdlib.h>
#include <io.h>
#define F_OK		0	/* access(): File exists	*/
#else
#include <sys/file.h>
extern char *sprintf();
#endif
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include "GloDef.h"
/*
 * Module Definitions:
 */
/*
 * Global Declarations:
 */
#ifdef MSDOS
int Glo_Build_LabLst(void);
#else
int Glo_Build_LabLst();
#endif
/*
 * Static Declarations:
 */
/*
 * External References:
 */
extern char		    infile[256];
extern STRING_PTR	    labels;
extern STRING_PTR	    filelist;
/*
 * Functions Called:
 */
#ifdef MSDOS
static void	glo_get_token(char *buffer, char *token);
static int	glo_get_next_file(char *token, int *ptr, char *spec);
#else
static void	glo_get_token();
static int	glo_get_next_file();
#endif
/*
 * Function Glo_Build_LabLst - Documentation Section
 *
 * Discussion:
 *	Read the input file and use its contents to build a list of labels for
 *	which glossary entries are needed.  If a file specification is seen
 *	during this operation, add the file to the filelist.
 *
 * Calling Synopsis:
 *	status = Glo_Build_LabLst ()
 *
 * Inputs:
 *	none
 *
 * Outputs:
 *	none
 *
 * Return Value:
 *	status	    ->	is a status code for the operation.  It is a boolean
 *			integer, passed by value.  If TRUE, the routine 
 *			completed normally.  If FALSE, an error occurred.
 *
 * Global Data:
 *	labels	    ->	the list of labels is created by this routine.
 *
 *	filelist    ->	if one or more files are seen by the routine, they are
 *			added to the file list.
 *
 * Files Used:
 *	The input file is read.
 *
 * Assumed Entry State:
 *	none
 *
 * Normal Exit State:
 *	status = TRUE	normal successful completion.
 *
 * Error Conditions:
 *	status = FALSE	an error occurred.  The reason for the error is written
 *			to SYS$OUTPUT.
 *
 * Algorithm:
 *	A. Open the input file.
 *	B. For each record in the file,
 *	    1. If it is an \indexentry,
 *	       or If it is a \glossaryentry, (TeX 3.0)
 *		a. Parse out the label.
 *		b. If the label does not already appear in the label list,
 *		    1. Build a STRING to contain the label.
 *		    2. Chain it into the label list.
 *	    2. If it is a \glofile,
 *		a. For each file in the list,
 *		    1. Parse out the file specification.
 *		    2. Build a STRING to contain the file specification.
 *		    3. Chain it into the file list.
 *	    3. Else,
 *		a. Ignore the record.
 *	C. Close the input file.
 *
 * Special Notes:
 *	none
 */
/*
 * Function Glo_Build_LabLst - Code Section
 */
int Glo_Build_LabLst ()
{
/*
 * Local Declarations
 */
  int		    status;
  FILE	            *file;
  char	            linebf[linesz];
  char              label[linesz];
  int		    found;
  STRING_PTR	    new;
  STRING_PTR	    old;
  char	            spec[linesz];
  int               ptr;
  char              fname[256];
/*
 * Module Body
 */
/* Open the input file */
  (void)sprintf(fname,"%s.glo",infile);
  if ((file = fopen(fname, "r")) == NULL) {
    (void)printf("Could not open the input file\n");
    return (FALSE);
  }
/* For each record in the file,	*/
  status = TRUE;
  while (fgets(linebf, linesz, file) != 0) {
/*
 * If the record begins with \indexentry (or \glossaryentry), then it is
 * a record containing a label.  Process the label by parsing it out,
 * checking for duplicates, and if unique, building a label list entry for it.
 */
    if ((strncmp(linebf, "\\indexentry{", 12)) == 0) {
      glo_get_token(&linebf[12], label);
      if (strlen(label) == 0) continue;
      old = labels;
      found = FALSE;
      while (old != 0) {
	if (strcmp(label,old->desc) == 0) found = TRUE;
	if (old->next == 0) break;
	old = old->next;
      }
      if (!found) {
	new = (STRING_PTR) malloc(sizeof(STRING));
	if (new == 0) continue;
	new->next = 0;
	new->desc=strdup(label);
	if (old == 0) labels = new;
	else old->next = new;
      }
    }
    else if ((strncmp(linebf, "\\glossaryentry{", 15)) == 0) {
      glo_get_token(&linebf[15], label);
      if (strlen(label) == 0) continue;
      old = labels;
      found = FALSE;
      while (old != 0) {
	if (strcmp(label,old->desc) == 0) found = TRUE;
	if (old->next == 0) break;
	old = old->next;
      }
      if (!found) {
	new = (STRING_PTR) malloc(sizeof(STRING));
	if (new == 0) continue;
	new->next = 0;
	new->desc=strdup(label);
	if (old == 0) labels = new;
	else old->next = new;
      }
    }
/*
 * If the record begins with \glofile, then it is a record containing one or
 * more glossary definition file specifications.  For each specification, parse
 * it out and build a file list entry for it.
 */
    else if ((strncmp(linebf, "\\glofile{", 9)) == 0) {
      glo_get_token(&linebf[9], label);
      if (strlen(label) == 0) continue;
      ptr = 0;
      while (glo_get_next_file(label, &ptr, spec) != 0) {
	new = (STRING_PTR) malloc(sizeof(STRING));
	if (new == 0) continue;
	new->next = 0;
	new->desc=strdup(spec);
	old = filelist;
	if (old == 0) filelist = new;
	else {
	  while (old->next != 0) old = old->next;
/*@@@		old->next = file;*/
	}
      }
    }
/*
 * If the record begins with anything else, ignore it.
 */
    else continue;
  }
  (void)fclose(file);
  return (status);
}

/*
 * Function Glo_Get_Token - Documentation Section
 *
 * Discussion:
 *	Parse a token from the command line.  The token begins at the first
 *	character position in the buffer and ends with a "}".
 *
 * Calling Synopsis:
 *	Call Glo_Get_Token (buffer, token)
 *
 * Inputs:
 *	buffer	    ->	is an ASCIZ string containing the token to be found.
 *
 * Outputs:
 *	token	    ->	is an ASCIZ string into which the token is copied.
 *
 * Return Value:
 *	none
 *
 * Global Data:
 *	none
 *
 * Files Used:
 *	none
 *
 * Assumed Entry State:
 *	none
 *
 * Normal Exit State:
 *	The token is returned.  
 *
 * Error Conditions:
 *	none
 *
 * Algorithm:
 *	A. Locate the trailing "}" or end of string.
 *	B. Copy the substring starting at the beginning of the buffer to the
 *	   end found to the token. 
 *	C. Null terminate the string.
 *
 * Special Notes:
 *	none
 */
/*
 * Function Glo_Get_Token - Code Section
 */
static void	glo_get_token (buffer, token)
char	    *buffer;
char	    *token;
{
/*
 * Local Declarations
 */
  int		    len;	    /* Length of the substring		    */
/*
 * Module Body
 */
  len = strcspn(buffer, "}");
  (void)strncpy(token, buffer, len);
  token[len] = '\0';
}

/*
 * Function Glo_Get_Next_File - Documentation Section
 *
 * Discussion:
 *	Parse the next file specification out of the token list.  Files are
 *	separated by commas.  Return the length of the specification.
 *
 * Calling Synopsis:
 *	length = Glo_Get_Next_File (token, ptr, spec)
 *
 * Inputs:
 *	token	    ->	is an ASCIZ string containing the list of files.
 *
 *	ptr	    ->	is an integer (passed by reference) which is the 
 *			starting index in the token string for the search.
 *
 * Outputs:
 *	spec	    ->	is an ASCIZ string containing the next file spec.
 *
 * Return Value:
 *	length	    ->	is the length of the spec string.  A value of 0 
 *			indicates that there is no file spec.
 *
 * Global Data:
 *	none
 *
 * Files Used:
 *	none
 *
 * Assumed Entry State:
 *	none
 *
 * Normal Exit State:
 *	length != 0	spec contains a file spec.  ptr is updated to point
 *			to the starting place for the next search.
 *	length == 0	no spec was found.
 *
 * Error Conditions:
 *	none
 *
 * Algorithm:
 *	A. Find the next "," in the string.
 *	B. Copy the substring into the output variable.
 *	C. Adjust the pointer.
 *	D. Return the length of the string.
 *
 * Special Notes:
 *	none
 */
/*
 * Function Glo_Get_Next_File - Code Section
 */
static int	glo_get_next_file (token, ptr, spec)
char	    *token;
int	    *ptr;
char	    *spec;
{
/*
 * Local Declarations
 */
  int	length;
/*
 * Module Body
 */
  length = strcspn (&token[*ptr], ",");
  if (length != 0) {
    (void)strncpy (spec, &token[*ptr], length);
    spec[length] = 0;
    (*ptr) += length;
    if (token[*ptr] == ',') (*ptr)++;
  }
  return (length);
}