4 /* Copyright (C) 1989-1992, 2000, 2001, 2003, 2004, 2005
5 Free Software Foundation, Inc.
6 Written by James Clark (jjc@jclark.com)
8 This file is part of groff.
10 groff is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License along
21 with groff; see the file COPYING. If not, write to the Free Software
22 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
32 #include "stringclass.h"
34 #include "searchpath.h"
36 // The include search path initially contains only the current directory.
37 static search_path
include_search_path(0, 0, 0, 1);
39 int compatible_flag
= 0;
43 extern "C" const char *Version_string
;
45 int do_file(const char *filename
);
48 void usage(FILE *stream
)
50 fprintf(stream
, "usage: %s [ -Crtv ] [ -I file ] [ files ]\n", program_name
);
53 int main(int argc
, char **argv
)
55 program_name
= argv
[0];
57 static const struct option long_options
[] = {
58 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
59 { "version", no_argument
, 0, 'v' },
62 while ((opt
= getopt_long(argc
, argv
, "CI:rtv", long_options
, NULL
)) != EOF
)
66 printf("GNU soelim (groff) version %s\n", Version_string
);
74 include_search_path
.command_line_dir(optarg
);
82 case CHAR_MAX
+ 1: // --help
95 nbad
+= !do_file("-");
97 for (int i
= optind
; i
< argc
; i
++)
98 nbad
+= !do_file(argv
[i
]);
99 if (ferror(stdout
) || fflush(stdout
) < 0)
100 fatal("output error");
108 printf(".lf %d %s\n", current_lineno
, current_filename
);
110 printf("%% file %s, line %d\n", current_filename
, current_lineno
);
114 void do_so(const char *line
)
116 const char *p
= line
;
121 for (const char *q
= p
;
122 success
&& *q
!= '\0' && *q
!= '\n' && *q
!= ' ';
139 filename
+= char(*q
);
140 if (success
&& filename
.length() > 0) {
142 const char *fn
= current_filename
;
143 int ln
= current_lineno
;
145 if (do_file(filename
.contents())) {
146 current_filename
= fn
;
153 fputs(".so", stdout
);
157 int do_file(const char *filename
)
159 char *file_name_in_path
= 0;
160 FILE *fp
= include_search_path
.open_file_cautious(filename
,
163 string
whole_filename(file_name_in_path
? file_name_in_path
: filename
);
164 whole_filename
+= '\0';
165 a_delete file_name_in_path
;
167 error("can't open `%1': %2", whole_filename
.contents(), strerror(err
));
170 current_filename
= whole_filename
.contents();
173 enum { START
, MIDDLE
, HAD_DOT
, HAD_s
, HAD_so
, HAD_l
, HAD_lf
} state
= START
;
231 if (c
== ' ' || c
== '\n' || compatible_flag
) {
233 for (; c
!= EOF
&& c
!= '\n'; c
= getc(fp
))
238 do_so(line
.contents());
242 fputs(".so", stdout
);
263 if (c
== ' ' || c
== '\n' || compatible_flag
) {
265 for (; c
!= EOF
&& c
!= '\n'; c
= getc(fp
))
270 interpret_lf_args(line
.contents());
271 printf(".lf%s", line
.contents());
275 fputs(".lf", stdout
);
286 fputs(".\n", stdout
);
289 fputs(".l\n", stdout
);
292 fputs(".s\n", stdout
);
295 fputs(".lf\n", stdout
);
298 fputs(".so\n", stdout
);
308 current_filename
= 0;