summaryrefslogtreecommitdiff
path: root/modules/format.c
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2008-06-14 11:02:12 -0600
committerEric Blake <ebb9@byu.net>2008-06-16 07:19:00 -0600
commitc05ce945d2a377eb37365eada8f0dc402479a94e (patch)
tree910fb1ffe5de1c8404115dfe1f91eb004a7aaa25 /modules/format.c
parentb4c7d8547c2072cf84237d2abd3474c17b009081 (diff)
downloadm4-c05ce945d2a377eb37365eada8f0dc402479a94e.tar.gz
Stage 25a: Use obstack_printf for easier output.
* ltdl/m4/gnulib-cache.m4: Import obstack-printf-posix module. * m4/macro.c (trace_format): Delete; use obstack_printf instead. (trace_header, trace_pre, trace_post): All callers updated. * m4/output.c (m4_shipout_int, m4_tmpname): Use obstack_printf. (m4_divert_text): Speed up syncline output. * modules/m4.c (dumpdef): Handle embedded NUL. (numb_obstack): Speed up eval output. (maketemp): Use obstack_printf. * modules/format.c (format): Likewise. Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'modules/format.c')
-rw-r--r--modules/format.c49
1 files changed, 15 insertions, 34 deletions
diff --git a/modules/format.c b/modules/format.c
index f5695e45..e2a1a423 100644
--- a/modules/format.c
+++ b/modules/format.c
@@ -152,10 +152,8 @@ format (m4 *context, m4_obstack *obs, int argc, m4_macro_args *argv)
behavior in printf. */
char ok[128];
- /* Buffer and stuff. */
- char *base; /* Current position in obs. */
- size_t len; /* Length of formatted text. */
- char *str; /* Malloc'd buffer of formatted text. */
+ /* Check that formatted text succeeded with correct type. */
+ int result = 0;
enum {CHAR, INT, LONG, DOUBLE, STR} datatype;
f = fmt = ARG_STR (i, argc, argv);
@@ -349,56 +347,39 @@ format (m4 *context, m4_obstack *obs, int argc, m4_macro_args *argv)
}
*p++ = c;
*p = '\0';
- base = obstack_next_free (obs);
- len = obstack_room (obs);
switch (datatype)
{
case CHAR:
- str = asnprintf (base, &len, fstart, width,
- ARG_INT (i, argc, argv));
+ result = obstack_printf (obs, fstart, width,
+ ARG_INT (i, argc, argv));
break;
case INT:
- str = asnprintf (base, &len, fstart, width, prec,
- ARG_INT (i, argc, argv));
+ result = obstack_printf (obs, fstart, width, prec,
+ ARG_INT (i, argc, argv));
break;
case LONG:
- str = asnprintf (base, &len, fstart, width, prec,
- ARG_LONG (i, argc, argv));
+ result = obstack_printf (obs, fstart, width, prec,
+ ARG_LONG (i, argc, argv));
break;
case DOUBLE:
- str = asnprintf (base, &len, fstart, width, prec,
- ARG_DOUBLE (i, argc, argv));
+ result = obstack_printf (obs, fstart, width, prec,
+ ARG_DOUBLE (i, argc, argv));
break;
case STR:
- str = asnprintf (base, &len, fstart, width, prec,
- ARG_STR (i, argc, argv));
+ result = obstack_printf (obs, fstart, width, prec,
+ ARG_STR (i, argc, argv));
break;
default:
abort ();
}
-
- if (str == NULL)
- /* NULL is unexpected (EILSEQ and EINVAL are not possible
- based on our construction of fstart, leaving only ENOMEM,
- which should always be fatal). */
- m4_error (context, EXIT_FAILURE, errno, me,
- _("unable to format output for `%s'"), f);
- else if (str == base)
- /* The output was already computed in place, but we need to
- account for its size. */
- obstack_blank_fast (obs, len);
- else
- {
- /* The output exceeded available obstack space, copy the
- allocated string. */
- obstack_grow (obs, str, len);
- free (str);
- }
+ /* Since obstack_printf can only fail with EILSEQ or EINVAL, but
+ we constructed fstart, the result should not be negative. */
+ assert (0 <= result);
}
}