2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "stringclass.h"
25 #include "searchpath.h"
26 #include "macropath.h"
31 #define STARTUP_FILE "eqnrc"
34 extern "C" const char *Version_string;
36 static char *delim_search (char *, int);
37 static int inline_equation (FILE *, string &, string &);
39 char start_delim = '\0';
40 char end_delim = '\0';
44 int one_size_reduction_flag = 0;
45 int compatible_flag = 0;
46 int no_newline_in_delim_flag = 0;
50 int read_line(FILE *fp, string *p)
54 while ((c = getc(fp)) != EOF) {
55 if (!invalid_input_char(c))
58 error("invalid input character code `%1'", c);
63 return p->length() > 0;
66 void do_file(FILE *fp, const char *filename)
70 printf(".lf 1 %s\n", filename);
71 current_filename = filename;
73 while (read_line(fp, &linebuf)) {
74 if (linebuf.length() >= 4
75 && linebuf[0] == '.' && linebuf[1] == 'l' && linebuf[2] == 'f'
76 && (linebuf[3] == ' ' || linebuf[3] == '\n' || compatible_flag)) {
77 put_string(linebuf, stdout);
79 if (interpret_lf_args(linebuf.contents() + 3))
82 else if (linebuf.length() >= 4
86 && (linebuf[3] == ' ' || linebuf[3] == '\n'
87 || compatible_flag)) {
88 put_string(linebuf, stdout);
89 int start_lineno = current_lineno + 1;
92 if (!read_line(fp, &linebuf))
93 fatal("end of file before .EN");
94 if (linebuf.length() >= 3 && linebuf[0] == '.' && linebuf[1] == 'E') {
96 && (linebuf.length() == 3 || linebuf[3] == ' '
97 || linebuf[3] == '\n' || compatible_flag))
99 else if (linebuf[2] == 'Q' && linebuf.length() > 3
100 && (linebuf[3] == ' ' || linebuf[3] == '\n'
108 init_lex(str.contents(), current_filename, start_lineno);
112 restore_compatibility();
113 if (non_empty_flag) {
114 printf(".lf %d\n", current_lineno - 1);
117 printf(".lf %d\n", current_lineno);
118 put_string(linebuf, stdout);
120 else if (start_delim != '\0' && linebuf.search(start_delim) >= 0
121 && inline_equation(fp, linebuf, str))
124 put_string(linebuf, stdout);
126 current_filename = 0;
130 // Handle an inline equation. Return 1 if it was an inline equation,
132 static int inline_equation(FILE *fp, string &linebuf, string &str)
135 char *ptr = &linebuf[0];
136 char *start = delim_search(ptr, start_delim);
138 // It wasn't a delimiter after all.
139 linebuf.set_length(linebuf.length() - 1); // strip the '\0'
145 if (no_newline_in_delim_flag && strchr(start + 1, end_delim) == 0) {
146 error("missing `%1'", end_delim);
147 char *nl = strchr(start + 1, '\n');
153 int start_lineno = current_lineno;
159 char *end = strchr(ptr, end_delim);
167 if (!read_line(fp, &linebuf))
168 fatal("unterminated `%1' at line %2, looking for `%3'",
169 start_delim, start_lineno, end_delim);
175 printf(".as1 %s ", LINE_STRING);
176 html_begin_suppress();
179 init_lex(str.contents(), current_filename, start_lineno);
182 printf(".as1 %s ", LINE_STRING);
186 start = delim_search(ptr, start_delim);
188 char *nl = strchr(ptr, '\n');
195 restore_compatibility();
196 printf(".lf %d\n", current_lineno);
198 printf(".lf %d\n", current_lineno + 1);
202 /* Search for delim. Skip over number register and string names etc. */
204 static char *delim_search(char *ptr, int delim)
209 if (*ptr++ == '\\') {
221 if (*++ptr != '\\' && *ptr != '\0'
222 && *++ptr != '\\' && *ptr != '\0')
226 while (*++ptr != '\0')
249 void usage(FILE *stream)
252 "usage: %s [ -rvDCNR ] -dxx -fn -sn -pn -mn -Mdir -Ts [ files ... ]\n",
256 int main(int argc, char **argv)
258 program_name = argv[0];
259 static char stderr_buf[BUFSIZ];
260 setbuf(stderr, stderr_buf);
262 int load_startup_file = 1;
263 static const struct option long_options[] = {
264 { "help", no_argument, 0, CHAR_MAX + 1 },
265 { "version", no_argument, 0, 'v' },
268 while ((opt = getopt_long(argc, argv, "DCRvd:f:p:s:m:T:M:rN", long_options,
275 case 'R': // don't load eqnrc
276 load_startup_file = 0;
279 config_macro_path.command_line_dir(optarg);
283 printf("GNU eqn (groff) version %s\n", Version_string);
288 if (optarg[0] == '\0' || optarg[1] == '\0')
289 error("-d requires two character argument");
290 else if (invalid_input_char(optarg[0]))
291 error("bad delimiter `%1'", optarg[0]);
292 else if (invalid_input_char(optarg[1]))
293 error("bad delimiter `%1'", optarg[1]);
295 start_delim = optarg[0];
296 end_delim = optarg[1];
304 if (strcmp(device, "ps:html") == 0) {
310 if (!set_gsize(optarg))
311 error("invalid size `%1'", optarg);
316 if (sscanf(optarg, "%d", &n) == 1)
317 set_script_reduction(n);
319 error("bad size `%1'", optarg);
325 if (sscanf(optarg, "%d", &n) == 1)
328 error("bad size `%1'", optarg);
332 one_size_reduction_flag = 1;
335 warning("-D option is obsolete: use `set draw_lines 1' instead");
339 no_newline_in_delim_flag = 1;
341 case CHAR_MAX + 1: // --help
354 printf(".if !'\\*(.T'%s' "
355 ".if !'\\*(.T'html' " // the html device uses `-Tps' to render
356 // equations as images
357 ".tm warning: %s should have been given a `-T\\*(.T' option\n",
358 device, program_name);
359 printf(".if '\\*(.T'html' "
361 ".tm warning: %s should have been given a `-Tps' option\n",
362 device, program_name);
363 printf(".if '\\*(.T'html' "
365 ".tm warning: (it is advisable to invoke groff via: groff -Thtml -e)\n",
367 if (load_startup_file) {
369 FILE *fp = config_macro_path.open_file(STARTUP_FILE, &path);
379 for (int i = optind; i < argc; i++)
380 if (strcmp(argv[i], "-") == 0)
384 FILE *fp = fopen(argv[i], "r");
386 fatal("can't open `%1': %2", argv[i], strerror(errno));
388 do_file(fp, argv[i]);
392 if (ferror(stdout) || fflush(stdout) < 0)
393 fatal("output error");