summaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2007-10-25 08:27:28 -0600
committerEric Blake <ebb9@byu.net>2007-12-10 06:27:15 -0700
commitd28166a2233b32f0f37bdd486a590a814209b765 (patch)
treefdc4af4c032ada22b0144592f637cd7d005eb1a8 /src/input.c
parentbd9900d65eb9cd5add0f107e94b513fa267495ba (diff)
downloadm4-d28166a2233b32f0f37bdd486a590a814209b765.tar.gz
Stage 6: convert builtins to push arg at a time.
* src/m4.h (includes): Include <limits.h> here, instead of in individual files. (input_block): New typedef. (trace_pre, trace_post, push_string_finish): Update prototypes. (obstack_print, input_print, push_arg, push_args): New prototypes. * src/input.c (push_string_finish): Change return type. (input_print): New function. * src/debug.c (trace_format): Add %B specifier, and use new function. (trace_pre): Remove redundant argc parameter. (trace_post): Likewise, and change signature. (obstack_print): New function. * src/macro.c (expand_macro): Update caller. (push_arg, push_args): New functions. * src/builtin.c (m4_ifdef, m4_ifelse, m4_shift, m4_substr) (m4_patsubst, expand_user_macro): Use new functions. (mkstemp_helper, m4_maketemp): Avoid extra trailing NULs. * src/m4.c (max_debug_argument_length, main): Set to INT_MAX, not 0, for unlimited. * src/output.c: Update includes. * src/symtab.c: Likewise. (cherry picked from commit 6dcf7d2e3c5deac2d16ee9a29b6a307474603dc7) Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c80
1 files changed, 57 insertions, 23 deletions
diff --git a/src/input.c b/src/input.c
index 551b43d6..4e5d2990 100644
--- a/src/input.c
+++ b/src/input.c
@@ -77,7 +77,7 @@ typedef enum input_type input_type;
/* A block of input to be scanned. */
struct input_block
{
- struct input_block *prev; /* Previous input_block on the input stack. */
+ input_block *prev; /* Previous input_block on the input stack. */
input_type type; /* See enum values. */
const char *file; /* File where this input is from. */
int line; /* Line where this input is from. */
@@ -101,8 +101,6 @@ struct input_block
u;
};
-typedef struct input_block input_block;
-
/* Current input file name. */
const char *current_file;
@@ -208,8 +206,7 @@ push_file (FILE *fp, const char *title, bool close)
if (debug_level & DEBUG_TRACE_INPUT)
DEBUG_MESSAGE1 ("input read from %s", title);
- i = (input_block *) obstack_alloc (current_input,
- sizeof (struct input_block));
+ i = (input_block *) obstack_alloc (current_input, sizeof *i);
i->type = INPUT_FILE;
i->file = (char *) obstack_copy0 (&file_names, title, strlen (title));
i->line = 1;
@@ -242,8 +239,7 @@ push_macro (builtin_func *func)
next = NULL;
}
- i = (input_block *) obstack_alloc (current_input,
- sizeof (struct input_block));
+ i = (input_block *) obstack_alloc (current_input, sizeof *i);
i->type = INPUT_MACRO;
i->file = current_file;
i->line = current_line;
@@ -267,8 +263,7 @@ push_string_init (void)
while (isp && pop_input (false));
/* Reserve the next location on the obstack. */
- next = (input_block *) obstack_alloc (current_input,
- sizeof (struct input_block));
+ next = (input_block *) obstack_alloc (current_input, sizeof *next);
next->type = INPUT_STRING;
next->file = current_file;
next->line = current_line;
@@ -281,30 +276,35 @@ push_string_init (void)
| push_file () or push_macro () has invalidated the previous call to |
| push_string_init (), so we just give up. If the new object is |
| void, we do not push it. The function push_string_finish () |
-| returns a pointer to the finished object. This pointer is only |
-| for temporary use, since reading the next token might release the |
-| memory used for the object. |
+| returns an opaque pointer to the finished object, which can then |
+| be printed with input_print when tracing is enabled. This pointer |
+| is only for temporary use, since reading the next token will |
+| invalidate the object. |
`-------------------------------------------------------------------*/
-const char *
+const input_block *
push_string_finish (void)
{
- const char *ret = NULL;
+ input_block *ret = NULL;
+ size_t len = obstack_object_size (current_input);
if (next == NULL)
- return NULL;
+ {
+ assert (!len);
+ return NULL;
+ }
- if (obstack_object_size (current_input) > 0)
+ if (len)
{
obstack_1grow (current_input, '\0');
next->u.u_s.string = (char *) obstack_finish (current_input);
next->prev = isp;
isp = next;
- ret = isp->u.u_s.string; /* for immediate use only */
input_change = true;
+ ret = isp;
}
else
- obstack_free (current_input, next); /* people might leave garbage on it. */
+ obstack_free (current_input, next);
next = NULL;
return ret;
}
@@ -322,8 +322,7 @@ void
push_wrapup (const char *s)
{
input_block *i;
- i = (input_block *) obstack_alloc (wrapup_stack,
- sizeof (struct input_block));
+ i = (input_block *) obstack_alloc (wrapup_stack, sizeof *i);
i->prev = wsp;
i->type = INPUT_STRING;
i->file = current_file;
@@ -421,7 +420,7 @@ pop_wrapup (void)
}
current_input = wrapup_stack;
- wrapup_stack = (struct obstack *) xmalloc (sizeof (struct obstack));
+ wrapup_stack = (struct obstack *) xmalloc (sizeof *wrapup_stack);
obstack_init (wrapup_stack);
isp = wsp;
@@ -443,6 +442,41 @@ init_macro_token (token_data *td)
TOKEN_DATA_TYPE (td) = TOKEN_FUNC;
TOKEN_DATA_FUNC (td) = isp->u.func;
}
+
+/*--------------------------------------------------------------.
+| Dump a representation of INPUT to the obstack OBS, for use in |
+| tracing. |
+`--------------------------------------------------------------*/
+void
+input_print (struct obstack *obs, const input_block *input)
+{
+ int maxlen = max_debug_argument_length;
+
+ assert (input);
+ switch (input->type)
+ {
+ case INPUT_STRING:
+ obstack_print (obs, input->u.u_s.string, SIZE_MAX, &maxlen);
+ break;
+ case INPUT_FILE:
+ obstack_grow (obs, "<file: ", strlen ("<file: "));
+ obstack_grow (obs, input->file, strlen (input->file));
+ obstack_1grow (obs, '>');
+ break;
+ case INPUT_MACRO:
+ {
+ const builtin *bp = find_builtin_by_addr (input->u.func);
+ assert (bp);
+ obstack_1grow (obs, '<');
+ obstack_grow (obs, bp->name, strlen (bp->name));
+ obstack_1grow (obs, '>');
+ }
+ break;
+ default:
+ assert (!"input_print");
+ abort ();
+ }
+}
/*-----------------------------------------------------------------.
@@ -680,9 +714,9 @@ input_init (void)
current_file = "";
current_line = 0;
- current_input = (struct obstack *) xmalloc (sizeof (struct obstack));
+ current_input = (struct obstack *) xmalloc (sizeof *current_input);
obstack_init (current_input);
- wrapup_stack = (struct obstack *) xmalloc (sizeof (struct obstack));
+ wrapup_stack = (struct obstack *) xmalloc (sizeof *wrapup_stack);
obstack_init (wrapup_stack);
obstack_init (&file_names);