2 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
4 * Copyright (C) 1989 - 1992, 2000, 2001, 2003 - 2005
5 * Free Software Foundation, Inc.
6 * Written by James Clark (jjc@jclark.com)
8 * This 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 * This 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.
24 #include "soelim-config.h"
33 #include "file_case.h"
36 #include "searchpath.h"
37 #include "stringclass.h"
39 // The include search path initially contains only the current directory.
40 static search_path
include_search_path(0, 0, 0, 1); // FIXME
42 int compatible_flag
= 0;
46 int do_file(const char *);
48 void usage(FILE *stream
)
50 fprintf(stream
, "Synopsis: %s [ -Crtv ] [ -I dir ] [ files ]\n",
54 int main(int argc
, char **argv
)
56 program_name
= argv
[0];
58 static const struct option long_options
[] = {
59 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
60 { "version", no_argument
, 0, 'v' },
63 while ((opt
= getopt_long(argc
, argv
, "CI:rtv", long_options
, NULL
)) != EOF
)
66 puts(L_P_SOELIM
" (" T_ROFF
") v" VERSION
);
73 include_search_path
.command_line_dir(optarg
);
81 case CHAR_MAX
+ 1: // --help
94 nbad
+= !do_file("-");
96 for (int i
= optind
; i
< argc
; i
++)
97 nbad
+= !do_file(argv
[i
]);
98 if (ferror(stdout
) || fflush(stdout
) < 0)
99 fatal("output error");
107 printf(".lf %d %s\n", current_lineno
, current_filename
);
109 printf("%% file %s, line %d\n", current_filename
, current_lineno
);
113 void do_so(const char *line
)
115 const char *p
= line
;
120 for (const char *q
= p
;
121 success
&& *q
!= '\0' && *q
!= '\n' && *q
!= ' ';
138 filename
+= char(*q
);
139 if (success
&& filename
.length() > 0) {
141 const char *fn
= current_filename
;
142 int ln
= current_lineno
;
144 if (do_file(filename
.contents())) {
145 current_filename
= fn
;
152 fputs(".so", stdout
);
156 int do_file(const char *filename
)
159 enum { START
, MIDDLE
, HAD_DOT
, HAD_s
, HAD_so
, HAD_l
, HAD_lf
} state
= START
;
162 if ((fcp
= include_search_path
.open_file_cautious(filename
,
163 fcp
->mux_default
| fcp
->mux_unpack
)) == NULL
) {
164 error("can't open `%1': %2", filename
, strerror(errno
));
168 current_filename
= fcp
->path();
172 int c
= fcp
->get_c();
228 if (c
== ' ' || c
== '\n' || compatible_flag
) {
230 for (; c
!= EOF
&& c
!= '\n'; c
= fcp
->get_c())
235 do_so(line
.contents());
239 fputs(".so", stdout
);
260 if (c
== ' ' || c
== '\n' || compatible_flag
) {
262 for (; c
!= EOF
&& c
!= '\n'; c
= fcp
->get_c())
267 interpret_lf_args(line
.contents());
268 printf(".lf%s", line
.contents());
272 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;