File: doc2html.c

package info (click to toggle)
gnuplot 5.2.6%2Bdfsg1-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,968 kB
  • sloc: ansic: 81,983; cpp: 7,323; makefile: 2,186; sh: 1,542; lisp: 667; perl: 302; pascal: 194; tcl: 88; python: 46
file content (555 lines) | stat: -rw-r--r-- 17,620 bytes parent folder | download
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#ifndef lint
static char *RCSid() { return RCSid("$Id: doc2html.c,v 1.9 2016/01/16 12:33:43 broeker Exp $"); }
#endif

/* GNUPLOT - doc2html.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.
]*/

/*
 * doc2html.c  -- program to convert Gnuplot .DOC format to MS Windows
 * HTML help (.html) format.
 *
 * Derived from doc2rtf and doc2html (version 3.7.3) by B. Maerkisch
 *
 * usage:  doc2html file.doc outputdirectory [-d]
 *
 */

/* note that tables must begin in at least the second column to */
/* be formatted correctly and tabs are forbidden */

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

#ifdef WXHELP
/* The Microsoft help compiler splits topics and creates the index automatically.
   To create help files for wxWidgets we need to do this manually.
   Note that this might overwrite the stub index file from CVS. */
#define SPLIT_FILES
#define CREATE_INDEX
#endif

#include "syscfg.h"
#include "stdfn.h"
#define MAX_LINE_LEN 10230
#include "doc2x.h"
#include "xref.h"
#include "version.h"

static TBOOLEAN debug = FALSE;
static char path[PATH_MAX];
static const char name[] = "wgnuplot";

void convert __PROTO((FILE *, FILE *, FILE *, FILE *));
void process_line __PROTO((char *, FILE *, FILE *, FILE *));

int
main (int argc, char **argv)
{
    FILE *infile;
    FILE *outfile;
    FILE *contents;
    FILE *index = NULL;
    char filename[PATH_MAX];
    char *last_char;

    if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
	debug = TRUE;
    if (argc != 3 && !debug) {
	fprintf(stderr, "Usage: %s infile outpath\n", argv[0]);
	exit(EXIT_FAILURE);
    }
    if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for reading\n",
		argv[0], argv[1]);
	exit(EXIT_FAILURE);
    }
    strcpy(path, argv[2]);
    /* make sure there's a path separator at the end */
    last_char = path + strlen(path);
    if ((*last_char != DIRSEP1) && (*last_char != DIRSEP2)) {
        *last_char++ = DIRSEP1;
        *last_char = 0;
    }
    strcpy(filename, path);
    strcat(filename, name);
    strcat(filename, ".html");
    if ((outfile = fopen(filename, "w")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for writing\n",
		argv[0], filename);
	fclose(infile);
	exit(EXIT_FAILURE);
    }
    strcpy(filename, path);
    strcat(filename, name);
    strcat(filename, ".hhc");
    if ((contents = fopen(filename, "w")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for writing\n",
		argv[0], filename);
	fclose(infile);
	fclose(outfile);
	exit(EXIT_FAILURE);
    }
#ifdef CREATE_INDEX
    strcpy(filename, path);
    strcat(filename, name);
    strcat(filename, ".hhk");
    if ((index = fopen(filename, "w")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for writing\n",
		argv[0], filename);
	fclose(infile);
	fclose(outfile);
	fclose(contents);
	exit(EXIT_FAILURE);
    }
#endif
    parse(infile);
    convert(infile, outfile, contents, index);
    return EXIT_SUCCESS;
}


