File: xref.c

package info (click to toggle)
gnuplot 6.0.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 14,940 kB
  • sloc: ansic: 95,319; cpp: 7,590; makefile: 2,470; javascript: 2,328; sh: 1,531; lisp: 664; perl: 304; pascal: 191; tcl: 88; python: 46
file content (418 lines) | stat: -rw-r--r-- 10,986 bytes parent folder | download | duplicates (3)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* GNUPLOT - xref.c */

/*[
 * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 * this file is used by doc2ipf, doc2html, doc2rtf, doc2info, and doc2web
 *
 * MUST be included after termdoc.c (since termdoc.c redefines fgets() )
 *
 * it contains functions needed to handle xrefs, most of them from
 *     doc2rtf (most likely) by Maurice Castro
 *  or doc2ipf by Roger Fearick
 *  or doc2html by Russel Lang
 *
 * I have modified the functions a little to make them more flexible
 * (lookup returns list instead of list->line) or let them work with all
 * four programs (adding three parameters to refs).
 *
 * I switched the search order of lookup. Makes more sense to me
 *
 * Stefan Bodewig 1/29/1996
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define DOCS_XREF_MAIN

#include "syscfg.h"
#include "stdfn.h"
#include "doc2x.h"
#include "xref.h"

struct LIST *list = NULL;
struct LIST *head = NULL;

struct LIST *keylist = NULL;
struct LIST *keyhead = NULL;

void dump_list(void);

int maxlevel = 0;		/* how deep are the topics nested? */
int listitems = 0;		/* number of topics */

/* for debugging (invoke from gdb !) */
void
dump_list()
{
    struct LIST *element = head;
    while (element) {
	fprintf(stderr, "%p level %d, line %d, \"%s\"\n", element,
		element->level, element->line, element->string);
	element = element->next;
    }
}


generic *
xmalloc(size_t size)
{
    generic *p = malloc(size);

    if (!p) {
        fprintf(stderr, "Malloc failed\n");
	exit(EXIT_FAILURE);
    }
    return p;
}

/* scan the file and build a list of line numbers where particular levels are */
void
parse(FILE *a)
{
    static char line[MAX_LINE_LEN+1];
    char *c;
    int lineno = 0;
    int lastline = 0;

    /* insert a special level 0 listitem
     * this one is the starting point for the table of contents in the html
     * version and the Top-Node of the info version.
     *
     * Added this to support multiple level 1 items.     --SB
     */
    listitems = 1;
    head = (list = (struct LIST *) xmalloc(sizeof(struct LIST)));
    list->prev = NULL;
    list->line = 0;
    list->level = 0;
    list->string = (char *) xmalloc(1);
    list->string[0] = NUL;
    list->next = NULL;

    while (get_line(line, sizeof(line), a)) {
	lineno++;
	if (isdigit((int)line[0])) {	/* start of new section */
	    listitems++;

	    if (list == NULL) {	/* impossible with the new level 0 item */
		head = (list = (struct LIST *) xmalloc(sizeof(struct LIST)));
		list->prev = NULL;
	    } else {
		list->next = (struct LIST *) xmalloc(sizeof(struct LIST));
		list->next->prev = list;
		list = list->next;
		list->next = NULL;
	    }

	    list->line = lastline = lineno;
	    list->level = line[0] - '0';
	    list->string = (char *) xmalloc(strlen(line) + 1);
	    c = strtok(&(line[1]), "\n");
	    strcpy(list->string, c);
	    list->next = NULL;
	    if (list->level > maxlevel)
		maxlevel = list->level;
	}
	if (line[0] == '?') {	/* keywords */
	    if (keylist == NULL) {
		keyhead = (keylist = (struct LIST *) xmalloc(sizeof(struct LIST)));
		keylist->prev = NULL;
	    } else {
		keylist->next = (struct LIST *) xmalloc(sizeof(struct LIST));
		keylist->next->prev = keylist;
		keylist = keylist->next;
	    }

	    keylist->line = lastline;
	    keylist->level = list->level;
	    c = strtok(&(line[1]), "\n");
	    if (c == NULL || *c == '\0')
		c = list->string;
	    keylist->string = (char *) malloc(strlen(c) + 1);
	    strcpy(keylist->string, c);
	    keylist->next = NULL;
	}
    }
    rewind(a);
}

/* look up a topic in text reference */
/*
 * Original version from doc2rtf (|| ipf || html) scanned keylist before list.
 * This way we get a reference to `plot` for the topic `splot` instead
 * of one to `splot`. Switched the search order -SB.
 */
struct LIST *
lookup(char *s)
{
    char *c;
    char tokstr[MAX_LINE_LEN+1];
    char *match;
    int l;

    strcpy(tokstr, s);

    /* first try titles */
    match = strtok(tokstr, " \n\t");
    if (match == NULL) {
	fprintf(stderr, "Error in lookup(\"%s\")\n", s);

	/* there should a line number, but it is local to parse()  */
	fprintf(stderr,
		"Possible missing link character (`) near above line number\n");
	exit(3);
    }
    l = 0;			/* level */

    list = head;
    while (list != NULL) {
	c = list->string;
	while (isspace((int)(*c)))
	    c++;
	if (!strcmp(match, c)) {
	    l = list->level;
	    match = strtok(NULL, "\n\t ");
	    if (match == NULL) {
		return (list);
	    }
	}
	if (l > list->level)
	    break;
	list = list->next;
    }

    /* then try the ? keyword entries */
    keylist = keyhead;
    while (keylist != NULL) {
	c = keylist->string;
	while (isspace((int)(*c)))
	    c++;
	if (!strcmp(s, c))
	    return (keylist);
	keylist = keylist->next;
    }

    return (NULL);
}

