Initial release of Maemo 5 port of gnuplot
[gnuplot] / docs / doc2rnh.c
1 #ifndef lint
2 static char *RCSid() { return RCSid("$Id: doc2rnh.c,v 1.16 2005/06/03 05:11:55 sfeam Exp $"); }
3 #endif
4
5 /* GNUPLOT - doc2rnh.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  * doc2rnh.c  -- program to convert Gnuplot .DOC format to
39  *               Digital Standard Runoff for VMS HELP files
40  *               (gnuplot.doc, including the terminal documentation
41  *                is no longer formated for VMS HELP by default)
42  *
43  * From hlp2ms by Thomas Williams
44  *
45  * Modified by Russell Lang, 2nd October 1989
46  * to make vms help level 1 and 2 create the same ms section level.
47  *
48  * Modified to become doc2ms by David Kotz (David.Kotz@Dartmouth.edu) 12/89
49  * Added table and backquote support.
50  *
51  * Adapted from doc2ms.c (the unix 'runoff' text-processor)
52  * by Lucas Hart 3/97
53  *
54  * right margin is adjusted two spaces for each level to compensate
55  * for the indentation by VMS HELP
56  *
57  * the page width can be adjusted by changing the value of DSR_RM
58  * usage: $ MCR []doc2rnh gnuplot.doc gnuplot.rnh
59  *        $ RUNOFF gnuplot.rnh
60  *
61  *
62  */
63
64 #ifdef HAVE_CONFIG_H
65 # include "config.h"
66 #endif
67
68 #include "syscfg.h"
69 #include "stdfn.h"
70 #include "doc2x.h"
71
72 extern TBOOLEAN single_top_level;
73
74 #define LINE_SKIP               3
75 #define DSR_RM          70
76
77 void init __PROTO((FILE *));
78 void convert __PROTO((FILE *, FILE *));
79 void process_line __PROTO((char *, FILE *));
80 void section __PROTO((char *, FILE *));
81 void putrnh __PROTO((char *, FILE *));
82 void putrnh_ __PROTO((char *, FILE *));
83 void finish __PROTO((FILE *));
84
85 static TBOOLEAN intable = FALSE;
86 static TBOOLEAN rnh_table = FALSE;
87 static TBOOLEAN initial_entry = FALSE;
88
89 int
90 main (int argc, char **argv)
91 {
92     FILE *infile;
93     FILE *outfile;
94     infile = stdin;
95     outfile = stdout;
96
97     single_top_level = TRUE;
98
99     if (argc > 3) {
100         fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
101         exit(EXIT_FAILURE);
102     }
103     if (argc >= 2) {
104         if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
105             fprintf(stderr, "%s: Can't open %s for reading\n",
106                     argv[0], argv[1]);
107             exit(EXIT_FAILURE);
108         }
109     }
110     if (argc == 3) {
111         if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
112             fprintf(stderr, "%s: Can't open %s for writing\n",
113                     argv[0], argv[2]);
114             exit(EXIT_FAILURE);
115         }
116     }
117     init(outfile);
118     convert(infile, outfile);
119     finish(outfile);
120     return EXIT_SUCCESS;
121 }
122
123
124 void
125 init(FILE *b)
126 {
127     /*     */
128     (void) fputs("\
129 .no paging\n\
130 .no flags all\n\
131 .left margin 1\n\
132 .right margin 70\n\
133 .no justify\n", b);
134 }
135
136
137 void
138 convert(FILE *a, FILE *b)
139 {
140     static char line[MAX_LINE_LEN+1];
141
142     while (get_line(line, sizeof(line), a)) {
143         process_line(line, b);
144     }
145 }
146
147 void
148 process_line(char *line, FILE *b)
149 {
150     switch (line[0]) {          /* control character */
151     case '?':{                  /* interactive help entry */
152             break;              /* ignore */
153         }
154     case '@':{                  /* start/end table */
155             if (rnh_table) {
156                 (void) fputs(".end literal\n", b);
157                 rnh_table = FALSE;
158                 intable = FALSE;
159             } else {
160 /*                       (void) fputs(".literal\n",b);  */
161                 intable = TRUE;
162                 rnh_table = FALSE;
163                 initial_entry = TRUE;
164             }
165             /* ignore rest of line */
166             break;
167         }
168     case '^':{                  /* html table entry */
169             break;              /* ignore */
170         }
171     case '=':                   /* latex index entry */
172     case '#':{                  /* latex table entry */
173             break;              /* ignore */
174         }
175     case '%':{                  /* troff table entry */
176             break;              /* ignore */
177         }
178 #if 0
179 /* 'C' is taken care of by termdoc.c */
180     case 'C':{                  /*  new Comment designator */
181             break;              /* ignore */
182         }
183 #endif
184     case '\n':                  /* empty text line */
185     case ' ':{                  /* normal text line */
186
187 /* most tables are simple; no flags means no protected characters
188  * other than period (command indicator) in first column
189  *
190  * However, for ease of maintainence, two tables have sublevels
191  * and descriptions, corresponding to the printed table entries,
192  * encapsulated by the table markers.  Therefore we need to
193  * do some more work.
194  *
195  * Doc2hlp just ignores the table headings and treats
196  * lower levels irrespectively, but we need to break
197  * the level designators out of the table format.
198  *
199  *  The first entry in a table will have either
200  *     - a level number in the first column => remainder of text in
201  *                                             table is help text
202  *     - spaces in the first two columns => rest of text is literal
203  *                                          to be placed in table format
204  *
205  */
206
207 /* use the "cleartext" table or other text in tables */
208
209 /*                if (intable)  { */ /* its already literal */
210             if (rnh_table) {    /* its a literal */
211                 putrnh(line + 1, b);
212                 break;
213             }
214             switch (line[1]) {
215             case ' ':{
216                     if ((intable) && (initial_entry)) {
217                         rnh_table = TRUE;
218                         initial_entry = FALSE;
219                         fputs(".literal\n", b);
220                         putrnh(line + 1, b);
221                         break;
222                     }
223                     /* verbatim mode */
224                     fputs(".literal\n", b);
225                     putrnh(line + 1, b);
226                     fputs(".end literal\n", b);
227                     break;
228                 }
229
230 /*
231  *  "." in first column is the DSR command character;
232  *  therefore, include the preceeding " "
233  */
234             case '.':{
235                     putrnh(line, b);
236                     break;
237                 }
238             default:{
239                     if (line[0] == '\n')
240                         fputs(".skip\n", b);    /* totally blank line */
241                     else
242                         putrnh(line + 1, b);
243                     break;
244                 }
245                 break;
246             }
247             break;
248         }
249     default:{
250             if (isdigit((int) line[0])) {       /* start of section */
251
252 /* some HELP text is surrounded by table flags */
253 /* doc2rnh will ignore the flags */
254
255                 if (intable) {
256                     if (initial_entry) {
257                         initial_entry = FALSE;
258                         rnh_table = FALSE;
259                     }
260                 }
261                 section(line, b);
262             } else
263                 fprintf(stderr, "unknown control code '%c' in column 1\n",
264                         line[0]);
265             break;
266         }
267     }
268 }
269
270
271 /* process a line with a digit control char */
272 /* starts a new [sub]section */
273 /* We want to retain section number, so its simpler than w/ TeX or roff */
274
275 void
276 section(char *line, FILE *b)
277 {
278     int sh_i;
279     static int old = 1;
280 /*
281    (void) sscanf(line,"%d",&sh_i);
282    *
283    * check to make sure this works with terminals also
284  */
285     sh_i = atoi(line);
286
287     if (sh_i > old) {
288         do
289             if (old != 1)       /* this line added by rjl */
290                 (void) fputs(".rm-2\n", b);
291         while (++old < sh_i);
292     } else if (sh_i < old) {
293         do
294             if (sh_i != 1)      /* this line added by rjl */
295                 (void) fputs(".rm+2\n", b);
296         while (--old > sh_i);
297     }
298     /* added by dfk to capitalize section headers */
299     /* Header name starts at [2] */
300 /* omit for online documentation
301  *    if (islower(line[2]))
302  *       line[2] = toupper(line[2]);
303  */
304     old = sh_i;
305
306     (void) fputs(".indent -1;\n", b);
307     (void) putrnh_(line, b);
308     (void) fputs(".br;\n", b);
309 }
310
311 /*
312  * dummy function in case we need to convert some characters in
313  * output string ala doc2tex and doc2ms
314  */
315
316 void
317 putrnh(char *s, FILE *file)
318 {
319     (void) fputs(s, file);
320 }
321
322 /*
323  * LBR$OUTPUT_HELP treats spaces and "/"s as list separators for topics,
324  * but they are used in gnuplot.doc for the printed docs; convert to
325  * "_" and "|"   Modeled after section heading conversions in doc2tex
326  * and doc2ms.
327  *
328  */
329
330 void
331 putrnh_(char *s, FILE *file)
332 {
333     int i, s_len, last_chr;
334
335     last_chr = s_len = strlen(s);
336
337     for (i = s_len - 1; i > 2; i--) {   /* any trailing spaces to drop? */
338         if (s[i] != ' ') {
339             last_chr = i;
340             break;
341         }
342     }
343
344     for (i = 0; i <= last_chr; i++) {
345         if (i > 2) {
346             switch (s[i]) {
347             case ' ':
348                 (void) fputc('_', file);
349                 break;
350             case '/':
351                 (void) fputc('|', file);
352                 break;
353             default:
354                 (void) fputc(s[i], file);
355             }
356         } else {
357             (void) fputc(s[i], file);
358         }
359     }
360 }
361
362 void
363 finish(FILE *b)
364 {
365     return;
366 }