summaryrefslogtreecommitdiff
path: root/usr.bin/m4/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/m4/main.c')
-rw-r--r--usr.bin/m4/main.c56
1 files changed, 45 insertions, 11 deletions
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);
}