1 /* $OpenBSD: trace.c,v 1.16 2010/09/07 19:58:09 marco 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.9 2012/11/17 01:54:24 svnexp Exp $
41 #define TRACE_EXPANSION 2
43 #define TRACE_FILENAME 8
44 #define TRACE_LINENO 16
47 #define TRACE_NEWFILE 128 /* not implemented yet */
48 #define TRACE_INPUT 256 /* not implemented yet */
50 static unsigned int letter_to_flag(int);
51 static void print_header(struct input_file
*);
52 static int frame_level(void);
55 unsigned int trace_flags
= TRACE_QUOTE
| TRACE_EXPANSION
;
58 trace_file(const char *name
)
61 if (traceout
&& traceout
!= stderr
)
63 traceout
= fopen(name
, "w");
65 err(1, "can't open %s", name
);
75 return TRACE_EXPANSION
;
83 return TRACE_FILENAME
;
100 set_trace_flags(const char *s
)
105 if (*s
== '+' || *s
== '-')
108 f
|= letter_to_flag(*s
++);
128 for (framep
= fp
, level
= 0; framep
!= 0;
129 level
++,framep
= mstack
[framep
- 3].sfra
)
135 print_header(struct input_file
*inp
)
137 fprintf(traceout
, "m4trace:");
138 if (trace_flags
& TRACE_FILENAME
)
139 fprintf(traceout
, "%s:", inp
->name
);
140 if (trace_flags
& TRACE_LINENO
)
141 fprintf(traceout
, "%lu:", inp
->lineno
);
142 fprintf(traceout
, " -%d- ", frame_level());
143 if (trace_flags
& TRACE_ID
)
144 fprintf(traceout
, "id %lu: ", expansion_id
);
148 trace(const char *argv
[], int argc
, struct input_file
*inp
)
153 if (trace_flags
& TRACE_CONT
) {
154 fprintf(traceout
, "%s ...\n", argv
[1]);
157 fprintf(traceout
, "%s", argv
[1]);
158 if ((trace_flags
& TRACE_ARGS
) && argc
> 2) {
164 for (i
= 2; i
< argc
; i
++) {
165 fprintf(traceout
, "%s%s%s%s", delim
,
166 (trace_flags
& TRACE_QUOTE
) ? lquote
: "",
168 (trace_flags
& TRACE_QUOTE
) ? rquote
: "");
173 fprintf(traceout
, "%c", RPAREN
);
175 if (trace_flags
& TRACE_CONT
) {
176 fprintf(traceout
, " -> ???\n");
178 fprintf(traceout
, argc
> 2 ? "%s(...)" : "%s", argv
[1]);
180 if (trace_flags
& TRACE_EXPANSION
)
181 return buffer_mark();
183 fprintf(traceout
, "\n");
189 finish_trace(size_t mark
)
191 fprintf(traceout
, " -> ");
192 if (trace_flags
& TRACE_QUOTE
)
193 fprintf(traceout
, "%s", lquote
);
194 dump_buffer(traceout
, mark
);
195 if (trace_flags
& TRACE_QUOTE
)
196 fprintf(traceout
, "%s", rquote
);
197 fprintf(traceout
, "\n");