2 static char *RCSid() { return RCSid("$Id: doc2rtf.c,v 1.15 2005/06/03 05:11:55 sfeam Exp $"); }
5 /* GNUPLOT - doc2rtf.c */
8 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
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.
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,
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
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.
33 * This software is provided "as is" without express or implied warranty
34 * to the extent permitted by applicable law.
38 * doc2rtf.c -- program to convert Gnuplot .DOC format to MS windows
41 * This involves stripping all lines with a leading digit or
42 * a leading @, #, or %.
43 * Modified by Maurice Castro from doc2gih.c by Thomas Williams
45 * usage: doc2rtf file.doc file.rtf [-d]
49 /* note that tables must begin in at least the second column to */
50 /* be formatted correctly and tabs are forbidden */
58 #define MAX_LINE_LEN 1023
62 static TBOOLEAN debug = FALSE;
64 void footnote __PROTO((int, char *, FILE *));
65 void convert __PROTO((FILE *, FILE *));
66 void process_line __PROTO((char *, FILE *));
69 main (int argc, char **argv)
73 if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
76 if (argc != 3 && !debug) {
77 fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
80 if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
81 fprintf(stderr, "%s: Can't open %s for reading\n",
85 if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
86 fprintf(stderr, "%s: Can't open %s for writing\n",
91 convert(infile, outfile);
92 return (EXIT_SUCCESS);
95 /* generate an RTF footnote with reference char c and text s */
97 footnote(int c, char *s, FILE *b)
99 fprintf(b, "%c{\\footnote %c %s}\n", c, c, s);
103 convert(FILE *a, FILE *b)
105 static char line[MAX_LINE_LEN+1];
107 /* generate rtf header */
108 fprintf(b, "{\\rtf1\\ansi "); /* vers 1 rtf, ansi char set */
109 fprintf(b, "\\deff0"); /* default font font 0 */
110 /* font table: font 0 proportional, font 1 fixed */
111 fprintf(b, "{\\fonttbl{\\f0\\fswiss Arial;}{\\f1\\fmodern Courier New;}}\n");
113 /* process each line of the file */
114 while (get_line(line, sizeof(line), a)) {
115 process_line(line, b);
118 /* close final page and generate trailer */
119 fprintf(b, "}{\\plain \\page}\n");
120 /* fprintf(b,"}\n"); */ /* HBB: HACK ALERT: only without this, hc31 works */
125 process_line(char *line, FILE *b)
127 static int line_count = 0;
128 static char line2[MAX_LINE_LEN+1];
129 static int last_line;
132 static int startpage = 1;
133 char str[MAX_LINE_LEN+1];
134 char topic[MAX_LINE_LEN+1];
138 static int llpara = 0;
139 static int inquote = FALSE;
140 static int inref = FALSE;
147 while (line[i] != NUL) {
159 case '`': /* backquotes mean boldface or link */
160 if (line[1] == ' ') /* tabular line */
162 else if ((!inref) && (!inquote)) {
163 k = i + 1; /* index into current string */
164 l = 0; /* index into topic string */
165 while ((line[k] != '`') && (line[k] != NUL))
166 topic[l++] = line[k++];
168 klist = lookup(topic);
169 if (klist && (k = klist->line) > 0 && (k != last_line)) {
180 fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
188 if (inquote && inref)
189 fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
196 sprintf(topic, "%d", inref);
206 while (topic[k] != NUL)
207 line2[j++] = topic[k++];
223 switch (line[0]) { /* control character */
224 case '?':{ /* interactive help entry */
225 if ((line2[1] != NUL) && (line2[1] != ' '))
226 footnote('K', &(line2[1]), b);
229 case '@':{ /* start/end table */
232 case '^':{ /* html link escape */
235 case '=': /* latex index entry */
236 case '#':{ /* latex table entry */
239 case '%':{ /* troff table entry */
242 case '\n': /* empty text line */
243 fprintf(b, "\\par\n");
248 case ' ':{ /* normal text line */
249 if ((line2[1] == NUL) || (line2[1] == '\n')) {
250 fprintf(b, "\\par\n");
255 if (line2[1] == ' ') {
257 fprintf(b, "\\par\n");
259 fprintf(b, "{\\pard \\plain \\f1\\fs20 ");
260 fprintf(b, "%s", &line2[1]);
261 fprintf(b, "}\\par\n");
268 fprintf(b, "\\par\n"); /* blank line between paragraphs */
270 para = 1; /* not in para so start one */
272 fprintf(b, "\\pard \\plain \\qj \\fs20 \\f0 ");
274 fprintf(b, "%s \n", &line2[1]);
279 if (isdigit((int)line[0])) { /* start of section */
280 if (startpage) { /* use new level 0 item */
281 refs(0, b, "\\par", NULL, "\\par{\\uldb %s}{\\v loc%d}\n");
282 fprintf(b, "}{\\plain \\page}\n");
284 refs(last_line, b, "\\par", NULL, "\\par{\\uldb %s}{\\v loc%d}\n");
285 fprintf(b, "}{\\plain \\page}\n");
287 para = 0; /* not in a paragraph */
289 last_line = line_count;
292 sprintf(str, "browse:%05d", line_count);
293 footnote('+', str, b);
294 footnote('$', &(line2[1]), b); /* title */
295 fprintf(b, "{\\b \\fs24 %s}\\plain\\par\\par\n", &(line2[1]));
296 /* output unique ID */
297 sprintf(str, "loc%d", line_count);
298 footnote('#', str, b);
300 fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
301 line[0], line_count);