Home | History | Annotate | Line # | Download | only in m4
trace.c revision 1.7.4.1
      1  1.7.4.1      yamt /*	$NetBSD: trace.c,v 1.7.4.1 2012/04/17 00:09:35 yamt Exp $	*/
      2      1.6  christos /* $OpenBSD: trace.c,v 1.15 2006/03/24 08:03:44 espie Exp $ */
      3      1.1        tv /*
      4      1.1        tv  * Copyright (c) 2001 Marc Espie.
      5      1.1        tv  *
      6      1.1        tv  * Redistribution and use in source and binary forms, with or without
      7      1.1        tv  * modification, are permitted provided that the following conditions
      8      1.1        tv  * are met:
      9      1.1        tv  * 1. Redistributions of source code must retain the above copyright
     10      1.1        tv  *    notice, this list of conditions and the following disclaimer.
     11      1.1        tv  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1        tv  *    notice, this list of conditions and the following disclaimer in the
     13      1.1        tv  *    documentation and/or other materials provided with the distribution.
     14      1.1        tv  *
     15      1.1        tv  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
     16      1.1        tv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17      1.1        tv  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     18      1.1        tv  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
     19      1.1        tv  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20      1.1        tv  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     21      1.1        tv  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.1        tv  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.1        tv  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.1        tv  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25      1.1        tv  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26      1.1        tv  */
     27      1.5       jmc #if HAVE_NBTOOL_CONFIG_H
     28      1.5       jmc #include "nbtool_config.h"
     29      1.5       jmc #endif
     30      1.6  christos #include <sys/cdefs.h>
     31  1.7.4.1      yamt __RCSID("$NetBSD: trace.c,v 1.7.4.1 2012/04/17 00:09:35 yamt Exp $");
     32      1.5       jmc 
     33      1.1        tv #include <sys/types.h>
     34      1.6  christos #include <err.h>
     35      1.1        tv #include <stddef.h>
     36      1.6  christos #include <stdint.h>
     37      1.1        tv #include <stdio.h>
     38      1.1        tv #include <stdlib.h>
     39      1.1        tv #include "mdef.h"
     40      1.1        tv #include "stdd.h"
     41      1.1        tv #include "extern.h"
     42      1.1        tv 
     43      1.4        tv FILE *traceout;
     44      1.1        tv 
     45      1.1        tv #define TRACE_ARGS 	1
     46      1.1        tv #define TRACE_EXPANSION 2
     47      1.1        tv #define TRACE_QUOTE	4
     48      1.1        tv #define TRACE_FILENAME	8
     49      1.1        tv #define TRACE_LINENO	16
     50      1.1        tv #define TRACE_CONT	32
     51      1.1        tv #define TRACE_ID	64
     52      1.1        tv #define TRACE_NEWFILE	128	/* not implemented yet */
     53      1.1        tv #define TRACE_INPUT	256	/* not implemented yet */
     54      1.1        tv 
     55      1.6  christos static unsigned int letter_to_flag(int);
     56      1.6  christos static void print_header(struct input_file *);
     57      1.6  christos static int frame_level(void);
     58      1.1        tv 
     59      1.1        tv 
     60      1.6  christos unsigned int trace_flags = TRACE_QUOTE | TRACE_EXPANSION;
     61      1.1        tv 
     62      1.1        tv void
     63      1.6  christos trace_file(const char *name)
     64      1.1        tv {
     65      1.1        tv 
     66      1.6  christos 	if (traceout && traceout != stderr)
     67      1.1        tv 		fclose(traceout);
     68      1.1        tv 	traceout = fopen(name, "w");
     69      1.1        tv 	if (!traceout)
     70      1.1        tv 		err(1, "can't open %s", name);
     71      1.1        tv }
     72      1.1        tv 
     73      1.1        tv static unsigned int
     74      1.6  christos letter_to_flag(int c)
     75      1.1        tv {
     76      1.1        tv 	switch(c) {
     77      1.1        tv 	case 'a':
     78      1.1        tv 		return TRACE_ARGS;
     79      1.1        tv 	case 'e':
     80      1.1        tv 		return TRACE_EXPANSION;
     81      1.1        tv 	case 'q':
     82      1.1        tv 		return TRACE_QUOTE;
     83      1.1        tv 	case 'c':
     84      1.1        tv 		return TRACE_CONT;
     85      1.1        tv 	case 'x':
     86      1.1        tv 		return TRACE_ID;
     87      1.1        tv 	case 'f':
     88      1.1        tv 		return TRACE_FILENAME;
     89      1.1        tv 	case 'l':
     90      1.1        tv 		return TRACE_LINENO;
     91      1.1        tv 	case 'p':
     92      1.1        tv 		return TRACE_NEWFILE;
     93      1.1        tv 	case 'i':
     94      1.1        tv 		return TRACE_INPUT;
     95      1.1        tv 	case 't':
     96      1.1        tv 		return TRACE_ALL;
     97      1.1        tv 	case 'V':
     98      1.1        tv 		return ~0;
     99      1.1        tv 	default:
    100      1.1        tv 		return 0;
    101      1.1        tv 	}
    102      1.1        tv }
    103      1.1        tv 
    104      1.1        tv void
    105      1.6  christos set_trace_flags(const char *s)
    106      1.1        tv {
    107      1.1        tv 	char mode = 0;
    108      1.1        tv 	unsigned int f = 0;
    109      1.1        tv 
    110      1.1        tv 	if (*s == '+' || *s == '-')
    111      1.1        tv 		mode = *s++;
    112      1.1        tv 	while (*s)
    113      1.1        tv 		f |= letter_to_flag(*s++);
    114      1.1        tv 	switch(mode) {
    115      1.1        tv 	case 0:
    116      1.6  christos 		trace_flags = f;
    117      1.1        tv 		break;
    118      1.1        tv 	case '+':
    119      1.6  christos 		trace_flags |= f;
    120      1.1        tv 		break;
    121      1.1        tv 	case '-':
    122      1.6  christos 		trace_flags &= ~f;
    123      1.1        tv 		break;
    124      1.1        tv 	}
    125      1.1        tv }
    126      1.1        tv 
    127      1.1        tv static int
    128  1.7.4.1      yamt frame_level(void)
    129      1.1        tv {
    130      1.1        tv 	int level;
    131      1.1        tv 	int framep;
    132      1.1        tv 
    133      1.1        tv 	for (framep = fp, level = 0; framep != 0;
    134      1.6  christos 		level++,framep = mstack[framep-3].sfra)
    135      1.1        tv 		;
    136      1.1        tv 	return level;
    137      1.1        tv }
    138      1.1        tv 
    139      1.1        tv static void
    140      1.6  christos print_header(struct input_file *inp)
    141      1.1        tv {
    142      1.6  christos 	fprintf(traceout, "m4trace:");
    143      1.6  christos 	if (trace_flags & TRACE_FILENAME)
    144      1.6  christos 		fprintf(traceout, "%s:", inp->name);
    145      1.6  christos 	if (trace_flags & TRACE_LINENO)
    146      1.7  christos 		fprintf(traceout, "%lu:", TOKEN_LINE(inp));
    147      1.6  christos 	fprintf(traceout, " -%d- ", frame_level());
    148      1.6  christos 	if (trace_flags & TRACE_ID)
    149      1.6  christos 		fprintf(traceout, "id %lu: ", expansion_id);
    150      1.1        tv }
    151      1.1        tv 
    152      1.6  christos size_t
    153      1.6  christos trace(const char *argv[], int argc, struct input_file *inp)
    154      1.1        tv {
    155      1.6  christos 	if (!traceout)
    156      1.6  christos 		traceout = stderr;
    157      1.1        tv 	print_header(inp);
    158      1.6  christos 	if (trace_flags & TRACE_CONT) {
    159      1.6  christos 		fprintf(traceout, "%s ...\n", argv[1]);
    160      1.1        tv 		print_header(inp);
    161      1.1        tv 	}
    162      1.6  christos 	fprintf(traceout, "%s", argv[1]);
    163      1.6  christos 	if ((trace_flags & TRACE_ARGS) && argc > 2) {
    164      1.1        tv 		char delim[3];
    165      1.1        tv 		int i;
    166      1.1        tv 
    167      1.1        tv 		delim[0] = LPAREN;
    168      1.1        tv 		delim[1] = EOS;
    169      1.1        tv 		for (i = 2; i < argc; i++) {
    170      1.6  christos 			fprintf(traceout, "%s%s%s%s", delim,
    171      1.6  christos 			    (trace_flags & TRACE_QUOTE) ? lquote : "",
    172      1.1        tv 			    argv[i],
    173      1.6  christos 			    (trace_flags & TRACE_QUOTE) ? rquote : "");
    174      1.1        tv 			delim[0] = COMMA;
    175      1.1        tv 			delim[1] = ' ';
    176      1.1        tv 			delim[2] = EOS;
    177      1.1        tv 		}
    178      1.6  christos 		fprintf(traceout, "%c", RPAREN);
    179      1.1        tv 	}
    180      1.6  christos 	if (trace_flags & TRACE_CONT) {
    181      1.6  christos 		fprintf(traceout, " -> ???\n");
    182      1.1        tv 		print_header(inp);
    183      1.6  christos 		fprintf(traceout, argc > 2 ? "%s(...)" : "%s", argv[1]);
    184      1.1        tv 	}
    185      1.6  christos 	if (trace_flags & TRACE_EXPANSION)
    186      1.1        tv 		return buffer_mark();
    187      1.1        tv 	else {
    188      1.6  christos 		fprintf(traceout, "\n");
    189      1.6  christos 		return SIZE_MAX;
    190      1.1        tv 	}
    191      1.1        tv }
    192      1.1        tv 
    193      1.1        tv void
    194      1.6  christos finish_trace(size_t mark)
    195      1.1        tv {
    196      1.6  christos 	fprintf(traceout, " -> ");
    197      1.6  christos 	if (trace_flags & TRACE_QUOTE)
    198      1.6  christos 		fprintf(traceout, "%s", lquote);
    199      1.6  christos 	dump_buffer(traceout, mark);
    200      1.6  christos 	if (trace_flags & TRACE_QUOTE)
    201      1.6  christos 		fprintf(traceout, "%s", rquote);
    202      1.6  christos 	fprintf(traceout, "\n");
    203      1.1        tv }
    204