Re: a type fix, obstack_print
Eric Blake <[email protected]>
| Newsgroups | gmane.comp.gnu.m4.patches |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 According to Eric Blake on 2/23/2008 9:00 AM: | I'll go one step further, and make the branch consistently use size_t for | --arglength and truncation. | | But on the head, I named the comparable M4 function | m4_shipout_string_trunc rather than obstack_print, so maybe I'll use a | name along those lines. I'll get something committed later today. Like so. - -- Don't work too hard, make some time for fun as well! Eric Blake [email protected] -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (Cygwin) Comment: Public key at home.comcast.net/~ericblake/eblake.gpg Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHwFGP84KuGfSFAYARAgwlAJ9MyuavGFPCm+73aS4gNWjzS96XdACgmTDu dbIQY4S77Z8G4WT8D1x3hx8= =YRra -----END PGP SIGNATURE----- _______________________________________________ M4-patches mailing list [email protected] http://lists.gnu.org/mailman/listinfo/m4-patches
m4.patch324
(text/plain, 16.4 KB)
From 0b1129f73235ba35fa46f1afcef5bd5c0cdbf73c Mon Sep 17 00:00:00 2001 From: Eric Blake <[email protected]> Date: Sat, 23 Feb 2008 09:30:43 -0700 Subject: [PATCH] s/shipout_text/divert_text/. * src/m4.h (shipout_text): Rename... (divert_text): ...to this, so that shipout_ prefix can be used for functions that do not do #line tracking. * src/output.c (divert_text): Rename. * src/macro.c (expand_token): Adjust all callers. Signed-off-by: Eric Blake <[email protected]> --- ChangeLog | 7 +++++++ src/m4.h | 2 +- src/macro.c | 8 ++++---- src/output.c | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1269290..e902f67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2008-02-23 Eric Blake <[email protected]> + s/shipout_text/divert_text/. + * src/m4.h (shipout_text): Rename... + (divert_text): ...to this, so that shipout_ prefix can be used for + functions that do not do #line tracking. + * src/output.c (divert_text): Rename. + * src/macro.c (expand_token): Adjust all callers. + Stage 18: try harder to reuse argv in recursion. When pushing arguments that contain an existing $@ ref, reuse the ref rather than creating another layer of wrappers. diff --git a/src/m4.h b/src/m4.h index 54deb42..037482d 100644 --- a/src/m4.h +++ b/src/m4.h @@ -424,7 +424,7 @@ extern int output_current_line; void output_init (void); void output_exit (void); void output_text (const char *, int); -void shipout_text (struct obstack *, const char *, int, int); +void divert_text (struct obstack *, const char *, int, int); void make_diversion (int); void insert_diversion (int); void insert_file (FILE *); diff --git a/src/macro.c b/src/macro.c index 32ff62d..b81697a 100644 --- a/src/macro.c +++ b/src/macro.c @@ -305,10 +305,10 @@ expand_token (struct obstack *obs, token_type t, token_data *td, int line, && peek_token () != TOKEN_OPEN)) { #ifdef ENABLE_CHANGEWORD - shipout_text (obs, TOKEN_DATA_ORIG_TEXT (td), - TOKEN_DATA_LEN (td), line); + divert_text (obs, TOKEN_DATA_ORIG_TEXT (td), + TOKEN_DATA_LEN (td), line); #else - shipout_text (obs, TOKEN_DATA_TEXT (td), TOKEN_DATA_LEN (td), line); + divert_text (obs, TOKEN_DATA_TEXT (td), TOKEN_DATA_LEN (td), line); #endif /* !ENABLE_CHANGEWORD */ /* The word just appended is unquoted, but the heuristics of safe_quote are applicable. */ @@ -324,7 +324,7 @@ expand_token (struct obstack *obs, token_type t, token_data *td, int line, assert (!"expand_token"); abort (); } - shipout_text (obs, TOKEN_DATA_TEXT (td), TOKEN_DATA_LEN (td), line); + divert_text (obs, TOKEN_DATA_TEXT (td), TOKEN_DATA_LEN (td), line); return result; } diff --git a/src/output.c b/src/output.c index d252d74..73a0e47 100644 --- a/src/output.c +++ b/src/output.c @@ -469,7 +469,7 @@ output_text (const char *text, int length) `--------------------------------------------------------------------*/ void -shipout_text (struct obstack *obs, const char *text, int length, int line) +divert_text (struct obstack *obs, const char *text, int length, int line) { static bool start_of_output_line = true; const char *cursor; -- 1.5.4 From c91769acf89e163c06e86be0f17022572f33bbab Mon Sep 17 00:00:00 2001 From: Eric Blake <[email protected]> Date: Sat, 23 Feb 2008 09:42:02 -0700 Subject: [PATCH] s/obstack_print/shipout_string_trunc/. * src/m4.h (obstack_print): Rename... (shipout_string_trunc): ...to this, to leave obstack_ prefix for actual obstack API. * src/debug.c (trace_format): Adjust caller. (obstack_print): Move... * src/output.c (shipout_string_trunc): ...here, including rename. * src/input.c (input_print): Adjust caller. * src/macro.c (arg_print): Likewise. Reported by Ralf Wildenhues. Signed-off-by: Eric Blake <[email protected]> --- ChangeLog | 11 +++++++++++ src/debug.c | 30 +----------------------------- src/input.c | 6 +++--- src/m4.h | 2 +- src/macro.c | 23 ++++++++++++++--------- src/output.c | 29 +++++++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index e902f67..2211052 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2008-02-23 Eric Blake <[email protected]> + s/obstack_print/shipout_string_trunc/. + * src/m4.h (obstack_print): Rename... + (shipout_string_trunc): ...to this, to leave obstack_ prefix for + actual obstack API. + * src/debug.c (trace_format): Adjust caller. + (obstack_print): Move... + * src/output.c (shipout_string_trunc): ...here, including rename. + * src/input.c (input_print): Adjust caller. + * src/macro.c (arg_print): Likewise. + Reported by Ralf Wildenhues. + s/shipout_text/divert_text/. * src/m4.h (shipout_text): Rename... (divert_text): ...to this, so that shipout_ prefix can be used for diff --git a/src/debug.c b/src/debug.c index 737ee52..1c0c99b 100644 --- a/src/debug.c +++ b/src/debug.c @@ -294,7 +294,7 @@ trace_format (const char *fmt, ...) break; } - if (obstack_print (&trace, s, SIZE_MAX, &maxlen)) + if (shipout_string_trunc (&trace, s, SIZE_MAX, &maxlen)) break; } @@ -401,31 +401,3 @@ trace_post (const char *name, int id, macro_arguments *argv, trace_format (" -> %l%B%r", expanded); trace_flush (); } - -/* Dump the string STR of length LEN to the obstack OBS. If LEN is - SIZE_MAX, use strlen (STR) instead. If MAX_LEN is non-NULL, - truncate the dump at MAX_LEN bytes and return true if MAX_LEN was - reached; otherwise, return false and update MAX_LEN as - appropriate. */ -bool -obstack_print (struct obstack *obs, const char *str, size_t len, int *max_len) -{ - int max = max_len ? *max_len : INT_MAX; - - if (len == SIZE_MAX) - len = strlen (str); - if (len < max) - { - obstack_grow (obs, str, len); - max -= len; - } - else - { - obstack_grow (obs, str, max); - obstack_grow (obs, "...", 3); - max = 0; - } - if (max_len) - *max_len = max; - return max == 0; -} diff --git a/src/input.c b/src/input.c index e2bf0aa..e2d332a 100644 --- a/src/input.c +++ b/src/input.c @@ -687,7 +687,7 @@ input_print (struct obstack *obs, const input_block *input) switch (input->type) { case INPUT_STRING: - obstack_print (obs, input->u.u_s.str, input->u.u_s.len, &maxlen); + shipout_string_trunc (obs, input->u.u_s.str, input->u.u_s.len, &maxlen); break; case INPUT_FILE: obstack_grow (obs, "<file: ", strlen ("<file: ")); @@ -704,8 +704,8 @@ input_print (struct obstack *obs, const input_block *input) switch (chain->type) { case CHAIN_STR: - if (obstack_print (obs, chain->u.u_s.str, chain->u.u_s.len, - &maxlen)) + if (shipout_string_trunc (obs, chain->u.u_s.str, + chain->u.u_s.len, &maxlen)) return; break; case CHAIN_ARGV: diff --git a/src/m4.h b/src/m4.h index 037482d..7694ba0 100644 --- a/src/m4.h +++ b/src/m4.h @@ -249,7 +249,6 @@ void trace_pre (const char *, int, macro_arguments *); void trace_post (const char *, int, macro_arguments *, const input_block *); -bool obstack_print (struct obstack *, const char *, size_t, int *); /* File: input.c --- lexical definitions. */ @@ -425,6 +424,7 @@ void output_init (void); void output_exit (void); void output_text (const char *, int); void divert_text (struct obstack *, const char *, int, int); +bool shipout_string_trunc (struct obstack *, const char *, size_t, int *); void make_diversion (int); void insert_diversion (int); void insert_file (FILE *); diff --git a/src/macro.c b/src/macro.c index b81697a..366584a 100644 --- a/src/macro.c +++ b/src/macro.c @@ -1212,23 +1212,27 @@ arg_print (struct obstack *obs, macro_arguments *argv, unsigned int index, { if (quote_each && max_len) len = *max_len; - if (use_sep && obstack_print (obs, sep, sep_len, plen)) + if (use_sep && shipout_string_trunc (obs, sep, sep_len, plen)) return true; use_sep = true; token = arg_token (argv, i, NULL); switch (TOKEN_DATA_TYPE (token)) { case TOKEN_TEXT: - if (quotes && obstack_print (obs, quotes->str1, quotes->len1, plen)) + if (quotes && shipout_string_trunc (obs, quotes->str1, quotes->len1, + plen)) return true; - if (obstack_print (obs, TOKEN_DATA_TEXT (token), - TOKEN_DATA_LEN (token), &len) && !quote_each) + if (shipout_string_trunc (obs, TOKEN_DATA_TEXT (token), + TOKEN_DATA_LEN (token), &len) + && !quote_each) return true; - if (quotes && obstack_print (obs, quotes->str2, quotes->len2, plen)) + if (quotes && shipout_string_trunc (obs, quotes->str2, quotes->len2, + plen)) return true; break; case TOKEN_COMP: - if (quotes && obstack_print (obs, quotes->str1, quotes->len1, plen)) + if (quotes && shipout_string_trunc (obs, quotes->str1, quotes->len1, + plen)) return true; chain = token->u.u_c.chain; done = false; @@ -1237,8 +1241,8 @@ arg_print (struct obstack *obs, macro_arguments *argv, unsigned int index, switch (chain->type) { case CHAIN_STR: - if (obstack_print (obs, chain->u.u_s.str, chain->u.u_s.len, - &len)) + if (shipout_string_trunc (obs, chain->u.u_s.str, + chain->u.u_s.len, &len)) done = true; break; case CHAIN_ARGV: @@ -1256,7 +1260,8 @@ arg_print (struct obstack *obs, macro_arguments *argv, unsigned int index, } if (done && !quote_each) return true; - if (quotes && obstack_print (obs, quotes->str2, quotes->len2, plen)) + if (quotes && shipout_string_trunc (obs, quotes->str2, quotes->len2, + plen)) return true; break; case TOKEN_FUNC: diff --git a/src/output.c b/src/output.c index 73a0e47..56ac1f4 100644 --- a/src/output.c +++ b/src/output.c @@ -571,6 +571,35 @@ divert_text (struct obstack *obs, const char *text, int length, int line) } } } + +/* Dump the string STR of length LEN to the obstack OBS. If LEN is + SIZE_MAX, use strlen (STR) instead. If MAX_LEN is non-NULL, + truncate the dump at MAX_LEN bytes and return true if MAX_LEN was + reached; otherwise, return false and update MAX_LEN as + appropriate. */ +bool +shipout_string_trunc (struct obstack *obs, const char *str, size_t len, + int *max_len) +{ + int max = max_len ? *max_len : INT_MAX; + + if (len == SIZE_MAX) + len = strlen (str); + if (len < max) + { + obstack_grow (obs, str, len); + max -= len; + } + else + { + obstack_grow (obs, str, max); + obstack_grow (obs, "...", 3); + max = 0; + } + if (max_len) + *max_len = max; + return max == 0; +} /* Functions for use by diversions. */ -- 1.5.4 From 9d400bc11d96c313d041e73b8f7b9a2d069d1ae5 Mon Sep 17 00:00:00 2001 From: Eric Blake <[email protected]> Date: Sat, 23 Feb 2008 09:58:03 -0700 Subject: [PATCH] Use size_t consistently for string length truncation. * src/m4.h (max_debug_argument_length): Change type to size_t. (shipout_string_trunc, arg_print): Change parameter type. * src/debug.c (trace_format, trace_pre): Adjust all callers. * src/input.c (input_print): Likewise. * src/m4.c (max_debug_argument_length, main): Likewise. * src/macro.c (arg_print): Likewise. * src/output.c (shipout_string_trunc): Likewise. Reported by Ralf Wildenhues. Signed-off-by: Eric Blake <[email protected]> --- ChangeLog | 10 ++++++++++ src/debug.c | 6 +++--- src/input.c | 2 +- src/m4.c | 9 +++++---- src/m4.h | 6 +++--- src/macro.c | 4 ++-- src/output.c | 4 ++-- 7 files changed, 26 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2211052..f867f1f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2008-02-23 Eric Blake <[email protected]> + Use size_t consistently for string length truncation. + * src/m4.h (max_debug_argument_length): Change type to size_t. + (shipout_string_trunc, arg_print): Change parameter type. + * src/debug.c (trace_format, trace_pre): Adjust all callers. + * src/input.c (input_print): Likewise. + * src/m4.c (max_debug_argument_length, main): Likewise. + * src/macro.c (arg_print): Likewise. + * src/output.c (shipout_string_trunc): Likewise. + Reported by Ralf Wildenhues. + s/obstack_print/shipout_string_trunc/. * src/m4.h (obstack_print): Rename... (shipout_string_trunc): ...to this, to leave obstack_ prefix for diff --git a/src/debug.c b/src/debug.c index 1c0c99b..46e1306 100644 --- a/src/debug.c +++ b/src/debug.c @@ -248,7 +248,7 @@ trace_format (const char *fmt, ...) char ch; int d; const char *s; - int maxlen; + size_t maxlen; va_start (args, fmt); @@ -260,7 +260,7 @@ trace_format (const char *fmt, ...) if (ch == '\0') break; - maxlen = INT_MAX; + maxlen = SIZE_MAX; switch (*fmt++) { case 'B': @@ -364,7 +364,7 @@ trace_pre (const char *name, int id, macro_arguments *argv) if (arg_argc (argv) > 1 && (debug_level & DEBUG_TRACE_ARGS)) { - int len = max_debug_argument_length; + size_t len = max_debug_argument_length; trace_format ("("); arg_print (&trace, argv, 1, (debug_level & DEBUG_TRACE_QUOTE) ? &curr_quote : NULL, diff --git a/src/input.c b/src/input.c index e2d332a..9d8b8f3 100644 --- a/src/input.c +++ b/src/input.c @@ -680,7 +680,7 @@ pop_wrapup (void) void input_print (struct obstack *obs, const input_block *input) { - int maxlen = max_debug_argument_length; + size_t maxlen = max_debug_argument_length; token_chain *chain; assert (input); diff --git a/src/m4.c b/src/m4.c index 0ace6dc..fe8c548 100644 --- a/src/m4.c +++ b/src/m4.c @@ -47,7 +47,7 @@ int no_gnu_extensions = 0; int prefix_all_builtins = 0; /* Max length of arguments in trace output (-lsize). */ -int max_debug_argument_length = INT_MAX; +size_t max_debug_argument_length = SIZE_MAX; /* Suppress warnings about missing arguments. */ int suppress_warnings = 0; @@ -572,9 +572,10 @@ main (int argc, char *const *argv, char *const *envp) break; case 'l': - max_debug_argument_length = atoi (optarg); - if (max_debug_argument_length <= 0) - max_debug_argument_length = INT_MAX; + { + int tmp = atoi (optarg); + max_debug_argument_length = tmp <= 0 ? SIZE_MAX : (size_t) tmp; + } break; case 'o': diff --git a/src/m4.h b/src/m4.h index 7694ba0..7e35acc 100644 --- a/src/m4.h +++ b/src/m4.h @@ -129,7 +129,7 @@ extern int debug_level; /* -d */ extern size_t hash_table_size; /* -H */ extern int no_gnu_extensions; /* -G */ extern int prefix_all_builtins; /* -P */ -extern int max_debug_argument_length; /* -l */ +extern size_t max_debug_argument_length;/* -l */ extern int suppress_warnings; /* -Q */ extern int warning_status; /* -E */ extern int nesting_limit; /* -L */ @@ -424,7 +424,7 @@ void output_init (void); void output_exit (void); void output_text (const char *, int); void divert_text (struct obstack *, const char *, int, int); -bool shipout_string_trunc (struct obstack *, const char *, size_t, int *); +bool shipout_string_trunc (struct obstack *, const char *, size_t, size_t *); void make_diversion (int); void insert_diversion (int); void insert_file (FILE *); @@ -500,7 +500,7 @@ size_t arg_len (macro_arguments *, unsigned int); builtin_func *arg_func (macro_arguments *, unsigned int); struct obstack *arg_scratch (void); bool arg_print (struct obstack *, macro_arguments *, unsigned int, - const string_pair *, bool, const char *, int *, bool); + const string_pair *, bool, const char *, size_t *, bool); macro_arguments *make_argv_ref (macro_arguments *, const char *, size_t, bool, bool); void push_arg (struct obstack *, macro_arguments *, unsigned int); diff --git a/src/macro.c b/src/macro.c index 366584a..0846f8d 100644 --- a/src/macro.c +++ b/src/macro.c @@ -1194,9 +1194,9 @@ arg_scratch (void) bool arg_print (struct obstack *obs, macro_arguments *argv, unsigned int index, const string_pair *quotes, bool flatten, const char *sep, - int *max_len, bool quote_each) + size_t *max_len, bool quote_each) { - int len = max_len ? *max_len : INT_MAX; + size_t len = max_len ? *max_len : INT_MAX; unsigned int i; token_data *token; token_chain *chain; diff --git a/src/output.c b/src/output.c index 56ac1f4..7b2d6de 100644 --- a/src/output.c +++ b/src/output.c @@ -579,9 +579,9 @@ divert_text (struct obstack *obs, const char *text, int length, int line) appropriate. */ bool shipout_string_trunc (struct obstack *obs, const char *str, size_t len, - int *max_len) + size_t *max_len) { - int max = max_len ? *max_len : INT_MAX; + size_t max = max_len ? *max_len : INT_MAX; if (len == SIZE_MAX) len = strlen (str); -- 1.5.4