1*89a07cf8Schristos /* $NetBSD: soelim.cpp,v 1.1.1.1 2016/01/13 18:41:49 christos Exp $ */
2*89a07cf8Schristos
3*89a07cf8Schristos // -*- C++ -*-
4*89a07cf8Schristos /* Copyright (C) 1989-1992, 2000, 2001, 2003, 2004, 2005
5*89a07cf8Schristos Free Software Foundation, Inc.
6*89a07cf8Schristos Written by James Clark (jjc@jclark.com)
7*89a07cf8Schristos
8*89a07cf8Schristos This file is part of groff.
9*89a07cf8Schristos
10*89a07cf8Schristos groff is free software; you can redistribute it and/or modify it under
11*89a07cf8Schristos the terms of the GNU General Public License as published by the Free
12*89a07cf8Schristos Software Foundation; either version 2, or (at your option) any later
13*89a07cf8Schristos version.
14*89a07cf8Schristos
15*89a07cf8Schristos groff is distributed in the hope that it will be useful, but WITHOUT ANY
16*89a07cf8Schristos WARRANTY; without even the implied warranty of MERCHANTABILITY or
17*89a07cf8Schristos FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18*89a07cf8Schristos for more details.
19*89a07cf8Schristos
20*89a07cf8Schristos You should have received a copy of the GNU General Public License along
21*89a07cf8Schristos with groff; see the file COPYING. If not, write to the Free Software
22*89a07cf8Schristos Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
23*89a07cf8Schristos
24*89a07cf8Schristos #include "lib.h"
25*89a07cf8Schristos
26*89a07cf8Schristos #include <ctype.h>
27*89a07cf8Schristos #include <assert.h>
28*89a07cf8Schristos #include <stdlib.h>
29*89a07cf8Schristos #include <errno.h>
30*89a07cf8Schristos #include "errarg.h"
31*89a07cf8Schristos #include "error.h"
32*89a07cf8Schristos #include "stringclass.h"
33*89a07cf8Schristos #include "nonposix.h"
34*89a07cf8Schristos #include "searchpath.h"
35*89a07cf8Schristos
36*89a07cf8Schristos // The include search path initially contains only the current directory.
37*89a07cf8Schristos static search_path include_search_path(0, 0, 0, 1);
38*89a07cf8Schristos
39*89a07cf8Schristos int compatible_flag = 0;
40*89a07cf8Schristos int raw_flag = 0;
41*89a07cf8Schristos int tex_flag = 0;
42*89a07cf8Schristos
43*89a07cf8Schristos extern "C" const char *Version_string;
44*89a07cf8Schristos
45*89a07cf8Schristos int do_file(const char *filename);
46*89a07cf8Schristos
47*89a07cf8Schristos
usage(FILE * stream)48*89a07cf8Schristos void usage(FILE *stream)
49*89a07cf8Schristos {
50*89a07cf8Schristos fprintf(stream, "usage: %s [ -Crtv ] [ -I file ] [ files ]\n", program_name);
51*89a07cf8Schristos }
52*89a07cf8Schristos
main(int argc,char ** argv)53*89a07cf8Schristos int main(int argc, char **argv)
54*89a07cf8Schristos {
55*89a07cf8Schristos program_name = argv[0];
56*89a07cf8Schristos int opt;
57*89a07cf8Schristos static const struct option long_options[] = {
58*89a07cf8Schristos { "help", no_argument, 0, CHAR_MAX + 1 },
59*89a07cf8Schristos { "version", no_argument, 0, 'v' },
60*89a07cf8Schristos { NULL, 0, 0, 0 }
61*89a07cf8Schristos };
62*89a07cf8Schristos while ((opt = getopt_long(argc, argv, "CI:rtv", long_options, NULL)) != EOF)
63*89a07cf8Schristos switch (opt) {
64*89a07cf8Schristos case 'v':
65*89a07cf8Schristos {
66*89a07cf8Schristos printf("GNU soelim (groff) version %s\n", Version_string);
67*89a07cf8Schristos exit(0);
68*89a07cf8Schristos break;
69*89a07cf8Schristos }
70*89a07cf8Schristos case 'C':
71*89a07cf8Schristos compatible_flag = 1;
72*89a07cf8Schristos break;
73*89a07cf8Schristos case 'I':
74*89a07cf8Schristos include_search_path.command_line_dir(optarg);
75*89a07cf8Schristos break;
76*89a07cf8Schristos case 'r':
77*89a07cf8Schristos raw_flag = 1;
78*89a07cf8Schristos break;
79*89a07cf8Schristos case 't':
80*89a07cf8Schristos tex_flag = 1;
81*89a07cf8Schristos break;
82*89a07cf8Schristos case CHAR_MAX + 1: // --help
83*89a07cf8Schristos usage(stdout);
84*89a07cf8Schristos exit(0);
85*89a07cf8Schristos break;
86*89a07cf8Schristos case '?':
87*89a07cf8Schristos usage(stderr);
88*89a07cf8Schristos exit(1);
89*89a07cf8Schristos break;
90*89a07cf8Schristos default:
91*89a07cf8Schristos assert(0);
92*89a07cf8Schristos }
93*89a07cf8Schristos int nbad = 0;
94*89a07cf8Schristos if (optind >= argc)
95*89a07cf8Schristos nbad += !do_file("-");
96*89a07cf8Schristos else
97*89a07cf8Schristos for (int i = optind; i < argc; i++)
98*89a07cf8Schristos nbad += !do_file(argv[i]);
99*89a07cf8Schristos if (ferror(stdout) || fflush(stdout) < 0)
100*89a07cf8Schristos fatal("output error");
101*89a07cf8Schristos return nbad != 0;
102*89a07cf8Schristos }
103*89a07cf8Schristos
set_location()104*89a07cf8Schristos void set_location()
105*89a07cf8Schristos {
106*89a07cf8Schristos if(!raw_flag) {
107*89a07cf8Schristos if(!tex_flag)
108*89a07cf8Schristos printf(".lf %d %s\n", current_lineno, current_filename);
109*89a07cf8Schristos else
110*89a07cf8Schristos printf("%% file %s, line %d\n", current_filename, current_lineno);
111*89a07cf8Schristos }
112*89a07cf8Schristos }
113*89a07cf8Schristos
do_so(const char * line)114*89a07cf8Schristos void do_so(const char *line)
115*89a07cf8Schristos {
116*89a07cf8Schristos const char *p = line;
117*89a07cf8Schristos while (*p == ' ')
118*89a07cf8Schristos p++;
119*89a07cf8Schristos string filename;
120*89a07cf8Schristos int success = 1;
121*89a07cf8Schristos for (const char *q = p;
122*89a07cf8Schristos success && *q != '\0' && *q != '\n' && *q != ' ';
123*89a07cf8Schristos q++)
124*89a07cf8Schristos if (*q == '\\') {
125*89a07cf8Schristos switch (*++q) {
126*89a07cf8Schristos case 'e':
127*89a07cf8Schristos case '\\':
128*89a07cf8Schristos filename += '\\';
129*89a07cf8Schristos break;
130*89a07cf8Schristos case ' ':
131*89a07cf8Schristos filename += ' ';
132*89a07cf8Schristos break;
133*89a07cf8Schristos default:
134*89a07cf8Schristos success = 0;
135*89a07cf8Schristos break;
136*89a07cf8Schristos }
137*89a07cf8Schristos }
138*89a07cf8Schristos else
139*89a07cf8Schristos filename += char(*q);
140*89a07cf8Schristos if (success && filename.length() > 0) {
141*89a07cf8Schristos filename += '\0';
142*89a07cf8Schristos const char *fn = current_filename;
143*89a07cf8Schristos int ln = current_lineno;
144*89a07cf8Schristos current_lineno--;
145*89a07cf8Schristos if (do_file(filename.contents())) {
146*89a07cf8Schristos current_filename = fn;
147*89a07cf8Schristos current_lineno = ln;
148*89a07cf8Schristos set_location();
149*89a07cf8Schristos return;
150*89a07cf8Schristos }
151*89a07cf8Schristos current_lineno++;
152*89a07cf8Schristos }
153*89a07cf8Schristos fputs(".so", stdout);
154*89a07cf8Schristos fputs(line, stdout);
155*89a07cf8Schristos }
156*89a07cf8Schristos
do_file(const char * filename)157*89a07cf8Schristos int do_file(const char *filename)
158*89a07cf8Schristos {
159*89a07cf8Schristos char *file_name_in_path = 0;
160*89a07cf8Schristos FILE *fp = include_search_path.open_file_cautious(filename,
161*89a07cf8Schristos &file_name_in_path);
162*89a07cf8Schristos int err = errno;
163*89a07cf8Schristos string whole_filename(file_name_in_path ? file_name_in_path : filename);
164*89a07cf8Schristos whole_filename += '\0';
165*89a07cf8Schristos a_delete file_name_in_path;
166*89a07cf8Schristos if (fp == 0) {
167*89a07cf8Schristos error("can't open `%1': %2", whole_filename.contents(), strerror(err));
168*89a07cf8Schristos return 0;
169*89a07cf8Schristos }
170*89a07cf8Schristos current_filename = whole_filename.contents();
171*89a07cf8Schristos current_lineno = 1;
172*89a07cf8Schristos set_location();
173*89a07cf8Schristos enum { START, MIDDLE, HAD_DOT, HAD_s, HAD_so, HAD_l, HAD_lf } state = START;
174*89a07cf8Schristos for (;;) {
175*89a07cf8Schristos int c = getc(fp);
176*89a07cf8Schristos if (c == EOF)
177*89a07cf8Schristos break;
178*89a07cf8Schristos switch (state) {
179*89a07cf8Schristos case START:
180*89a07cf8Schristos if (c == '.')
181*89a07cf8Schristos state = HAD_DOT;
182*89a07cf8Schristos else {
183*89a07cf8Schristos putchar(c);
184*89a07cf8Schristos if (c == '\n') {
185*89a07cf8Schristos current_lineno++;
186*89a07cf8Schristos state = START;
187*89a07cf8Schristos }
188*89a07cf8Schristos else
189*89a07cf8Schristos state = MIDDLE;
190*89a07cf8Schristos }
191*89a07cf8Schristos break;
192*89a07cf8Schristos case MIDDLE:
193*89a07cf8Schristos putchar(c);
194*89a07cf8Schristos if (c == '\n') {
195*89a07cf8Schristos current_lineno++;
196*89a07cf8Schristos state = START;
197*89a07cf8Schristos }
198*89a07cf8Schristos break;
199*89a07cf8Schristos case HAD_DOT:
200*89a07cf8Schristos if (c == 's')
201*89a07cf8Schristos state = HAD_s;
202*89a07cf8Schristos else if (c == 'l')
203*89a07cf8Schristos state = HAD_l;
204*89a07cf8Schristos else {
205*89a07cf8Schristos putchar('.');
206*89a07cf8Schristos putchar(c);
207*89a07cf8Schristos if (c == '\n') {
208*89a07cf8Schristos current_lineno++;
209*89a07cf8Schristos state = START;
210*89a07cf8Schristos }
211*89a07cf8Schristos else
212*89a07cf8Schristos state = MIDDLE;
213*89a07cf8Schristos }
214*89a07cf8Schristos break;
215*89a07cf8Schristos case HAD_s:
216*89a07cf8Schristos if (c == 'o')
217*89a07cf8Schristos state = HAD_so;
218*89a07cf8Schristos else {
219*89a07cf8Schristos putchar('.');
220*89a07cf8Schristos putchar('s');
221*89a07cf8Schristos putchar(c);
222*89a07cf8Schristos if (c == '\n') {
223*89a07cf8Schristos current_lineno++;
224*89a07cf8Schristos state = START;
225*89a07cf8Schristos }
226*89a07cf8Schristos else
227*89a07cf8Schristos state = MIDDLE;
228*89a07cf8Schristos }
229*89a07cf8Schristos break;
230*89a07cf8Schristos case HAD_so:
231*89a07cf8Schristos if (c == ' ' || c == '\n' || compatible_flag) {
232*89a07cf8Schristos string line;
233*89a07cf8Schristos for (; c != EOF && c != '\n'; c = getc(fp))
234*89a07cf8Schristos line += c;
235*89a07cf8Schristos current_lineno++;
236*89a07cf8Schristos line += '\n';
237*89a07cf8Schristos line += '\0';
238*89a07cf8Schristos do_so(line.contents());
239*89a07cf8Schristos state = START;
240*89a07cf8Schristos }
241*89a07cf8Schristos else {
242*89a07cf8Schristos fputs(".so", stdout);
243*89a07cf8Schristos putchar(c);
244*89a07cf8Schristos state = MIDDLE;
245*89a07cf8Schristos }
246*89a07cf8Schristos break;
247*89a07cf8Schristos case HAD_l:
248*89a07cf8Schristos if (c == 'f')
249*89a07cf8Schristos state = HAD_lf;
250*89a07cf8Schristos else {
251*89a07cf8Schristos putchar('.');
252*89a07cf8Schristos putchar('l');
253*89a07cf8Schristos putchar(c);
254*89a07cf8Schristos if (c == '\n') {
255*89a07cf8Schristos current_lineno++;
256*89a07cf8Schristos state = START;
257*89a07cf8Schristos }
258*89a07cf8Schristos else
259*89a07cf8Schristos state = MIDDLE;
260*89a07cf8Schristos }
261*89a07cf8Schristos break;
262*89a07cf8Schristos case HAD_lf:
263*89a07cf8Schristos if (c == ' ' || c == '\n' || compatible_flag) {
264*89a07cf8Schristos string line;
265*89a07cf8Schristos for (; c != EOF && c != '\n'; c = getc(fp))
266*89a07cf8Schristos line += c;
267*89a07cf8Schristos current_lineno++;
268*89a07cf8Schristos line += '\n';
269*89a07cf8Schristos line += '\0';
270*89a07cf8Schristos interpret_lf_args(line.contents());
271*89a07cf8Schristos printf(".lf%s", line.contents());
272*89a07cf8Schristos state = START;
273*89a07cf8Schristos }
274*89a07cf8Schristos else {
275*89a07cf8Schristos fputs(".lf", stdout);
276*89a07cf8Schristos putchar(c);
277*89a07cf8Schristos state = MIDDLE;
278*89a07cf8Schristos }
279*89a07cf8Schristos break;
280*89a07cf8Schristos default:
281*89a07cf8Schristos assert(0);
282*89a07cf8Schristos }
283*89a07cf8Schristos }
284*89a07cf8Schristos switch (state) {
285*89a07cf8Schristos case HAD_DOT:
286*89a07cf8Schristos fputs(".\n", stdout);
287*89a07cf8Schristos break;
288*89a07cf8Schristos case HAD_l:
289*89a07cf8Schristos fputs(".l\n", stdout);
290*89a07cf8Schristos break;
291*89a07cf8Schristos case HAD_s:
292*89a07cf8Schristos fputs(".s\n", stdout);
293*89a07cf8Schristos break;
294*89a07cf8Schristos case HAD_lf:
295*89a07cf8Schristos fputs(".lf\n", stdout);
296*89a07cf8Schristos break;
297*89a07cf8Schristos case HAD_so:
298*89a07cf8Schristos fputs(".so\n", stdout);
299*89a07cf8Schristos break;
300*89a07cf8Schristos case MIDDLE:
301*89a07cf8Schristos putc('\n', stdout);
302*89a07cf8Schristos break;
303*89a07cf8Schristos case START:
304*89a07cf8Schristos break;
305*89a07cf8Schristos }
306*89a07cf8Schristos if (fp != stdin)
307*89a07cf8Schristos fclose(fp);
308*89a07cf8Schristos current_filename = 0;
309*89a07cf8Schristos return 1;
310*89a07cf8Schristos }
311