add -P flag to m4(1)

Peter Avalos pavalos at theshell.com
Sun Dec 31 11:16:31 PST 2006


On Sat, Dec 30, 2006 at 07:37:36PM +0100, Joerg Sonnenberger wrote:
> On Sat, Dec 30, 2006 at 10:29:41AM -0800, Matthew Dillon wrote:
> > :Here's a patch that will add the -P option to our m4.  Please let me know
> > :what you think.  This is needed to bring in the latest flex sources.
> > :
> > :http://www.theshell.com/~pavalos/wip/m4.patch
> > :
> > :Thanks,
> > :Peter
> > 
> >     Hmm.  Running the getopt loop twice is kinda a hack.  If you absolutely
> >     have to do it at least put getopt's control string in one place so it
> >     isn't duplicated.
> 

We need to know if -P is set before we handle the -D and -U options.

> ...and use optreset please.
> 

Ok, how's this one look:

http://www.theshell.com/~pavalos/wip/m4-2.patch

--Peter

Index: usr.bin/m4/m4.1
===================================================================
RCS file: /home/dcvs/src/usr.bin/m4/m4.1,v
retrieving revision 1.3
diff -u -r1.3 m4.1
--- usr.bin/m4/m4.1	27 Dec 2006 21:29:02 -0000	1.3
+++ usr.bin/m4/m4.1	28 Dec 2006 00:10:12 -0000
@@ -12,7 +12,7 @@
 .Nm
 .Op Fl d Ar flags
 .Op Fl t Ar name
-.Op Fl gs
+.Op Fl Pgs
 .Op Fl D Ar name Ns Op = Ns Ar value
 .Op Fl U Ar name
 .Op Fl I Ar dirname
@@ -69,6 +69,19 @@
 .It Fl U Ar name
 Undefine the symbol
 .Ar name .
+.It Fl P
+Prefixes all
+.Nm
+builtin macros with the string
+.Li m4_ .
+This changes the macro names
+.Li dnl
+to
+.Li m4_dnl ,
+.Li index
+to
+.Li m4_index ,
+and so forth.
 .It Fl I Ar dirname
 Add directory
 .Ar dirname
Index: usr.bin/m4/main.c
===================================================================
RCS file: /home/dcvs/src/usr.bin/m4/main.c,v
retrieving revision 1.3
diff -u -r1.3 main.c
--- usr.bin/m4/main.c	27 Dec 2006 21:29:02 -0000	1.3
+++ usr.bin/m4/main.c	31 Dec 2006 07:34:19 -0000
@@ -82,6 +82,7 @@
 int oindex = 0; 		/* diversion index..	       */
 char null[] = "";		/* as it says.. just a null..  */
 const char *m4wraps = "";       /* m4wrap string default..     */
+int m4prefix = 0;		/* prefix keywords with m4_    */
 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 */
@@ -168,6 +169,7 @@
 	int c;
 	int n;
 	int rval;
+	const char *optstr = "D:I:PU:gd:o:st:";
 	char *p;
 
 	setlocale(LC_ALL, "");
@@ -177,6 +179,14 @@
 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
 		signal(SIGINT, onintr);
 
+	/*
+	 * We need to know if -P is there before checking -D and -U.
+	 */
+	while ((c = getopt(argc, argv, optstr)) != -1)
+		if (c == 'P')
+			m4prefix = 1;
+	optind = optreset = 1;
+
 	initkwds();
 	initspaces();
 	STACKMAX = INITSTACKMAX;
@@ -188,7 +198,7 @@
 	outfile = NULL;
 	resizedivs(MAXOUT);
 
-	while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
+	while ((c = getopt(argc, argv, optstr)) != -1)
 		switch(c) {
 		case 'D':               /* define something..*/
 			for (p = optarg; *p; p++)
@@ -203,6 +213,8 @@
 		case 'I':
 			addtoincludepath(optarg);
 			break;
+		case 'P':
+			break;
 		case 'U':               /* undefine...       */
 			remhash(optarg, TOP);
 			break;
@@ -563,9 +575,16 @@
 	size_t i;
 	unsigned int h;
 	ndptr p;
+	char *k;
 
 	for (i = 0; i < MAXKEYS; i++) {
-		h = hash(keywrds[i].knam);
+		k = (char *)keywrds[i].knam;
+		if (m4prefix) {
+			if (asprintf(&k, "m4_%s", k) == -1)
+				err(1, "asprintf");
+			keywrds[i].knam = k;
+		}
+		h = hash(k);
 		p = (ndptr) xalloc(sizeof(struct ndblock));
 		p->nxtptr = hashtab[h % HASHSIZE];
 		hashtab[h % HASHSIZE] = p;
Attachment:
pgp00011.pgp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pgp00011.pgp
Type: application/octet-stream
Size: 189 bytes
Desc: "Description: PGP signature"
URL: <http://lists.dragonflybsd.org/pipermail/submit/attachments/20061231/bbfb6d66/attachment-0019.obj>


More information about the Submit mailing list