Debian Codegraph @

tools/man2tcl.c

raw
1 /*
2 * man2tcl.c --
3 *
4 * This file contains a program that turns a man page of the form used
5 * for Tcl and Tk into a Tcl script that invokes a Tcl command for each
6 * construct in the man page. The script can then be eval'ed to translate
7 * the manual entry into some other format such as MIF or HTML.
8 *
9 * Usage:
10 *
11 * man2tcl ?fileName?
12 *
13 * Copyright (c) 1995 Sun Microsystems, Inc.
14 *
15 * See the file "license.terms" for information on usage and redistribution of
16 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 */
18
19 static char sccsid[] = "@(#) man2tcl.c 1.3 95/08/12 17:34:08";
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <errno.h>
26
27 /*
28 * Imported things that aren't defined in header files:
29 */
30
31 /*
32 * Some <errno.h> define errno to be something complex and thread-aware; in
33 * that case we definitely do not want to declare errno ourselves!
34 */
35
36 #ifndef errno
37 extern int errno;
38 #endif
39
40 /*
41 * Current line number, used for error messages.
42 */
43
44 static int lineNumber;
45
46 /*
47 * The variable below is set to 1 if an error occurs anywhere while reading in
48 * the file.
49 */
50
51 static int status;
52
53 /*
54 * The variable below is set to 1 if output should be generated. If it's 0, it
55 * means we're doing a prepass to make sure that the file can be properly
56 * parsed.
57 */
58
59 static int writeOutput;
60
61 #define PRINT(args) if (writeOutput) { printf args; }
62 #define PRINTC(chr) if (writeOutput) { putc((chr), stdout); }
63
64 /*
65 * Prototypes for functions defined in this file:
66 */
67
68 static void DoMacro(char *line);
69 static void DoText(char *line);
70 static void QuoteText(char *string, int count);
71
72 /*
73 *----------------------------------------------------------------------
74 *
75 * main --
76 *
77 * This function is the main program, which does all of the work of the
78 * program.
79 *
80 * Results:
81 * None: exits with a 0 return status to indicate success, or 1 to
82 * indicate that there were problems in the translation.
83 *
84 * Side effects:
85 * A Tcl script is output to standard output. Error messages may be
86 * output on standard error.
87 *
88 *----------------------------------------------------------------------
89 */
90
91 int
92 main(
93 int argc, /* Number of command-line arguments. */
94 char **argv) /* Values of command-line arguments. */
95 {
96 FILE *f;
97 #define MAX_LINE_SIZE 4000
98 char line[MAX_LINE_SIZE];
99 char *p;
100
101 /*
102 * Find the file to read, and open it if it isn't stdin.
103 */
104
105 if (argc == 1) {
106 f = stdin;
107 } else if (argc == 2) {
108 f = fopen(argv[1], "r");
109 if (f == NULL) {
110 fprintf(stderr, "Couldn't read \"%s\": %s\n", argv[1],
111 strerror(errno));
112 exit(1);
113 }
114 } else {
115 fprintf(stderr, "Usage: %s ?fileName?\n", argv[0]);
116 }
117
118 /*
119 * Make two passes over the file. In the first pass, just check to make
120 * sure we can handle everything. If there are problems, generate output
121 * and stop. If everything is OK, make a second pass to actually generate
122 * output.
123 */
124
125 for (writeOutput = 0; writeOutput < 2; writeOutput++) {
126 lineNumber = 0;
127 status = 0;
128 while (fgets(line, MAX_LINE_SIZE, f) != NULL) {
129 for (p = line; *p != 0; p++) {
130 if (*p == '\n') {
131 *p = 0;
132 break;
133 }
134 }
135 lineNumber++;
136
137 if (((line[0] == '.') || (line[0] == '\'')) && (line[1] == '\\') && (line[2] == '\"')) {
138 /*
139 * This line is a comment. Ignore it.
140 */
141
142 continue;
143 }
144
145 if (strlen(line) >= MAX_LINE_SIZE -1) {
146 fprintf(stderr, "Too long line. Max is %d chars.\n",
147 MAX_LINE_SIZE - 1);
148 exit(1);
149 }
150
151 if ((line[0] == '.') || (line[0] == '\'')) {
152 /*
153 * This line is a macro invocation.
154 */
155
156 DoMacro(line);
157 } else {
158 /*
159 * This line is text, possibly with formatting characters
160 * embedded in it.
161 */
162
163 DoText(line);
164 }
165 }
166 if (status != 0) {
167 break;
168 }
169 fseek(f, 0, SEEK_SET);
170 }
171 exit(status);
172 }
173
174 /*
175 *----------------------------------------------------------------------
176 *
177 * DoMacro --
178 *
179 * This function is called to handle a macro invocation. It parses the
180 * arguments to the macro and generates a Tcl command to handle the
181 * invocation.
182 *
183 * Results:
184 * None.
185 *
186 * Side effects:
187 * A Tcl command is written to stdout.
188 *
189 *----------------------------------------------------------------------
190 */
191
192 static void
193 DoMacro(
194 char *line) /* The line of text that contains the macro
195 * invocation. */
196 {
197 char *p, *end;
198 int quote;
199
200 /*
201 * If there is no macro name, then just skip the whole line.
202 */
203
204 if ((line[1] == 0) || (isspace(line[1]))) {
205 return;
206 }
207
208 PRINT(("macro"));
209 if (*line != '.') {
210 PRINT(("2"));
211 }
212
213 /*
214 * Parse the arguments to the macro (including the name), in order.
215 */
216
217 p = line+1;
218 while (1) {
219 PRINTC(' ');
220 if (*p == '"') {
221 /*
222 * The argument is delimited by quotes.
223 */
224
225 for (end = p+1; *end != '"'; end++) {
226 if (*end == 0) {
227 fprintf(stderr,
228 "Unclosed quote in macro call on line %d.\n",
229 lineNumber);
230 status = 1;
231 break;
232 }
233 }
234 QuoteText(p+1, (end-(p+1)));
235 } else {
236 quote = 0;
237 for (end = p+1; (*end != 0) && (quote || !isspace(*end)); end++) {
238 if (*end == '\'') {
239 quote = !quote;
240 }
241 }
242 QuoteText(p, end-p);
243 }
244 if (*end == 0) {
245 break;
246 }
247 p = end+1;
248 while (isspace(*p)) {
249 /*
250 * Skip empty space before next argument.
251 */
252
253 p++;
254 }
255 if (*p == 0) {
256 break;
257 }
258 }
259 PRINTC('\n');
260 }
261
262 /*
263 *----------------------------------------------------------------------
264 *
265 * DoText --
266 *
267 * This function is called to handle a line of troff text. It parses the
268 * text, generating Tcl commands for text and for formatting stuff such
269 * as font changes.
270 *
271 * Results:
272 * None.
273 *
274 * Side effects:
275 * Tcl commands are written to stdout.
276 *
277 *----------------------------------------------------------------------
278 */
279
280 static void
281 DoText(
282 char *line) /* The line of text. */
283 {
284 char *p, *end;
285
286 /*
287 * Divide the line up into pieces consisting of backslash sequences, tabs,
288 * and other text.
289 */
290
291 p = line;
292 while (*p != 0) {
293 if (*p == '\t') {
294 PRINT(("tab\n"));
295 p++;
296 } else if (*p != '\\') {
297 /*
298 * Ordinary text.
299 */
300
301 for (end = p+1; (*end != '\\') && (*end != 0); end++) {
302 /* Empty loop body. */
303 }
304 PRINT(("text "));
305 QuoteText(p, end-p);
306 PRINTC('\n');
307 p = end;
308 } else {
309 /*
310 * A backslash sequence. There are particular ones that we
311 * understand; output an error message for anything else and just
312 * ignore the backslash.
313 */
314
315 p++;
316 if (*p == 'f') {
317 /*
318 * Font change.
319 */
320
321 PRINT(("font %c\n", p[1]));
322 p += 2;
323 } else if (*p == '-') {
324 PRINT(("dash\n"));
325 p++;
326 } else if (*p == 'e') {
327 PRINT(("text \\\\\n"));
328 p++;
329 } else if (*p == '.') {
330 PRINT(("text .\n"));
331 p++;
332 } else if (*p == '&') {
333 p++;
334 } else if (*p == '0') {
335 PRINT(("text { }\n"));
336 p++;
337 } else if (*p == '(') {
338 if ((p[1] == 0) || (p[2] == 0)) {
339 fprintf(stderr, "Bad \\( sequence on line %d.\n",
340 lineNumber);
341 status = 1;
342 } else {
343 PRINT(("char {\\(%c%c}\n", p[1], p[2]));
344 p += 3;
345 }
346 } else if (*p == 'N' && *(p+1) == '\'') {
347 int ch;
348
349 p += 2;
350 sscanf(p,"%d",&ch);
351 PRINT(("text \\u%04x\n", ch));
352 while(*p&&*p!='\'') p++;
353 p++;
354 } else if (*p != 0) {
355 PRINT(("char {\\%c}\n", *p));
356 p++;
357 }
358 }
359 }
360 PRINT(("newline\n"));
361 }
362
363 /*
364 *----------------------------------------------------------------------
365 *
366 * QuoteText --
367 *
368 * Copy the "string" argument to stdout, adding quote characters around
369 * any special Tcl characters so that they'll just be treated as ordinary
370 * text.
371 *
372 * Results:
373 * None.
374 *
375 * Side effects:
376 * Text is written to stdout.
377 *
378 *----------------------------------------------------------------------
379 */
380
381 static void
382 QuoteText(
383 char *string, /* The line of text. */
384 int count) /* Number of characters to write from
385 * string. */
386 {
387 if (count == 0) {
388 PRINT(("{}"));
389 return;
390 }
391 for ( ; count > 0; string++, count--) {
392 switch (*string) {
393 case '\\':
394 if (*(string+1) == 'N' && *(string+2) == '\'') {
395 int ch;
396
397 string += 3;
398 count -= 3;
399 sscanf(string,"%d",&ch);
400 PRINT(("\\u%04x", ch));
401 while(count>0&&*string!='\'') {string++;count--;}
402 continue;
403 } else if (*(string+1) == '0') {
404 PRINT(("\\ "));
405 string++;
406 count--;
407 continue;
408 }
409 case '$': case '[': case '{': case ' ': case ';':
410 case '"': case '\t':
411 PRINTC('\\');
412 default:
413 PRINTC(*string);
414 }
415 }
416 }
417
418 /*
419 * Local Variables:
420 * mode: c
421 * c-basic-offset: 4
422 * fill-column: 78
423 * End:
424 */