1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

Implement -s.

Triggered by recent FreeBSD changes.

- emits #line directives at every file change (like FreeBSD)
- maintains a synch_lineno variable to verify when the output gets out
of synch with the input, so that it can emit #line to re-synch as well
(unlike FreeBSD)

To do: either handle \end-of-line, or recognize when a macro expansion
is in progress, so that line synch don't perturb cpp on multi-line
expansions.

With this, we should have a fully POSIX-compliant m4.

ok miod@
This commit is contained in:
espie 2002-04-28 14:37:12 +00:00
parent 2464465152
commit aec72266de
6 changed files with 73 additions and 19 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: eval.c,v 1.44 2002/04/26 16:15:16 espie Exp $ */
/* $OpenBSD: eval.c,v 1.45 2002/04/28 14:37:12 espie Exp $ */
/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
/*
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.2 (Berkeley) 4/27/95";
#else
static char rcsid[] = "$OpenBSD: eval.c,v 1.44 2002/04/26 16:15:16 espie Exp $";
static char rcsid[] = "$OpenBSD: eval.c,v 1.45 2002/04/28 14:37:12 espie Exp $";
#endif
#endif /* not lint */
@ -753,9 +753,12 @@ dopaste(const char *pfile)
int c;
if ((pf = fopen(pfile, "r")) != NULL) {
if (synch_lines)
fprintf(active, "#line 1 \"%s\"\n", pfile);
while ((c = getc(pf)) != EOF)
putc(c, active);
(void) fclose(pf);
emit_synchline();
return (1);
} else
return (0);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: extern.h,v 1.29 2002/02/16 21:27:48 millert Exp $ */
/* $OpenBSD: extern.h,v 1.30 2002/04/28 14:37:12 espie Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*-
@ -71,6 +71,8 @@ extern void remhash(const char *, int);
extern void outputstr(const char *);
extern int builtin_type(const char *);
extern char *builtin_realname(int);
extern void do_emit_synchline(void);
#define emit_synchline() do { if (synch_lines) do_emit_synchline(); } while(0)
/* misc.c */
extern void chrsave(int);
@ -148,5 +150,6 @@ extern char *m4wraps; /* m4wrap string default. */
extern char *null; /* as it says.. just a null. */
extern char rquote[MAXCCHARS+1];/* right quote character (') */
extern char scommt[MAXCCHARS+1];/* start character for comment */
extern int synch_lines; /* line synchronisation directives */
extern int mimic_gnu; /* behaves like gnu-m4 */

View File

@ -1,4 +1,4 @@
.\" @(#) $OpenBSD: m4.1,v 1.24 2002/04/18 18:57:23 espie Exp $
.\" @(#) $OpenBSD: m4.1,v 1.25 2002/04/28 14:37:12 espie Exp $
.\"
.\"
.Dd January 26, 1993
@ -12,6 +12,7 @@
.Op Fl d Ar flags
.Op Fl t Ar name
.Op Fl g
.Op Fl s
.Oo
.Fl D Ns Ar name Ns Op Ar =value
.Oc
@ -104,6 +105,9 @@ In this mode, changequote with
two empty parameters deactivates quotes, translit handles simple character
ranges (e.g., a-z), regular expressions mimic emacs behavior,
and the number of diversions is unlimited.
.It Fl s
Output line synchronization directives, suitable for
.Xr cpp 1 .
.El
.Sh SYNTAX
.Nm m4

