View | Details | Raw Unified | Return to bug 195929 | Differences between
and this patch

Collapse All | Expand All

(-)compile.c (-43 / +45 lines)
Lines 64-80 Link Here
64
	int	lh_ref;
64
	int	lh_ref;
65
} *labels[LHSZ];
65
} *labels[LHSZ];
66
66
67
static char	 *compile_addr(char *, struct s_addr *);
67
static const char	 *compile_addr(const char *, struct s_addr *);
68
static char	 *compile_ccl(char **, char *);
68
static       char	 *compile_ccl(const char **, char *);
69
static char	 *compile_delimited(char *, char *, int);
69
static const char	 *compile_delimited(const char *, char *, int);
70
static char	 *compile_flags(char *, struct s_subst *);
70
static const char	 *compile_flags(const char *, struct s_subst *);
71
static regex_t	 *compile_re(char *, int);
71
static       regex_t	 *compile_re(const char *, int);
72
static char	 *compile_subst(char *, struct s_subst *);
72
static const char	 *compile_subst(const char *, struct s_subst *);
73
static char	 *compile_text(void);
73
static       char	 *compile_text(void);
74
static char	 *compile_tr(char *, struct s_tr **);
74
static const char	 *compile_tr(const char *, struct s_tr **);
75
static struct s_command
75
static struct s_command
76
		**compile_stream(struct s_command **);
76
		**compile_stream(struct s_command **);
77
static char	 *duptoeol(char *, const char *);
77
static char	 *duptoeol(const char *, const char *);
78
static void	  enterlabel(struct s_command *);
78
static void	  enterlabel(struct s_command *);
79
static struct s_command
79
static struct s_command
80
		 *findlabel(char *);
80
		 *findlabel(char *);
