2 /* Copyright (C) 1989-2018 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/>. */
28 #include "stringclass.h"
30 #include "searchpath.h"
33 // The include search path initially contains only the current directory.
34 static search_path include_search_path(0, 0, 0, 1);
36 int compatible_flag = 0;
40 extern "C" const char *Version_string;
42 int do_file(const char *);
45 void usage(FILE *stream)
47 fprintf(stream, "usage: %s [ -Crtv ] [ -I dir ] [ files ]\n", program_name);
50 int main(int argc, char **argv)
52 program_name = argv[0];
54 static const struct option long_options[] = {
55 { "help", no_argument, 0, CHAR_MAX + 1 },
56 { "version", no_argument, 0, 'v' },
59 while ((opt = getopt_long(argc, argv, "CI:rtv", long_options, NULL)) != EOF)
62 printf("GNU soelim (groff) version %s\n", Version_string);
69 include_search_path.command_line_dir(optarg);
77 case CHAR_MAX + 1: // --help
90 nbad += !do_file("-");
92 for (int i = optind; i < argc; i++)
93 nbad += !do_file(argv[i]);
94 if (ferror(stdout) || fflush(stdout) < 0)
95 fatal("output error");
103 printf(".lf %d %s\n", current_lineno, current_filename);
105 printf("%% file %s, line %d\n", current_filename, current_lineno);
109 void do_so(const char *line)
111 const char *p = line;
116 for (const char *q = p;
117 success && *q != '\0' && *q != '\n' && *q != ' ';
134 filename += char(*q);
135 if (success && filename.length() > 0) {
137 const char *fn = current_filename;
138 int ln = current_lineno;
140 if (do_file(filename.contents())) {
141 current_filename = fn;
148 fputs(".so", stdout);
152 int do_file(const char *filename)
154 char *file_name_in_path = 0;
155 FILE *fp = include_search_path.open_file_cautious(filename,
158 string whole_filename(file_name_in_path ? file_name_in_path : filename);
159 whole_filename += '\0';
160 free(file_name_in_path);
162 error("can't open '%1': %2", whole_filename.contents(), strerror(err));
165 normalize_for_lf(whole_filename);
166 current_filename = whole_filename.contents();
169 enum { START, MIDDLE, HAD_DOT, HAD_s, HAD_so, HAD_l, HAD_lf } state = START;
227 if (c == ' ' || c == '\n' || compatible_flag) {
229 for (; c != EOF && c != '\n'; c = getc(fp))
234 do_so(line.contents());
238 fputs(".so", stdout);
259 if (c == ' ' || c == '\n' || compatible_flag) {
261 for (; c != EOF && c != '\n'; c = getc(fp))
266 interpret_lf_args(line.contents());
267 printf(".lf%s", line.contents());
271 fputs(".lf", stdout);
282 fputs(".\n", stdout);
285 fputs(".l\n", stdout);
288 fputs(".s\n", stdout);
291 fputs(".lf\n", stdout);
294 fputs(".so\n", stdout);
304 current_filename = 0;