summaryrefslogtreecommitdiff
path: root/m4/debug.c
blob: eb0e32fd7c875a9c3940fe7ef6efda8c1c043d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* GNU m4 -- A simple macro processor
   Copyright (C) 1991-1994, 2006-2010, 2013-2014, 2017 Free Software
   Foundation, Inc.

   This file is part of GNU M4.

   GNU M4 is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   GNU M4 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include <sys/stat.h>
#include <stdarg.h>

#include "m4private.h"
#include "close-stream.h"

static void set_debug_file (m4 *, const m4_call_info *, FILE *);



/* Function to decode the debugging flags OPTS of length LEN; or
   SIZE_MAX if OPTS is NUL-terminated.  If OPTS is NULL, use the
   default flags.  Used by main while processing option -d, and by the
   builtin debugmode ().  */
int
m4_debug_decode (m4 *context, const char *opts, size_t len)
{
  int previous = context->debug_level;
  int level;
  char mode = '\0';

  if (!opts)
    opts = "";
  if (len == SIZE_MAX)
    len = strlen (opts);
  if (!len)
    level = M4_DEBUG_TRACE_DEFAULT | previous;
  else
    {
      if (*opts == '-' || *opts == '+')
        {
          len--;
          mode = *opts++;
        }
      for (level = 0; len--; opts++)
        {
          switch (*opts)
            {
            case 'a':
              level |= M4_DEBUG_TRACE_ARGS;
              break;

            case 'e':
              level |= M4_DEBUG_TRACE_EXPANSION;
              break;

            case 'q':
              level |= M4_DEBUG_TRACE_QUOTE;
              break;

            case 't':
              level |= M4_DEBUG_TRACE_ALL;
              break;

            case 'l':
              level |= M4_DEBUG_TRACE_LINE;
              break;

            case 'f':
              level |= M4_DEBUG_TRACE_FILE;
              break;

            case 'p':
              level |= M4_DEBUG_TRACE_PATH;
              break;

            case 'c':
              level |= M4_DEBUG_TRACE_CALL;
              break;

            case 'i':
              level |= M4_DEBUG_TRACE_INPUT;
              break;

            case 'x':
              level |= M4_DEBUG_TRACE_CALLID;
              break;

            case 'm':
              level |= M4_DEBUG_TRACE_MODULE;
              break;

            case 's':
              level |= M4_DEBUG_TRACE_STACK;
              break;

            case 'd':
              level |= M4_DEBUG_TRACE_DEREF;
              break;

            case 'o':
               level |= M4_DEBUG_TRACE_OUTPUT_DUMPDEF;
               break;

            case 'V':
              level |= M4_DEBUG_TRACE_VERBOSE;
              break;

            default:
              return -1;
            }
        }
    }

  switch (mode)
    {
    case '\0':
      /* Replace old level.  */
      break;

    case '-':
      /* Subtract flags.  */
      level = previous & ~level;
      break;

    case '+':
      /* Add flags.  */
      level |= previous;
      break;

    default:
      assert (!"m4_debug_decode");
    }
  context->debug_level = level;
  return level;
}

/* Change the debug output stream to FP.  If the underlying file is the
   same as stdout, use stdout instead so that debug messages appear in the
   correct relative position.  Report errors on behalf of CALLER.  */
static void
set_debug_file (m4 *context, const m4_call_info *caller, FILE *fp)
{
  FILE *debug_file;
  struct stat stdout_stat, debug_stat;

  assert (context);

  debug_file = m4_get_debug_file (context);
  if (debug_file != NULL && debug_file != stderr && debug_file != stdout
      && close_stream (debug_file) != 0)
    m4_error (context, 0, errno, caller, _("error writing to debug stream"));

  debug_file = fp;
  m4_set_debug_file (context, fp);

  if (debug_file != NULL && debug_file != stdout)
    {
      if (fstat (fileno (stdout), &stdout_stat) < 0)
        return;
      if (fstat (fileno (debug_file), &debug_stat) < 0)
        return;

      /* mingw has a bug where fstat on a regular file reports st_ino
         of 0.  On normal system, st_ino should never be 0.  */
      if (stdout_stat.st_ino == debug_stat.st_ino
          && stdout_stat.st_dev == debug_stat.st_dev
          && stdout_stat.st_ino != 0)
        {
          if (debug_file != stderr && close_stream (debug_file) != 0)
            m4_error (context, 0, errno, caller,
                      _("error writing to debug stream"));
          m4_set_debug_file (context, stdout);
        }
    }
}

/* Change the debug output to file NAME.  If NAME is NULL, debug
   output is reverted to stderr, and if empty debug output is
   discarded.  Return true iff the output stream was changed.  Report
   errors on behalf of CALLER.  */
bool
m4_debug_set_output (m4 *context, const m4_call_info *caller, const char *name)
{
  FILE *fp;

  assert (context);

  if (name == NULL)
    set_debug_file (context, caller, stderr);
  else if (*name == '\0')
    set_debug_file (context, caller, NULL);
  else
    {
      fp = fopen (name, "a");
      if (fp == NULL)
        return false;

      if (set_cloexec_flag (fileno (fp), true) != 0)
        m4_warn (context, errno, caller,
                 _("cannot protect debug file across forks"));
      set_debug_file (context, caller, fp);
    }
  return true;
}

/* Print the header of a one-line debug message, starting with "m4debug:".  */
void
m4_debug_message_prefix (m4 *context)
{
  FILE *debug_file;

  assert (context);

  debug_file = m4_get_debug_file (context);
  fputs ("m4debug:", debug_file);
  if (m4_get_current_line (context))
    {
      if (m4_is_debug_bit (context, M4_DEBUG_TRACE_FILE))
        xfprintf (debug_file, "%s:", m4_get_current_file (context));
      if (m4_is_debug_bit (context, M4_DEBUG_TRACE_LINE))
        xfprintf (debug_file, "%d:", m4_get_current_line (context));
    }
  putc (' ', debug_file);
}

/* If the current debug mode includes MODE, and there is a current
   debug file, then output a debug message described by FORMAT.  A
   message header is supplied, as well as a trailing newline.  */
void
m4_debug_message (m4 *context, int mode, const char *format, ...)
{
  /* Check that mode has exactly one bit set.  */
  assert (mode && (mode & (mode - 1)) == 0);
  assert (format);

  if (m4_get_debug_file (context) != NULL
      && m4_is_debug_bit (context, mode))
    {
      va_list args;

      m4_debug_message_prefix (context);
      va_start (args, format);
      xvfprintf (m4_get_debug_file (context), format, args);
      va_end (args);
      putc ('\n', m4_get_debug_file (context));
    }
}