1 /* $NetBSD: trace.c,v 1.8 2012/03/20 20:34:58 matt Exp $ */
2 /* $OpenBSD: trace.c,v 1.15 2006/03/24 08:03:44 espie Exp $ */
4 * Copyright (c) 2001 Marc Espie.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
19 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: trace.c,v 1.8 2012/03/20 20:34:58 matt Exp $");
33 #include <sys/types.h>
46 #define TRACE_EXPANSION 2
48 #define TRACE_FILENAME 8
49 #define TRACE_LINENO 16
52 #define TRACE_NEWFILE 128 /* not implemented yet */
53 #define TRACE_INPUT 256 /* not implemented yet */
55 static unsigned int letter_to_flag(int);
56 static void print_header(struct input_file
*);
57 static int frame_level(void);
60 unsigned int trace_flags
= TRACE_QUOTE
| TRACE_EXPANSION
;
63 trace_file(const char *name
)
66 if (traceout
&& traceout
!= stderr
)
68 traceout
= fopen(name
, "w");
70 err(1, "can't open %s", name
);
80 return TRACE_EXPANSION
;
88 return TRACE_FILENAME
;
105 set_trace_flags(const char *s
)
110 if (*s
== '+' || *s
== '-')
113 f
|= letter_to_flag(*s
++);
133 for (framep
= fp
, level
= 0; framep
!= 0;
134 level
++,framep
= mstack
[framep
-3].sfra
)
140 print_header(struct input_file
*inp
)
142 fprintf(traceout
, "m4trace:");
143 if (trace_flags
& TRACE_FILENAME
)
144 fprintf(traceout
, "%s:", inp
->name
);
145 if (trace_flags
& TRACE_LINENO
)
146 fprintf(traceout
, "%lu:", TOKEN_LINE(inp
));
147 fprintf(traceout
, " -%d- ", frame_level());
148 if (trace_flags
& TRACE_ID
)
149 fprintf(traceout
, "id %lu: ", expansion_id
);
153 trace(const char *argv
[], int argc
, struct input_file
*inp
)
158 if (trace_flags
& TRACE_CONT
) {
159 fprintf(traceout
, "%s ...\n", argv
[1]);
162 fprintf(traceout
, "%s", argv
[1]);
163 if ((trace_flags
& TRACE_ARGS
) && argc
> 2) {
169 for (i
= 2; i
< argc
; i
++) {
170 fprintf(traceout
, "%s%s%s%s", delim
,
171 (trace_flags
& TRACE_QUOTE
) ? lquote
: "",
173 (trace_flags
& TRACE_QUOTE
) ? rquote
: "");
178 fprintf(traceout
, "%c", RPAREN
);
180 if (trace_flags
& TRACE_CONT
) {
181 fprintf(traceout
, " -> ???\n");
183 fprintf(traceout
, argc
> 2 ? "%s(...)" : "%s", argv
[1]);
185 if (trace_flags
& TRACE_EXPANSION
)
186 return buffer_mark();
188 fprintf(traceout
, "\n");
194 finish_trace(size_t mark
)
196 fprintf(traceout
, " -> ");
197 if (trace_flags
& TRACE_QUOTE
)
198 fprintf(traceout
, "%s", lquote
);
199 dump_buffer(traceout
, mark
);
200 if (trace_flags
& TRACE_QUOTE
)
201 fprintf(traceout
, "%s", rquote
);
202 fprintf(traceout
, "\n");