Annotation of OpenXM_contrib/gnuplot/docs/doc2tex.c, Revision 1.1.1.1
1.1 maekawa 1: /*
2: * $Id: doc2tex.c,v 1.21 1998/06/18 14:59:12 ddenholm Exp $
3: *
4: */
5:
6: /* GNUPLOT - doc2tex.c */
7:
8: /*[
9: * Copyright 1986 - 1993, 1998 Thomas Williams, Colin Kelley
10: *
11: * Permission to use, copy, and distribute this software and its
12: * documentation for any purpose with or without fee is hereby granted,
13: * provided that the above copyright notice appear in all copies and
14: * that both that copyright notice and this permission notice appear
15: * in supporting documentation.
16: *
17: * Permission to modify the software is granted, but not the right to
18: * distribute the complete modified source code. Modifications are to
19: * be distributed as patches to the released version. Permission to
20: * distribute binaries produced by compiling modified sources is granted,
21: * provided you
22: * 1. distribute the corresponding source modifications from the
23: * released version in the form of a patch file along with the binaries,
24: * 2. add special version identification to distinguish your version
25: * in addition to the base release version number,
26: * 3. provide your name and address as the primary contact for the
27: * support of your modified version, and
28: * 4. retain our contact information in regard to use of the base
29: * software.
30: * Permission to distribute the released version of the source code along
31: * with corresponding source modifications in the form of a patch file is
32: * granted with same provisions 2 through 4 for binary distributions.
33: *
34: * This software is provided "as is" without express or implied warranty
35: * to the extent permitted by applicable law.
36: ]*/
37:
38: /*
39: * doc2tex.c -- program to convert Gnuplot .DOC format to LaTeX document
40: * Also will work for VMS .HLP files.
41: * Modified by Russell Lang from hlp2ms.c by Thomas Williams
42: * Extended by David Kotz to support quotes ("), backquotes, tables.
43: * Extended by Jens Emmerich to handle '_', '---', paired single
44: * quotes. Changed "-handling. Added pre/post-verbatim hooks.
45: *
46: *
47: * usage: doc2tex [file.doc [file.tex]]
48: *
49: * where file.doc is a Gnuplot .DOC file, and file.tex will be an
50: * article document suitable for printing with LaTeX.
51: *
52: * typical usage for GNUPLOT:
53: *
54: * doc2tex gnuplot.doc gnuplot.tex
55: * latex gnuplot.tex ; latex gnuplot.tex
56: */
57:
58: #ifdef HAVE_CONFIG_H
59: # include "config.h"
60: #endif
61:
62: #include "ansichek.h"
63: #include "stdfn.h"
64: #include "doc2x.h"
65:
66: void init __PROTO((FILE *));
67: void convert __PROTO((FILE *, FILE *));
68: void process_line __PROTO((char *, FILE *));
69: void section __PROTO((char *, FILE *));
70: void puttex __PROTO((char *, FILE *));
71: void finish __PROTO((FILE *));
72:
73: static boolean intable = FALSE;
74: static boolean verb = FALSE;
75:
76: int main(argc, argv)
77: int argc;
78: char **argv;
79: {
80: FILE *infile;
81: FILE *outfile;
82:
83: infile = stdin;
84: outfile = stdout;
85:
86: if (argc > 3) {
87: fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
88: exit(EXIT_FAILURE);
89: }
90: if (argc >= 2) {
91: if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
92: fprintf(stderr, "%s: Can't open %s for reading\n",
93: argv[0], argv[1]);
94: exit(EXIT_FAILURE);
95: }
96: }
97: if (argc == 3) {
98: if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
99: fprintf(stderr, "%s: Can't open %s for writing\n",
100: argv[0], argv[2]);
101: exit(EXIT_FAILURE);
102: }
103: }
104: init(outfile);
105: convert(infile, outfile);
106: finish(outfile);
107: exit(EXIT_SUCCESS);
108: }
109:
110:
111: void init(b)
112: FILE *b;
113: {
114: (void) fputs("\\input{titlepag.tex}\n", b);
115: }
116:
117:
118: void convert(a, b)
119: FILE *a, *b;
120: {
121: static char line[MAX_LINE_LEN+1];
122:
123: while (get_line(line, sizeof(line), a))
124: process_line(line, b);
125:
126: }
127:
128: void process_line(line, b)
129: char *line;
130: FILE *b;
131: {
132: switch (line[0]) { /* control character */
133: case '?':{ /* interactive help entry */
134: break; /* ignore */
135: }
136: case '@':{ /* start/end table */
137: if (intable) {
138: (void) fputs("\\hline\n\\end{tabular}\n", b);
139: (void) fputs("\\end{center}\n", b);
140: intable = FALSE;
141: } else {
142: if (verb) {
143: (void) fputs("\\end{verbatim}\n", b);
144: (void) fputs("\\postverbatim\n", b);
145: verb = FALSE;
146: }
147: (void) fputs("\n\\begin{center}\n", b);
148: /* moved to gnuplot.doc by RCC
149: (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
150: */
151: intable = TRUE;
152: }
153: /* ignore rest of line */
154: break;
155: }
156: case '#':{ /* latex table entry */
157: if (intable)
158: (void) fputs(line + 1, b); /* copy directly */
159: else {
160: fprintf(stderr, "warning: # line found outside of table\n");
161: fprintf(stderr, "%s\n", line + 1);
162: }
163:
164: break;
165: }
166: case '^':{ /* external link escape */
167: break; /* ignore */
168: }
169: case '%':{ /* troff table entry */
170: break; /* ignore */
171: }
172: case '\n': /* empty text line */
173: case ' ':{ /* normal text line */
174: if (intable)
175: break; /* ignore while in table */
176: if (line[1] == ' ') {
177: /* verbatim mode */
178: if (!verb) {
179: (void) fputs("\\preverbatim\n", b);
180: (void) fputs("\\begin{verbatim}\n", b);
181: verb = TRUE;
182: }
183: (void) fputs(line + 2, b);
184: } else {
185: if (verb) {
186: (void) fputs("\\end{verbatim}\n", b);
187: (void) fputs("\\postverbatim\n", b);
188: verb = FALSE;
189: }
190: if (line[0] == '\n')
191: puttex(line, b); /* handle totally blank line */
192: else
193: puttex(line + 1, b);
194: }
195: break;
196: }
197: default:{
198: if (isdigit((int) line[0])) { /* start of section */
199: if (!intable) /* ignore while in table */
200: section(line, b);
201: } else
202: fprintf(stderr, "unknown control code '%c' in column 1\n",
203: line[0]);
204: break;
205: }
206: }
207: }
208:
209: /* process a line with a digit control char */
210: /* starts a new [sub]section */
211:
212: void section(line, b)
213: char *line;
214: FILE *b;
215: {
216: static char string[MAX_LINE_LEN+1];
217: int sh_i;
218:
219: if (verb) {
220: (void) fputs("\\end{verbatim}\n", b);
221: (void) fputs("\\postverbatim\n", b);
222: verb = FALSE;
223: }
224: (void) sscanf(line, "%d %[^\n]s", &sh_i, string);
225: switch (sh_i) {
226: case 1:
227: (void) fprintf(b, "\\part{");
228: break;
229: case 2:
230: (void) fprintf(b, "\\section{");
231: break;
232: case 3:
233: (void) fprintf(b, "\\subsection{");
234: break;
235: case 4:
236: (void) fprintf(b, "\\subsubsection{");
237: break;
238: case 5:
239: (void) fprintf(b, "\\subsubsubsection{");
240: break;
241: default:
242: case 6:
243: (void) fprintf(b, "\\paragraph{");
244: break;
245: }
246: if (islower((int) string[0]))
247: string[0] = toupper(string[0]);
248: puttex(string, b);
249: (void) fprintf(b, "}\n");
250: }
251:
252: /* put text in string str to file while buffering special TeX characters */
253: void puttex(str, file)
254: FILE *file;
255: register char *str;
256: {
257: register char ch;
258: static boolean inquote = FALSE;
259: int i;
260:
261: while ((ch = *str++) != NUL) {
262: switch (ch) {
263: case '#':
264: case '$':
265: case '%':
266: case '&':
267: case '{':
268: case '}':
269: (void) fputc('\\', file);
270: (void) fputc(ch, file);
271: break;
272: case '\\':
273: (void) fputs("$\\backslash$", file);
274: break;
275: case '~':
276: (void) fputs("\\~{\\ }", file);
277: break;
278: case '^':
279: (void) fputs("\\verb+^+", file);
280: break;
281: case '>':
282: case '<':
283: case '|':
284: (void) fputc('$', file);
285: (void) fputc(ch, file);
286: (void) fputc('$', file);
287: break;
288: case '"':
289: (void) fputs("{\\tt\"}", file);
290: break;
291: case '\'':
292: if (*str == '\'') {
293: (void) fputs("{'\\,'}", file);
294: str++;
295: } else {
296: (void) fputc(ch, file);
297: }
298: break;
299: case '-':
300: if ((*str == '-') && (*(str + 1) == '-')) {
301: (void) fputs(" --- ", file);
302: str += 2;
303: } else {
304: (void) fputc(ch, file);
305: }
306: break;
307: case '`': /* backquotes mean boldface */
308: if (inquote) {
309: (void) fputs("}", file);
310: inquote = FALSE;
311: } else {
312: (void) fputs("{\\bf ", file);
313: inquote = TRUE;
314: }
315: break;
316: case '_': /* emphasised text ? */
317: for (i = 0; isalpha((int) (*(str + i))); i++);
318: if ((i > 0) && (*(str + i) == '_') &&
319: isspace((int) (*(str + i + 1)))) {
320: (void) fputs("{\\em ", file);
321: for (; *str != '_'; str++) {
322: (void) fputc(*str, file);
323: }
324: str++;
325: (void) fputs("\\/}", file);
326: } else {
327: (void) fputs("{\\tt\\_}", file);
328: }
329: break;
330: default:
331: (void) fputc(ch, file);
332: break;
333: }
334: }
335: }
336:
337:
338: void finish(b)
339: FILE *b;
340: {
341: (void) fputs("\\end{document}\n", b);
342: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>