2 static char *RCSid() { return RCSid("$Id: doc2ipf.c,v 1.19 2006/05/13 09:05:39 mikulik Exp $"); }
5 /* GNUPLOT - doc2ipf.c */
8 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
10 * Permission to use, copy, and distribute this software and its
11 * documentation for any purpose with or without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and
13 * that both that copyright notice and this permission notice appear
14 * in supporting documentation.
16 * Permission to modify the software is granted, but not the right to
17 * distribute the complete modified source code. Modifications are to
18 * be distributed as patches to the released version. Permission to
19 * distribute binaries produced by compiling modified sources is granted,
21 * 1. distribute the corresponding source modifications from the
22 * released version in the form of a patch file along with the binaries,
23 * 2. add special version identification to distinguish your version
24 * in addition to the base release version number,
25 * 3. provide your name and address as the primary contact for the
26 * support of your modified version, and
27 * 4. retain our contact information in regard to use of the base
29 * Permission to distribute the released version of the source code along
30 * with corresponding source modifications in the form of a patch file is
31 * granted with same provisions 2 through 4 for binary distributions.
33 * This software is provided "as is" without express or implied warranty
34 * to the extent permitted by applicable law.
38 * doc2ipf.c -- program to convert Gnuplot .DOC format to OS/2
39 * ipfc (.inf/.hlp) format.
41 * Modified by Roger Fearick from doc2rtf by M Castro
43 * usage: doc2ipf gnuplot.doc gnuplot.ipf
47 /* note that tables must begin in at least the second column to */
48 /* be formatted correctly and tabs are forbidden */
57 #define MAX_LINE_LEN 1023
65 extern void *xmalloc __PROTO((size_t));
67 void convert __PROTO((FILE *, FILE *));
68 void process_line __PROTO((char *, FILE *));
70 /* malloc's are not being checked ! */
72 struct TABENTRY { /* may have MAX_COL column tables */
73 struct TABENTRY *next;
74 char col[MAX_COL][256];
77 struct TABENTRY table;
78 struct TABENTRY *tableins = &table;
80 int tablewidth[MAX_COL] = {0, 0, 0, 0, 0, 0}; /* there must be the correct
81 number of zeroes here */
84 #define TmpFileName "doc2ipf.tmp"
85 static TBOOLEAN debug = FALSE;
89 main (int argc, char **argv)
93 if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
96 if (argc != 3 && !debug) {
97 fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
100 if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
101 fprintf(stderr, "%s: Can't open %s for reading\n",
105 if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
106 fprintf(stderr, "%s: Can't open %s for writing\n",
112 convert(infile, outfile);
117 convert(FILE *a, FILE *b)
119 static char line[MAX_LINE_LEN+1];
121 /* generate ipf header */
122 fprintf(b, ":userdoc.\n:prolog.\n");
123 fprintf(b, ":title.GNUPLOT\n");
124 fprintf(b, ":docprof toc=123456.\n:eprolog.\n");
126 /* process each line of the file */
127 while (get_line(line, sizeof(line), a)) {
128 process_line(line, b);
131 /* close final page and generate trailer */
132 fprintf(b, "\n:euserdoc.\n");
138 process_line(char *line, FILE *b)
140 static int line_count = 0;
141 static char line2[MAX_LINE_LEN+1];
142 static int last_line;
147 static int startpage = 1;
148 char str[MAX_LINE_LEN+1];
149 char topic[MAX_LINE_LEN+1];
153 static int inquote = FALSE;
154 static int inref = FALSE;
155 static int intable = FALSE;
156 static int intablebut = FALSE;
157 static int introffheader = FALSE;
158 static char tablechar = '@';
159 static FILE *bo = NULL, *bt = NULL;
160 static char tabledelim[4] = "%@\n";
161 static int nblanks = 0;
166 if (debug && introffheader) {
167 fprintf(stderr, "%s\n", line);
174 while (line[nblanks] == ' ')
176 while (line[i] != NUL) {
185 /* FIXME: this fails for '$' entry in 'unitary operators' */
186 if (intable && (tablechar != '$') && (line[0] == '%')) {
188 if (line[i + 1] == '$' || line[i] == 'x' || line[i] == '|') {
189 while (line[i] != '$')
190 line2[j++] = line[i++];
193 while (line[i] != '$')
195 if (line[i + 1] == ',')
197 if (line[i + 1] == ' ')
199 line2[j] = line[++i];
206 strcpy(&line2[j], "&colon.");
207 j += strlen("&colon.") - 1;
211 /* real hack to solve \&_ in postscript doc tables */
212 /* (which are a special case hack anyway. */
213 if (j > 0 && line2[j - 1] == '\\') {
217 strcpy(&line2[j], "&.");
218 j += strlen("&.") - 1;
225 case '`': /* backquotes mean boldface or link */
230 if ((!inref) && (!inquote)) {
231 k = i + 1; /* index into current string */
232 l = 0; /* index into topic string */
233 while ((line[k] != '`') && (line[k] != 0)) {
239 klist = lookup(topic);
240 if (klist != NULL && (k = klist->line) > 0) {
241 sprintf(hyplink1, ":link reftype=hd res=%d.", k);
242 strcpy(line2 + j, hyplink1);
243 j += strlen(hyplink1) - 1;
248 fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
249 strcpy(line2 + j, ":hp2.");
254 if (inquote && inref)
255 fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
257 strcpy(line2 + j, ":ehp2.");
263 strcpy(line2 + j, ":elink.");
271 /* Makes code less readable but fixes warnings like
272 <..\docs\gnuplot.ipf:6546> Warning 204: Invalid macro [.gnuplot_iris4d]
273 which is triggered by a '.' character in the first column */
275 strcpy(line2+j, "&per.");
286 if ((j >= sizeof(line2))) {
287 fprintf(stderr, "MAX_LINE_LEN exceeded\n");
288 if (inref || inquote)
289 fprintf(stderr, "Possible missing link character (`) near above line number\n");
297 switch (line[0]) { /* control character */
298 case '?':{ /* interactive help entry */
299 if (line[1] != '\n') /* skip empty index entries */
300 fprintf(b, ":i1.%s", line+1);
305 case '@':{ /* start/end table */
309 introffheader = TRUE;
314 for (j = 0; j < MAX_COL; j++)
316 } else { /* dump table */
318 introffheader = FALSE; /* position is no longer in a troff header */
321 fprintf(b, ":table frame=none rules=vert cols=\'");
322 for (j = 0; j < MAX_COL; j++)
323 if (tablewidth[j] > 0)
324 fprintf(b, " %d", tablewidth[j]);
326 tableins = tableins->next;
327 if (tableins->next != NULL)
328 header = (tableins->next->col[0][1] == '_');
330 tableins->next = tableins->next->next;
331 while (tableins != NULL) {
332 fprintf(b, ":row.\n");
333 for (j = 0; j < tablecols; j++)
335 fprintf(b, ":c.:hp9.%s:ehp9.\n", tableins->col[j]);
337 fprintf(b, ":c.%s\n", tableins->col[j]);
338 tableins = tableins->next;
339 /* skip troff 'horizontal rule' command */
341 if (tableins->col[0][1] == '_')
342 tableins = tableins->next;
345 fprintf(b, ":etable.\n");
348 while (get_line(str, sizeof(str), bt))
358 case '=':{ /* index entry */
359 fprintf(b, ":i1.%s", line+1);
362 case '#':{ /* latex table entry */
365 case '%':{ /* troff table entry */
369 fprintf(stderr, ">%s\n", line2);
370 fprintf(stderr, "tablechar: %c\n", tablechar);
372 if ((line[1] == '.') && (strchr(line2, tablechar) == NULL)) /* ignore troff commands */
374 pt = strchr(line2, '(');
376 tablechar = *(pt + 1);
378 fprintf(stderr, "tablechar: %c\n", tablechar);
380 pt = strchr(line2 + 2, '.');
382 introffheader = FALSE;
385 if ((line[1] == '.') && (strchr(line+2, tablechar) == NULL)) { /* ignore troff commands */
386 introffheader = TRUE;
390 tableins->next = xmalloc(sizeof(struct TABENTRY));
391 tableins = tableins->next;
392 tableins->next = NULL;
394 tabledelim[1] = tablechar;
395 line2[0] = tablechar;
396 while ((pt = strtok(tablerow, tabledelim + 1)) != NULL) {
397 if (*pt != NUL) { /* ignore null columns */
398 char *tagend, *tagstart;
399 /* this fails on format line */
401 while (*pt==' ') pt++; /* strip spaces */
402 strcpy(tableins->col[j], " ");
403 strcat(tableins->col[j], pt);
405 while (pt[k-1]==' ') k--; /* strip spaces */
406 /* length calculation is not correct if we have ipf tag replacements! */
408 if (((strchr(pt, ':')!=NULL) && (strchr(pt, '.')!=NULL)) ||
409 ((strchr(pt, '&')!=NULL) && (strchr(pt, '.')!=NULL)))
410 fprintf(stderr, "Warning: likely overestimating table width (%s)\n", pt);
412 /* crudely filter out ipf tags:
413 "&tag." and ":tag." are recognized,
414 (works since all '&' and ':' characters have already been replaced)
416 for (tagend = tagstart = pt; tagstart; ) {
417 tagstart = strchr(tagend, '&');
419 tagstart = strchr(tagend, ':');
421 tagend = strchr(tagstart, '.');
423 k -= tagend - tagstart;
426 k += 2; /* add some space */
427 if (k > tablewidth[j])
436 tableins->col[j++][0] = NUL;
440 case '\n': /* empty text line */
441 /* previously this used to emit ":p." to start a new paragraph,
442 now we just note the end of a paragraph or table */
446 case ' ':{ /* normal text line */
447 if (intable && !intablebut)
449 if (intablebut) { /* indexed items in table, copy
450 to file after table by saving in
451 a temp file meantime */
454 bt = fopen(TmpFileName, "w+");
456 fprintf(stderr, "Can't open %s\n", TmpFileName);
462 if (intablebut && (bt == NULL))
464 if ((line2[1] == 0) || (line2[1] == '\n')) {
468 if (line2[1] == ' ') {
469 /* start table in a new paragraph */
471 fprintf(bo, ":p.%s\n", &line2[1]);
472 tabl = 1; /* not in table so start one */
475 fprintf(bo, ".br\n%s\n", &line2[1]);
480 para = 1; /* not in para so start one */
483 fprintf(bo, "%s \n", &line2[1]);
493 if (isdigit((int)line[0])) { /* start of section */
498 bt = fopen(TmpFileName, "w+");
500 fprintf(stderr, "Can't open %s\n", TmpFileName);
508 /* disabled by BM: this doesn't do anything? */
509 if (startpage) /* use new level 0 item */
510 refs(0, bo, NULL, NULL, NULL);
512 refs(last_line, bo, NULL, NULL, NULL);
515 fprintf(stderr, "%d: %s\n", line_count, &line2[1]);
517 klist = lookup(&line2[2]);
521 /* end all sections in an empty paragraph to prevent empty sections */
522 /* we therefore do no longer have to start sections with an empty paragraph */
524 fprintf(bo, ":p.\n");
526 /*if( k<0 ) fprintf(bo,":h%c.", line[0]=='1'?line[0]:line[0]-1);
529 #ifdef IPF_MENU_SECTIONS
530 /* To make navigation with the old IBM online help viewer (View)
531 easier, the following code creates additional panels which contain
532 references to sub-sections. These are not really needed for
533 Aaron Lawrence's NewView and are therefore disabled by default.
536 /* is this section a leaf ? */
540 leaf = (klist->next->level <= klist->level);
542 /* if not create a reference panel */
544 fprintf(bo, ":h%c res=%d x=left y=top width=20%% height=100%% group=1.%s\n",
545 line[0], line_count, line2+1);
546 fprintf(bo, ":link auto reftype=hd res=%d.\n", line_count+20000);
547 fprintf(bo, ":hp2.%s:ehp2.\n.br\n", line2+1);
548 refs(line_count, bo, NULL, NULL, ":link reftype=hd res=%d.%s:elink.\n.br\n");
549 fprintf(bo, ":h%c res=%d x=right y=top width=80%% height=100%% group=2 hide.",
550 line[0]+1, line_count+20000);
553 fprintf(bo, ":h%c res=%d x=right y=top width=80%% height=100%% group=2.", line[0], line_count);
556 fprintf(bo, ":h%c res=%d.", line[0], line_count);
558 fprintf(bo, "%s\n", line2+1); /* title */
562 fprintf(bo, ".im titlepag.ipf\n");
564 para = 0; /* not in a paragraph */
565 tabl = 0; /* not in a table */
566 last_line = line_count;
569 fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
570 line[0], line_count);