[go: up one dir, main page]

File: preprocess.c

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (451 lines) | stat: -rw-r--r-- 8,230 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 *	cook - file construction tool
 *	Copyright (C) 1992, 1993, 1994, 1997, 1998, 1999 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to eliminate include files
 */

#include <ac/ctype.h>
#include <ac/stdlib.h>
#include <ac/unistd.h>

#include <error_intl.h>
#include <input/file_text.h>
#include <mem.h>
#include <preprocess.h>
#include <str.h>
#include <trace.h>
#include <str_list.h>

static	string_list_ty	search;
static	string_ty	*ofn;
static	FILE		*ofp;


/*
 * NAME
 *	preprocess_include
 *
 * SYNOPSIS
 *	void preprocess_include(char *path);
 *
 * DESCRIPTION
 *	The preprocess_include function is used to append
 *	to the include search path.
 *
 * ARGUMENTS
 *	path	- path to append
 */

void
preprocess_include(path)
	char		*path;
{
	string_ty	*s;

	trace(("preprocess_include(path = \"%s\")\n{\n"/*}*/, path));
	s = str_from_c(path);
	string_list_append_unique(&search, s);
	str_free(s);
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	source
 *
 * SYNOPSIS
 *	int source(char *line);
 *
 * DESCRIPTION
 *	The source function is used to test if a line of tect is
 *	a .so directive, and to insert the contents of the sourced
 *	file at this point.
 *
 * ARGUMENTS
 *	line	- pointer to line of text
 *
 * RETURNS
 *	int;	zero if is not a .so directive,
 *		non-zero if it is a .so directive
 */

static void scan _((string_ty *)); /* forward */

static int source _((char *));

static int
source(line)
	char		*line;
{
	string_ty	*filename;
	size_t		j;
	int		result;
	char		*ep;
	static string_ty *dot;

	/*
	 * see if this is a .so directive
	 */
	if (*line != '.')
		return 0;
	trace(("source(line = \"%s\")\n{\n"/*}*/, line));
	result = 0;
	line++;
	while (isspace(*line))
		line++;
	if (line[0] != 's' || line[1] != 'o' || !isspace(line[2]))
		goto ret;
	line += 3;
	while (isspace(*line))
		line++;
	if (!*line)
		goto ret;

	/*
	 * find the end of the argument
	 */
	for (ep = line + 1; *ep && !isspace(*ep); ++ep)
		;
	filename = str_n_from_c(line, ep - line);

	/*
	 * no need to search when it's an absolute path
	 */
	if (*line == '/')
	{
		scan(filename);
		str_free(filename);
		result = 1;
		goto ret;
	}

	/*
	 * search for the name in the search list
	 */
	if (!dot)
		dot = str_from_c(".");
	for (j = 0; j < search.nstrings; ++j)
	{
		string_ty	*s;
		string_ty	*dir;

		dir = search.string[j];
		if (str_equal(dir, dot))
			s = str_copy(filename);
		else
			s = str_format("%S/%S", dir, filename);
		if (access(s->str_text, F_OK) == 0)
		{
			str_free(filename);
			scan(s);
			str_free(s);
			result = 1;
			goto ret;
		}
		str_free(s);
	}
	str_free(filename);

	/*
	 * let {ps,n,t,dit,pt}roff bomb later
	 */
	result = 0;

	/*
	 * here for all exits
	 */
	ret:
	trace(("return %d;\n", result));
	trace((/*{*/"}\n"));
	return result;
}

/*
 * NAME
 *	lf_directive
 *
 * SYNOPSIS
 *	int lf_directive(char *line, string_ty **ifn, long *lino);
 *
 * DESCRIPTION
 *	The lf_directive function is used to test if a line of text is
 *	a .lf directive, and to adjust the file position to this point.
 *
 * ARGUMENTS
 *	line	- pointer to line of text
 *	ifn	- file name (ptr) if needs changing
 *	lino	- line number (ptr) if needs changing
 *
 * RETURNS
 *	int;	zero if is not a .so directive,
 *		non-zero if it is a .so directive
 */

static int lf_directive _((char *line, string_ty **ifn, long *lino));

static int
lf_directive(line, ifn, lino)
	char		*line;
	string_ty	**ifn;
	long		*lino;
{
	int		result;
	long		n;
	string_ty	*s;
	char		*ep;

	/*
	 * see if this is a .so directive
	 */
	if (*line != '.')
		return 0;
	trace(("source(line = \"%s\")\n{\n"/*}*/, line));
	result = 0;
	line++;
	while (isspace(*line))
		line++;
	if (line[0] != 'l' || line[1] != 'f' || !isspace(line[2]))
		goto ret;
	line += 3;
	while (isspace(*line))
		line++;
	if (!*line)
		goto ret;

	/*
	 * find the line number
	 */
	for (ep = line + 1; *ep && !isspace(*ep); ++ep)
		;
	s = str_n_from_c(line, ep - line);
	n = atol(s->str_text);
	str_free(s);
	if (n <= 0)
		goto ret;
	*lino = n - 1;
	result = 1;

	/*
	 * find the file name
	 */
	line = ep;
	while (*line && isspace(*line))
		line++;
	if (!*line)
		goto ret;
	for (ep = line + 1; *ep && !isspace(*ep); ++ep)
		;
	s = str_n_from_c(line, ep - line);
	if (*ifn)
		str_free(*ifn);
	*ifn = s;

	/*
	 * here for all exits
	 */
	ret:
	trace(("return %d;\n", result));
	trace((/*{*/"}\n"));
	return result;
}


/*
 * NAME
 *	resync
 *
 * SYNOPSOS
 *	void resync(FILE *fp, char *name, long line);
 *
 * DESCRIPTION
 *	The resync function is used to emit appropriate
 *	*roff requests to resynchronize the *roff engine
 *	to the correct file name and line numner,
 *	so that error messages, etc al, are meaningful.
 *
 * ARGUMENTS
 *	fp	- file stream to print on
 *	name	- name of input file
 *	line	- num number in the input file
 */

static void resync _((FILE *, string_ty *, long));

static void
resync(fp, file, line)
	FILE		*fp;
	string_ty	*file;
	long		line;
{
	fprintf(fp, ".lf %ld %s\n", line, file->str_text);
}


/*
 * NAME
 *	scan
 *
 * SYNOPSIS
 *	void scan(char *path);
 *
 * DESCRIPTION
 *	The scan function is used to can a file, copying its contents
 *	to the output, replacing .so directives with the contents of
 *	the included files.
 *
 * ARGUMENTS
 *	path	- name of file to scan
 */

static void scan _((string_ty *));

static void
scan(ifn)
	string_ty	*ifn;
{
	input_ty	*ifp;
	size_t		pos;
	static size_t	max;
	static char	*line;
	long		lino;
	int		c;
	string_ty	*ifn2;

	trace(("scan(ifn = \"%s\")\n{\n"/*}*/, ifn ? ifn->str_text : "-"));
	ifp = input_file_text_open(ifn);
	ifn2 = str_copy(input_filename(ifp));

	lino = 1;
	resync(ofp, ifn2, lino);

	pos = 0;
	for (;;)
	{
		if (pos >= max)
		{
			max += 100;
			line = mem_change_size(line, max);
		}
		c = input_getc(ifp);
		switch (c)
		{
		case INPUT_EOF:
			if (!pos)
				break;
			/* fall through... */

		case '\n':
			line[pos] = 0;
			if (source(line) || lf_directive(line, &ifn2, &lino))
				resync(ofp, ifn2, lino + 1);
			else
			{
				fputs(line, ofp);
				putc('\n', ofp);
			}
			fflush_and_check(ofp, ofn->str_text);
			lino++;
			pos = 0;
			continue;

		default:
			line[pos++] = c;
			continue;
		}
		break;
	}

	input_delete(ifp);
	str_free(ifn2);
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	preprocess
 *
 * SYNOPSIS
 *	void preprocess(char *infile, char *outfile);
 *
 * DESCRIPTION
 *	The preprocess function is used to process an *roff file and
 *	eliminate the .so directives, replacing them with the contents
 *	of the included files.
 *
 * ARGUMENTS
 *	infile	- name of file to scan, NULL means stdin
 *	outfile	- name of file to hold result, NULL means stdout
 */

void
preprocess(ifile, ofile)
	char		*ifile;
	char		*ofile;
{
	string_ty	*s;
	sub_context_ty	*scp;

	/*
	 * default the search path iff the user specified nothing
	 */
	trace(("preprocess(ifile = \"%s\", ofile = \"%s\")\n{\n"/*}*/,
		ifile ? ifile : "-", ofile ? ofile : "-"));
	if (!search.nstrings)
		preprocess_include(".");

	/*
	 * open the output file
	 */
	if (ofile)
	{
		ofn = str_from_c(ofile);
		ofp = fopen_and_check(ofn->str_text, "w");
	}
	else
	{
		scp = sub_context_new();
		ofn = subst_intl(scp, i18n("standard output"));
		sub_context_delete(scp);
		ofp = stdout;
	}

	/*
	 * scan the input
	 */
	if (ifile)
		s = str_from_c(ifile);
	else
		s = 0;
	scan(s);
	if (s)
		str_free(s);

	/*
	 * close up and go home
	 */
	fflush_and_check(ofp, ofn->str_text);
	if (ofp != stdout)
		fclose_and_check(ofp, ofn->str_text);
	str_free(ofn);
	ofp = 0;
	ofn = 0;
	trace((/*{*/"}\n"));
}