/*
 * find title-entry for keyword-entry
 */
struct LIST *
lkup_by_number(int line)
{
    struct LIST *run = head;

    while (run->next && run->next->line <= line)
	run = run->next;

    if (run->next)
	return run;
    else
	return NULL;
}

/*
 * free the whole list (I never trust the OS -SB)
 */
void
list_free()
{
    struct LIST *run;

    for (run = head; run->next; run = run->next)
	; /* do nothing */

    for (run = run->prev; run; run = run->prev) {
	free(run->next->string);
	free(run->next);
    }
    free(head->string);
    free(head);

    for (run = keyhead; run->next; run = run->next)
	; /* do nothing */
    for (run = run->prev; run; run = run->prev) {
	free(run->next->string);
	free(run->next);
    }
    free(keyhead->string);
    free(keyhead);
}


/* search through the list to find any references */
/*
 * writes a menu of all subtopics of the topic located at l
 * format must contain %s for the title of the subtopic and may contain
 * a %d for the line number of the subtopic (used by doc2html and doc2rtf
 * The whole menu is preceded by start and gets the trailer end
 */
void
refs( int l, FILE *f, char *start, char *end, char *format)
{
    int curlevel, i;
    char *c;
    int inlist = FALSE;

    /* find current line */
    list = head;
    while (list->line != l)
	list = list->next;
    curlevel = list->level;
    list = list->next;		/* look at next element before going on */

    if ((start != NULL) && (list != NULL) && (list->level > curlevel)) {
	/* don't write start if there's no menu at all */
	inlist = TRUE;
	fprintf(f, "%s", start);
    }
    while (list != NULL) {
	/* we are onto the next topic so stop */
	if (list->level <= curlevel)
	    break;
	/* these are the next topics down the list */
	if (list->level == curlevel + 1) {
	    c = list->string;
	    while (isspace((int)(*c)))
		c++;		/* strip leading whitespace */

	    if (format != NULL) {
		for (i = 0; format[i] != '%' && format[i] != '\0'; i++);
		if (format[i] != '\0') {
		    if (format[i + 1] == 'd') {
			/* line number has to be printed first */
			fprintf(f, format, list->line, c);
		    }
		    else {
			++i;
			for (; format[i] != '%' && format[i] != '\0'; i++);
			if (format[i] != '\0')	/* line number is second */
			    fprintf(f, format, c, list->line);
			else	/* no line number at all */
			    fprintf(f, format, c);
		    }
		}
	    }
	}
	list = list->next;
    }
    if (inlist && end)		/* trailer */
	fprintf(f, "%s", end);
}


/*
 * Same as refs() but writes a multi-column table.
 * Two additional parameters
 *	ncolumns = split table of subtopics into this many columns
 *	char *newcolumn inserted after every maxrows to start a new column
 *
 * The whole menu is bracketed by start ... end as with refs().
 */
void
reftable( int l, FILE *f, char *start, char *end, char *format,
      int ncolumns, char *newcolumn)
{
    int curlevel, i;
    char *c;
    int inlist = FALSE;
    int entries = 0;	/* entries (so far) in this column */
    int maxrows = 1;
    struct LIST *savehead;

    /* find current line */
    list = head;
    while (list->line != l)
	list = list->next;
    curlevel = list->level;
    list = list->next;	/* look at next element before going on */
    savehead = list;

    /* Count number of subtopics in advance so we know how many to leave room for */
    for (entries = 0; list != NULL; list = list->next) {
	if (list->level <= curlevel)
	    break;
	if (list->level == curlevel + 1)
	    ++entries;
    }
    /* Find size of columns */
    maxrows = 1 + (entries / ncolumns);
    list = savehead;
    entries = 0;

    if ((start != NULL) && (list != NULL) && (list->level > curlevel)) {
	/* don't write start if there's no menu at all */
	inlist = TRUE;
	fprintf(f, "%s", start);
    }
    while (list != NULL) {
	/* we are onto the next topic so stop */
	if (list->level <= curlevel)
	    break;
	/* these are the next topics down the list */
	if (list->level == curlevel + 1) {
	    c = list->string;
	    while (isspace((int)(*c)))
		c++;		/* strip leading whitespace */

	    if (format != NULL) {
		for (i = 0; format[i] != '%' && format[i] != '\0'; i++);
		if (format[i] != '\0') {
		    if (format[i + 1] == 'd') {
			/* line number has to be printed first */
			fprintf(f, format, list->line, c);
		    }
		    else {
			++i;
			for (; format[i] != '%' && format[i] != '\0'; i++);
			if (format[i] != '\0')	/* line number is second */
			    fprintf(f, format, c, list->line);
			else	/* no line number at all */
			    fprintf(f, format, c);
		    }
		}
		if ((++entries >= maxrows) && (newcolumn != NULL)) {
		    fprintf(f, "%s", newcolumn);
		    entries = 0;
		}
	    }
	}
	list = list->next;
    }
    if (inlist && end)		/* trailer */
	fprintf(f, "%s", end);
}