2 /* Copyright (C) 1989-1992, 2000, 2001, 2003, 2004, 2005, 2009
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 3 of the License, or
11 (at your option) any later version.
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "stringclass.h"
31 #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 a_delete file_name_in_path;
162 error("can't open `%1': %2", whole_filename.contents(), strerror(err));
165 current_filename = whole_filename.contents();
168 enum { START, MIDDLE, HAD_DOT, HAD_s, HAD_so, HAD_l, HAD_lf } state = START;
226 if (c == ' ' || c == '\n' || compatible_flag) {
228 for (; c != EOF && c != '\n'; c = getc(fp))
233 do_so(line.contents());
237 fputs(".so", stdout);
258 if (c == ' ' || c == '\n' || compatible_flag) {
260 for (; c != EOF && c != '\n'; c = getc(fp))
265 interpret_lf_args(line.contents());
266 printf(".lf%s", line.contents());
270 fputs(".lf", stdout);
281 fputs(".\n", stdout);
284 fputs(".l\n", stdout);
287 fputs(".s\n", stdout);
290 fputs(".lf\n", stdout);
293 fputs(".so\n", stdout);
303 current_filename = 0;