| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief API: cgraph.h |
| 4 |
* @ingroup cgraph_utils |
| 5 |
*/ |
| 6 |
/************************************************************************* |
| 7 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 8 |
* All rights reserved. This program and the accompanying materials |
| 9 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 10 |
* which accompanies this distribution, and is available at |
| 11 |
* https://www.eclipse.org/legal/epl-v10.html |
| 12 |
* |
| 13 |
* Contributors: Details at https://graphviz.org |
| 14 |
*************************************************************************/ |
| 15 |
|
| 16 |
#include "config.h" |
| 17 |
|
| 18 |
#include <cgraph/cghdr.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <util/agxbuf.h> |
| 23 |
#include <util/gv_ctype.h> |
| 24 |
#include <util/gv_math.h> |
| 25 |
#include <util/streq.h> |
| 26 |
|
| 27 |
static agerrlevel_t agerrno; /* Last error level */ |
| 28 |
static agerrlevel_t agerrlevel = AGWARN; /* Report errors >= agerrlevel */ |
| 29 |
static int agmaxerr; |
| 30 |
|
| 31 |
static agxbuf last; ///< last message |
| 32 |
static agusererrf usererrf; /* User-set error function */ |
| 33 |
|
| 34 |
agusererrf agseterrf(agusererrf newf) { |
| 35 |
agusererrf oldf = usererrf; |
| 36 |
usererrf = newf; |
| 37 |
return oldf; |
| 38 |
} |
| 39 |
|
| 40 |
agerrlevel_t agseterr(agerrlevel_t lvl) { |
| 41 |
agerrlevel_t oldv = agerrlevel; |
| 42 |
agerrlevel = lvl; |
| 43 |
return oldv; |
| 44 |
} |
| 45 |
|
| 46 |
char *aglasterr(void) { |
| 47 |
// Extract a heap-allocated copy of the last message. Note that this resets |
| 48 |
// `last` to an empty buffer ready to be written to again. |
| 49 |
char *buf = agxbdisown(&last); |
| 50 |
|
| 51 |
// store the message back again so multiple calls to `aglasterr` can be made |
| 52 |
// without losing the last error |
| 53 |
agxbput(&last, buf); |
| 54 |
|
| 55 |
// was there no last message? |
| 56 |
if (streq(buf, "")) { |
| 57 |
free(buf); |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
return buf; |
| 62 |
} |
| 63 |
|
| 64 |
/// default error reporting implementation |
| 65 |
static int default_usererrf(char *message) { |
| 66 |
// `fputs`, escaping characters that may interfere with a terminal |
| 67 |
for (const char *p = message; *p != '\0'; ++p) { |
| 68 |
|
| 69 |
if (gv_iscntrl(*p) && !gv_isspace(*p)) { |
| 70 |
const int rc = fprintf(stderr, "\\%03o", (unsigned)*p); |
| 71 |
if (rc < 0) { |
| 72 |
return rc; |
| 73 |
} |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
const int rc = putc(*p, stderr); |
| 78 |
if (rc < 0) { |
| 79 |
return rc; |
| 80 |
} |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
/// Report messages using a user-supplied or default write function |
| 86 |
static void out(agerrlevel_t level, const char *fmt, va_list args) { |
| 87 |
// find out how much space we need to construct this string |
| 88 |
size_t bufsz; |
| 89 |
{ |
| 90 |
va_list args2; |
| 91 |
va_copy(args2, args); |
| 92 |
int rc = vsnprintf(NULL, 0, fmt, args2); |
| 93 |
va_end(args2); |
| 94 |
if (rc < 0) { |
| 95 |
fprintf(stderr, "%s: vsnprintf failure\n", __func__); |
| 96 |
return; |
| 97 |
} |
| 98 |
bufsz = (size_t)rc + 1; // account for NUL terminator |
| 99 |
} |
| 100 |
|
| 101 |
// allocate a buffer for the string |
| 102 |
char *buf = malloc(bufsz); |
| 103 |
if (buf == NULL) { |
| 104 |
fprintf(stderr, "%s: could not allocate memory\n", __func__); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// determine how errors are to be reported |
| 109 |
const agusererrf errf = usererrf ? usererrf : default_usererrf; |
| 110 |
|
| 111 |
if (level != AGPREV) { |
| 112 |
(void)errf(level == AGERR ? "Error" : "Warning"); |
| 113 |
(void)errf(": "); |
| 114 |
} |
| 115 |
|
| 116 |
// construct the full error in our buffer |
| 117 |
int rc = vsnprintf(buf, bufsz, fmt, args); |
| 118 |
if (rc < 0) { |
| 119 |
free(buf); |
| 120 |
fprintf(stderr, "%s: vsnprintf failure\n", __func__); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// yield our constructed error |
| 125 |
(void)errf(buf); |
| 126 |
|
| 127 |
free(buf); |
| 128 |
} |
| 129 |
|
| 130 |
static int agerr_va(agerrlevel_t level, const char *fmt, va_list args) { |
| 131 |
agerrlevel_t lvl; |
| 132 |
|
| 133 |
/* Use previous error level if continuation message; |
| 134 |
* Convert AGMAX to AGERROR; |
| 135 |
* else use input level |
| 136 |
*/ |
| 137 |
lvl = (level == AGPREV ? agerrno : (level == AGMAX) ? AGERR : level); |
| 138 |
|
| 139 |
/* store this error level */ |
| 140 |
agerrno = lvl; |
| 141 |
agmaxerr = imax(agmaxerr, (int)agerrno); |
| 142 |
|
| 143 |
/* We report all messages whose level is bigger than the user set agerrlevel |
| 144 |
* Setting agerrlevel to AGMAX turns off immediate error reporting. |
| 145 |
*/ |
| 146 |
if (lvl >= agerrlevel) { |
| 147 |
out(level, fmt, args); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
if (level != AGPREV) |
| 152 |
agxbclear(&last); |
| 153 |
vagxbprint(&last, fmt, args); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
int agerr(agerrlevel_t level, const char *fmt, ...) { |
| 158 |
va_list args; |
| 159 |
int ret; |
| 160 |
|
| 161 |
va_start(args, fmt); |
| 162 |
ret = agerr_va(level, fmt, args); |
| 163 |
va_end(args); |
| 164 |
return ret; |
| 165 |
} |
| 166 |
|
| 167 |
void agerrorf(const char *fmt, ...) { |
| 168 |
va_list args; |
| 169 |
|
| 170 |
va_start(args, fmt); |
| 171 |
agerr_va(AGERR, fmt, args); |
| 172 |
va_end(args); |
| 173 |
} |
| 174 |
|
| 175 |
void agwarningf(const char *fmt, ...) { |
| 176 |
va_list args; |
| 177 |
|
| 178 |
va_start(args, fmt); |
| 179 |
agerr_va(AGWARN, fmt, args); |
| 180 |
va_end(args); |
| 181 |
} |
| 182 |
|
| 183 |
int agerrors(void) { return agmaxerr; } |
| 184 |
|
| 185 |
int agreseterrors(void) { |
| 186 |
int rc = agmaxerr; |
| 187 |
agmaxerr = 0; |
| 188 |
return rc; |
| 189 |
} |