-
Notifications
You must be signed in to change notification settings - Fork 0
/
auxlib.cc
132 lines (112 loc) · 3.19 KB
/
auxlib.cc
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
#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include "auxlib.h"
static int exitstatus = EXIT_SUCCESS;
static const char* execname = NULL;
static const char* debugflags = "";
static bool alldebugflags = false;
void set_execname (char* argv0) {
execname = basename (argv0);
}
const char* get_execname (void) {
assert (execname != NULL);
return execname;
}
static void eprint_signal (const char* kind, int signal) {
eprintf (", %s %d", kind, signal);
const char* sigstr = strsignal (signal);
if (sigstr != NULL) fprintf (stderr, " %s", sigstr);
}
void eprint_status (const char* command, int status) {
if (status == 0) return;
eprintf ("%s: status 0x%04X", command, status);
if (WIFEXITED (status)) {
eprintf (", exit %d", WEXITSTATUS (status));
}
if (WIFSIGNALED (status)) {
eprint_signal ("Terminated", WTERMSIG (status));
#ifdef WCOREDUMP
if (WCOREDUMP (status)) eprintf (", core dumped");
#endif
}
if (WIFSTOPPED (status)) {
eprint_signal ("Stopped", WSTOPSIG (status));
}
if (WIFCONTINUED (status)) {
eprintf (", Continued");
}
eprintf ("\n");
}
int get_exitstatus (void) {
return exitstatus;
}
void veprintf (const char* format, va_list args) {
assert (execname != NULL);
assert (format != NULL);
fflush (NULL);
if (strstr (format, "%:") == format) {
fprintf (stderr, "%s: ", get_execname ());
format += 2;
}
vfprintf (stderr, format, args);
fflush (NULL);
}
void eprintf (const char* format, ...) {
va_list args;
va_start (args, format);
veprintf (format, args);
va_end (args);
}
void errprintf (const char* format, ...) {
va_list args;
va_start (args, format);
veprintf (format, args);
va_end (args);
exitstatus = EXIT_FAILURE;
}
void syserrprintf (const char* object) {
errprintf ("%:%s: %s\n", object, strerror (errno));
}
void set_exitstatus (int newexitstatus) {
if (exitstatus < newexitstatus) exitstatus = newexitstatus;
DEBUGF ('x', "exitstatus = %d\n", exitstatus);
}
void __stubprintf (const char* file, int line, const char* func,
const char* format, ...) {
va_list args;
fflush (NULL);
printf ("%s: %s[%d] %s: ", execname, file, line, func);
va_start (args, format);
vprintf (format, args);
va_end (args);
fflush (NULL);
}
void set_debugflags (const char* flags) {
debugflags = flags;
if (strchr (debugflags, '@') != NULL) alldebugflags = true;
DEBUGF ('x', "Debugflags = \"%s\", all = %d\n",
debugflags, alldebugflags);
}
bool is_debugflag (char flag) {
return alldebugflags or strchr (debugflags, flag) != NULL;
}
void __debugprintf (char flag, const char* file, int line,
const char* func, const char* format, ...) {
va_list args;
if (not is_debugflag (flag)) return;
fflush (NULL);
va_start (args, format);
fprintf (stderr, "DEBUGF(%c): %s[%d] %s():\n",
flag, file, line, func);
vfprintf (stderr, format, args);
va_end (args);
fflush (NULL);
}
RCSC("$Id: auxlib.cc,v 1.2 2013-10-11 18:56:07-07 - - $")