File: trace.cc

package info (click to toggle)
tardy 1.28-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,732 kB
  • sloc: cpp: 16,173; sh: 3,862; makefile: 1,785; ansic: 1,248; awk: 272
file content (425 lines) | stat: -rw-r--r-- 8,531 bytes parent folder | download
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// tardy - a tar post-processor
// Copyright (C) 1991-2009, 2011, 2012 Peter Miller
//
// This program 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.
//
// This program 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 <libtardy/ac/stdarg.h>
#include <libtardy/ac/stddef.h>
#include <libtardy/ac/stdio.h>
#include <libtardy/ac/string.h>
#include <libexplain/fflush.h>
#include <libexplain/program_name.h>

#include <libtardy/rcstring.h>
#include <libtardy/trace.h>


#define INDENT 2

struct known_t
{
    known_t() :
        flag(0),
        flag_p(0),
        next(0)
    {
    }

    rcstring filename;
    int flag;
    int *flag_p;
    known_t *next;
};

static rcstring file_name;
static int line_number;
static int page_width;
static known_t *known;
static int depth;


int
trace_pretest(const char *file, int *result)
{
    rcstring s = rcstring(file).basename();
    known_t *kp = known;
    for (; kp; kp = kp->next)
    {
        if (s == kp->filename)
        {
            break;
        }
    }
    if (!kp)
    {
        kp = new known_t;
        kp->filename = s;
        kp->next = known;
        kp->flag = 2; // disabled
        known = kp;
    }
    kp->flag_p = result;
    *result = kp->flag;
    return *result;
}


void
trace_where(const char *file, int line)
{
    file_name = rcstring(file).basename();
    line_number = line;
}


static void
trace_putchar(int c)
{
    static char buffer[200];
    static char *cp;
    static int in_col;
    static int out_col;

    if (!page_width)
    {
        // don't use last column, many terminals are dumb
        page_width = 79;
        // allow for progname, filename and line number (8 each)
        page_width -= 24;
        if (page_width < 16)
            page_width = 16;
    }
    if (!cp)
    {
        strcpy(buffer, explain_program_name_get());
        cp = buffer + strlen(buffer);
        if (cp > buffer + 6)
        cp = buffer + 6;
        *cp++ = ':';
        *cp++ = '\t';
        strcpy(cp, file_name.c_str());
        cp += file_name.size();
        *cp++ = ':';
        *cp++ = '\t';
        snprintf(cp, buffer + sizeof(buffer) - cp, "%d:\t", line_number);
        cp += strlen(cp);
        in_col = 0;
        out_col = 0;
    }
    switch (c)
    {
    case '\n':
        *cp++ = '\n';
        *cp = 0;
        explain_fflush_or_die(stdout);
        fputs(buffer, stderr);
        explain_fflush_or_die(stderr);
        cp = 0;
        break;

    case ' ':
        if (out_col)
            ++in_col;
        break;

    case '\t':
        if (out_col)
            in_col = (in_col/INDENT + 1) * INDENT;
        break;

    case '}':
    case ')':
    case ']':
        if (depth > 0)
            --depth;
        // fall through

    default:
        if (!out_col)
        {
            if (c != '#')
                // modulo so never too long
                in_col = (INDENT * depth) % page_width;
            else
                in_col = 0;
        }
        if (in_col >= page_width)
        {
            trace_putchar('\n');
            trace_putchar(c);
            return;
        }
        while (((out_col + 8) & -8) <= in_col && out_col + 1 < in_col)
        {
            *cp++ = '\t';
            out_col = (out_col + 8) & -8;
        }
        while (out_col < in_col)
        {
            *cp++ = ' ';
            ++out_col;
        }
        if (c == '{' || c == '(' || c == '[')
            ++depth;
        *cp++ = c;
        in_col++;
        out_col++;
        break;
    }
}


void
trace_printf(const char *s, ...)
{
    va_list ap;
    va_start(ap, s);
    char buffer[2000];
    vsnprintf(buffer, sizeof(buffer), s, ap);
    va_end(ap);
    for (s = buffer; *s; ++s)
        trace_putchar(*s);
}


