2 /* Copyright (C) 1989-1992, 2000, 2001, 2003, 2004, 2005
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 2, or (at your option) any later
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 along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
30 #include "file_case.h"
31 #include "stringclass.h"
33 #include "searchpath.h"
35 // The include search path initially contains only the current directory.
36 static search_path
include_search_path(0, 0, 0, 1);
38 int compatible_flag
= 0;
42 extern "C" const char *Version_string
;
44 int do_file(const char *);
47 void usage(FILE *stream
)
49 fprintf(stream
, "usage: %s [ -Crtv ] [ -I dir ] [ files ]\n", program_name
);
52 int main(int argc
, char **argv
)
54 program_name
= argv
[0];
56 static const struct option long_options
[] = {
57 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
58 { "version", no_argument
, 0, 'v' },
61 while ((opt
= getopt_long(argc
, argv
, "CI:rtv", long_options
, NULL
)) != EOF
)
64 printf("GNU soelim (groff) version %s\n", Version_string
);
71 include_search_path
.command_line_dir(optarg
);
79 case CHAR_MAX
+ 1: // --help
92 nbad
+= !do_file("-");
94 for (int i
= optind
; i
< argc
; i
++)
95 nbad
+= !do_file(argv
[i
]);
96 if (ferror(stdout
) || fflush(stdout
) < 0)
97 fatal("output error");
105 printf(".lf %d %s\n", current_lineno
, current_filename
);
107 printf("%% file %s, line %d\n", current_filename
, current_lineno
);
111 void do_so(const char *line
)
113 const char *p
= line
;
118 for (const char *q
= p
;
119 success
&& *q
!= '\0' && *q
!= '\n' && *q
!= ' ';
136 filename
+= char(*q
);
137 if (success
&& filename
.length() > 0) {
139 const char *fn
= current_filename
;
140 int ln
= current_lineno
;
142 if (do_file(filename
.contents())) {
143 current_filename
= fn
;
150 fputs(".so", stdout
);
154 int do_file(const char *filename
)
157 enum { START
, MIDDLE
, HAD_DOT
, HAD_s
, HAD_so
, HAD_l
, HAD_lf
} state
= START
;
160 if ((fcp
= include_search_path
.open_file_cautious(filename
,
161 fcp
->mux_default
| fcp
->mux_unpack
)) == NULL
) {
162 error("can't open `%1': %2", filename
, strerror(errno
));
166 current_filename
= fcp
->path();
170 int c
= fcp
->get_c();
226 if (c
== ' ' || c
== '\n' || compatible_flag
) {
228 for (; c
!= EOF
&& c
!= '\n'; c
= fcp
->get_c())
233 do_so(line
.contents());
237 fputs(".so", stdout
);
258 if (c
== ' ' || c
== '\n' || compatible_flag
) {
260 for (; c
!= EOF
&& c
!= '\n'; c
= fcp
->get_c())
265 interpret_lf_args(line
.contents());
266 printf(".lf%s", line
.contents());
270 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;