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/>. */
28 #include "stringclass.h"
30 #include "searchpath.h"
32 // The include search path initially contains only the current directory.
33 static search_path include_search_path(0, 0, 0, 1);
35 int compatible_flag = 0;
39 extern "C" const char *Version_string;
41 int do_file(const char *);
44 void usage(FILE *stream)
46 fprintf(stream, "usage: %s [ -Crtv ] [ -I dir ] [ files ]\n", program_name);
49 int main(int argc, char **argv)
51 program_name = argv[0];
53 static const struct option long_options[] = {
54 { "help", no_argument, 0, CHAR_MAX + 1 },
55 { "version", no_argument, 0, 'v' },
58 while ((opt = getopt_long(argc, argv, "CI:rtv", long_options, NULL)) != EOF)
61 printf("GNU soelim (groff) version %s\n", Version_string);
68 include_search_path.command_line_dir(optarg);
76 case CHAR_MAX + 1: // --help
89 nbad += !do_file("-");
91 for (int i = optind; i < argc; i++)
92 nbad += !do_file(argv[i]);
93 if (ferror(stdout) || fflush(stdout) < 0)
94 fatal("output error");
102 printf(".lf %d %s\n", current_lineno, current_filename);
104 printf("%% file %s, line %d\n", current_filename, current_lineno);
108 void do_so(const char *line)
110 const char *p = line;
115 for (const char *q = p;
116 success && *q != '\0' && *q != '\n' && *q != ' ';
133 filename += char(*q);
134 if (success && filename.length() > 0) {
136 const char *fn = current_filename;
137 int ln = current_lineno;
139 if (do_file(filename.contents())) {
140 current_filename = fn;
147 fputs(".so", stdout);
151 int do_file(const char *filename)
153 char *file_name_in_path = 0;
154 FILE *fp = include_search_path.open_file_cautious(filename,
157 string whole_filename(file_name_in_path ? file_name_in_path : filename);
158 whole_filename += '\0';
159 a_delete file_name_in_path;
161 error("can't open `%1': %2", whole_filename.contents(), strerror(err));
164 current_filename = whole_filename.contents();
167 enum { START, MIDDLE, HAD_DOT, HAD_s, HAD_so, HAD_l, HAD_lf } state = START;
225 if (c == ' ' || c == '\n' || compatible_flag) {
227 for (; c != EOF && c != '\n'; c = getc(fp))
232 do_so(line.contents());
236 fputs(".so", stdout);
257 if (c == ' ' || c == '\n' || compatible_flag) {
259 for (; c != EOF && c != '\n'; c = getc(fp))
264 interpret_lf_args(line.contents());
265 printf(".lf%s", line.contents());
269 fputs(".lf", stdout);
280 fputs(".\n", stdout);
283 fputs(".l\n", stdout);
286 fputs(".s\n", stdout);
289 fputs(".lf\n", stdout);
292 fputs(".so\n", stdout);
302 current_filename = 0;