1 /* GNUPLOT - doc2gih.c */
2 
3 /*[
4  * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
5  *
6  * Permission to use, copy, and distribute this software and its
7  * documentation for any purpose with or without fee is hereby granted,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.
11  *
12  * Permission to modify the software is granted, but not the right to
13  * distribute the complete modified source code.  Modifications are to
14  * be distributed as patches to the released version.  Permission to
15  * distribute binaries produced by compiling modified sources is granted,
16  * provided you
17  *   1. distribute the corresponding source modifications from the
18  *    released version in the form of a patch file along with the binaries,
19  *   2. add special version identification to distinguish your version
20  *    in addition to the base release version number,
21  *   3. provide your name and address as the primary contact for the
22  *    support of your modified version, and
23  *   4. retain our contact information in regard to use of the base
24  *    software.
25  * Permission to distribute the released version of the source code along
26  * with corresponding source modifications in the form of a patch file is
27  * granted with same provisions 2 through 4 for binary distributions.
28  *
29  * This software is provided "as is" without express or implied warranty
30  * to the extent permitted by applicable law.
31 ]*/
32 
33 /*
34  * doc2gih.c  -- program to convert Gnuplot .DOC format to gnuplot
35  * interactive help (.GIH) format.
36  *
37  * This involves stripping all lines with a leading digit or
38  * a leading @, #, or %.
39  * Modified by Russell Lang from hlp2ms.c by Thomas Williams
40  *
41  * usage:  doc2gih [file.doc [file.gih]]
42  *
43  * Original version by David Kotz used the following one line script!
44  * sed '/^[0-9@#%]/d' file.doc > file.gih
45  */
46 
47 #ifdef HAVE_CONFIG_H
48 # include "config.h"
49 #endif
50 
51 #include "syscfg.h"
52 #include "stdfn.h"
53 #include "doc2x.h"
54 
55 void convert(FILE *, FILE *);
56 void process_line(char *, FILE *);
57 
58 int
main(int argc,char ** argv)59 main (int argc, char **argv)
60 {
61     FILE *infile;
62     FILE *outfile;
63 
64     infile = stdin;
65     outfile = stdout;
66 
67     if (argc > 3) {
68 	fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
69 	exit(EXIT_FAILURE);
70     }
71     if (argc >= 2) {
72 	if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
73 	    fprintf(stderr, "%s: Can't open %s for reading\n",
74 		    argv[0], argv[1]);
75 	    exit(EXIT_FAILURE);
76 	}
77     }
78     if (argc == 3) {
79 	if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
80 	    fprintf(stderr, "%s: Can't open %s for writing\n",
81 		    argv[0], argv[2]);
82 	    exit(EXIT_FAILURE);
83 	}
84     }
85 
86     convert(infile, outfile);
87 
88     return EXIT_SUCCESS;
89 }
90 
91 
92 void
convert(FILE * inf,FILE * outf)93 convert (FILE *inf, FILE *outf)
94 {
95     static char line[MAX_LINE_LEN+1];
96 
97     while (get_line(line, sizeof(line), inf))
98         process_line(line, outf);
99 }
100 
101 
102 void
process_line(char * line,FILE * b)103 process_line(char *line, FILE *b)
104 {
105     static int line_count = 0;
106 
107     line_count++;
108 
109     switch (line[0]) {		/* control character */
110     case '?':{			/* interactive help entry */
111 	    (void) fputs(line, b);
112 	    break;
113 	}
114     case '@':{			/* start/end table */
115 	    break;		/* ignore */
116 	}
117     case '#':{			/* latex table entry */
118 	    if (line[1] == 'b' && line[2] == ' ') {	/* bullet */
119 		fputs(" * ", b);
120 		fputs(line+2, b);
121 	    }  else if (line[1] == '#') {
122 		fputs("   ", b);
123 		fputs(line+2, b);
124 	    }
125 	    break;		/* ignore */
126 	}
127     case '=':{			/* latex index entry */
128 	    break;		/* ignore */
129 	}
130     case 'F':{			/* latex embedded figure */
131 	    break;		/* ignore */
132 	}
133     case '%':{			/* troff table entry */
134 	    break;		/* ignore */
135 	}
136     case '^':{			/* html entry */
137 	    break;		/* ignore */
138 	}
139     case '\n':			/* empty text line */
140     case ' ':{			/* normal text line */
141 	    (void) fputs(line, b);
142 	    break;
143 	}
144     default:{
145 	    if (isdigit((int)line[0])) {	/* start of section */
146 		/* ignore */
147 	    } else
148 		fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
149 			line[0], line_count);
150 	    break;
151 	}
152     }
153 }
154