home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / m4-1.4-src.tgz / tar.out / fsf / m4 / src / debug.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  10KB  |  422 lines

  1. /* GNU m4 -- A simple macro processor
  2.    Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.   
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.   
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.   
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #include "m4.h"
  20.  
  21. #include <sys/stat.h>
  22.  
  23. #if __STDC__
  24. #include <stdarg.h>
  25. #else
  26. #include <varargs.h>
  27. #endif
  28.  
  29. /* File for debugging output.  */
  30. FILE *debug = NULL;
  31.  
  32. /* Obstack for trace messages.  */
  33. static struct obstack trace;
  34.  
  35. extern int expansion_level;
  36.  
  37. static void debug_set_file _((FILE *));
  38.  
  39. /*----------------------------------.
  40. | Initialise the debugging module.  |
  41. `----------------------------------*/
  42.  
  43. void
  44. debug_init (void)
  45. {
  46.   debug_set_file (stderr);
  47.   obstack_init (&trace);
  48. }
  49.  
  50. /*-----------------------------------------------------------------.
  51. | Function to decode the debugging flags OPTS.  Used by main while |
  52. | processing option -d, and by the builtin debugmode ().       |
  53. `-----------------------------------------------------------------*/
  54.  
  55. int
  56. debug_decode (const char *opts)
  57. {
  58.   int level;
  59.  
  60.   if (opts == NULL || *opts == '\0')
  61.     level = DEBUG_TRACE_DEFAULT;
  62.   else
  63.     {
  64.       for (level = 0; *opts; opts++)
  65.     {
  66.       switch (*opts)
  67.         {
  68.         case 'a':
  69.           level |= DEBUG_TRACE_ARGS;
  70.           break;
  71.  
  72.         case 'e':
  73.           level |= DEBUG_TRACE_EXPANSION;
  74.           break;
  75.  
  76.         case 'q':
  77.           level |= DEBUG_TRACE_QUOTE;
  78.           break;
  79.  
  80.         case 't':
  81.           level |= DEBUG_TRACE_ALL;
  82.           break;
  83.  
  84.         case 'l':
  85.           level |= DEBUG_TRACE_LINE;
  86.           break;
  87.  
  88.         case 'f':
  89.           level |= DEBUG_TRACE_FILE;
  90.           break;
  91.  
  92.         case 'p':
  93.           level |= DEBUG_TRACE_PATH;
  94.           break;
  95.  
  96.         case 'c':
  97.           level |= DEBUG_TRACE_CALL;
  98.           break;
  99.  
  100.         case 'i':
  101.           level |= DEBUG_TRACE_INPUT;
  102.           break;
  103.  
  104.         case 'x':
  105.           level |= DEBUG_TRACE_CALLID;
  106.           break;
  107.  
  108.         case 'V':
  109.           level |= DEBUG_TRACE_VERBOSE;
  110.           break;
  111.  
  112.         default:
  113.           return -1;
  114.         }
  115.     }
  116.     }
  117.  
  118.   /* This is to avoid screwing up the trace output due to changes in the
  119.      debug_level.  */
  120.  
  121.   obstack_free (&trace, obstack_finish (&trace));
  122.  
  123.   return level;
  124. }
  125.  
  126. /*------------------------------------------------------------------------.
  127. | Change the debug output stream to FP.  If the underlying file is the      |
  128. | same as stdout, use stdout instead so that debug messages appear in the |
  129. | correct relative position.                          |
  130. `------------------------------------------------------------------------*/
  131.  
  132. static void
  133. debug_set_file (FILE *fp)
  134. {
  135.   struct stat stdout_stat, debug_stat;
  136.  
  137.   if (debug != NULL && debug != stderr && debug != stdout)
  138.     fclose (debug);
  139.   debug = fp;
  140.  
  141.   if (debug != NULL && debug != stdout)
  142.     {
  143.       if (fstat (fileno (stdout), &stdout_stat) < 0)
  144.     return;
  145.       if (fstat (fileno (debug), &debug_stat) < 0)
  146.     return;
  147.  
  148.       if (stdout_stat.st_ino == debug_stat.st_ino
  149.       && stdout_stat.st_dev == debug_stat.st_dev)
  150.     {
  151.       if (debug != stderr)
  152.         fclose (debug);
  153.       debug = stdout;
  154.     }
  155.     }
  156. }
  157.  
  158. /*-----------------------------------------------------------.
  159. | Serialize files.  Used before executing a system command.  |
  160. `-----------------------------------------------------------*/
  161.  
  162. void
  163. debug_flush_files (void)
  164. {
  165.   fflush (stdout);
  166.   fflush (stderr);
  167.   if (debug != NULL && debug != stdout && debug != stderr)
  168.     fflush (debug);
  169. }
  170.  
  171. /*-------------------------------------------------------------------------.
  172. | Change the debug output to file NAME.  If NAME is NULL, debug output is  |
  173. | reverted to stderr, and if empty debug output is discarded.  Return TRUE |
  174. | iff the output stream was changed.                       |
  175. `-------------------------------------------------------------------------*/
  176.  
  177. boolean
  178. debug_set_output (const char *name)
  179. {
  180.   FILE *fp;
  181.  
  182.   if (name == NULL)
  183.     debug_set_file (stderr);
  184.   else if (*name == '\0')
  185.     debug_set_file (NULL);
  186.   else
  187.     {
  188.       fp = fopen (name, "a");
  189.       if (fp == NULL)
  190.     return FALSE;
  191.  
  192.       debug_set_file (fp);
  193.     }
  194.   return TRUE;
  195. }
  196.  
  197. /*-----------------------------------------------------------------------.
  198. | Print the header of a one-line debug message, starting by "m4 debug".     |
  199. `-----------------------------------------------------------------------*/
  200.  
  201. void
  202. debug_message_prefix (void)
  203. {
  204.   fprintf (debug, "m4 debug: ");
  205.   if (debug_level & DEBUG_TRACE_FILE)
  206.     fprintf (debug, "%s: ", current_file);
  207.   if (debug_level & DEBUG_TRACE_LINE)
  208.     fprintf (debug, "%d: ", current_line);
  209. }
  210.  
  211. /* The rest of this file contains the functions for macro tracing output.
  212.    All tracing output for a macro call is collected on an obstack TRACE,
  213.    and printed whenever the line is complete.  This prevents tracing
  214.    output from interfering with other debug messages generated by the
  215.    various builtins.  */
  216.  
  217. /*---------------------------------------------------------------------.
  218. | Tracing output is formatted here, by a simplified printf-to-obstack  |
  219. | function trace_format ().  Understands only %S, %s, %d, %l (optional |
  220. | left quote) and %r (optional right quote).                   |
  221. `---------------------------------------------------------------------*/
  222.  
  223. #if __STDC__
  224. static void
  225. trace_format (const char *fmt, ...)
  226. #else
  227. static void
  228. trace_format (...)
  229. #endif
  230. {
  231. #if ! __STDC__
  232.   const char *fmt;
  233. #endif
  234.   va_list args;
  235.   char ch;
  236.  
  237.   int d;
  238.   char nbuf[32];
  239.   const char *s;
  240.   int slen;
  241.   int maxlen;
  242.  
  243. #if __STDC__
  244.   va_start (args, fmt);
  245. #else
  246.   va_start (args);
  247.   fmt = va_arg (args, const char *);
  248. #endif
  249.  
  250.   while (TRUE)
  251.     {
  252.       while ((ch = *fmt++) != '\0' && ch != '%')
  253.     obstack_1grow (&trace, ch);
  254.  
  255.       if (ch == '\0')
  256.     break;
  257.  
  258.       maxlen = 0;
  259.       switch (*fmt++)
  260.     {
  261.     case 'S':
  262.       maxlen = max_debug_argument_length;
  263.       /* fall through */
  264.  
  265.     case 's':
  266.       s = va_arg (args, const char *);
  267.       break;
  268.  
  269.     case 'l':
  270.       s = (debug_level & DEBUG_TRACE_QUOTE) ? lquote.string : "";
  271.       break;
  272.  
  273.     case 'r':
  274.       s = (debug_level & DEBUG_TRACE_QUOTE) ? rquote.string : "";
  275.       break;
  276.  
  277.     case 'd':
  278.       d = va_arg (args, int);
  279.       sprintf (nbuf, "%d", d);
  280.       s = nbuf;
  281.       break;
  282.  
  283.     default:
  284.       s = "";
  285.       break;
  286.     }
  287.  
  288.       slen = strlen (s);
  289.       if (maxlen == 0 || maxlen > slen)
  290.     obstack_grow (&trace, s, slen);
  291.       else
  292.     {
  293.       obstack_grow (&trace, s, maxlen);
  294.       obstack_grow (&trace, "...", 3);
  295.     }
  296.     }
  297.  
  298.   va_end (args);
  299. }
  300.  
  301. /*------------------------------------------------------------------.
  302. | Format the standard header attached to all tracing output lines.  |
  303. `------------------------------------------------------------------*/
  304.  
  305. static void
  306. trace_header (int id)
  307. {
  308.   trace_format ("m4trace:");
  309.   if (debug_level & DEBUG_TRACE_FILE)
  310.     trace_format ("%s:", current_file);
  311.   if (debug_level & DEBUG_TRACE_LINE)
  312.     trace_format ("%d:", current_line);
  313.   trace_format (" -%d- ", expansion_level);
  314.   if (debug_level & DEBUG_TRACE_CALLID)
  315.     trace_format ("id %d: ", id);
  316. }
  317.  
  318. /*----------------------------------------------------.
  319. | Print current tracing line, and clear the obstack.  |
  320. `----------------------------------------------------*/
  321.  
  322. static void
  323. trace_flush (void)
  324. {
  325.   char *line;
  326.  
  327.   obstack_1grow (&trace, '\0');
  328.   line = obstack_finish (&trace);
  329.   DEBUG_PRINT1 ("%s\n", line);
  330.   obstack_free (&trace, line);
  331. }
  332.  
  333. /*-------------------------------------------------------------.
  334. | Do pre-argument-collction tracing for macro NAME.  Used from |
  335. | expand_macro ().                           |
  336. `-------------------------------------------------------------*/
  337.  
  338. void
  339. trace_prepre (const char *name, int id)
  340. {
  341.   trace_header (id);
  342.   trace_format ("%s ...", name);
  343.   trace_flush ();
  344. }
  345.  
  346. /*-----------------------------------------------------------------------.
  347. | Format the parts of a trace line, that can be made before the macro is |
  348. | actually expanded.  Used from expand_macro ().             |
  349. `-----------------------------------------------------------------------*/
  350.  
  351. void
  352. trace_pre (const char *name, int id, int argc, token_data **argv)
  353. {
  354.   int i;
  355.   const builtin *bp;
  356.  
  357.   trace_header (id);
  358.   trace_format ("%s", name);
  359.  
  360.   if (argc > 1 && (debug_level & DEBUG_TRACE_ARGS))
  361.     {
  362.       trace_format ("(");
  363.  
  364.       for (i = 1; i < argc; i++)
  365.     {
  366.       if (i != 1)
  367.         trace_format (", ");
  368.  
  369.       switch (TOKEN_DATA_TYPE (argv[i]))
  370.         {
  371.         case TOKEN_TEXT:
  372.           trace_format ("%l%S%r", TOKEN_DATA_TEXT (argv[i]));
  373.           break;
  374.  
  375.         case TOKEN_FUNC:
  376.           bp = find_builtin_by_addr (TOKEN_DATA_FUNC (argv[i]));
  377.           if (bp == NULL)
  378.         {
  379.           M4ERROR ((warning_status, 0, "\
  380. INTERNAL ERROR: Builtin not found in builtin table! (trace_pre ())"));
  381.           abort ();
  382.         }
  383.           trace_format ("<%s>", bp->name);
  384.           break;
  385.  
  386.         default:
  387.           M4ERROR ((warning_status, 0,
  388.             "INTERNAL ERROR: Bad token data type (trace_pre ())"));
  389.           abort ();
  390.         }
  391.  
  392.     }
  393.       trace_format (")");
  394.     }
  395.  
  396.   if (debug_level & DEBUG_TRACE_CALL)
  397.     {
  398.       trace_format (" -> ???");
  399.       trace_flush ();
  400.     }
  401. }
  402.  
  403. /*-------------------------------------------------------------------.
  404. | Format the final part of a trace line and print it all.  Used from |
  405. | expand_macro ().                             |
  406. `-------------------------------------------------------------------*/
  407.  
  408. void
  409. trace_post (const char *name, int id, int argc, token_data **argv,
  410.         const char *expanded)
  411. {
  412.   if (debug_level & DEBUG_TRACE_CALL)
  413.     {
  414.       trace_header (id);
  415.       trace_format ("%s%s", name, (argc > 1) ? "(...)" : "");
  416.     }
  417.  
  418.   if (expanded && (debug_level & DEBUG_TRACE_EXPANSION))
  419.     trace_format (" -> %l%S%r", expanded);
  420.   trace_flush ();
  421. }
  422.