Initial release of Maemo 5 port of gnuplot
[gnuplot] / docs / doc2gih.c
1 #ifndef lint
2 static char *RCSid() { return RCSid("$Id: doc2gih.c,v 1.15 2005/06/03 05:11:55 sfeam Exp $"); }
3 #endif
4
5 /* GNUPLOT - doc2gih.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  * doc2gih.c  -- program to convert Gnuplot .DOC format to gnuplot
39  * interactive help (.GIH) format.
40  *
41  * This involves stripping all lines with a leading digit or
42  * a leading @, #, or %.
43  * Modified by Russell Lang from hlp2ms.c by Thomas Williams
44  *
45  * usage:  doc2gih [file.doc [file.gih]]
46  *
47  * Original version by David Kotz used the following one line script!
48  * sed '/^[0-9@#%]/d' file.doc > file.gih
49  */
50
51 #ifdef HAVE_CONFIG_H
52 # include "config.h"
53 #endif
54
55 #include "syscfg.h"
56 #include "stdfn.h"
57 #include "doc2x.h"
58
59 void convert __PROTO((FILE *, FILE *));
60 void process_line __PROTO((char *, FILE *));
61
62 int
63 main (int argc, char **argv)
64 {
65     FILE *infile;
66     FILE *outfile;
67
68     infile = stdin;
69     outfile = stdout;
70
71     if (argc > 3) {
72         fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
73         exit(EXIT_FAILURE);
74     }
75     if (argc >= 2) {
76         if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
77             fprintf(stderr, "%s: Can't open %s for reading\n",
78                     argv[0], argv[1]);
79             exit(EXIT_FAILURE);
80         }
81     }
82     if (argc == 3) {
83         if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
84             fprintf(stderr, "%s: Can't open %s for writing\n",
85                     argv[0], argv[2]);
86             exit(EXIT_FAILURE);
87         }
88     }
89
90     convert(infile, outfile);
91
92     return EXIT_SUCCESS;
93 }
94
95
96 void
97 convert (FILE *inf, FILE *outf)
98 {
99     static char line[MAX_LINE_LEN+1];
100
101     while (get_line(line, sizeof(line), inf))
102         process_line(line, outf);
103 }
104
105
106 void
107 process_line(char *line, FILE *b)
108 {
109     static int line_count = 0;
110
111     line_count++;
112
113     switch (line[0]) {          /* control character */
114     case '?':{                  /* interactive help entry */
115             (void) fputs(line, b);
116             break;
117         }
118     case '@':{                  /* start/end table */
119             break;              /* ignore */
120         }
121     case '#':{                  /* latex table entry */
122             break;              /* ignore */
123         }
124     case '=':{                  /* latex index entry */
125             break;              /* ignore */
126         }
127     case '%':{                  /* troff table entry */
128             break;              /* ignore */
129         }
130     case '^':{                  /* html entry */
131             break;              /* ignore */
132         }
133     case '\n':                  /* empty text line */
134     case ' ':{                  /* normal text line */
135             (void) fputs(line, b);
136             break;
137         }
138     default:{
139             if (isdigit((int)line[0])) {        /* start of section */
140                 /* ignore */
141             } else
142                 fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
143                         line[0], line_count);
144             break;
145         }
146     }
147 }