1*71c7dcb9SLionel Sambuc /* $NetBSD: trace.c,v 1.8 2012/03/20 20:34:58 matt Exp $ */
22e8d1edaSArun Thomas /* $OpenBSD: trace.c,v 1.15 2006/03/24 08:03:44 espie Exp $ */
32e8d1edaSArun Thomas /*
42e8d1edaSArun Thomas * Copyright (c) 2001 Marc Espie.
52e8d1edaSArun Thomas *
62e8d1edaSArun Thomas * Redistribution and use in source and binary forms, with or without
72e8d1edaSArun Thomas * modification, are permitted provided that the following conditions
82e8d1edaSArun Thomas * are met:
92e8d1edaSArun Thomas * 1. Redistributions of source code must retain the above copyright
102e8d1edaSArun Thomas * notice, this list of conditions and the following disclaimer.
112e8d1edaSArun Thomas * 2. Redistributions in binary form must reproduce the above copyright
122e8d1edaSArun Thomas * notice, this list of conditions and the following disclaimer in the
132e8d1edaSArun Thomas * documentation and/or other materials provided with the distribution.
142e8d1edaSArun Thomas *
152e8d1edaSArun Thomas * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
162e8d1edaSArun Thomas * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172e8d1edaSArun Thomas * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
182e8d1edaSArun Thomas * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
192e8d1edaSArun Thomas * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
202e8d1edaSArun Thomas * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
212e8d1edaSArun Thomas * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222e8d1edaSArun Thomas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232e8d1edaSArun Thomas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242e8d1edaSArun Thomas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
252e8d1edaSArun Thomas * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262e8d1edaSArun Thomas */
272e8d1edaSArun Thomas #if HAVE_NBTOOL_CONFIG_H
282e8d1edaSArun Thomas #include "nbtool_config.h"
292e8d1edaSArun Thomas #endif
302e8d1edaSArun Thomas #include <sys/cdefs.h>
31*71c7dcb9SLionel Sambuc __RCSID("$NetBSD: trace.c,v 1.8 2012/03/20 20:34:58 matt Exp $");
322e8d1edaSArun Thomas
332e8d1edaSArun Thomas #include <sys/types.h>
342e8d1edaSArun Thomas #include <err.h>
352e8d1edaSArun Thomas #include <stddef.h>
362e8d1edaSArun Thomas #include <stdint.h>
372e8d1edaSArun Thomas #include <stdio.h>
382e8d1edaSArun Thomas #include <stdlib.h>
392e8d1edaSArun Thomas #include "mdef.h"
402e8d1edaSArun Thomas #include "stdd.h"
412e8d1edaSArun Thomas #include "extern.h"
422e8d1edaSArun Thomas
432e8d1edaSArun Thomas FILE *traceout;
442e8d1edaSArun Thomas
452e8d1edaSArun Thomas #define TRACE_ARGS 1
462e8d1edaSArun Thomas #define TRACE_EXPANSION 2
472e8d1edaSArun Thomas #define TRACE_QUOTE 4
482e8d1edaSArun Thomas #define TRACE_FILENAME 8
492e8d1edaSArun Thomas #define TRACE_LINENO 16
502e8d1edaSArun Thomas #define TRACE_CONT 32
512e8d1edaSArun Thomas #define TRACE_ID 64
522e8d1edaSArun Thomas #define TRACE_NEWFILE 128 /* not implemented yet */
532e8d1edaSArun Thomas #define TRACE_INPUT 256 /* not implemented yet */
542e8d1edaSArun Thomas
552e8d1edaSArun Thomas static unsigned int letter_to_flag(int);
562e8d1edaSArun Thomas static void print_header(struct input_file *);
572e8d1edaSArun Thomas static int frame_level(void);
582e8d1edaSArun Thomas
592e8d1edaSArun Thomas
602e8d1edaSArun Thomas unsigned int trace_flags = TRACE_QUOTE | TRACE_EXPANSION;
612e8d1edaSArun Thomas
622e8d1edaSArun Thomas void
trace_file(const char * name)632e8d1edaSArun Thomas trace_file(const char *name)
642e8d1edaSArun Thomas {
652e8d1edaSArun Thomas
662e8d1edaSArun Thomas if (traceout && traceout != stderr)
672e8d1edaSArun Thomas fclose(traceout);
682e8d1edaSArun Thomas traceout = fopen(name, "w");
692e8d1edaSArun Thomas if (!traceout)
702e8d1edaSArun Thomas err(1, "can't open %s", name);
712e8d1edaSArun Thomas }
722e8d1edaSArun Thomas
732e8d1edaSArun Thomas static unsigned int
letter_to_flag(int c)742e8d1edaSArun Thomas letter_to_flag(int c)
752e8d1edaSArun Thomas {
762e8d1edaSArun Thomas switch(c) {
772e8d1edaSArun Thomas case 'a':
782e8d1edaSArun Thomas return TRACE_ARGS;
792e8d1edaSArun Thomas case 'e':
802e8d1edaSArun Thomas return TRACE_EXPANSION;
812e8d1edaSArun Thomas case 'q':
822e8d1edaSArun Thomas return TRACE_QUOTE;
832e8d1edaSArun Thomas case 'c':
842e8d1edaSArun Thomas return TRACE_CONT;
852e8d1edaSArun Thomas case 'x':
862e8d1edaSArun Thomas return TRACE_ID;
872e8d1edaSArun Thomas case 'f':
882e8d1edaSArun Thomas return TRACE_FILENAME;
892e8d1edaSArun Thomas case 'l':
902e8d1edaSArun Thomas return TRACE_LINENO;
912e8d1edaSArun Thomas case 'p':
922e8d1edaSArun Thomas return TRACE_NEWFILE;
932e8d1edaSArun Thomas case 'i':
942e8d1edaSArun Thomas return TRACE_INPUT;
952e8d1edaSArun Thomas case 't':
962e8d1edaSArun Thomas return TRACE_ALL;
972e8d1edaSArun Thomas case 'V':
982e8d1edaSArun Thomas return ~0;
992e8d1edaSArun Thomas default:
1002e8d1edaSArun Thomas return 0;
1012e8d1edaSArun Thomas }
1022e8d1edaSArun Thomas }
1032e8d1edaSArun Thomas
1042e8d1edaSArun Thomas void
set_trace_flags(const char * s)1052e8d1edaSArun Thomas set_trace_flags(const char *s)
1062e8d1edaSArun Thomas {
1072e8d1edaSArun Thomas char mode = 0;
1082e8d1edaSArun Thomas unsigned int f = 0;
1092e8d1edaSArun Thomas
1102e8d1edaSArun Thomas if (*s == '+' || *s == '-')
1112e8d1edaSArun Thomas mode = *s++;
1122e8d1edaSArun Thomas while (*s)
1132e8d1edaSArun Thomas f |= letter_to_flag(*s++);
1142e8d1edaSArun Thomas switch(mode) {
1152e8d1edaSArun Thomas case 0:
1162e8d1edaSArun Thomas trace_flags = f;
1172e8d1edaSArun Thomas break;
1182e8d1edaSArun Thomas case '+':
1192e8d1edaSArun Thomas trace_flags |= f;
1202e8d1edaSArun Thomas break;
1212e8d1edaSArun Thomas case '-':
1222e8d1edaSArun Thomas trace_flags &= ~f;
1232e8d1edaSArun Thomas break;
1242e8d1edaSArun Thomas }
1252e8d1edaSArun Thomas }
1262e8d1edaSArun Thomas
1272e8d1edaSArun Thomas static int
frame_level(void)128*71c7dcb9SLionel Sambuc frame_level(void)
1292e8d1edaSArun Thomas {
1302e8d1edaSArun Thomas int level;
1312e8d1edaSArun Thomas int framep;
1322e8d1edaSArun Thomas
1332e8d1edaSArun Thomas for (framep = fp, level = 0; framep != 0;
1342e8d1edaSArun Thomas level++,framep = mstack[framep-3].sfra)
1352e8d1edaSArun Thomas ;
1362e8d1edaSArun Thomas return level;
1372e8d1edaSArun Thomas }
1382e8d1edaSArun Thomas
1392e8d1edaSArun Thomas static void
print_header(struct input_file * inp)1402e8d1edaSArun Thomas print_header(struct input_file *inp)
1412e8d1edaSArun Thomas {
1422e8d1edaSArun Thomas fprintf(traceout, "m4trace:");
1432e8d1edaSArun Thomas if (trace_flags & TRACE_FILENAME)
1442e8d1edaSArun Thomas fprintf(traceout, "%s:", inp->name);
1452e8d1edaSArun Thomas if (trace_flags & TRACE_LINENO)
146*71c7dcb9SLionel Sambuc fprintf(traceout, "%lu:", TOKEN_LINE(inp));
1472e8d1edaSArun Thomas fprintf(traceout, " -%d- ", frame_level());
1482e8d1edaSArun Thomas if (trace_flags & TRACE_ID)
1492e8d1edaSArun Thomas fprintf(traceout, "id %lu: ", expansion_id);
1502e8d1edaSArun Thomas }
1512e8d1edaSArun Thomas
1522e8d1edaSArun Thomas size_t
trace(const char * argv[],int argc,struct input_file * inp)1532e8d1edaSArun Thomas trace(const char *argv[], int argc, struct input_file *inp)
1542e8d1edaSArun Thomas {
1552e8d1edaSArun Thomas if (!traceout)
1562e8d1edaSArun Thomas traceout = stderr;
1572e8d1edaSArun Thomas print_header(inp);
1582e8d1edaSArun Thomas if (trace_flags & TRACE_CONT) {
1592e8d1edaSArun Thomas fprintf(traceout, "%s ...\n", argv[1]);
1602e8d1edaSArun Thomas print_header(inp);
1612e8d1edaSArun Thomas }
1622e8d1edaSArun Thomas fprintf(traceout, "%s", argv[1]);
1632e8d1edaSArun Thomas if ((trace_flags & TRACE_ARGS) && argc > 2) {
1642e8d1edaSArun Thomas char delim[3];
1652e8d1edaSArun Thomas int i;
1662e8d1edaSArun Thomas
1672e8d1edaSArun Thomas delim[0] = LPAREN;
1682e8d1edaSArun Thomas delim[1] = EOS;
1692e8d1edaSArun Thomas for (i = 2; i < argc; i++) {
1702e8d1edaSArun Thomas fprintf(traceout, "%s%s%s%s", delim,
1712e8d1edaSArun Thomas (trace_flags & TRACE_QUOTE) ? lquote : "",
1722e8d1edaSArun Thomas argv[i],
1732e8d1edaSArun Thomas (trace_flags & TRACE_QUOTE) ? rquote : "");
1742e8d1edaSArun Thomas delim[0] = COMMA;
1752e8d1edaSArun Thomas delim[1] = ' ';
1762e8d1edaSArun Thomas delim[2] = EOS;
1772e8d1edaSArun Thomas }
1782e8d1edaSArun Thomas fprintf(traceout, "%c", RPAREN);
1792e8d1edaSArun Thomas }
1802e8d1edaSArun Thomas if (trace_flags & TRACE_CONT) {
1812e8d1edaSArun Thomas fprintf(traceout, " -> ???\n");
1822e8d1edaSArun Thomas print_header(inp);
1832e8d1edaSArun Thomas fprintf(traceout, argc > 2 ? "%s(...)" : "%s", argv[1]);
1842e8d1edaSArun Thomas }
1852e8d1edaSArun Thomas if (trace_flags & TRACE_EXPANSION)
1862e8d1edaSArun Thomas return buffer_mark();
1872e8d1edaSArun Thomas else {
1882e8d1edaSArun Thomas fprintf(traceout, "\n");
1892e8d1edaSArun Thomas return SIZE_MAX;
1902e8d1edaSArun Thomas }
1912e8d1edaSArun Thomas }
1922e8d1edaSArun Thomas
1932e8d1edaSArun Thomas void
finish_trace(size_t mark)1942e8d1edaSArun Thomas finish_trace(size_t mark)
1952e8d1edaSArun Thomas {
1962e8d1edaSArun Thomas fprintf(traceout, " -> ");
1972e8d1edaSArun Thomas if (trace_flags & TRACE_QUOTE)
1982e8d1edaSArun Thomas fprintf(traceout, "%s", lquote);
1992e8d1edaSArun Thomas dump_buffer(traceout, mark);
2002e8d1edaSArun Thomas if (trace_flags & TRACE_QUOTE)
2012e8d1edaSArun Thomas fprintf(traceout, "%s", rquote);
2022e8d1edaSArun Thomas fprintf(traceout, "\n");
2032e8d1edaSArun Thomas }
204