diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2006-03-20 20:27:46 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2006-03-20 20:27:46 +0000 |
commit | 59c4ef87b769c1a36b20f45eab3060bb9dbc0d22 (patch) | |
tree | 6d9a63d7a5d65c7f2ca064e8561040d20885aaab /usr.bin/m4 | |
parent | 0f5ef76916936b0c2154ed95cdab6cfd11c8f6a2 (diff) |
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@
Diffstat (limited to 'usr.bin/m4')
-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 |
6 files changed, 73 insertions, 7 deletions
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_" |