summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2008-08-21 21:01:05 +0000
committerMarc Espie <espie@cvs.openbsd.org>2008-08-21 21:01:05 +0000
commita5cd275931f70b7325013321e283072d2a12ee69 (patch)
tree0cae634f711fba74cb553077510602a7956100e0 /usr.bin
parent941d9a3ae19d7dd0b7479dcc1c0c6e0c20bac4b9 (diff)
extend format support to cater to recent GNU autoconf
okay otto@, some useful ideas from miod@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/m4/gnum4.c102
1 files changed, 72 insertions, 30 deletions
diff --git a/usr.bin/m4/gnum4.c b/usr.bin/m4/gnum4.c
index f96011e3a0f..8d3842f4f7b 100644
--- a/usr.bin/m4/gnum4.c
+++ b/usr.bin/m4/gnum4.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gnum4.c,v 1.38 2008/08/16 12:23:50 espie Exp $ */
+/* $OpenBSD: gnum4.c,v 1.39 2008/08/21 21:01:04 espie Exp $ */
/*
* Copyright (c) 1999 Marc Espie
@@ -508,43 +508,85 @@ doformat(const char *argv[], int argc)
{
const char *format = argv[2];
int pos = 3;
+ int left_padded;
+ long width;
+ size_t l;
+ const char *thisarg;
+ char temp[2];
+ long extra;
while (*format != 0) {
if (*format != '%') {
addchar(*format++);
+ continue;
+ }
+
+ format++;
+ if (*format == '%') {
+ addchar(*format++);
+ continue;
+ }
+ if (*format == 0) {
+ addchar('%');
+ break;
+ }
+
+ if (*format == '*') {
+ format++;
+ if (pos >= argc)
+ m4errx(1,
+ "Format with too many format specifiers.");
+ width = strtol(argv[pos++], NULL, 10);
+ } else {
+ width = strtol(format, (char **)&format, 10);
+ }
+ if (width < 0) {
+ left_padded = 1;
+ width = -width;
} else {
+ left_padded = 0;
+ }
+ if (*format == '.') {
format++;
- if (*format == '%' || *format == 0) {
- addchar('%');
- if (*format == '%')
- format++;
+ if (*format == '*') {
+ format++;
+ if (pos >= argc)
+ m4errx(1,
+ "Format with too many format specifiers.");
+ extra = strtol(argv[pos++], NULL, 10);
} else {
- int left_padded = 0;
- unsigned long width;
- size_t l;
-
- if (*format == '-') {
- left_padded = 1;
- format++;
- }
- width = strtoul(format, (char **)&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(' ');
- }
+ extra = strtol(format, (char **)&format, 10);
}
+ } else {
+ extra = LONG_MAX;
+ }
+ if (pos >= argc)
+ m4errx(1, "Format with too many format specifiers.");
+ switch(*format) {
+ case 's':
+ thisarg = argv[pos++];
+ break;
+ case 'c':
+ temp[0] = strtoul(argv[pos++], NULL, 10);
+ temp[1] = 0;
+ thisarg = temp;
+ break;
+ default:
+ m4errx(1, "Unsupported format specification: %s.",
+ argv[2]);
+ }
+ format++;
+ l = strlen(thisarg);
+ if (l > extra)
+ l = extra;
+ if (!left_padded) {
+ while (l < width--)
+ addchar(' ');
+ }
+ addchars(thisarg, l);
+ if (left_padded) {
+ while (l < width--)
+ addchar(' ');
}
}
pbstr(getstring());