Initial release of Maemo 5 port of gnuplot
[gnuplot] / docs / doc2ipf.c
1 #ifndef lint
2 static char *RCSid() { return RCSid("$Id: doc2ipf.c,v 1.19 2006/05/13 09:05:39 mikulik Exp $"); }
3 #endif
4
5 /* GNUPLOT - doc2ipf.c */
6
7 /*[
8  * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
9  *
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.
15  *
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,
20  * provided you
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
28  *    software.
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.
32  *
33  * This software is provided "as is" without express or implied warranty
34  * to the extent permitted by applicable law.
35 ]*/
36
37 /*
38  * doc2ipf.c  -- program to convert Gnuplot .DOC format to OS/2
39  * ipfc  (.inf/.hlp) format.
40  *
41  * Modified by Roger Fearick from doc2rtf by M Castro
42  *
43  * usage:  doc2ipf gnuplot.doc gnuplot.ipf
44  *
45  */
46
47 /* note that tables must begin in at least the second column to */
48 /* be formatted correctly and tabs are forbidden */
49
50 #ifdef HAVE_CONFIG_H
51 # include "config.h"
52 #endif
53
54 #include "syscfg.h"
55 #include "stdfn.h"
56
57 #define MAX_LINE_LEN 1023
58
59 #include "doc2x.h"
60 #include "xref.h"
61
62 #define MAX_COL 6
63
64 /* From xref.c */
65 extern void *xmalloc __PROTO((size_t));
66
67 void convert __PROTO((FILE *, FILE *));
68 void process_line __PROTO((char *, FILE *));
69
70 /* malloc's are not being checked ! */
71
72 struct TABENTRY {               /* may have MAX_COL column tables */
73     struct TABENTRY *next;
74     char col[MAX_COL][256];
75 };
76
77 struct TABENTRY table;
78 struct TABENTRY *tableins = &table;
79 int tablecols = 0;
80 int tablewidth[MAX_COL] = {0, 0, 0, 0, 0, 0};   /* there must be the correct
81                                                            number of zeroes here */
82 int tablelines = 0;
83
84 #define TmpFileName "doc2ipf.tmp"
85 static TBOOLEAN debug = FALSE;
86
87
88 int
89 main (int argc, char **argv)
90 {
91     FILE *infile;
92     FILE *outfile;
93     if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
94         debug = TRUE;
95
96     if (argc != 3 && !debug) {
97         fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
98         exit(EXIT_FAILURE);
99     }
100     if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
101         fprintf(stderr, "%s: Can't open %s for reading\n",
102                 argv[0], argv[1]);
103         exit(EXIT_FAILURE);
104     }
105     if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
106         fprintf(stderr, "%s: Can't open %s for writing\n",
107                 argv[0], argv[2]);
108         fclose(infile);
109         exit(EXIT_FAILURE);
110     }
111     parse(infile);
112     convert(infile, outfile);
113     return EXIT_SUCCESS;
114 }
115
116 void
117 convert(FILE *a, FILE *b)
118 {
119     static char line[MAX_LINE_LEN+1];
120
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");
125
126     /* process each line of the file */
127     while (get_line(line, sizeof(line), a)) {
128         process_line(line, b);
129     }
130
131     /* close final page and generate trailer */
132     fprintf(b, "\n:euserdoc.\n");
133
134     list_free();
135 }
136
137 void
138 process_line(char *line, FILE *b)
139 {
140     static int line_count = 0;
141     static char line2[MAX_LINE_LEN+1];
142     static int last_line;
143     char hyplink1[64];
144     char *pt, *tablerow;
145     int i;
146     int j;
147     static int startpage = 1;
148     char str[MAX_LINE_LEN+1];
149     char topic[MAX_LINE_LEN+1];
150     int k, l;
151     static int tabl = 0;
152     static int para = 0;
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;
162     struct LIST *klist;
163
164     line_count++;
165
166     if (debug && introffheader) {
167         fprintf(stderr, "%s\n", line);
168     }
169     if (bo == NULL)
170         bo = b;
171     i = 0;
172     j = 0;
173     nblanks = 0;
174     while (line[nblanks] == ' ')
175         ++nblanks;
176     while (line[i] != NUL) {
177         if (introffheader) {
178             if (line[i] != '\n')
179                 line2[j] = line[i];
180             else
181                 line2[j] = NUL;
182         } else
183             switch (line[i]) {
184             case '$':
185                 /* FIXME: this fails for '$' entry in 'unitary operators' */
186                 if (intable && (tablechar != '$') && (line[0] == '%')) {
187                     ++i;
188                     if (line[i + 1] == '$' || line[i] == 'x' || line[i] == '|') {
189                         while (line[i] != '$')
190                             line2[j++] = line[i++];
191                         --j;
192                     } else {
193                         while (line[i] != '$')
194                             i++;
195                         if (line[i + 1] == ',')
196                             i++;
197                         if (line[i + 1] == ' ')
198                             i++;
199                         line2[j] = line[++i];
200                     }
201                 } else
202                     line2[j] = line[i];
203                 break;
204
205             case ':':
206                 strcpy(&line2[j], "&colon.");
207                 j += strlen("&colon.") - 1;
208                 break;
209
210             case '&':
211                 /* real hack to solve \&_ in postscript doc tables */
212                 /* (which are a special case hack anyway. */
213                 if (j > 0 && line2[j - 1] == '\\') {
214                     j -= 2;
215                     break;
216                 }
217                 strcpy(&line2[j], "&amp.");
218                 j += strlen("&amp.") - 1;
219                 break;
220
221             case '\r':
222             case '\n':
223                 break;
224
225             case '`':           /* backquotes mean boldface or link */
226                 if (nblanks > 7) {
227                     line2[j] = line[i];
228                     break;
229                 }
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)) {
234                         topic[l] = line[k];
235                         k++;
236                         l++;
237                     }
238                     topic[l] = 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;
244
245                         inref = k;
246                     } else {
247                         if (debug)
248                             fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
249                         strcpy(line2 + j, ":hp2.");
250                         j += 4;
251                         inquote = TRUE;
252                     }
253                 } else {
254                     if (inquote && inref)
255                         fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
256                     if (inquote) {
257                         strcpy(line2 + j, ":ehp2.");
258                         j += 5;
259                         inquote = FALSE;
260                     }
261                     if (inref) {
262                         /* must be inref */
263                         strcpy(line2 + j, ":elink.");
264                         j += 6;
265                         inref = FALSE;
266                     }
267                 }
268                 break;
269
270             case '.':
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 */
274                 if (i==1) {
275                     strcpy(line2+j, "&per.");
276                     j += 4;
277                 } else
278                     line2[j] = line[i];
279                 break;
280
281             default:
282                 line2[j] = line[i];
283             }
284         i++;
285         j++;
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");
290             abort();
291         }
292         line2[j] = NUL;
293     }
294
295     i = 1;
296
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);
301             if (intable)
302                 intablebut = TRUE;
303             break;
304         }
305     case '@':{                  /* start/end table */
306             intable = !intable;
307             if (intable) {
308                 tablechar = '@';
309                 introffheader = TRUE;
310                 intablebut = FALSE;
311                 tablelines = 0;
312                 tablecols = 0;
313                 tableins = &table;
314                 for (j = 0; j < MAX_COL; j++)
315                     tablewidth[j] = 0;
316             } else {            /* dump table */
317                 int header = 0;
318                 introffheader = FALSE; /* position is no longer in a troff header */
319                 intablebut = FALSE;
320                 tableins = &table;
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]);
325                 fprintf(b, "\'.\n");
326                 tableins = tableins->next;
327                 if (tableins->next != NULL)
328                     header = (tableins->next->col[0][1] == '_');
329                 if (header)
330                     tableins->next = tableins->next->next;
331                 while (tableins != NULL) {
332                     fprintf(b, ":row.\n");
333                     for (j = 0; j < tablecols; j++)
334                         if (header)
335                             fprintf(b, ":c.:hp9.%s:ehp9.\n", tableins->col[j]);
336                         else
337                             fprintf(b, ":c.%s\n", tableins->col[j]);
338                     tableins = tableins->next;
339                     /* skip troff 'horizontal rule' command */              
340                     if (tableins)
341                         if (tableins->col[0][1] == '_')
342                             tableins = tableins->next;
343                     header = 0;
344                 }
345                 fprintf(b, ":etable.\n");
346                 if (bt != NULL) {
347                     rewind(bt);
348                     while (get_line(str, sizeof(str), bt))
349                         fputs(str, b);
350                     fclose(bt);
351                     remove(TmpFileName);
352                     bt = NULL;
353                     bo = b;
354                 }
355             }
356             break;
357         }
358     case '=':{                  /* index entry */
359             fprintf(b, ":i1.%s", line+1);
360             break;
361         }
362     case '#':{                  /* latex table entry */
363             break;              /* ignore */
364         }
365     case '%':{                  /* troff table entry */
366             if (intable) {
367                 if (introffheader) {
368                     if (debug) {
369                        fprintf(stderr, ">%s\n", line2);
370                        fprintf(stderr, "tablechar: %c\n", tablechar);
371                     }
372                     if ((line[1] == '.') && (strchr(line2, tablechar) == NULL)) /* ignore troff commands */
373                         break;
374                     pt = strchr(line2, '(');
375                     if (pt != NULL)
376                         tablechar = *(pt + 1);
377                     if (debug) {
378                        fprintf(stderr, "tablechar: %c\n", tablechar);
379                     }
380                     pt = strchr(line2 + 2, '.');
381                     if (pt != NULL)
382                         introffheader = FALSE;
383                     break;
384                 }
385                 if ((line[1] == '.') && (strchr(line+2, tablechar) == NULL)) {  /* ignore troff commands */
386                     introffheader = TRUE;
387                     break;
388                 }
389                 tablerow = line2;
390                 tableins->next = xmalloc(sizeof(struct TABENTRY));
391                 tableins = tableins->next;
392                 tableins->next = NULL;
393                 j = 0;
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 */
400                         assert(j < MAX_COL);
401                         while (*pt==' ') pt++; /* strip spaces */               
402                         strcpy(tableins->col[j], " ");
403                         strcat(tableins->col[j], pt);
404                         k = strlen(pt);
405                         while (pt[k-1]==' ') k--; /* strip spaces */
406                         /* length calculation is not correct if we have ipf tag replacements! */
407                         if (debug) {
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);
411                         }
412                         /* crudely filter out ipf tags:
413                              "&tag." and ":tag." are recognized, 
414                              (works since all '&' and ':' characters have already been replaced)
415                         */
416                         for (tagend = tagstart = pt; tagstart; ) {
417                             tagstart = strchr(tagend, '&');
418                             if (!tagstart)
419                                 tagstart = strchr(tagend, ':');
420                             if (tagstart) {
421                                 tagend = strchr(tagstart, '.');
422                                 if (tagend)
423                                     k -= tagend - tagstart;
424                             }
425                         }
426                         k += 2; /* add some space */
427                         if (k > tablewidth[j])
428                             tablewidth[j] = k;
429                         ++j;
430                         tablerow = NULL;
431                         if (j > tablecols)
432                             tablecols = j;
433                     }
434                 }
435                 while (j < MAX_COL)
436                     tableins->col[j++][0] = NUL;
437             }
438             break;              /* ignore */
439         }
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 */
443         para = 0;
444         tabl = 0;
445         break;
446     case ' ':{                  /* normal text line */
447             if (intable && !intablebut)
448                 break;
449             if (intablebut) {   /* indexed items in  table, copy
450                                    to file after table by saving in
451                                    a temp file meantime */
452                 if (bt == NULL) {
453                     fflush(bo);
454                     bt = fopen(TmpFileName, "w+");
455                     if (bt == NULL) {
456                         fprintf(stderr, "Can't open %s\n", TmpFileName);
457                     }
458                     else
459                         bo = bt;
460                 }
461             }
462             if (intablebut && (bt == NULL))
463                 break;
464             if ((line2[1] == 0) || (line2[1] == '\n')) {
465                     fprintf(bo, ":p.");
466                 para = 0;
467             }
468             if (line2[1] == ' ') {
469                 /* start table in a new paragraph */
470                 if (!tabl) {
471                     fprintf(bo, ":p.%s\n", &line2[1]);
472                     tabl = 1;   /* not in table so start one */
473                     para = 0;
474                 } else {
475                     fprintf(bo, ".br\n%s\n", &line2[1]);
476                 }
477             } else {
478                 if (!para) {
479                     fprintf(bo, ":p.");
480                     para = 1;   /* not in para so start one */
481                     tabl = 0;
482                 }
483                 fprintf(bo, "%s \n", &line2[1]);
484             }
485             fflush(bo);
486             break;
487         }
488     case '^':
489         break;                  /* ignore */
490     default:{
491             TBOOLEAN leaf;
492             
493             if (isdigit((int)line[0])) {        /* start of section */
494                 if (intable) {
495                     intablebut = TRUE;
496                     if (bt == NULL) {
497                         fflush(bo);
498                         bt = fopen(TmpFileName, "w+");
499                         if (bt == NULL) {
500                             fprintf(stderr, "Can't open %s\n", TmpFileName);
501                         }
502                         else
503                             bo = bt;
504                     }
505                 }
506
507 #if 0
508                 /* disabled by BM: this doesn't do anything? */
509                 if (startpage)  /* use new level 0 item */
510                     refs(0, bo, NULL, NULL, NULL);
511                 else
512                     refs(last_line, bo, NULL, NULL, NULL);
513 #endif          
514                 if (debug) {
515                    fprintf(stderr, "%d: %s\n", line_count, &line2[1]);
516                 }
517                 klist = lookup(&line2[2]);
518                 if (klist != NULL)
519                     k = klist->line;
520                     
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 */
523                 if (!startpage)
524                     fprintf(bo, ":p.\n");
525                 
526                 /*if( k<0 ) fprintf(bo,":h%c.", line[0]=='1'?line[0]:line[0]-1);
527                    else */
528
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.
534                 */
535
536                 /* is this section a leaf ? */
537                 leaf = TRUE:    
538                 if (klist)
539                     if (klist->next)
540                         leaf = (klist->next->level <= klist->level);
541                 
542                 /* if not create a reference panel */
543                 if (!leaf) {
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);
551                 }
552                 else {
553                     fprintf(bo, ":h%c res=%d x=right y=top width=80%% height=100%% group=2.", line[0], line_count);
554                 }
555 #else           
556                 fprintf(bo, ":h%c res=%d.", line[0], line_count);
557 #endif          
558                 fprintf(bo, "%s\n", line2+1);   /* title */
559                 
560                 /* add title page */
561                 if (startpage)
562                     fprintf(bo, ".im titlepag.ipf\n");
563                     
564                 para = 0;       /* not in a paragraph */
565                 tabl = 0;       /* not in a table     */
566                 last_line = line_count;
567                 startpage = 0;
568             } else
569                 fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
570                         line[0], line_count);
571         }
572         break;
573     }
574 }