Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 4 Feb 2002 02:21:24 +1100
From:      "Tim J. Robbins" <tim@robbins.dropbear.id.au>
To:        freebsd-standards@FreeBSD.ORG
Subject:   m4 -s option patch
Message-ID:  <20020204022124.A20605@descent.robbins.dropbear.id.au>

next in thread | raw e-mail | index | archive | help

--CE+1k2dSO48ffgeK
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

This patch adds the -s option to m4 which causes it to emit #line preprocessor
directives, removes the bogus -o and -t options which exist in the option
parsing code but do nothing and are not required by any standard.

File name and line number information is stored on a stack in the same
way the FILE*'s are for input, line numbers are incremented in the
`gpbc' macro which all input goes through. #line directives are emitted
when the file changes.

While cleaning up the manual page I noticed we're missing the traceon
and traceoff macros required by SUSV2. I haven't had time to implement
these yet.


Tim

--CE+1k2dSO48ffgeK
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="m4.diff"

Index: m4/eval.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/m4/eval.c,v
retrieving revision 1.14
diff -u -r1.14 eval.c
--- m4/eval.c	2001/07/24 14:09:47	1.14
+++ m4/eval.c	2002/02/03 15:05:03
@@ -584,7 +584,11 @@
 		errx(1, "too many include files");
 	if ((infile[ilevel + 1] = fopen(ifile, "r")) != NULL) {
 		ilevel++;
+		if ((inname[ilevel] = strdup(ifile)) == NULL)
+			err(1, NULL);
+		inlineno[ilevel] = 1;
 		bbase[ilevel] = bufbase = bp;
+		emitline();
 		return (1);
 	}
 	else
@@ -604,9 +608,11 @@
 	register int c;
 
 	if ((pf = fopen(pfile, "r")) != NULL) {
+		fprintf(active, "#line 1 \"%s\"\n", pfile);
 		while ((c = getc(pf)) != EOF)
 			putc(c, active);
 		(void) fclose(pf);
+		emitline();
 		return (1);
 	}
 	else
Index: m4/extern.h
===================================================================
RCS file: /home/ncvs/src/usr.bin/m4/extern.h,v
retrieving revision 1.5
diff -u -r1.5 extern.h
--- m4/extern.h	2001/07/24 14:09:47	1.5
+++ m4/extern.h	2002/02/03 15:05:04
@@ -53,6 +53,7 @@
 void	dopushdef __P((char *, char *));
 void	dosub __P((char *[], int));
 void	doundiv __P((char *[], int));
+void	emitline __P((void));
 void	eval __P((char *[], int, int));
 void	expand __P((char *[], int));
 void	getdiv __P((int));
@@ -72,6 +73,8 @@
 extern stae mstack[];		/* stack of m4 machine */
 extern FILE *active;		/* active output file pointer */
 extern FILE *infile[];		/* input file stack (0=stdin) */
+extern char *inname[];		/* names of these input files  */
+extern int inlineno[];		/* current number in each input*/
 extern FILE *outfile[];		/* diversion array(0=bitbucket) */
 extern int fp; 			/* m4 call frame pointer */
 extern int ilevel;		/* input file stack pointer */
@@ -92,3 +95,5 @@
 extern char *null;		/* as it says.. just a null. */
 extern char rquote;		/* right quote character (') */
 extern char scommt;		/* start character for comment */
+extern int synccpp;		/* Line synchronisation for C preprocessor */
+extern int chscratch;		/* Scratch space for gpbc() macro */  
Index: m4/m4.1
===================================================================
RCS file: /home/ncvs/src/usr.bin/m4/m4.1,v
retrieving revision 1.12
diff -u -r1.12 m4.1
--- m4/m4.1	2001/07/15 08:01:24	1.12
+++ m4/m4.1	2002/02/03 15:05:05
@@ -10,9 +10,9 @@
 .Sh SYNOPSIS
 .Nm
 .Oo
-.Fl D Ns Ar name Ns Op Ar =value
+.Fl D Ar name Ns Op Ar =value
 .Oc
-.Op Fl U Ns Ar name
+.Op Fl U Ar name
 .Op Ar filename
 \|.\|.\|.
 .Sh DESCRIPTION
@@ -41,13 +41,18 @@
 .Pp
 The options are as follows:
 .Bl -tag -width "-Dname[=value]xxx"
-.It Fl D Ns Ar name Ns Oo
+.It Fl s
+Emit
+.Em #line
+directives for
+.Xr cpp 1 .
+.It Fl D Ar name Ns Oo
 .Ar =value
 .Oc
 Define the symbol
 .Ar name
 to have some value (or NULL).
-.It Fl "U" Ns Ar "name"
+.It Fl "U" Ar "name"
 Undefine the symbol
 .Ar name .
 .El
@@ -177,7 +182,34 @@
 .It unix
 A pre-defined macro for testing the OS platform.
 .El
+.Sh DIAGNOSTICS
+.Ex -std
+.Pp
+The exit status can be specified by the input file using the
+.Em m4exit
+macro.
+.Sh STANDARDS
+The
+.Nm
+utility is compatible with the
+.St -susv2
+specification with the exception of the
+.Em traceon
+and
+.Em traceoff
+built-in macros, which are not implemented.
+.Pp
+The
+.Em expr ,
+.Em paste ,
+.Em spaste
+and
+.Em unix
+built-in macros are extensions to the standard.
+.Sh "SEE ALSO"
+.Xr cpp 1
 .Sh AUTHORS
 .An Ozan Yigit Aq oz@sis.yorku.ca
-and
 .An Richard A. O'Keefe Aq ok@goanna.cs.rmit.OZ.AU
+.Sh BUGS
+The tracing macros are not implemented.
Index: m4/main.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/m4/main.c,v
retrieving revision 1.11
diff -u -r1.11 main.c
--- m4/main.c	2001/07/24 14:09:47	1.11
+++ m4/main.c	2002/02/03 15:05:07
@@ -80,6 +80,8 @@
 int sp; 			/* current m4  stack pointer   */
 int fp; 			/* m4 call frame pointer       */
 FILE *infile[MAXINP];		/* input file stack (0=stdin)  */
+char *inname[MAXINP];		/* names of these input files  */
+int inlineno[MAXINP];		/* current number in each input*/
 FILE *outfile[MAXOUT];		/* diversion array(0=bitbucket)*/
 FILE *active;			/* active output file pointer  */
 char *m4temp;			/* filename for diversions     */
@@ -92,6 +94,8 @@
 char rquote = RQUOTE;		/* right quote character (')   */
 char scommt = SCOMMT;		/* start character for comment */
 char ecommt = ECOMMT;		/* end character for comment   */
+int synccpp = 0;		/* Line synchronisation for C preprocessor */
+int chscratch = 0;		/* Scratch space for gpbc() macro */
 
 struct keyblk keywrds[] = {	/* m4 keywords to be installed */
 	"include",      INCLTYPE,
@@ -159,7 +163,7 @@
 
 	initkwds();
 
-	while ((c = getopt(argc, argv, "tD:U:o:")) != -1)
+	while ((c = getopt(argc, argv, "D:U:s")) != -1)
 		switch(c) {
 
 		case 'D':               /* define something..*/
@@ -173,7 +177,9 @@
 		case 'U':               /* undefine...       */
 			remhash(optarg, TOP);
 			break;
-		case 'o':		/* specific output   */
+		case 's':
+			synccpp = 1;
+			break;
 		case '?':
 			usage();
 		}
@@ -195,6 +201,10 @@
  		sp = -1;		/* stack pointer initialized */
 		fp = 0; 		/* frame pointer initialized */
 		infile[0] = stdin;	/* default input (naturally) */
+		if ((inname[0] = strdup("-")) == NULL)
+			err(1, NULL);
+		inlineno[0] = 1;
+		emitline();
 		macro();
 	} else
 		for (; argc--; ++argv) {
@@ -206,6 +216,10 @@
 			sp = -1;
 			fp = 0;
 			infile[0] = ifp;
+			if ((inname[0] = strdup(p)) == NULL)
+				err(1, NULL);
+			inlineno[0] = 1;
+			emitline();
 			macro();
 			if (ifp != stdin)
 				(void)fclose(ifp);
@@ -282,7 +296,9 @@
 				break;			/* all done thanks.. */
 			--ilevel;
 			(void) fclose(infile[ilevel+1]);
+			free(inname[ilevel+1]);
 			bufbase = bbase[ilevel];
+			emitline();
 			continue;
 		}
 	/*
@@ -418,4 +434,13 @@
 		p->defn = null;
 		p->type = keywrds[i].ktyp | STATIC;
 	}
+}
+
+/* Emit preprocessor #line directive if -s option used. */
+void
+emitline()
+{
+	if (synccpp)
+		fprintf(active, "#line %d \"%s\"\n", inlineno[ilevel],
+		    inname[ilevel]);
 }
Index: m4/mdef.h
===================================================================
RCS file: /home/ncvs/src/usr.bin/m4/mdef.h,v
retrieving revision 1.3
diff -u -r1.3 mdef.h
--- m4/mdef.h	1995/05/30 06:31:35	1.3
+++ m4/mdef.h	2002/02/03 15:05:08
@@ -142,7 +142,9 @@
  *      pushf() - push a call frame entry onto stack
  *      pushs() - push a string pointer onto stack
  */
-#define gpbc()   (bp > bufbase) ? (*--bp ? *bp : EOF) : getc(infile[ilevel])
+#define gpbc()   (bp > bufbase) ? (*--bp ? *bp : EOF) : \
+	((chscratch = getc(infile[ilevel])) == '\n' && ++inlineno[ilevel], \
+	chscratch)
 #define pushf(x) if (sp < STACKMAX) mstack[++sp].sfra = (x)
 #define pushs(x) if (sp < STACKMAX) mstack[++sp].sstr = (x)
 
Index: m4/misc.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/m4/misc.c,v
retrieving revision 1.8
diff -u -r1.8 misc.c
--- m4/misc.c	2001/07/24 14:09:47	1.8
+++ m4/misc.c	2002/02/03 15:05:08
@@ -219,6 +219,7 @@
 void
 usage()
 {
-	fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
+	fprintf(stderr,
+"usage: m4 [-s] [-D name[=val]]... [-U name]... file...\n");
 	exit(1);
 }

--CE+1k2dSO48ffgeK--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-standards" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020204022124.A20605>