File: doc2ipf.c

package info (click to toggle)
gnuplot 6.0.3%2Bdfsg1-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 14,960 kB
  • sloc: ansic: 95,874; cpp: 7,199; makefile: 2,470; javascript: 2,339; sh: 1,531; lisp: 664; perl: 304; pascal: 191; tcl: 88; python: 46
file content (608 lines) | stat: -rw-r--r-- 16,011 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* GNUPLOT - doc2ipf.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.
]*/

/*
 * doc2ipf.c  -- program to convert Gnuplot .DOC format to OS/2
 * ipfc  (.inf/.hlp) format.
 *
 * Modified by Roger Fearick from doc2rtf by M Castro
 *
 * usage:  doc2ipf gnuplot.doc gnuplot.ipf
 *
 */

/* 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

#include "syscfg.h"
#include "stdfn.h"

#define MAX_LINE_LEN 1023

#include "doc2x.h"
#include "xref.h"

#define MAX_COL 6

/* From xref.c */
extern void *xmalloc(size_t);

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

/* malloc's are not being checked ! */

struct TABENTRY {		/* may have MAX_COL column tables */
    struct TABENTRY *next;
    char col[MAX_COL][256];
};

struct TABENTRY table;
struct TABENTRY *tableins = &table;
int tablecols = 0;
int tablewidth[MAX_COL] = {0, 0, 0, 0, 0, 0};	/* there must be the correct
							   number of zeroes here */
int tablelines = 0;

#define TmpFileName "doc2ipf.tmp"
static TBOOLEAN debug = FALSE;


int
main(int argc, char **argv)
{
    FILE *infile;
    FILE *outfile;
    if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
	debug = TRUE;

    if (argc != 3 && !debug) {
	fprintf(stderr, "Usage: %s infile outfile\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);
    }
    if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for writing\n",
		argv[0], argv[2]);
	fclose(infile);
	exit(EXIT_FAILURE);
    }
    parse(infile);
    convert(infile, outfile);
    return EXIT_SUCCESS;
}

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

    /* generate ipf header */
    fprintf(b, ":userdoc.\n:prolog.\n");
    fprintf(b, ":title.GNUPLOT\n");
    fprintf(b, ":docprof toc=123456.\n:eprolog.\n");

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

    /* close final page and generate trailer */
    fprintf(b, "\n:euserdoc.\n");

    list_free();
}

