2 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "stringclass.h"
23 #include "searchpath.h"
24 #include "macropath.h"
29 #define STARTUP_FILE "eqnrc"
32 extern "C" const char *Version_string;
34 static char *delim_search (char *, int);
35 static int inline_equation (FILE *, string &, string &);
37 char start_delim = '\0';
38 char end_delim = '\0';
42 int one_size_reduction_flag = 0;
43 int compatible_flag = 0;
44 int no_newline_in_delim_flag = 0;
47 eqnmode_t output_format;
49 int read_line(FILE *fp, string *p)
53 while ((c = getc(fp)) != EOF) {
54 if (!invalid_input_char(c))
57 error("invalid input character code `%1'", c);
62 return p->length() > 0;
65 void do_file(FILE *fp, const char *filename)
69 if (output_format == troff)
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 if (output_format == mathml)
117 printf(".lf %d\n", current_lineno - 1);
121 if (output_format == troff)
122 printf(".lf %d\n", current_lineno);
123 put_string(linebuf, stdout);
125 else if (start_delim != '\0' && linebuf.search(start_delim) >= 0
126 && inline_equation(fp, linebuf, str))
129 put_string(linebuf, stdout);
131 current_filename = 0;
135 // Handle an inline equation. Return 1 if it was an inline equation,
137 static int inline_equation(FILE *fp, string &linebuf, string &str)
140 char *ptr = &linebuf[0];
141 char *start = delim_search(ptr, start_delim);
143 // It wasn't a delimiter after all.
144 linebuf.set_length(linebuf.length() - 1); // strip the '\0'
150 if (no_newline_in_delim_flag && strchr(start + 1, end_delim) == 0) {
151 error("missing `%1'", end_delim);
152 char *nl = strchr(start + 1, '\n');
158 int start_lineno = current_lineno;
164 char *end = strchr(ptr, end_delim);
172 if (!read_line(fp, &linebuf))
173 fatal("unterminated `%1' at line %2, looking for `%3'",
174 start_delim, start_lineno, end_delim);
179 if (output_format == troff && html) {
180 printf(".as1 %s ", LINE_STRING);
181 html_begin_suppress();
184 init_lex(str.contents(), current_filename, start_lineno);
186 if (output_format == troff && html) {
187 printf(".as1 %s ", LINE_STRING);
191 if (output_format == mathml)
194 /* skip leading spaces */
195 while ((*ptr != '\0') && (*ptr == ' '))
198 start = delim_search(ptr, start_delim);
200 char *nl = strchr(ptr, '\n');
207 restore_compatibility();
208 if (output_format == troff)
209 printf(".lf %d\n", current_lineno);
211 if (output_format == troff)
212 printf(".lf %d\n", current_lineno + 1);
216 /* Search for delim. Skip over number register and string names etc. */
218 static char *delim_search(char *ptr, int delim)
223 if (*ptr++ == '\\') {
235 if (*++ptr != '\\' && *ptr != '\0'
236 && *++ptr != '\\' && *ptr != '\0')
240 while (*++ptr != '\0')
263 void usage(FILE *stream)
266 "usage: %s [ -rvDCNR ] -dxx -fn -sn -pn -mn -Mdir -Ts [ files ... ]\n",
270 int main(int argc, char **argv)
272 program_name = argv[0];
273 static char stderr_buf[BUFSIZ];
274 setbuf(stderr, stderr_buf);
276 int load_startup_file = 1;
277 static const struct option long_options[] = {
278 { "help", no_argument, 0, CHAR_MAX + 1 },
279 { "version", no_argument, 0, 'v' },
282 while ((opt = getopt_long(argc, argv, "DCRvd:f:p:s:m:T:M:rN", long_options,
289 case 'R': // don't load eqnrc
290 load_startup_file = 0;
293 config_macro_path.command_line_dir(optarg);
296 printf("GNU eqn (groff) version %s\n", Version_string);
300 if (optarg[0] == '\0' || optarg[1] == '\0')
301 error("-d requires two character argument");
302 else if (invalid_input_char(optarg[0]))
303 error("bad delimiter `%1'", optarg[0]);
304 else if (invalid_input_char(optarg[1]))
305 error("bad delimiter `%1'", optarg[1]);
307 start_delim = optarg[0];
308 end_delim = optarg[1];
316 if (strcmp(device, "ps:html") == 0) {
320 else if (strcmp(device, "MathML") == 0) {
321 output_format = mathml;
322 load_startup_file = 0;
324 else if (strcmp(device, "mathml:xhtml") == 0) {
326 output_format = mathml;
327 load_startup_file = 0;
332 if (!set_gsize(optarg))
333 error("invalid size `%1'", optarg);
338 if (sscanf(optarg, "%d", &n) == 1)
339 set_script_reduction(n);
341 error("bad size `%1'", optarg);
347 if (sscanf(optarg, "%d", &n) == 1)
350 error("bad size `%1'", optarg);
354 one_size_reduction_flag = 1;
357 warning("-D option is obsolete: use `set draw_lines 1' instead");
361 no_newline_in_delim_flag = 1;
363 case CHAR_MAX + 1: // --help
376 if (output_format == troff) {
377 printf(".if !'\\*(.T'%s' "
378 ".if !'\\*(.T'html' " // the html device uses `-Tps' to render
379 // equations as images
380 ".tm warning: %s should have been given a `-T\\*(.T' option\n",
381 device, program_name);
382 printf(".if '\\*(.T'html' "
384 ".tm warning: %s should have been given a `-Tps' option\n",
385 device, program_name);
386 printf(".if '\\*(.T'html' "
388 ".tm warning: (it is advisable to invoke groff via: groff -Thtml -e)\n",
391 if (load_startup_file) {
393 FILE *fp = config_macro_path.open_file(STARTUP_FILE, &path);
403 for (int i = optind; i < argc; i++)
404 if (strcmp(argv[i], "-") == 0)
408 FILE *fp = fopen(argv[i], "r");
410 fatal("can't open `%1': %2", argv[i], strerror(errno));
412 do_file(fp, argv[i]);
416 if (ferror(stdout) || fflush(stdout) < 0)
417 fatal("output error");