diff options
author | 2006-03-20 20:27:45 +0000 | |
---|---|---|
committer | 2006-03-20 20:27:45 +0000 | |
commit | 09d1584fdedb31718f16dad81479716dc1eb3ffd (patch) | |
tree | dade753354cf53119e51e6724608a082c9ee0b22 | |
parent | Don't increment a pointer *before* testing it for NULL (diff) | |
download | wireguard-openbsd-09d1584fdedb31718f16dad81479716dc1eb3ffd.tar.xz wireguard-openbsd-09d1584fdedb31718f16dad81479716dc1eb3ffd.zip |
add limited support for format builtin in gnu-m4 mode, because I'm fed
up of patching it away in various autoconf derivatives.
okay miod@
-rw-r--r-- | regress/usr.bin/m4/Makefile | 8 | ||||
-rw-r--r-- | regress/usr.bin/m4/gnuformat.m4 | 1 | ||||
-rw-r--r-- | regress/usr.bin/m4/gnuformat.out | 1 | ||||
-rw-r--r-- | usr.bin/m4/eval.c | 8 | ||||
-rw-r--r-- | usr.bin/m4/extern.h | 5 | ||||
-rw-r--r-- | usr.bin/m4/gnum4.c | 48 | ||||
-rw-r--r-- | usr.bin/m4/m4.1 | 13 | ||||
-rw-r--r-- | usr.bin/m4/main.c | 3 | ||||
-rw-r--r-- | usr.bin/m4/mdef.h | 3 |
9 files changed, 81 insertions, 9 deletions
diff --git a/regress/usr.bin/m4/Makefile b/regress/usr.bin/m4/Makefile index 6b89ec7a220..b0d32a25ac8 100644 --- a/regress/usr.bin/m4/Makefile +++ b/regress/usr.bin/m4/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.22 2005/09/06 15:33:21 espie Exp $ +# $OpenBSD: Makefile,v 1.23 2006/03/20 20:27:45 espie Exp $ FIBOMAX=25 M4=m4 @@ -10,7 +10,8 @@ REGRESS_TARGETS= test-ff_after_dnl test-m4wrap test-m4wrap2 \ test-m4wrap3 test-gm4wrap3 test-fibo \ test-patterns trip test-strangequotes test-redef test-quotes \ test-weird test-args test-esyscmd test-eval test-gnupatterns \ - test-gnupatterns2 test-comments test-synch1 test-synch1bis + test-gnupatterns2 test-comments test-synch1 test-synch1bis \ + test-gnuformat test-ff_after_dnl: ff_after_dnl.m4 ${M4} ff_after_dnl.m4 | diff - ${.CURDIR}/ff_after_dnl.out @@ -39,6 +40,9 @@ test-gnupatterns: test-gnupatterns2: ${M4} -g ${.CURDIR}/gnupatterns2.m4 | diff - ${.CURDIR}/gnupatterns2.out +test-gnuformat: + ${M4} -g ${.CURDIR}/gnuformat.m4 | diff - ${.CURDIR}/gnuformat.out + fibo.out: perl ${.CURDIR}/fibo.pl ${FIBOMAX} >$@ diff --git a/regress/usr.bin/m4/gnuformat.m4 b/regress/usr.bin/m4/gnuformat.m4 new file mode 100644 index 00000000000..b9cf8b7d92c --- /dev/null +++ b/regress/usr.bin/m4/gnuformat.m4 @@ -0,0 +1 @@ +format(`a%15sa%%b%-15sbc%3scd%-3sd', `string', `pouet', `toolong', `toolong2') diff --git a/regress/usr.bin/m4/gnuformat.out b/regress/usr.bin/m4/gnuformat.out new file mode 100644 index 00000000000..3f484d61d4d --- /dev/null +++ b/regress/usr.bin/m4/gnuformat.out @@ -0,0 +1 @@ +a stringa%bpouet bctoolongcdtoolong2d diff --git a/usr.bin/m4/eval.c b/usr.bin/m4/eval.c index 8a841a2b2f6..fc2b0ab4448 100644 --- a/usr.bin/m4/eval.c +++ b/usr.bin/m4/eval.c @@ -1,4 +1,4 @@ -/* $OpenBSD: eval.c,v 1.60 2006/03/20 10:55:19 espie Exp $ */ +/* $OpenBSD: eval.c,v 1.61 2006/03/20 20:27:45 espie Exp $ */ /* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */ /* @@ -283,6 +283,12 @@ expand_builtin(const char *argv[], int argc, int td) if (argc > 2) (void) dopaste(argv[2]); break; + case FORMATTYPE: + if (mimic_gnu) + doformat(argv, argc); + else + m4errx(1, "format builtin is only available in gnu-m4 mode."); + break; #endif case CHNQTYPE: dochq(argv, ac); diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h index 93f873882ca..01df741404a 100644 --- a/usr.bin/m4/extern.h +++ b/usr.bin/m4/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.44 2006/03/20 10:55:19 espie Exp $ */ +/* $OpenBSD: extern.h,v 1.45 2006/03/20 20:27:45 espie Exp $ */ /* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */ /*- @@ -55,7 +55,8 @@ extern void doprintlineno(struct input_file *); extern void doprintfilename(struct input_file *); extern void doesyscmd(const char *); -extern void getdivfile(const char *); +extern void getdivfile(const char *); +extern void doformat(const char *[], int); /* look.c */ diff --git a/usr.bin/m4/gnum4.c b/usr.bin/m4/gnum4.c index 0b71338263f..973df3b030c 100644 --- a/usr.bin/m4/gnum4.c +++ b/usr.bin/m4/gnum4.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gnum4.c,v 1.32 2006/03/20 10:55:19 espie Exp $ */ +/* $OpenBSD: gnum4.c,v 1.33 2006/03/20 20:27:45 espie Exp $ */ /* * Copyright (c) 1999 Marc Espie @@ -503,6 +503,52 @@ doregexp(const char *argv[], int argc) } void +doformat(const char *argv[], int argc) +{ + const char *format = argv[2]; + int pos = 3; + while (*format != 0) { + if (*format != '%') { + addchar(*format++); + } else { + format++; + if (*format == '%' || *format == 0) { + addchar('%'); + if (*format == '%') + format++; + } else { + int left_padded = 0; + unsigned long width; + size_t l; + + if (*format == '-') { + left_padded = 1; + format++; + } + width = strtoul(format, &format, 10); + if (*format != 's') { + m4errx(1, "Unsupported format specification: %s.", argv[2]); + } + format++; + if (pos >= argc) + m4errx(1, "Format with too many values."); + l = strlen(argv[pos]); + if (!left_padded) { + while (l < width--) + addchar(' '); + } + addchars(argv[pos++], l); + if (left_padded) { + while (l < width--) + addchar(' '); + } + } + } + } + pbstr(getstring()); +} + +void doesyscmd(const char *cmd) { int p[2]; diff --git a/usr.bin/m4/m4.1 b/usr.bin/m4/m4.1 index ca688d3bcc0..60c9440afce 100644 --- a/usr.bin/m4/m4.1 +++ b/usr.bin/m4/m4.1 @@ -1,4 +1,4 @@ -.\" @(#) $OpenBSD: m4.1,v 1.43 2005/09/30 20:34:26 jaredy Exp $ +.\" @(#) $OpenBSD: m4.1,v 1.44 2006/03/20 20:27:45 espie Exp $ .\" .\" Copyright (c) 1989, 1993 .\" The Regents of the University of California. All rights reserved. @@ -251,6 +251,16 @@ specifies the minimum number of digits in the result. .It Fn expr expr This is an alias for .Ic eval . +.It Fn format formatstring arg1 ... +Returns +.Fa formatstring +with escape sequences substituted with +.Fa arg1 +and following arguments, in a way similar to +.Xr printf 3 . +This built-in is only available in GNU-m4 compatibility mode, and the +left-padding flag, an optional field width and the %s data type +are the only supported parameters. .It Fn ifdef name yes no If the macro named by the first argument is defined then return the second argument, otherwise the third. @@ -439,6 +449,7 @@ For portability, one should not use the macros .Ic builtin , .Ic esycmd , .Ic expr , +.Ic format , .Ic indir , .Ic paste , .Ic patsubst , diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c index f489cd38b6d..3ade512698f 100644 --- a/usr.bin/m4/main.c +++ b/usr.bin/m4/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.70 2006/03/20 10:55:19 espie Exp $ */ +/* $OpenBSD: main.c,v 1.71 2006/03/20 20:27:45 espie Exp $ */ /* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */ /*- @@ -111,6 +111,7 @@ struct keyblk keywrds[] = { /* m4 keywords to be installed */ { "esyscmd", ESYSCMDTYPE}, { "__file__", FILENAMETYPE | NOARGS}, { "__line__", LINETYPE | NOARGS}, + { "format", FORMATTYPE}, #endif { "popdef", POPDTYPE }, { "pushdef", PUSDTYPE }, diff --git a/usr.bin/m4/mdef.h b/usr.bin/m4/mdef.h index e47dbd93013..d5e97e10234 100644 --- a/usr.bin/m4/mdef.h +++ b/usr.bin/m4/mdef.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mdef.h,v 1.28 2003/06/30 22:13:33 espie Exp $ */ +/* $OpenBSD: mdef.h,v 1.29 2006/03/20 20:27:45 espie Exp $ */ /* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */ /* @@ -84,6 +84,7 @@ #define ESYSCMDTYPE 41 #define TRACEONTYPE 42 #define TRACEOFFTYPE 43 +#define FORMATTYPE 44 #define BUILTIN_MARKER "__builtin_" |