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 "stringclass.h"
32 #include "searchpath.h"
34 // The include search path initially contains only the current directory.
35 static search_path
include_search_path(0, 0, 0, 1);
37 int compatible_flag
= 0;
41 extern "C" const char *Version_string
;
43 int do_file(const char *filename
);
46 void usage(FILE *stream
)
48 fprintf(stream
, "usage: %s [ -Crtv ] [ -I file ] [ files ]\n", program_name
);
51 int main(int argc
, char **argv
)
53 program_name
= argv
[0];
55 static const struct option long_options
[] = {
56 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
57 { "version", no_argument
, 0, 'v' },
60 while ((opt
= getopt_long(argc
, argv
, "CI:rtv", long_options
, NULL
)) != EOF
)
64 printf("GNU soelim (groff) version %s\n", Version_string
);
72 include_search_path
.command_line_dir(optarg
);
80 case CHAR_MAX
+ 1: // --help
93 nbad
+= !do_file("-");
95 for (int i
= optind
; i
< argc
; i
++)
96 nbad
+= !do_file(argv
[i
]);
97 if (ferror(stdout
) || fflush(stdout
) < 0)
98 fatal("output error");
106 printf(".lf %d %s\n", current_lineno
, current_filename
);
108 printf("%% file %s, line %d\n", current_filename
, current_lineno
);
112 void do_so(const char *line
)
114 const char *p
= line
;
119 for (const char *q
= p
;
120 success
&& *q
!= '\0' && *q
!= '\n' && *q
!= ' ';
137 filename
+= char(*q
);
138 if (success
&& filename
.length() > 0) {
140 const char *fn
= current_filename
;
141 int ln
= current_lineno
;
143 if (do_file(filename
.contents())) {
144 current_filename
= fn
;
151 fputs(".so", stdout
);
155 int do_file(const char *filename
)
157 char *file_name_in_path
= 0;
158 FILE *fp
= include_search_path
.open_file_cautious(filename
,
161 string
whole_filename(file_name_in_path
? file_name_in_path
: filename
);
162 whole_filename
+= '\0';
163 a_delete file_name_in_path
;
165 error("can't open `%1': %2", whole_filename
.contents(), strerror(err
));
168 current_filename
= whole_filename
.contents();
171 enum { START
, MIDDLE
, HAD_DOT
, HAD_s
, HAD_so
, HAD_l
, HAD_lf
} state
= START
;
229 if (c
== ' ' || c
== '\n' || compatible_flag
) {
231 for (; c
!= EOF
&& c
!= '\n'; c
= getc(fp
))
236 do_so(line
.contents());
240 fputs(".so", stdout
);
261 if (c
== ' ' || c
== '\n' || compatible_flag
) {
263 for (; c
!= EOF
&& c
!= '\n'; c
= getc(fp
))
268 interpret_lf_args(line
.contents());
269 printf(".lf%s", line
.contents());
273 fputs(".lf", stdout
);
284 fputs(".\n", stdout
);
287 fputs(".l\n", stdout
);
290 fputs(".s\n", stdout
);
293 fputs(".lf\n", stdout
);
296 fputs(".so\n", stdout
);
306 current_filename
= 0;