File: readfile.c

package info (click to toggle)
smake 1.2a23-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 2,820 kB
  • ctags: 2,459
  • sloc: ansic: 14,285; sh: 2,538; makefile: 113; pascal: 41
file content (367 lines) | stat: -rw-r--r-- 8,780 bytes parent folder | download
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
/* @(#)readfile.c	1.50 04/02/25 Copyright 1985 J. Schilling */
#ifndef lint
static	char sccsid[] =
	"@(#)readfile.c	1.50 04/02/25 Copyright 1985 J. Schilling";
#endif
/*
 *	Make program
 *	File/string reading routines
 *
 *	Copyright (c) 1985 by J. Schilling
 */
/*
 * 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, 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; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <mconfig.h>
#include <stdio.h>
#include <standard.h>
#include <stdxlib.h>
#include <strdefs.h>
#include <schily.h>
#include "make.h"

LOCAL	int	fillrdbuf	__PR((void));
EXPORT	char	*peekrdbuf	__PR((void));
EXPORT	char	*getrdbuf	__PR((void));
EXPORT	void	setincmd	__PR((BOOL isincmd));
EXPORT	void	getch		__PR((void));
EXPORT	int	peekch		__PR((void));
EXPORT	void	skipline	__PR((void));
EXPORT	void	readstring	__PR((char *str, char *strname));
EXPORT	void	readfile	__PR((char *name, BOOL must_exist));
EXPORT	void	doinclude	__PR((char *name, BOOL must_exist));
EXPORT	void	makeincs	__PR((void));

#if	defined(unix) || defined(IS_UNIX)
#	define	RDBUF_SIZE	1024
#else
#	define	RDBUF_SIZE	512
#endif

/*
 * Several variables needed for reading with look ahead
 * to allow easy parsing of make files.
 */
EXPORT	int	lastc		= 0;		/* last input character	    */
EXPORT	int	firstc		= 0;		/* first character in line  */
LOCAL	FILE	*mfp		= (FILE *)NULL;	/* currently open make file */
EXPORT	char	*mfname 	= NULL;		/* name of current make file */
LOCAL	int	olineno		= 1;		/* old line number (include) */
EXPORT	int	lineno		= 1;		/* current line number	    */
EXPORT	int	col		= 0;		/* current column	    */
LOCAL	BOOL	incmd		= FALSE;	/* cmd list line starts \n\t */
LOCAL	char	*readbfp;			/* current read buf pointer  */
LOCAL	char	*readbfstart;			/* start of current read buf */
LOCAL	char	*readbfend;			/* end of current read buf  */
LOCAL	char	rdbuf[RDBUF_SIZE];		/* the real read buffer	    */
LOCAL	char	*rd_buffer	= rdbuf;	/* a pointer to start of buf */

/*
 * Get or peek a character from current Makefile.
 */
#define	mygetc()	((readbfp >= readbfend) ? fillrdbuf() : *readbfp++)
#define	mypeekc()	((readbfp >= readbfend) ? (fillrdbuf() == EOF ?	\
						EOF:*--readbfp) : *readbfp)

/*
 * Fill or refill the read buffer that is used by the mygetc() CPP macro.
 */
LOCAL int
fillrdbuf()
{
	if (mfp == (FILE *) NULL)	/* EOF while reading from a string. */
		return (EOF);
	readbfp = rd_buffer;
	readbfstart = rd_buffer;	/* used for better error reporting */
	readbfend = rd_buffer + fileread(mfp, rd_buffer, RDBUF_SIZE);
	if (readbfp >= readbfend)
		return (EOF);
	return ((int) *readbfp++);
}

EXPORT char *
peekrdbuf()
{
	return (readbfp);
}

EXPORT char *
getrdbuf()
{
	return (readbfstart);
}

/*
 * Switch the bahaviour of the reader for parsing commandlines/others.
 */
EXPORT void
setincmd(isincmd)
	BOOL	isincmd;
{
	incmd = isincmd ? TRUE:FALSE;
}

/*
 * Get a character.
 * Handle backslash-newline combinations and special conditions
 * for comment and command lines.
 * Count lines for error messages.
 */
EXPORT void
getch()
{
	col++;
	lastc = mygetc();
	if (lastc == EOF)
		return;
	if (lastc == '\n') {
		firstc = mypeekc();
		lineno++;
		col = 0;
		return;
	} else if (lastc == '\\' && !incmd && mypeekc() == '\n') {
		lastc = mygetc();
		firstc = mypeekc();
		lineno++;
		col = 0;
		for (;;) {		/* Skip white space at start of line */
			register int	c;

			c = mypeekc();
			if (c != ' ' && c != '\t') {
				lastc = ' ';
				return;
			}
			mygetc();
			col++;
		}
	}

	if (lastc == '#' && !incmd) {
		if (mfp == (FILE *) NULL)	/* Do not skip past # when */
			return;			/* reading from string.	   */
		skipline();
	}
}

EXPORT int
peekch()
{
	return (mypeekc());
}

/*
 * Fast method to skip to the end of a commented out line.
 * Always use the POSIX method (skip to next un-escaped new line).
 */
