diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2002-04-28 14:37:13 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2002-04-28 14:37:13 +0000 |
commit | 2f15e29fadc2bc35285820205ae7ff94f2d19be8 (patch) | |
tree | 77ba7576a9503cf0fede4d33937d28f1badeb600 /usr.bin/m4 | |
parent | 46b4ea21951a8b60d15104c3a547e37ce9d1ad07 (diff) |
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@
Diffstat (limited to 'usr.bin/m4')
-rw-r--r-- | usr.bin/m4/eval.c | 7 | ||||
-rw-r--r-- | usr.bin/m4/extern.h | 5 | ||||
-rw-r--r-- | usr.bin/m4/m4.1 | 6 | ||||
-rw-r--r-- | usr.bin/m4/main.c | 56 | ||||
-rw-r--r-- | usr.bin/m4/mdef.h | 3 | ||||
-rw-r--r-- | usr.bin/m4/misc.c | 15 |
6 files changed, 73 insertions, 19 deletions
diff --git a/usr.bin/m4/eval.c b/usr.bin/m4/eval.c index 5af3c8774c4..951863936a4 100644 --- a/usr.bin/m4/eval.c +++ b/usr.bin/m4/eval.c @@ -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); diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h index 9170fa89c04..7fb9000613b 100644 --- a/usr.bin/m4/extern.h +++ b/usr.bin/m4/extern.h @@ -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 */ diff --git a/usr.bin/m4/m4.1 b/usr.bin/m4/m4.1 index ccd793d6e2a..ef78e7c9ca4 100644 --- a/usr.bin/m4/m4.1 +++ b/usr.bin/m4/m4.1 @@ -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 diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c index 9e29700fcb7..db3e834edcf 100644 --- a/usr.bin/m4/main.c +++ b/usr.bin/m4/main.c @@ -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); } diff --git a/usr.bin/m4/mdef.h b/usr.bin/m4/mdef.h index 4a10500be28..c387e088fd3 100644 --- a/usr.bin/m4/mdef.h +++ b/usr.bin/m4/mdef.h @@ -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; }; diff --git a/usr.bin/m4/misc.c b/usr.bin/m4/misc.c index 291c2192fde..4943518aa85 100644 --- a/usr.bin/m4/misc.c +++ b/usr.bin/m4/misc.c @@ -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 |