2 /* Copyright (C) 1989, 1990, 1991, 1992 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 2, or (at your option) any later
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 along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "stringclass.h"
24 #include "searchpath.h"
25 #include "macropath.h"
27 #define STARTUP_FILE "eqnrc"
31 static char *delim_search(char *, int);
32 static int inline_equation(FILE *, string &, string &);
34 char start_delim = '\0';
35 char end_delim = '\0';
39 int one_size_reduction_flag = 0;
40 int compatible_flag = 0;
41 int no_newline_in_delim_flag = 0;
43 int read_line(FILE *fp, string *p)
47 while ((c = getc(fp)) != EOF) {
48 if (!illegal_input_char(c))
51 error("illegal input character code `%1'", c);
56 return p->length() > 0;
59 void do_file(FILE *fp, const char *filename)
63 printf(".lf 1 %s\n", filename);
64 current_filename = filename;
66 while (read_line(fp, &linebuf)) {
67 if (linebuf.length() >= 4
68 && linebuf[0] == '.' && linebuf[1] == 'l' && linebuf[2] == 'f'
69 && (linebuf[3] == ' ' || linebuf[3] == '\n' || compatible_flag)) {
70 put_string(linebuf, stdout);
72 if (interpret_lf_args(linebuf.contents() + 3))
75 else if (linebuf.length() >= 4
79 && (linebuf[3] == ' ' || linebuf[3] == '\n' || compatible_flag)) {
80 put_string(linebuf, stdout);
81 int start_lineno = current_lineno + 1;
84 if (!read_line(fp, &linebuf))
85 fatal("end of file before .EN");
86 if (linebuf.length() >= 3 && linebuf[0] == '.' && linebuf[1] == 'E') {
88 && (linebuf.length() == 3 || linebuf[3] == ' '
89 || linebuf[3] == '\n' || compatible_flag))
91 else if (linebuf[2] == 'Q' && linebuf.length() > 3
92 && (linebuf[3] == ' ' || linebuf[3] == '\n'
100 init_lex(str.contents(), current_filename, start_lineno);
104 if (non_empty_flag) {
105 printf(".lf %d\n", current_lineno - 1);
108 restore_compatibility();
109 printf(".lf %d\n", current_lineno);
110 put_string(linebuf, stdout);
112 else if (start_delim != '\0' && linebuf.search(start_delim) >= 0
113 && inline_equation(fp, linebuf, str))
116 put_string(linebuf, stdout);
118 current_filename = 0;
122 /* Handle an inline equation. Return 1 if it was an inline equation,
125 static int inline_equation(FILE *fp, string &linebuf, string &str)
128 char *ptr = &linebuf[0];
129 char *start = delim_search(ptr, start_delim);
131 // It wasn't a delimiter after all.
132 linebuf.set_length(linebuf.length() - 1); // strip the '\0'
138 if (no_newline_in_delim_flag && strchr(start + 1, end_delim) == 0) {
139 error("missing `%1'", end_delim);
140 char *nl = strchr(start + 1, '\n');
146 int start_lineno = current_lineno;
152 char *end = strchr(ptr, end_delim);
160 if (!read_line(fp, &linebuf))
161 fatal("end of file before `%1'", end_delim);
166 init_lex(str.contents(), current_filename, start_lineno);
168 start = delim_search(ptr, start_delim);
170 char *nl = strchr(ptr, '\n');
177 printf(".lf %d\n", current_lineno);
179 restore_compatibility();
180 printf(".lf %d\n", current_lineno + 1);
184 /* Search for delim. Skip over number register and string names etc. */
186 static char *delim_search(char *ptr, int delim)
191 if (*ptr++ == '\\') {
203 if (*++ptr != '\\' && *ptr != '\0' && *++ptr != '\\' && *ptr != '\0')
207 while (*++ptr != '\0')
233 "usage: %s [ -rvDCNR ] -dxx -fn -sn -pn -mn -Mdir -Ts [ files ... ]\n",
238 int main(int argc, char **argv)
240 program_name = argv[0];
241 static char stderr_buf[BUFSIZ];
242 setbuf(stderr, stderr_buf);
244 int load_startup_file = 1;
245 while ((opt = getopt(argc, argv, "DCRvd:f:p:s:m:T:M:rN")) != EOF)
250 case 'R': // don't load eqnchar
251 load_startup_file = 0;
254 macro_path.command_line_dir(optarg);
258 extern const char *version_string;
259 fprintf(stderr, "GNU eqn version %s\n", version_string);
264 if (optarg[0] == '\0' || optarg[1] == '\0')
265 error("-d requires two character argument");
266 else if (illegal_input_char(optarg[0]))
267 error("bad delimiter `%1'", optarg[0]);
268 else if (illegal_input_char(optarg[1]))
269 error("bad delimiter `%1'", optarg[1]);
271 start_delim = optarg[0];
272 end_delim = optarg[1];
282 if (!set_gsize(optarg))
283 error("invalid size `%1'", optarg);
288 if (sscanf(optarg, "%d", &n) == 1)
289 set_script_reduction(n);
291 error("bad size `%1'", optarg);
297 if (sscanf(optarg, "%d", &n) == 1)
300 error("bad size `%1'", optarg);
304 one_size_reduction_flag = 1;
307 warning("-D option is obsolete: use `set draw_lines 1' instead");
311 no_newline_in_delim_flag = 1;
321 printf(".if !'\\*(.T'%s' "
322 ".tm warning: %s should have been given a `-T\\*(.T' option\n",
323 device, program_name);
324 if (load_startup_file) {
326 FILE *fp = macro_path.open_file(STARTUP_FILE, &path);
336 for (int i = optind; i < argc; i++)
337 if (strcmp(argv[i], "-") == 0)
341 FILE *fp = fopen(argv[i], "r");
343 fatal("can't open `%1': %2", argv[i], strerror(errno));
345 do_file(fp, argv[i]);
349 if (ferror(stdout) || fflush(stdout) < 0)
350 fatal("output error");