1 /* $OpenBSD: trace.c,v 1.6 2002/04/26 16:15:16 espie Exp $ */
3 * Copyright (c) 2001 Marc Espie.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
18 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/usr.bin/m4/trace.c,v 1.7 2002/07/15 02:15:12 jmallett Exp $
27 * $DragonFly: src/usr.bin/m4/trace.c,v 1.3 2006/12/27 21:29:02 pavalos Exp $
30 #include <sys/types.h>
42 int traced_macros
= 0;
45 #define TRACE_EXPANSION 2
47 #define TRACE_FILENAME 8
48 #define TRACE_LINENO 16
51 #define TRACE_NEWFILE 128 /* not implemented yet */
52 #define TRACE_INPUT 256 /* not implemented yet */
61 static unsigned int letter_to_flag(int);
62 static void print_header(struct input_file
*);
63 static struct t
*find_trace_entry(const char *);
64 static int frame_level(void);
66 static unsigned int flags
= TRACE_QUOTE
| TRACE_EXPANSION
;
69 find_trace_entry(const char *name
)
73 for (n
= l
; n
!= NULL
; n
= n
->next
)
74 if (STREQ(n
->name
, name
))
81 mark_traced(const char *name
, int on
)
94 for (n
= l
; n
!= NULL
; n
= n2
) {
101 n
= find_trace_entry(name
);
103 n
= xalloc(sizeof(struct t
));
104 n
->name
= xstrdup(name
);
113 is_traced(const char *name
)
117 for (n
= l
; n
!= NULL
; n
= n
->next
)
118 if (STREQ(n
->name
, name
))
120 return (flags
& TRACE_ALL
) ? 1 : 0;
124 trace_file(const char *name
)
127 if (traceout
!= stderr
)
129 traceout
= fopen(name
, "w");
131 err(1, "can't open %s", name
);
135 letter_to_flag(int c
)
141 return TRACE_EXPANSION
;
149 return TRACE_FILENAME
;
153 return TRACE_NEWFILE
;
166 set_trace_flags(const char *s
)
173 if (*s
== '+' || *s
== '-')
176 f
|= letter_to_flag(*s
++);
196 for (framep
= fp
, level
= 0; framep
!= 0;
197 level
++,framep
= mstack
[framep
-2].sfra
)
203 print_header(struct input_file
*inp
)
205 fprintf(traceout
, "m4trace:");
206 if (flags
& TRACE_FILENAME
)
207 fprintf(traceout
, "%s:", inp
->name
);
208 if (flags
& TRACE_LINENO
)
209 fprintf(traceout
, "%lu:", inp
->lineno
);
210 fprintf(traceout
, " -%d- ", frame_level());
211 if (flags
& TRACE_ID
)
212 fprintf(traceout
, "id %lu: ", expansion_id
);
216 trace(const char *argv
[], int argc
, struct input_file
*inp
)
219 if (flags
& TRACE_CONT
) {
220 fprintf(traceout
, "%s ...\n", argv
[1]);
223 fprintf(traceout
, "%s", argv
[1]);
224 if ((flags
& TRACE_ARGS
) && argc
> 2) {
230 for (i
= 2; i
< argc
; i
++) {
231 fprintf(traceout
, "%s%s%s%s", delim
,
232 (flags
& TRACE_QUOTE
) ? lquote
: "",
234 (flags
& TRACE_QUOTE
) ? rquote
: "");
239 fprintf(traceout
, "%c", RPAREN
);
241 if (flags
& TRACE_CONT
) {
242 fprintf(traceout
, " -> ???\n");
244 fprintf(traceout
, argc
> 2 ? "%s(...)" : "%s", argv
[1]);
246 if (flags
& TRACE_EXPANSION
)
247 return buffer_mark();
249 fprintf(traceout
, "\n");
255 finish_trace(size_t mark
)
257 fprintf(traceout
, " -> ");
258 if (flags
& TRACE_QUOTE
)
259 fprintf(traceout
, "%s", lquote
);
260 dump_buffer(traceout
, mark
);
261 if (flags
& TRACE_QUOTE
)
262 fprintf(traceout
, "%s", rquote
);
263 fprintf(traceout
, "\n");