EXPORT void
skipline()
{
	register int	c = lastc;

	if (c == '\n')
		return;

	while (c != EOF) {
		c = mygetc();
		if (c == '\n') {
			lineno++;
			col = 0;
			lastc = c;
			firstc = mypeekc();
			return;
		} else if (c == '\\' && mypeekc() == '\n') {
			lineno++;
			col = 0;
			c = mygetc();
		}
	}
	firstc = lastc = c;
}

/*
 * Parse input from a string.
 */
EXPORT void
readstring(str, strname)
	char	*str;
	char	*strname;
{
	mfname = strname;
	readbfp = str;
	readbfstart = str;	/* used for better error reporting */
	readbfend = str + strlen(str);
	firstc = *str;
	incmd = FALSE;
	parsefile();
	mfname = NULL;
}

/*
 * Parse input from the current Makefile.
 */
EXPORT void
readfile(name, must_exist)
	char	*name;
	BOOL	must_exist;
{
	/*
	 * Diese Meldung ist noch falsch (Rekursion/Makefiles)
	 */
	if (DoWarn)
		error("Reading file '%s' in line %d of '%s'\n", name,
			olineno, mfname);

	if (streql(name, "-")) {
		mfp = stdin;
		name = "Standard in";
	} else {
		if ((mfp = fileopen(name, "ru")) == (FILE *)NULL && must_exist)
			comerr("Can not open '%s'.\n", name);
	}
	mfname = name;
	readbfp = readbfend;		/* Force immediate call of fillrdbuf.*/
	firstc = mypeekc();
	incmd = FALSE;
	if (mfp) {
		parsefile();
		fclose(mfp);
	}
	mfp = (FILE *) NULL;
	mfname = NULL;
	col = 0;
}

list_t	*Incs;
list_t	**inctail = &Incs;

/*
 * Handle the "include" directive in makefiles.
 * If an include file does not exists, first try to find a rule to make it.
 * If this does not help, try to call include failure exception handling.
 * This exception handling enables some automake features of smake in allowing
 * the to call a shell script that will create the missing (may be architecture
 * dependant) include file on the fly with something that will at least allow
 * smake to continue on this platform.
 */
EXPORT void
doinclude(name, must_exist)
	char	*name;
	BOOL	must_exist;
{
	int	slc = lastc;
	int	sfc = firstc;
	FILE	*smf = mfp;
	char	*smfn = mfname;
	int	slineno = lineno;
	int	scol = col;
	char	*srbp = readbfp;
	char	*srbs = readbfstart;
	char	*srbe = readbfend;
	char	*srbf = rd_buffer;
	char	include_buf[RDBUF_SIZE];
	obj_t	*st = default_tgt;
	obj_t	*o;
	list_t	*lp;
	char	includename[NAMEMAX*2];

	olineno = lineno-1;
	lastc	= 0;
	firstc	= 0;
	lineno	= 1;
	col	= 0;
	rd_buffer = include_buf;

	setup_dotvars();
	name = substitute(name, NullObj, 0, 0);
	name = strsave(name);

	xmake(name, FALSE);
	default_tgt = st;

	o = objlook(name, TRUE);

	if (Debug > 1)
		error("doinclude(%s, %d)= date: %s level: %d\n",
			name, must_exist, prtime(o->o_date), o->o_level);

	if (must_exist && o->o_date == 0 && IncludeFailed) {
		list_t	l;

		o->o_date = newtime;		/* Force to be out of date  */
		l.l_next = (list_t *)0;		/* Only one element:	    */
		l.l_obj = o;			/* The file to be included  */
		IncludeFailed->o_list = &l;	/* Make it $^		    */
		IncludeFailed->o_date = (date_t)0;
		omake(IncludeFailed, FALSE);	/* Try to apply rules	    */
		o->o_date = gftime(name);	/* Check if file is present */
	}

	if (must_exist || o->o_date != 0) {
		if (Prdep)
			error("Reading file '%s' from '%s'\n", name, mfname);

		/*
		 * Zurcksetzen des Datums bewirkt Neuauswertung
		 * der Abhngigkeitsliste.
		 * XXX Das kann Probleme bei make depend geben.
		 */
		o->o_date = 0;
		/*
		 * Now add this object to the list of objects that must be
		 * remade to force integrity of uour lists before we start
		 * to make the real targets.
		 */
		lp = (list_t *) fastalloc(sizeof (*lp));
		lp->l_obj = o;
		*inctail = lp;
		inctail = &lp->l_next;
		lp->l_next = 0;

		build_path(o->o_level, o->o_name, includename);
/*error("include '%s' -> '%s' %s\n", o->o_name, includename, prtime(o->o_date));*/
		readfile(includename, must_exist);
	}

	lastc = slc;
	firstc = sfc;
	mfp = smf;
	mfname = smfn;
	lineno = slineno;
	col = scol;
	readbfp = srbp;
	readbfstart = srbs;
	readbfend = srbe;
	rd_buffer = srbf;
}

/*
 * Re-make the included files.
 * This must be done because after they have been made the first time,
 * the dependency list may have changed. If we don't remake the included
 * files, the xxx.d dependency files will not be remade after we touch a file
 * that is not in the primary source list.
 */
EXPORT void
makeincs()
{
	list_t	*l;

	for (l = Incs; l != 0; l = l->l_next) {
/*		printf("inc(%s)\n", l->l_obj->o_name);*/
		omake(l->l_obj, TRUE);
	}
}