void
process_line(char *line, FILE *b)
{
    static int line_count = 0;
    static int prev_line = 0;
    static char line2[MAX_LINE_LEN+1];
    char hyplink1[64];
    char *pt, *tablerow;
    int i;
    int j;
    static int startpage = 2;
    char str[MAX_LINE_LEN+1];
    char topic[MAX_LINE_LEN+1];
    int k, l;
    static int tabl = 0;
    static int para = 0;
    static int inquote = FALSE;
    static int inref = FALSE;
    static int intable = FALSE;
    static int intablebut = FALSE;
    static int introffheader = FALSE;
    static char tablechar = '@';
    static FILE *bo = NULL, *bt = NULL;
    static char tabledelim[4] = "%@\n";
    static int nblanks = 0;
    struct LIST *klist;

    line_count++;

    if (debug && introffheader) {
	fprintf(stderr, "%s\n", line);
    }
    if (bo == NULL)
	bo = b;
    i = 0;
    j = 0;
    nblanks = 0;
    while (line[nblanks] == ' ')
	++nblanks;
    while (line[i] != NUL) {
	if (introffheader) {
	    if (line[i] != '\n')
		line2[j] = line[i];
	    else
		line2[j] = NUL;
	} else
	    switch (line[i]) {
	    case '$':
		/* FIXME: this fails for '$' entry in 'unitary operators' */
		if (intable && (tablechar != '$') && (line[0] == '%')) {
		    if (line[++i] == NUL || line[i+1] == NUL)
			break;
		    if (line[i + 1] == '$' || line[i] == 'x' || line[i] == '|') {
			while (line[i] && line[i] != '$')
			    line2[j++] = line[i++];
			--j;
		    } else {
			while (line[i] != '$' && line[i+1] != NUL)
			    i++;
			if (line[i + 1] == ',')
			    i++;
			if (line[i + 1] == ' ')
			    i++;
			line2[j] = line[++i];
		    }
		} else
		    line2[j] = line[i];
		break;

	    case ':':
		strcpy(&line2[j], "&colon.");
		j += strlen("&colon.") - 1;
		break;

	    case '&':
		/* real hack to solve \&_ in postscript doc tables */
		/* (which are a special case hack anyway. */
		if (j > 0 && line2[j - 1] == '\\') {
		    j -= 2;
		    break;
		}
		strcpy(&line2[j], "&amp.");
		j += strlen("&amp.") - 1;
		break;

	    case '\r':
	    case '\n':
		break;

	    case '`':		/* backquotes mean boldface or link */
		if (nblanks > 7) {
		    line2[j] = line[i];
		    break;
		}
		if ((!inref) && (!inquote)) {
		    k = i + 1;	/* index into current string */
		    l = 0;	/* index into topic string */
		    while ((line[k] != '`') && (line[k] != 0)) {
			topic[l] = line[k];
			k++;
			l++;
		    }
		    topic[l] = 0;
		    klist = lookup(topic);
		    if (klist != NULL && (k = klist->line) > 0) {
			sprintf(hyplink1, ":link reftype=hd res=%d.", k);
			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);
			strcpy(line2 + j, ":hp2.");
			j += 4;
			inquote = TRUE;
		    }
		} else {
		    if (inquote && inref)
			fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
		    if (inquote) {
			strcpy(line2 + j, ":ehp2.");
			j += 5;
			inquote = FALSE;
		    }
		    if (inref) {
			/* must be inref */
			strcpy(line2 + j, ":elink.");
			j += 6;
			inref = FALSE;
		    }
		}
		break;

	    case '.':
		/* Makes code less readable but fixes warnings like
		   <..\docs\gnuplot.ipf:6546> Warning 204: Invalid macro [.gnuplot_iris4d]
		   which is triggered by a '.' character in the first column */
		if (i==1) {
		    strcpy(line2+j, "&per.");
		    j += 4;
		} else
		    line2[j] = line[i];
		break;

	    default:
		line2[j] = line[i];
	    }
	i++;
	j++;
	if ((j >= sizeof(line2))) {
	    fprintf(stderr, "MAX_LINE_LEN exceeded\n");
	    if (inref || inquote)
		fprintf(stderr, "Possible missing link character (`) near above line number\n");
	    abort();
	}
	line2[j] = NUL;
    }

    i = 1;

    switch (line[0]) {		/* control character */
    case '?':{			/* interactive help entry */
	    if (line[1] != '\n') /* skip empty index entries */
		fprintf(b, ":i1.%s", line+1);
	    if (intable)
		intablebut = TRUE;
	    break;
	}
    case '@':{			/* start/end table */
	    intable = !intable;
	    if (intable) {
		tablechar = '@';
		introffheader = TRUE;
		intablebut = FALSE;
		tablelines = 0;
		tablecols = 0;
		tableins = &table;
		for (j = 0; j < MAX_COL; j++)
		    tablewidth[j] = 0;
	    } else {		/* dump table */
		int header = 0;
		introffheader = FALSE; /* position is no longer in a troff header */
		intablebut = FALSE;
		tableins = &table;
		fprintf(b, ":table frame=rules rules=none cols=\'");
		for (j = 0; j < MAX_COL; j++)
		    if (tablewidth[j] > 0)
			fprintf(b, " %d", tablewidth[j]);
		fprintf(b, "\'.\n");
		tableins = tableins->next;
		if (tableins->next != NULL)
		    header = (tableins->next->col[0][1] == '_');
		if (header)
		    tableins->next = tableins->next->next;
		while (tableins != NULL) {
		    fprintf(b, ":row.\n");
		    for (j = 0; j < tablecols; j++)
			if (header)
			    fprintf(b, ":c.:hp9.%s:ehp9.\n", tableins->col[j]);
			else
			    fprintf(b, ":c.%s\n", tableins->col[j]);
		    tableins = tableins->next;
		    /* skip troff 'horizontal rule' command */
		    if (tableins)
			if (tableins->col[0][1] == '_')
			    tableins = tableins->next;
		    header = 0;
		}
		fprintf(b, ":etable.\n");
		if (bt != NULL) {
		    rewind(bt);
		    while (get_line(str, sizeof(str), bt))
			fputs(str, b);
		    fclose(bt);
		    remove(TmpFileName);
		    bt = NULL;
		    bo = b;
		}
	    }
	    break;
	}
    case '=':{			/* index entry */
	    fprintf(b, ":i1.%s", line+1);
	    break;
	}
    case 'F':{			/* embedded figure */
	    if (1) {
		fprintf(b, ":artwork align=left name='%s.bmp'.\n", line2+1);
	    }
	    break;
	}
    case 'D':{			/* embedded figure + link to demo */
	    break;
	}
    case '#':{			/* latex table entry */
	    if (!intable) {
		/* Itemized list outside of table */
		switch (line[1]) {
		case 's':
		    (void) fputs(":ul.\n", b);
		    break;
		case 'e':
		    (void) fputs(":eul.\n", b);
		    break;
		case 'b':
		    /* Bullet */
		    fprintf(b, ":li.%s\n", line2+2);
		    break;
		case '#':
		    /* Continuation of bulleted line */
		    fprintf(b, "%s\n", line2+2);
		    break;
		default:
		    break;
		}
	    }
	    break;		/* ignore */
	}
    case '%':{			/* troff table entry */
	    if (intable) {
		if (introffheader) {
		    if (debug) {
		       fprintf(stderr, ">%s\n", line2);
		       fprintf(stderr, "tablechar: %c\n", tablechar);
		    }
		    if ((line[1] == '.') && (strchr(line2, tablechar) == NULL)) /* ignore troff commands */
			break;
		    pt = strchr(line2, '(');
		    if (pt != NULL)
			tablechar = *(pt + 1);
		    if (debug) {
		       fprintf(stderr, "tablechar: %c\n", tablechar);
		    }
		    pt = strchr(line2 + 2, '.');
		    if (pt != NULL)
			introffheader = FALSE;
		    break;
		}
		if ((line[1] == '.') && (strchr(line+2, tablechar) == NULL)) {	/* ignore troff commands */
		    introffheader = TRUE;
		    break;
		}
		tablerow = line2;
		tableins->next = xmalloc(sizeof(struct TABENTRY));
		tableins = tableins->next;
		tableins->next = NULL;
		j = 0;
		tabledelim[1] = tablechar;
		line2[0] = tablechar;
		while ((pt = strtok(tablerow, tabledelim + 1)) != NULL) {
		    if (*pt != NUL) {	/* ignore null columns */
			char *tagend, *tagstart;
			/* this fails on format line */
			if (j >= MAX_COL) {
			    fprintf(stderr,"j >= MAX_COL\n");
			    exit(EXIT_FAILURE);
			}
			while (*pt==' ') pt++; /* strip spaces */
			strcpy(tableins->col[j], " ");
			strcat(tableins->col[j], pt);
			k = strlen(pt);
			while (k>0 && pt[k-1]==' ') k--; /* strip spaces */
			/* length calculation is not correct if we have ipf tag replacements! */
			if (debug) {
			    if (((strchr(pt, ':')!=NULL) && (strchr(pt, '.')!=NULL)) ||
				((strchr(pt, '&')!=NULL) && (strchr(pt, '.')!=NULL)))
				fprintf(stderr, "Warning: likely overestimating table width (%s)\n", pt);
			}
			/* crudely filter out ipf tags:
			     "&tag." and ":tag." are recognized,
			     (works since all '&' and ':' characters have already been replaced)
			*/
			for (tagend = tagstart = pt; tagstart; ) {
			    tagstart = strchr(tagend, '&');
			    if (!tagstart)
				tagstart = strchr(tagend, ':');
			    if (tagstart) {
				tagend = strchr(tagstart, '.');
				if (tagend)
				    k -= tagend - tagstart;
			    }
			}
			if (k > 0) {  /* ignore empty slots */
			    k += 2; /* add some space */
			    if (k > tablewidth[j])
				tablewidth[j] = k;
			    ++j;
			}
			tablerow = NULL;
			if (j > tablecols)
			    tablecols = j;
		    }
		}
		while (j < MAX_COL)
		    tableins->col[j++][0] = NUL;
	    }
	    break;		/* ignore */
	}
    case '\n':			/* empty text line */
	/* previously this used to emit ":p." to start a new paragraph,
	   now we just note the end of a paragraph or table */
	para = 0;
	tabl = 0;
	break;
    case ' ':{			/* normal text line */
	    if (intable && !intablebut)
		break;
	    if (intablebut) {	/* indexed items in  table, copy
				   to file after table by saving in
				   a temp file meantime */
		if (bt == NULL) {
		    fflush(bo);
		    bt = fopen(TmpFileName, "w+");
		    if (bt == NULL) {
			fprintf(stderr, "Can't open %s\n", TmpFileName);
		    }
		    else
			bo = bt;
		}
	    }
	    if (intablebut && (bt == NULL))
		break;
	    if ((line2[1] == 0) || (line2[1] == '\n')) {
		    fprintf(bo, ":p.");
		para = 0;
	    }
	    if (line2[1] == ' ') {
		/* start table in a new paragraph */
		if (!tabl) {
		    fprintf(bo, ":p.%s\n", &line2[1]);
		    tabl = 1;	/* not in table so start one */
		    para = 0;
		} else {
		    fprintf(bo, ".br\n%s\n", &line2[1]);
		}
	    } else {
		if (!para) {
		    fprintf(bo, ":p.");
		    para = 1;	/* not in para so start one */
		    tabl = 0;
		}
		fprintf(bo, "%s \n", &line2[1]);
	    }
	    fflush(bo);
	    break;
	}
    case '^':
	break;			/* ignore */
    default:{
#ifdef IPF_MENU_SECTIONS
	    TBOOLEAN leaf;
#endif

	    if (isdigit((int)line[0])) {	/* start of section */
		if (intable) {
		    intablebut = TRUE;
		    if (bt == NULL) {
			fflush(bo);
			bt = fopen(TmpFileName, "w+");
			if (bt == NULL) {
			    fprintf(stderr, "Can't open %s\n", TmpFileName);
			}
			else
			    bo = bt;
		    }
		}

		if (debug) {
		   fprintf(stderr, "%d: %s\n", line_count, &line2[1]);
		}
		klist = lookup(&line2[2]);
		if (klist != NULL)
		    k = klist->line;

		/* end all sections in an empty paragraph to prevent empty sections */
		/* we therefore do no longer have to start sections with an empty paragraph */
		if (!startpage)
		    fprintf(bo, ":p.\n");

#ifdef IPF_MENU_SECTIONS
		/* To make navigation with the old IBM online help viewer (View)
		   easier, the following code creates additional panels which contain
		   references to sub-sections. These are not really needed for
		   Aaron Lawrence's NewView and are therefore disabled by default.
		*/

		/* is this section a leaf ? */
		leaf = TRUE:
		if (klist)
		    if (klist->next)
			leaf = (klist->next->level <= klist->level);

		/* if not create a reference panel */
		if (!leaf) {
		    fprintf(bo, ":h%c res=%d x=left y=top width=20%% height=100%% group=1.%s\n",
		            line[0], line_count, line2+1);
		    fprintf(bo, ":link auto reftype=hd res=%d.\n", line_count+20000);
		    fprintf(bo, ":hp2.%s:ehp2.\n.br\n", line2+1);
		    refs(line_count, bo, NULL, NULL, ":link reftype=hd res=%d.%s:elink.\n.br\n");
		    fprintf(bo, ":h%c res=%d x=right y=top width=80%% height=100%% group=2 hide.",
		            line[0]+1, line_count+20000);
		}
		else {
		    fprintf(bo, ":h%c res=%d x=right y=top width=80%% height=100%% group=2.", line[0], line_count);
		}
#else
		if (!startpage)	{ 
		    /* add list of subtopics */
		    refs(prev_line, bo, ":hp2.Subtopics:ehp2.\n.br\n:ul compact.\n", ":eul.\n", ":li.:link reftype=hd res=%d.%s:elink.\n");
		} else {
		    /* startpage was set to 2 initially, so we ignore this twice! */
		    startpage--;
		}

		fprintf(bo, ":h%c res=%d.", line[0], line_count);
#endif
		fprintf(bo, "%s\n", line2+1);	/* title */

		/* add title page */
		if (startpage)
		    fprintf(bo, ".im titlepag.ipf\n");

		para = 0;	/* not in a paragraph */
		tabl = 0;	/* not in a table     */
#ifdef IPF_MENU_SECTIONS
		startpage = 0;
#endif
		prev_line = line_count;
	    } else
		fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
			line[0], line_count);
	}
	break;
    }
}