This adds the -P option as supported by gnu m4, it prefixes builtin
macros with "m4_". It's needed in order to build a newer version
of flex with m4 from base. Several people have seen this when
I sent it around earlier this year but not much feedback on this
diff. Any comments/oks?

Index: extern.h
===================================================================
RCS file: /cvs/src/usr.bin/m4/extern.h,v
retrieving revision 1.48
diff -u -p -r1.48 extern.h
--- extern.h    21 Aug 2008 20:59:14 -0000      1.48
+++ extern.h    14 Oct 2009 10:48:42 -0000
@@ -173,4 +173,5 @@ extern char scommt[MAXCCHARS+1];/* start
 extern int  synch_lines;       /* line synchronisation directives */
 
 extern int mimic_gnu;          /* behaves like gnu-m4 */
+extern int prefix_builtins;    /* prefix builtin macros with m4_ */
 
Index: look.c
===================================================================
RCS file: /cvs/src/usr.bin/m4/look.c,v
retrieving revision 1.19
diff -u -p -r1.19 look.c
--- look.c      26 Jun 2009 22:03:17 -0000      1.19
+++ look.c      14 Oct 2009 10:48:42 -0000
@@ -231,11 +231,19 @@ void 
 setup_builtin(const char *name, unsigned int type)
 {
        ndptr n;
+       char *name2;
 
-       n = create_entry(name);
+       if(prefix_builtins) {
+               name2 = xalloc(strlen(name)+3+1, NULL);
+               memcpy(name2, "m4_", 3);
+               memcpy(name2 + 3, name, strlen(name)+1);
+       } else
+               name2 = (char *)name;
+
+       n = create_entry(name2);
        n->builtin_type = type;
        n->d = xalloc(sizeof(struct macro_definition), NULL);
-       n->d->defn = xstrdup(name);
+       n->d->defn = xstrdup(name2);
        n->d->type = type;
        n->d->next = NULL;
 }
Index: m4.1
===================================================================
RCS file: /cvs/src/usr.bin/m4/m4.1,v
retrieving revision 1.55
diff -u -p -r1.55 m4.1
--- m4.1        8 Feb 2009 17:15:10 -0000       1.55
+++ m4.1        14 Oct 2009 10:48:42 -0000
@@ -38,7 +38,7 @@
 .Nd macro language processor
 .Sh SYNOPSIS
 .Nm m4
-.Op Fl gs
+.Op Fl gPs
 .Oo
 .Sm off
 .Fl D Ar name Op No = Ar value
@@ -150,6 +150,13 @@ to the include path.
 .It Fl o Ar filename
 Send trace output to
 .Ar filename .
+.It Fl P
+Prefix all built-in macros with
+.Sq m4_ .
+For example, instead of writing
+.Ic define ,
+you must use
+.Ic m4_define .
 .It Fl s
 Output line synchronization directives, suitable for
 .Xr cpp 1 .
Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/m4/main.c,v
retrieving revision 1.76
diff -u -p -r1.76 main.c
--- main.c      16 Aug 2008 12:21:46 -0000      1.76
+++ main.c      14 Oct 2009 10:48:43 -0000
@@ -77,6 +77,7 @@ char rquote[MAXCCHARS+1] = {RQUOTE};  /* 
 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 */
+int  prefix_builtins = 0;      /* -P option to prefix builtin keywords */
 
 struct keyblk {
         char    *knam;          /* keyword name */
@@ -175,7 +176,6 @@ main(int argc, char *argv[])
                signal(SIGINT, onintr);
 
        init_macros();
-       initkwds();
        initspaces();
        STACKMAX = INITSTACKMAX;
 
@@ -186,7 +186,7 @@ main(int argc, char *argv[])
        outfile = NULL;
        resizedivs(MAXOUT);
 
-       while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
+       while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1)
                switch(c) {
 
                case 'D':               /* define something..*/
@@ -200,12 +200,14 @@ main(int argc, char *argv[])
                case 'I':
                        addtoincludepath(optarg);
                        break;
+               case 'P':
+                       prefix_builtins = 1;
+                       break;
                case 'U':               /* undefine...       */
                        macro_popdef(optarg);
                        break;
                case 'g':
                        mimic_gnu = 1;
-                       setup_builtin("format", FORMATTYPE);
                        break;
                case 'd':
                        set_trace_flags(optarg);
@@ -225,6 +227,10 @@ main(int argc, char *argv[])
 
         argc -= optind;
         argv += optind;
+
+       initkwds();
+       if (mimic_gnu)
+               setup_builtin("format", FORMATTYPE);
 
        active = stdout;                /* default active output     */
        bbase[0] = bufbase;
Index: misc.c
===================================================================
RCS file: /cvs/src/usr.bin/m4/misc.c,v
retrieving revision 1.40
diff -u -p -r1.40 misc.c
--- misc.c      21 Aug 2008 20:59:14 -0000      1.40
+++ misc.c      14 Oct 2009 10:48:43 -0000
@@ -341,7 +341,7 @@ xstrdup(const char *s)
 void
 usage()
 {
-       fprintf(stderr, "usage: m4 [-gs] [-Dname[=value]] [-d flags] "
+       fprintf(stderr, "usage: m4 [-gsP] [-Dname[=value]] [-d flags] "
                        "[-I dirname] [-o filename]\n"
                        "\t[-t macro] [-Uname] [file ...]\n");
        exit(1);

Reply via email to