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
|
/* #module IdxParse "3-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: IdxParse
*
* Author: R L Aurbach CR&DS MIS Group 26-Apr-1986
*
* Function:
* Parse an input line produced by LaTeX \index command into a form usable
* for the generation of an automatic index.
*
* Modification History:
*
* Version Initials Date Description
* ------------------------------------------------------------------------
* 1-001 RLA 26-Apr-1986 Original Code
* 2-002 RLA 10-Apr-1987 Add support for the Idx_Extract routine.
* 2-003 RLA 15-Apr-1987 Add support for cross-referencing
* 2-004 RLA 20-Apr-1987 Change the cross reference symbol to
* 3-001 F.H. 17-May-1991 converted to portable C
*/
/*
* Module IdxParse - 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 <stdio.h>
#include <string.h>
#include "IdxDef.h"
/*
* Module Definitions:
*/
#define TRUE 1
#define FALSE 0
/*
* Global Declarations:
*/
/*
* Static Declarations:
*/
#ifdef MSDOS
void idx_parse(char *linebf, char *token_1, char *token_2,
char *token_3, char *page_no, int *token_ct, int *flag);
int idx_extract(char *string, int *start, int *length);
#else
void idx_parse();
int idx_extract();
#endif
/*
* External References:
*/
/*
* Functions Called:
*/
/*
* Function Idx_Parse - Documentation Section
*
* Discussion:
* Parse an input line which is of the general form
* \indexentry{token_1>token_2>token_3}{page_no}
* into tokens for "token_1", "token_2", "token_3", and "page_no".
* Handle missing tokens and allow for imbedded LaTeX commands.
*
* Calling Synopsis:
* Call Idx_Parse (linebf, token_1, token_2, token_3, page_no, token_ct,
* flag)
*
* Inputs:
* linebf -> is the input string containing the line to parse.
* ASCIZ string passed by reference.
*
* Outputs:
* token_1 -> is the first token seen (if any).
* ASCIZ string passed by reference.
*
* token_2 -> is the second token seen (if any).
* ASCIZ string passed by reference.
*
* token_3 -> is the third token seen (if any).
* ASCIZ string passed by reference.
*
* page_no -> is the page number token (should always be present).
* ASCIZ string passed by refernce.
*
* token_ct -> is the number of tokens seen.
* Integer passed by reference.
*
* flag -> indicates if the page reference is a cross-reference.
* Boolean passed by reference.
*
* Return Value:
* none
*
* Global Data:
* none
*
* Files Used:
* none
*
* Assumed Entry State:
* none
*
* Normal Exit State:
* returns with the record processed.
*
* Error Conditions:
* none
*
* Algorithm:
* A. Initialize variables.
* B. Verify that the line begins with \indexentry.
* C. Extract the index item substring.
* D. Parse it into tokens.
* E. Extract the page reference substring.
*
* Special Notes:
* This logic is designed to:
* * copy sequences such as \{, \}, and $>$ from the input stream
* into tokens without recognizing them as token delimiters.
* * allow sequences such as {\bf text} in token without recognizing
* the special characters as delimiters.
* * allow up to three levels of index and one level of page-no,
* ignoring all other characters (which shouldn't exist...)
*/
/*
* Function Idx_Parse - Code Section
*/
void idx_parse(linebf,token_1,token_2,token_3,page_no,token_ct,flag)
char *linebf; /* Input string to be parsed */
char *token_1; /* First token string */
char *token_2; /* Second token string */
char *token_3; /* Third token string */
char *page_no; /* Page number token string */
int *token_ct; /* Number of item tokens seen */
int *flag; /* Cross-Reference flag */
{
/*
* Local Declarations
*/
int i; /* linebf counter */
int next; /* beginning of next substring */
int len; /* length of substring */
int math_mode = FALSE; /* math-mode flag */
char *ptr; /* pointer to token buffer */
int index; /* index into token buffer */
int end; /* end of substring */
int dummy;
/*
* Module Body
*/
/* Initialize assorted variables, flags, etc. */
token_1[0] = '\0';
token_2[0] = '\0';
token_3[0] = '\0';
page_no[0] = '\0';
*token_ct = 0;
*flag = FALSE;
ptr = token_1;
index = 0;
/* Verify that the string begins with \indexentry */
if (strncmp(linebf, "\\indexentry{", 12) != 0) return;
/* Locate the first substring. */
i = 11;
next = idx_extract(linebf, &i, &len);
if (len == 0) return;
*token_ct = 1;
end = i + len;
/* For all characters in the substring, parse out the tokens. */
for ( ; i < end; i++) {
switch (linebf[i]) {
case '$' :
if (linebf[i-1] != '\\') {
if (math_mode) math_mode = FALSE;
else math_mode = TRUE;
}
ptr[index++] = linebf[i];
break;
case '>' :
if (math_mode) ptr[index++] = linebf[i];
else {
if (index == 0) break;
ptr[index] = '\0';
index = 0;
switch (*token_ct) {
case 1 :
ptr = token_2; *token_ct = 2; break;
case 2 :
ptr = token_3; *token_ct = 3; break;
}
}
break;
case '&' :
if (linebf[i-1] == '\\') {
ptr[index++] = linebf[i];
break;
}
ptr[index] = '\0';
dummy = end-i;
(void)sprintf(page_no, "\\indexentry{%.*s}{}",
dummy, &linebf[++i]);
*flag = TRUE;
return;
default :
ptr[index++] = linebf[i]; break;
}
}
ptr[index] = '\0';
/* Now parse the second substring and copy it to the page-ref. */
i = next;
next = idx_extract(linebf, &i, &len);
(void)strncpy (page_no, &linebf[i], len);
page_no[len] = '\0';
}
/*
* Function Idx_Extract - Documentation Section
*
* Discussion:
* Extract a string contained within a {} pair, which may contain imbedded
* LaTeX commands.
*
* Calling Synopsis:
* next = Idx_Extract (string, start, length)
*
* Inputs:
* string -> is an ASCIZ string containing a substring enclosed in
* matching braces ({}).
*
* start -> is the string index of the first character of the
* string to be searched. It is passed by reference.
*
* Outputs:
* start -> is the string index of the first character of the
* substring, passed by reference.
*
* length -> is the number of characters in the substring, passed by
* reference.
*
* Return Value:
* next -> is the string index of the first character after the
* '}' which terminates the substring. If the substring
* is not terminated by '}', next is the string index of
* the '\0' character.
*
* Global Data:
* none
*
* Files Used:
* none
*
* Assumed Entry State:
* none
*
* Normal Exit State:
* none
*
* Error Conditions:
* none
*
* Algorithm:
* A. Scan the string for the first '{'.
* B. For all succeeding characters in the string,
* 1. If the character is '{' (not preceeded by '\\'),
* a. Increment the brace count.
* 2. If the character is '}' (not preceeded by '\\'),
* a. Decrement the brace count.
* b. If the brace count = 0,
* 1. Return the start and length of the substring.
*
* Special Notes:
* none
*/
/*
* Function Idx_Extract - Code Section
*/
int idx_extract (string, start, length)
char *string;
int *start;
int *length;
{
/*
* Local Declarations
*/
int i; /* string index */
int brace_ct = 0; /* brace count */
/*
* Module Body
*/
/*
* Scan the string for the first occurrence of a '{' character which is not
* preceeded by a '\\'. The next character marks the beginning of the
* substring to be extracted.
*/
for (i = *start; string[i] != '\0'; i++) {
if (string[i] != '{') continue;
if (i > 0) if (string[i-1] == '\\') continue;
*start = i + 1;
brace_ct++;
break;
}
/*
* If brace_ct == 0, no leading { was found. This is an error. Return
* start = next = end-of-string and length = 0.
*/
if (brace_ct == 0) {
*start = sizeof(string);
*length = 0;
return (sizeof(string));
}
/*
* Now search the string for a matching }.
*/
for (i = *start; string[i] != '\0'; i++) {
if ((string[i] == '{') && (string[i-1] != '\\')) brace_ct++;
if ((string[i] == '}') && (string[i-1] != '\\')) {
brace_ct--;
if (brace_ct == 0) {
*length = i - *start;
return (i + 1);
}
}
}
/*
* If we get here, no closing } was found. Treat the end of string as a
* matching }.
*/
*length = sizeof(string) - *start;
return (sizeof(string));
}
|