void
header(FILE *a, char * title)
{
    /* generate html header */
    fprintf(a, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
    fprintf(a, "<html>\n");
    fprintf(a, "<head>\n");
    fprintf(a, "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\n");
    fprintf(a, "<title>%s</title>\n", title);
    fprintf(a, "</head>\n");
    fprintf(a, "<body>\n");
}


void
footer(FILE *a)
{
    /* close final page and generate trailer */
    fprintf(a, "</body>\n");
    fprintf(a, "</html>\n");
}


void
convert(FILE *a, FILE *b, FILE *c, FILE *d)
{
    static char line[MAX_LINE_LEN+1];

    header(b, "gnuplot help");
    fprintf(b, "<h1 align=\"center\">gnuplot %s patchlevel %s</h1>\n", VERSION_MAJOR, PATCHLEVEL);

    header(c, "gnuplot help contents");
    fprintf(c, "<ul>\n");

    if (d) {
        header(d, "gnuplot help index");
        fprintf(d, "<ul>\n");
    }

    /* process each line of the file */
    while (get_line(line, sizeof(line), a)) {
	process_line(line, b, c, d);
    }

    footer(b);

    fprintf(c, "</ul>\n");
    footer(c);

    if (d) {
        fprintf(d, "</ul>\n");
        footer(d);
    }

    list_free();
}

void
process_line(char *line, FILE *b, FILE *c, FILE *d)
{
    static int line_count = 0;
    static char line2[MAX_LINE_LEN+1];
    static int last_line;

    static int level = 1;
    static TBOOLEAN startpage = TRUE;
    static TBOOLEAN tabl = FALSE;
    static TBOOLEAN intable = FALSE;
    static TBOOLEAN skiptable = FALSE;
    static TBOOLEAN forcetable = FALSE;
    static TBOOLEAN para = FALSE;
    static TBOOLEAN inhlink = FALSE;
    static TBOOLEAN inkey = FALSE;
    static TBOOLEAN inquote = FALSE;
    static int inref = 0;
    static int klink = 0;  /* link number counter */
    static char location[PATH_MAX+1] = {0};
    struct LIST *klist;
    int i, j, k, l;

    line_count++;
    i = 0;
    j = 0;
    while (line[i] != NUL) {
	switch (line[i]) {
	case '<':
	    line2[j++] = '&';
	    line2[j++] = 'l';
	    line2[j++] = 't';
	    line2[j] = ';';
	    break;
	case '>':
	    line2[j++] = '&';
	    line2[j++] = 'g';
	    line2[j++] = 't';
	    line2[j] = ';';
	    break;
	case '&':
	    line2[j++] = '&';
	    line2[j++] = 'a';
	    line2[j++] = 'm';
	    line2[j++] = 'p';
	    line2[j] = ';';
	    break;
	case '\r':
	case '\n':
	    break;
	case '`':		/* backquotes mean boldface or link */
	    if (line[1] == ' ')	/* tabular line */
		line2[j] = line[i];
	    else if ((!inref) && (!inquote)) {
                char topic[MAX_LINE_LEN+1];
		k = i + 1;	/* index into current string */
		l = 0;		/* index into topic string */
		while ((line[k] != '`') && (line[k] != NUL))
		    topic[l++] = line[k++];
		topic[l] = NUL;
		klist = lookup(topic);
		if (klist && (k = klist->line) > 0 && (k != last_line)) {
                    char hyplink1[MAX_LINE_LEN+1];
#if 0
                    /* K-link: index lookup via ActiveX and JavaScript. */
                    /* Deactivated, as none of the open source viewers seem to support this */
                    char id[15];
                    sprintf(id, "link%i", ++klink);
                    sprintf(hyplink1, "<OBJECT id=%s type=\"application/x-oleobject\" classid=\"clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11\">\n"
                                      "  <PARAM name=\"Command\" value=\"KLink\">\n"
                                      "  <PARAM name=\"Item1\" value=\"\">\n"
                                      "  <PARAM name=\"Item2\" value=\"%s\">\n"
                                      "</OBJECT>\n"
                                      "<a href=\"JavaScript:%s.Click()\">",
                                      id, topic, id);
                    if (debug)
                        fprintf(stderr, "hyper link \"%s\" - %s on line %d\n", topic, id, line_count);
#else
		    (void)klink;	/* Otherwise compiler warning about unused variable */
                    /* explicit links */
                    if ((klist->line) > 1)
                        sprintf(hyplink1, "<a href=\"loc%d.html\">", klist->line);
                    else
                        sprintf(hyplink1, "<a href=\"%s.html\">", name);
                    if (debug)
                        fprintf(stderr, "hyper link \"%s\" - loc%d.html on line %d\n", topic, klist->line, line_count);
#endif
                    strcpy(line2 + j, hyplink1);
		    j += strlen(hyplink1) - 1;

		    inref = k;
		} else {
		    if (debug)
			fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
		    line2[j++] = '<';
		    line2[j++] = 'b';
		    line2[j] = '>';
		    inquote = TRUE;
		}
	    } else {
		if (inquote && inref)
		    fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
		if (inquote) {
		    line2[j++] = '<';
		    line2[j++] = '/';
		    line2[j++] = 'b';
		    line2[j] = '>';
		    inquote = FALSE;
		}
		if (inref) {
		    /* must be inref */
		    line2[j++] = '<';
		    line2[j++] = '/';
		    line2[j++] = 'a';
		    line2[j] = '>';
		    inref = 0;
		}
	    }
	    break;
	default:
	    line2[j] = line[i];
	}
	i++;
	j++;
	line2[j] = NUL;
    }

    i = 1;

    if (inkey && !(line[0] == '?' || line[0] == '=')) {
        /* close keyword object */
        fprintf(b, "</OBJECT>\n");
        inkey = FALSE;
    }

    switch (line[0]) {		/* control character */
    case '=': 			/* latex index entry */
	    break;
    case '?':			/* interactive help entry */
            if ((line2[1] != NUL) && (line2[1] != ' ')) {
#ifndef CREATE_INDEX
                if (!inkey) {
                    /* open keyword object */
                    fprintf(b, "<OBJECT type=\"application/x-oleobject\" classid=\"clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e\">\n");
                    inkey = TRUE;
                }
                /* add keyword */
                fprintf(b, "  <param name=\"Keyword\" value=\"%s\">\n", &line2[1]);
#else
                fprintf(d, "<li> <object type=\"text/sitemap\">\n");
                fprintf(d, "  <param name=\"Name\" value=\"%s\">\n", &line2[1]);
                fprintf(d, "  <param name=\"Local\" value=\"%s.html\">\n", location);
                fprintf(d, "  </object>\n");
                /* NB: don't set inkey here */
#endif
                if (debug)
                    fprintf(stderr,"keyword defintion: \"%s\" on line %d.\n", line2 + 1, line_count);
            }
	    break;
    case '@':{			/* start/end table */
	    skiptable = !skiptable;
	    if (!skiptable) forcetable = FALSE;
	    intable = !intable; /* just for latex list support */
	    break;
	}
    case '^':{			/* html link escape */
            if ((!inhlink) && (line[3] == 'a') && (line[5] == 'h')) {
                char *str;
                inhlink = TRUE;
                /* remove trailing newline etc */
                str = line + strlen(line) - 1;
                while (*str=='\r' || *str=='\n') *str-- = NUL;
                fprintf(b, "%s", line + 2);
            } else if (inhlink) {
                inhlink = FALSE;
	        fputs(line + 2, b);	/* copy directly */
            } else {
                if (line[2] == '!') { /* hack for function sections */
                    const char magic[] = "<!-- INCLUDE_NEXT_TABLE -->";
                    if (strncmp(line+1, magic, strlen(magic)) == 0)
                        forcetable = TRUE;
                }
                inhlink = FALSE;
	        fputs(line + 1, b);	/* copy directly */
            }
	    break;		/* ignore */
	}
    case 'F':			/* latex embedded figure */
            if (para) fprintf(b, "</p><p align=\"justify\">\n");
            fprintf(b, "<img src=\"%s.png\" alt=\"%s\">\n", line2+1, line2+1);
            if (para) fprintf(b, "</p><p align=\"justify\">\n");
            break;
    case '#':{			/* latex table entry */
	    if (!intable) {  /* HACK: we just handle itemized lists */
		/* Itemized list outside of table */
		if (line[1] == 's')
		    (void) fputs("<ul>\n", b);
		else if (line[1] == 'e')
		    (void) fputs("</ul>\n", b);
		else if (line[1] == 'b') {
		    /* Bullet */
		    fprintf(b, "<li>%s\n", line2+2);
		}
		else if (line[1] == '#') {
		    /* Continuation of bulleted line */
		    fputs(line2+2, b);
		}
		else {
		    if (strchr(line, '\n'))
			*(strchr(line, '\n')) = '\0';
		    fprintf(b, "<li><pre>%s</pre>\n", line + 1);
		}
	    }
	    break;
	}
    case '%':{			/* troff table entry */
	    break;		/* ignore */
	}
    case '\n':			/* empty text line */
	if (tabl)
	    fprintf(b, "</pre>\n");
	if (para)
	    fprintf(b, "</p>\n");
	para = FALSE;
	tabl = FALSE;
	break;
    case ' ':{			/* normal text line */
            if (skiptable && !forcetable) break; /* break */
	    if ((line2[1] == NUL) || (line2[1] == '\n')) {
		fprintf(b, "\n");
		if (para)
		    fprintf(b, "</p>\n");
	        para = FALSE;
	        tabl = FALSE;
	    } else if (line2[1] == ' ') { /* in table */
                if (inhlink) {
                    int numspaces = 0;
                    while (line2[numspaces+1] == ' ') numspaces++;
                    fprintf(b, "<tt>%s</tt>", line2+1+numspaces);
                } else {
                    if (!tabl) {
		        if (para)
			    fprintf(b, "</p>\n");
		        fprintf(b, "<pre>\n");
		    }
		    fprintf(b, "%s\n", &line2[1]);
		    para = FALSE;
		    tabl = TRUE;
                }
	    } else {
                if (inhlink) {
                    /* no newline here! */
                    fprintf(b, "%s", &line2[1]);
                } else {
                    if (tabl) {
        	        fprintf(b, "</pre>\n");
		        tabl = 0;
                    }
                    if (!para) {
		        para = TRUE;	/* not in para so start one */
		        tabl = FALSE;
		        fprintf(b, "<p align=\"justify\">");
		    }
                    fprintf(b, "%s\n", &line2[1]);
                }
	    }
	    break;
	}
    default:{
	    if (isdigit((int)line[0])) {	/* start of section */
                int newlevel = line[0]-'0';
                char spacer[10];

		if (startpage) {
		} else {
	            if (tabl)
	                fprintf(b, "</pre>\n");
	            if (para)
	                fprintf(b, "</p>\n");
		}
		para = FALSE;	/* not in a paragraph */
		tabl = FALSE;

		if (!startpage)	/* add list of subtopics */
		    refs(last_line, b, "<h3>Subtopics</h3>\n<menu>\n", "</menu>\n", "\t<li><a href=\"loc%d.html\">%s</a></li>\n");

		last_line = line_count;
		fprintf(b, "\n");

                /* output unique ID */
                if (!startpage)
                    sprintf(location, "loc%d", line_count);
                else
                    strcpy(location, name);

#ifndef SPLIT_FILES
                /* let hhc auomatically split contents */
                if (!startpage) {
                    fprintf(b, "<OBJECT type=\"application/x-oleobject\" classid=\"clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e\">\n");
	            fprintf(b, "<param name=\"New HTML file\" value=\"%s.html\">\n", location);
	            fprintf(b, "<param name=\"New HTML title\" value=\"%s\">\n", &line2[2]);
                    fprintf(b, "</OBJECT>\n");
                    fprintf(b, "<h2>%s</h2>\n", &line2[2]);
                }
#else
		/* split contents manually */
                if (!startpage) {
		    char newfile[PATH_MAX];

                    /* close current file */
		    footer(b);
		    fclose(b);

                    /* open new file */
                    sprintf(newfile, "%s%s.html", path, location);
                    /* fprintf(stderr, "%s\n", newfile); */
                    if (!(b = fopen(newfile, "w"))) {
                        fprintf(stderr, "%s: Can't open %s for writing\n",
                            "doc2html", newfile);
                        exit(EXIT_FAILURE);
                    }
		    header(b, &line2[2]);
                    fprintf(b, "<h2>%s</h2>\n", &line2[2]);
		}
#endif

                /* create toc entry */
                if (newlevel > level) {
                    level = newlevel;
                    memset(spacer, '\t', level);
                    spacer[level] = NUL;
                    fprintf(c, "%s<ul>\n", spacer);
                } else if (newlevel < level) {
                    for ( ; newlevel < level; level--) {
                        memset(spacer, '\t', level);
                        spacer[level] = NUL;
                        fprintf(c, "%s</ul>\n", spacer);
                    }
                }
                level = newlevel;
                memset(spacer, '\t', level);
                spacer[level] = NUL;
                fprintf(c, "%s<li> <OBJECT type=\"text/sitemap\">\n", spacer);
		fprintf(c, "%s  <param name=\"Name\" value=\"%s\">\n", spacer, &line2[2]);
                fprintf(c, "%s  <param name=\"Local\" value=\"%s.html\">\n", spacer, location);
		fprintf(c, "%s  </OBJECT>\n", spacer);
                if (debug)
                    fprintf(stderr, "Section \"%s\", Level %i, ID=%s on line %d\n", line2 + 2, level, location, line_count);

                startpage = FALSE;
            } else
		fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
			line[0], line_count);
	    break;
	}
    }
}