View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $ */
/* $OpenBSD: main.c,v 1.54 2002/04/28 14:37:12 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $";
static char rcsid[] = "$OpenBSD: main.c,v 1.54 2002/04/28 14:37:12 espie Exp $";
#endif
#endif /* not lint */
@ -91,6 +91,7 @@ char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
int synch_lines = 0; /* line synchronisation for C preprocessor */
struct keyblk keywrds[] = { /* m4 keywords to be installed */
{ "include", INCLTYPE },
@ -166,6 +167,8 @@ static void macro(void);
static void initkwds(void);
static ndptr inspect(int, char *);
static int do_look_ahead(int, const char *);
static void reallyoutputstr(const char *);
static void reallyputchar(int);
static void enlarge_stack(void);
@ -192,7 +195,7 @@ main(int argc, char *argv[])
outfile = NULL;
resizedivs(MAXOUT);
while ((c = getopt(argc, argv, "gt:d:D:U:o:I:")) != -1)
while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
switch(c) {
case 'D': /* define something..*/
@ -215,6 +218,9 @@ main(int argc, char *argv[])
case 'd':
set_trace_flags(optarg);
break;
case 's':
synch_lines = 1;
break;
case 't':
mark_traced(optarg, 1);
break;
@ -357,6 +363,7 @@ macro()
if (ilevel <= 0)
break; /* all done thanks.. */
release_input(infile+ilevel--);
emit_synchline();
bufbase = bbase[ilevel];
continue;
}
@ -390,7 +397,7 @@ macro()
} else {
if (nlpar > 0) {
if (sp < 0)
putc(l, active);
reallyputchar(l);
else
CHRSAVE(l);
}
@ -400,22 +407,22 @@ macro()
}
else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
fputs(scommt, active);
reallyoutputstr(scommt);
for(;;) {
t = gpbc();
if (LOOK_AHEAD(t, ecommt)) {
fputs(ecommt, active);
reallyoutputstr(ecommt);
break;
}
if (t == EOF)
break;
putc(t, active);
reallyputchar(t);
}
}
else if (sp < 0) { /* not in a macro at all */
putc(t, active); /* output directly.. */
reallyputchar(t); /* output directly.. */
}
else switch(t) {
@ -488,13 +495,40 @@ void
outputstr(const char *s)
{
if (sp < 0)
while (*s)
putc(*s++, active);
reallyoutputstr(s);
else
while (*s)
CHRSAVE(*s++);
}
void
reallyoutputstr(const char *s)
{
if (synch_lines) {
while (*s) {
fputc(*s, active);
if (*s++ == '\n') {
infile[ilevel].synch_lineno++;
if (infile[ilevel].synch_lineno !=
infile[ilevel].lineno)
do_emit_synchline();
}
}
} else
fputs(s, active);
}
void
reallyputchar(int c)
{
putc(c, active);
if (synch_lines && c == '\n') {
infile[ilevel].synch_lineno++;
if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
do_emit_synchline();
}
}
/*
* build an input token..
* consider only those starting with _ or A-Za-z. This is a
@ -521,7 +555,7 @@ inspect(int c, char *tp)
outputstr(name);
while (isalnum(c = gpbc()) || c == '_') {
if (sp < 0)
putc(c, active);
reallyputchar(c);
else
CHRSAVE(c);
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: mdef.h,v 1.21 2001/09/27 11:40:33 espie Exp $ */
/* $OpenBSD: mdef.h,v 1.22 2002/04/28 14:37:12 espie Exp $ */
/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */
/*
@ -157,6 +157,7 @@ struct input_file {
FILE *file;
char *name;
unsigned long lineno;
unsigned long synch_lineno; /* used for -s */
int c;
};

View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.27 2002/04/26 16:15:16 espie Exp $ */
/* $OpenBSD: misc.c,v 1.28 2002/04/28 14:37:12 espie Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
/*
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: misc.c,v 1.27 2002/04/26 16:15:16 espie Exp $";
static char rcsid[] = "$OpenBSD: misc.c,v 1.28 2002/04/28 14:37:12 espie Exp $";
#endif
#endif /* not lint */
@ -288,7 +288,7 @@ xstrdup(const char *s)
void
usage()
{
fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname] [-I dirname...]\n");
fprintf(stderr, "usage: m4 [-gs] [-d flags] [-t macro] [-o file] [-Dname[=val]] [-Uname] [-I dirname...]\n");
exit(1);
}
@ -311,6 +311,15 @@ set_input(struct input_file *f, FILE *real, const char *name)
f->lineno = 1;
f->c = 0;
f->name = xstrdup(name);
emit_synchline();
}
void
do_emit_synchline()
{
fprintf(active, "#line %lu \"%s\"\n",
infile[ilevel].lineno, infile[ilevel].name);
infile[ilevel].synch_lineno = infile[ilevel].lineno;
}
void