void
trace_enable(const char *file)
{
    rcstring s = rcstring(file).basename();
    known_t *kp;
    for (kp = known; kp; kp = kp->next)
    {
        if (s == kp->filename)
        {
            break;
        }
    }
    if (!kp)
    {
        kp = new known_t;
        kp->filename = s;
        kp->next = known;
        known = kp;
    }
    kp->flag = 3; // enabled
    if (kp->flag_p)
        *kp->flag_p = kp->flag;
}


void
trace_char_real(char *name, char *vp)
{
    trace_printf("%s = '", name);
    if (*vp < ' ' || *vp > '~' || strchr("(){}[]", *vp))
    {
        const char *s = strchr("\bb\nn\tt\rr\ff", *vp);
        if (s)
        {
            trace_putchar('\\');
            trace_putchar(s[1]);
        }
        else
            trace_printf("\\%03o", (unsigned char)*vp);
    }
    else
    {
        if (strchr("'\\", *vp))
            trace_putchar('\\');
        trace_putchar(*vp);
    }
    trace_printf("'; /* 0x%02X, %d */\n", (unsigned char)*vp, *vp);
}


void
trace_char_unsigned_real(char *name, unsigned char *vp)
{
    trace_printf("%s = '", name);
    if (*vp < ' ' || *vp > '~' || strchr("(){}[]", *vp))
    {
        const char *s = strchr("\bb\nn\tt\rr\ff", *vp);
        if (s)
        {
            trace_putchar('\\');
            trace_putchar(s[1]);
        }
        else
            trace_printf("\\%03o", *vp);
    }
    else
    {
        if (strchr("'\\", *vp))
            trace_putchar('\\');
        trace_putchar(*vp);
    }
    trace_printf("'; /* 0x%02X, %d */\n", *vp, *vp);
}


void
trace_int_real(char *name, int *vp)
{
    trace_printf("%s = %d;\n", name, *vp);
}


void
trace_int_unsigned_real(char *name, unsigned int *vp)
{
    trace_printf("%s = %u;\n", name, *vp);
}


void
trace_long_real(char *name, long *vp)
{
    trace_printf("%s = %ld;\n", name, *vp);
}


void
trace_long_unsigned_real(char *name, unsigned long *vp)
{
    trace_printf("%s = %lu;\n", name, *vp);
}


void
trace_pointer_real(char *name, void *vptrptr)
{
    void **ptr_ptr = (void **)vptrptr;
    void *ptr = *ptr_ptr;
    if (!ptr)
        trace_printf("%s = NULL;\n", name);
    else
        trace_printf("%s = 0x%08lX;\n", name, (long)ptr);
}


void
trace_short_real(char *name, const short *vp)
{
    trace_printf("%s = %hd;\n", name, *vp);
}


void
trace_short_unsigned_real(const char *name, const unsigned short *vp)
{
    trace_printf("%s = %hu;\n", name, *vp);
}


void
trace_string_real(const char *name, const char *vp)
{
    trace_printf("%s = ", name);
    if (!vp)
    {
        trace_printf("NULL;\n");
        return;
    }
    trace_printf("\"");
    long count = 0;
    for (const char *s = vp; *s; ++s)
    {
        switch (*s)
        {
        case '('//)
:
        case '['//]
:
        case '{'//}
:
            ++count;
            break;

        case //(
')':
        case //[
']':
        case //{
'}':
            --count;
            break;
        }
    }
    if (count > 0)
        count = -count;
    else
        count = 0;
    for (const char *s = vp; *s; ++s)
    {
        int c = *s;
        if (c < ' ' || c > '~')
        {
            const char *cp = strchr("\bb\ff\nn\rr\tt", c);
            if (cp)
                trace_printf("\\%c", cp[1]);
            else
                trace_printf("\\%03o", (unsigned char)c);
        }
        else
        {
            switch (c)
            {
            case '(':
            case '[':
            case '{':
                ++count;
                if (count <= 0)
                {
                    trace_printf("\\%03o", (unsigned char)c);
                    goto next;
                }
                break;

            case ')':
            case ']':
            case '}':
                --count;
                if (count < 0)
                {
                    trace_printf("\\%03o", (unsigned char)c);
                    goto next;
                }
                break;

            case '\\':
            case '"':
                trace_printf("\\");
                break;

            default:
                break;
            }
            trace_printf("%c", c);
        }
        next:;
    }
    trace_printf("\";\n");
}


void
trace_indent_reset(void)
{
#ifdef DEBUG
    (void)trace_pretest_result;
#endif
    depth = 0;
}