Lines 153-160 Link Here
153
static struct s_command **
153
static struct s_command **
154
compile_stream(struct s_command **link)
154
compile_stream(struct s_command **link)
155
{
155
{
156
	char *p;
156
	const char *p;
157
	static char lbuf[_POSIX2_LINE_MAX + 1];	/* To save stack */
158
	struct s_command *cmd, *cmd2, *stack;
157
	struct s_command *cmd, *cmd2, *stack;
159
	struct s_format *fp;
158
	struct s_format *fp;
160
	char re[_POSIX2_LINE_MAX + 1];
159
	char re[_POSIX2_LINE_MAX + 1];
Lines 162-168 Link Here
162
161
163
	stack = 0;
162
	stack = 0;
164
	for (;;) {
163
	for (;;) {
165
		if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) {
164
		if ((p = cu_fgets(NULL)) == NULL) {
166
			if (stack != 0)
165
			if (stack != 0)
167
				errx(1, "%lu: %s: unexpected EOF (pending }'s)",
166
				errx(1, "%lu: %s: unexpected EOF (pending }'s)",
168
							linenum, fname);
167
							linenum, fname);
Lines 372-379 Link Here
372
 * in the case of a non-terminated string.  The character array d is filled
371
 * in the case of a non-terminated string.  The character array d is filled
373
 * with the processed string.
372
 * with the processed string.
374
 */
373
 */
375
static char *
374
static const char *
376
compile_delimited(char *p, char *d, int is_tr)
375
compile_delimited(const char *p, char *d, int is_tr)
377
{
376
{
378
	char c;
377
	char c;
379
378
Lines 416-425 Link Here
416
415
417
/* compile_ccl: expand a POSIX character class */
416
/* compile_ccl: expand a POSIX character class */
418
static char *
417
static char *
419
compile_ccl(char **sp, char *t)
418
compile_ccl(const char **sp, char *t)
420
{
419
{
421
	int c, d;
420
	int c, d;
422
	char *s = *sp;
421
	const char *s = *sp;
423
422
424
	*t++ = *s++;
423
	*t++ = *s++;
425
	if (*s == '^')
424
	if (*s == '^')
Lines 442-448 Link Here
442
 * Cflags are passed to regcomp.
441
 * Cflags are passed to regcomp.
443
 */
442
 */
444
static regex_t *
443
static regex_t *
445
compile_re(char *re, int case_insensitive)
444
compile_re(const char *re, int case_insensitive)
446
{
445
{
447
	regex_t *rep;
446
	regex_t *rep;
448
	int eval, flags;
447
	int eval, flags;
Lines 466-475 Link Here
466
 * point to a saved copy of it.  Nsub is the number of parenthesized regular
465
 * point to a saved copy of it.  Nsub is the number of parenthesized regular
467
 * expressions.
466
 * expressions.
468
 */
467
 */
469
static char *
468
static const char *
470
compile_subst(char *p, struct s_subst *s)
469
compile_subst(const char *p, struct s_subst *s)
471
{
470
{
472
	static char lbuf[_POSIX2_LINE_MAX + 1];
473
	int asize, size;
471
	int asize, size;
474
	u_char ref;
472
	u_char ref;
475
	char c, *text, *op, *sp;
473
	char c, *text, *op, *sp;
Lines 523-530 Link Here
523
					*sp++ = '\\';
521
					*sp++ = '\\';
524
			} else if (*p == c) {
522
			} else if (*p == c) {
525
				if (*++p == '\0' && more) {
523
				if (*++p == '\0' && more) {
526
					if (cu_fgets(lbuf, sizeof(lbuf), &more))
524
					const char *nextp;
527
						p = lbuf;
525
526
					nextp = cu_fgets(&more);
527
					if (nextp != NULL)
528
						p = nextp;
528
				}
529
				}
529
				*sp++ = '\0';
530
				*sp++ = '\0';
530
				size += sp - op;
531
				size += sp - op;
Lines 544-550 Link Here
544
			if ((text = realloc(text, asize)) == NULL)
545
			if ((text = realloc(text, asize)) == NULL)
545
				err(1, "realloc");
546
				err(1, "realloc");
546
		}
547
		}
547
	} while (cu_fgets(p = lbuf, sizeof(lbuf), &more));
548
	} while ((p = cu_fgets(&more)));
548
	errx(1, "%lu: %s: unterminated substitute in regular expression",
549
	errx(1, "%lu: %s: unterminated substitute in regular expression",
549
			linenum, fname);
550
			linenum, fname);
550
	/* NOTREACHED */
551
	/* NOTREACHED */
Lines 553-560 Link Here
553
/*
554
/*
554
 * Compile the flags of the s command
555
 * Compile the flags of the s command
555
 */
556
 */
556
static char *
557
static const char *
557
compile_flags(char *p, struct s_subst *s)
558
compile_flags(const char *p, struct s_subst *s)
558
{
559
{
559
	int gn;			/* True if we have seen g or n */
560
	int gn;			/* True if we have seen g or n */
560
	unsigned long nval;
561
	unsigned long nval;
Lines 594-605 Link Here
594
"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
595
"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
595
			gn = 1;
596
			gn = 1;
596
			errno = 0;
597
			errno = 0;
597
			nval = strtol(p, &p, 10);
598
			nval = strtol(p, &q, 10);
598
			if (errno == ERANGE || nval > INT_MAX)
599
			if (errno == ERANGE || nval > INT_MAX)
599
				errx(1,
600
				errx(1,
600
"%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
601
"%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
601
			s->n = nval;
602
			s->n = nval;
602
			p--;
603
			p = q - 1;
603
			break;
604
			break;
604
		case 'w':
605
		case 'w':
605
			p++;
606
			p++;
Lines 637-644 Link Here
637
/*
638
/*
638
 * Compile a translation set of strings into a lookup table.
639
 * Compile a translation set of strings into a lookup table.
639
 */
640
 */
640
static char *
641
static const char *
641
compile_tr(char *p, struct s_tr **py)
642
compile_tr(const char *p, struct s_tr **py)
642
{
643
{
643
	struct s_tr *y;
644
	struct s_tr *y;
644
	int i;
645
	int i;
Lines 733-748 Link Here
733
compile_text(void)
734
compile_text(void)
734
{
735
{
735
	int asize, esc_nl, size;
736
	int asize, esc_nl, size;
736
	char *text, *p, *op, *s;
737
	char *text, *s;
737
	char lbuf[_POSIX2_LINE_MAX + 1];
738
	const char *p, *op;
738
739
739
	asize = 2 * _POSIX2_LINE_MAX + 1;
740
	asize = 2 * _POSIX2_LINE_MAX + 1;
740
	if ((text = malloc(asize)) == NULL)
741
	if ((text = malloc(asize)) == NULL)
741
		err(1, "malloc");
742
		err(1, "malloc");
742
	size = 0;
743
	size = 0;
743
	while (cu_fgets(lbuf, sizeof(lbuf), NULL)) {
744
	while ((p = cu_fgets(NULL))) {
744
		op = s = text + size;
745
		op = s = text + size;
745
		p = lbuf;
746
		EATSPACE();
746
		EATSPACE();
747
		for (esc_nl = 0; *p != '\0'; p++) {
747
		for (esc_nl = 0; *p != '\0'; p++) {
748
			if (*p == '\\' && p[1] != '\0' && *++p == '\n')
748
			if (*p == '\\' && p[1] != '\0' && *++p == '\n')
Lines 761-769 Link Here
761
		}
761
		}
762
	}
762
	}
763
	text[size] = '\0';
763
	text[size] = '\0';
764
	if ((p = realloc(text, size + 1)) == NULL)
764
	if ((text = realloc(text, size + 1)) == NULL)
765
		err(1, "realloc");
765
		err(1, "realloc");
766
	return (p);
766
	return (text);
767
}
767
}
768
768
769
/*
769
/*
Lines 770-777 Link Here
770
 * Get an address and return a pointer to the first character after
770
 * Get an address and return a pointer to the first character after
771
 * it.  Fill the structure pointed to according to the address.
771
 * it.  Fill the structure pointed to according to the address.
772
 */
772
 */
773
static char *
773
static const char *
774
compile_addr(char *p, struct s_addr *a)
774
compile_addr(const char *p, struct s_addr *a)
775
{
775
{
776
	char *end, re[_POSIX2_LINE_MAX + 1];
776
	char *end, re[_POSIX2_LINE_MAX + 1];
777
	int icase;
777
	int icase;
Lines 825-846 Link Here
825
 *	Return a copy of all the characters up to \n or \0.
825
 *	Return a copy of all the characters up to \n or \0.
826
 */
826
 */
827
static char *
827
static char *
828
duptoeol(char *s, const char *ctype)
828
duptoeol(const char *s, const char *ctype)
829
{
829
{
830
	size_t len;
830
	size_t len;
831
	int ws;
831
	int ws;
832
	char *p, *start;
832
	char *p;
833
	const char *start;
833
834
834
	ws = 0;
835
	ws = 0;
835
	for (start = s; *s != '\0' && *s != '\n'; ++s)
836
	for (start = s; *s != '\0' && *s != '\n'; ++s)
836
		ws = isspace((unsigned char)*s);
837
		ws = isspace((unsigned char)*s);
837
	*s = '\0';
838
	if (ws)
838
	if (ws)
839
		warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
839
		warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
840
	len = s - start + 1;
840
	len = s - start;
841
	if ((p = malloc(len)) == NULL)
841
	if ((p = malloc(len + 1)) == NULL)
842
		err(1, "malloc");
842
		err(1, "malloc");
843
	return (memmove(p, start, len));
843
	memmove(p, start, len);
844
	p[len] = '\0';
845
	return p;
844
}
846
}
845
847
846
/*
848
/*
(-)extern.h (-2 / +2 lines)
Lines 48-56 Link Here
48
void	 cfclose(struct s_command *, struct s_command *);
48
void	 cfclose(struct s_command *, struct s_command *);
49
void	 compile(void);
49
void	 compile(void);
50
void	 cspace(SPACE *, const char *, size_t, enum e_spflag);
50
void	 cspace(SPACE *, const char *, size_t, enum e_spflag);
51
char	*cu_fgets(char *, int, int *);
51
const char	*cu_fgets(int *);
52
int	 mf_fgets(SPACE *, enum e_spflag);
52
int	 mf_fgets(SPACE *, enum e_spflag);
53
int	 lastline(void);
53
int	 lastline(void);
54
void	 process(void);
54
void	 process(void);
55
void	 resetstate(void);
55
void	 resetstate(void);
56
char	*strregerror(int, regex_t *);
56
const char	*strregerror(int, const regex_t *);
(-)main.c (-39 / +26 lines)
Lines 73-79 Link Here
73
struct s_compunit {
73
struct s_compunit {
74
	struct s_compunit *next;
74
	struct s_compunit *next;
75
	enum e_cut {CU_FILE, CU_STRING} type;
75
	enum e_cut {CU_FILE, CU_STRING} type;
76
	char *s;			/* Pointer to string or fname */
76
	const char *s;			/* Pointer to string or fname */
77
};
77
};
78
78
79
/*
79
/*
Lines 86-92 Link Here
86
 * Linked list of files to be processed
86
 * Linked list of files to be processed
87
 */
87
 */
88
struct s_flist {
88
struct s_flist {
89
	char *fname;
89
	const char *fname;
90
	struct s_flist *next;
90
	struct s_flist *next;
91
};
91
};
92
92
Lines 117-124 Link Here
117
static const char *inplace;	/* Inplace edit file extension. */
117
static const char *inplace;	/* Inplace edit file extension. */
118
u_long linenum;
118
u_long linenum;
119
119
120
static void add_compunit(enum e_cut, char *);
120
static void add_compunit(enum e_cut, const char *);
121
static void add_file(char *);
121
static void add_file(const char *);
122
static void usage(void);
122
static void usage(void);
123
123
124
int
124
int
Lines 214-227 Link Here
214
 * Like fgets, but go through the chain of compilation units chaining them
209
 * Like fgets, but go through the chain of compilation units chaining them
215
 * together.  Empty strings and files are ignored.
210
 * together.  Empty strings and files are ignored.
216
 */
211
 */
217
char *
212
const char *
218
cu_fgets(char *buf, int n, int *more)
213
cu_fgets(int *more)
219
{
214
{
220
	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
215
	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
221
	static FILE *f;		/* Current open file */
216
	static FILE *f;		/* Current open file */
222
	static char *s;		/* Current pointer inside string */
217
	static const char *s;	/* Current pointer inside string */
223
	static char string_ident[30];
218
	static char string_ident[30], *lastresult;
219
	static size_t lastsize;
224
	char *p;
220
	char *p;
221
	const char *start;
225
222
226
again:
223
again:
227
	switch (state) {
224
	switch (state) {
Lines 251-264 Link Here
251
			goto again;
248
			goto again;
252
		}
249
		}
253
	case ST_FILE:
250
	case ST_FILE:
254
		if ((p = fgets(buf, n, f)) != NULL) {
251
		p = lastresult;
252
		if (getline(&p, &lastsize, f) != -1) {
255
			linenum++;
253
			linenum++;
256
			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
254
			if (linenum == 1 && p[0] == '#' && p[1] == 'n')
257
				nflag = 1;
255
				nflag = 1;
258
			if (more != NULL)
256
			if (more != NULL)
259
				*more = !feof(f);
257
				*more = !feof(f);
260
			return (p);
258
			return (lastresult = p);
261
		}
259
		} else if (ferror(f))
260
			err(1, "%s", script->s);
262
		script = script->next;
261
		script = script->next;
263
		(void)fclose(f);
262
		(void)fclose(f);
264
		state = ST_EOF;
263
		state = ST_EOF;
Lines 266-304 Link Here
266
	case ST_STRING:
265
	case ST_STRING:
267
		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
266
		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
268
			nflag = 1;
267
			nflag = 1;
269
		p = buf;
268
		else if (s[0] == '\0') {
269
			state = ST_EOF;
270
			goto again;
271
		}
272
		start = s;
270
		for (;;) {
273
		for (;;) {
271
			if (n-- <= 1) {
272
				*p = '\0';
273
				linenum++;
274
				if (more != NULL)
275
					*more = 1;
276
				return (buf);
277
			}
278
			switch (*s) {
274
			switch (*s) {
279
			case '\0':
275
			case '\0':
280
				state = ST_EOF;
281
				if (s == script->s) {
282
					script = script->next;
283
					goto again;
284
				} else {
285
					script = script->next;
286
					*p = '\0';
287
					linenum++;
288
					if (more != NULL)
289
						*more = 0;
290
					return (buf);
291
				}
292
			case '\n':
276
			case '\n':
293
				*p++ = '\n';
277
				script = script->next;
294
				*p = '\0';
295
				s++;
296
				linenum++;
278
				linenum++;
297
				if (more != NULL)
279
				if (more != NULL)
298
					*more = 0;
280
					*more = 0;
299
				return (buf);
281
				return (start);
300
			default:
282
			default:
301
				*p++ = *s++;
283
				s++;
302
			}
284
			}
303
		}
285
		}
304
	}
286
	}
Lines 452-458 Link Here
452
 * Add a compilation unit to the linked list
434
 * Add a compilation unit to the linked list
453
 */
435
 */
454
static void
436
static void
455
add_compunit(enum e_cut type, char *s)
437
add_compunit(enum e_cut type, const char *s)
456
{
438
{
457
	struct s_compunit *cu;
439
	struct s_compunit *cu;
458
440
Lines 469-475 Link Here
469
 * Add a file to the linked list
451
 * Add a file to the linked list
470
 */
452
 */
471
static void
453
static void
472
add_file(char *s)
454
add_file(const char *s)
473
{
455
{
474
	struct s_flist *fp;
456
	struct s_flist *fp;
475
457
(-)misc.c (-2 / +2 lines)
Lines 55-62 Link Here
55
 * because of the silly semantics of regerror (we can never know the size of
55
 * because of the silly semantics of regerror (we can never know the size of
56
 * the buffer).
56
 * the buffer).
57
 */
57
 */
58
char *
58
const char *
59
strregerror(int errcode, regex_t *preg)
59
strregerror(int errcode, const regex_t *preg)
60
{
60
{
61
	static char *oe;
61
	static char *oe;
62
	size_t s;
62
	size_t s;

Return to bug 195929