| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
/* |
| 12 |
* Glenn Fowler |
| 13 |
* AT&T Research |
| 14 |
* |
| 15 |
* expression library |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <assert.h> |
| 21 |
#include <expr/exlib.h> |
| 22 |
#include <stdarg.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <util/exit.h> |
| 27 |
|
| 28 |
/* |
| 29 |
* library error handler |
| 30 |
*/ |
| 31 |
|
| 32 |
static char *make_msg(const char *format, va_list ap) { |
| 33 |
|
| 34 |
// retrieve buffered message |
| 35 |
char buf[64]; |
| 36 |
excontext(expr.program, buf, sizeof(buf)); |
| 37 |
|
| 38 |
// how many bytes do we need to construct the message? |
| 39 |
size_t len = (size_t)snprintf(NULL, 0, "%s\n -- ", buf); |
| 40 |
{ |
| 41 |
va_list ap2; |
| 42 |
va_copy(ap2, ap); |
| 43 |
int r = vsnprintf(NULL, 0, format, ap2); |
| 44 |
va_end(ap2); |
| 45 |
if (r < 0) { |
| 46 |
return strdup("malformed format"); |
| 47 |
} |
| 48 |
len += (size_t)r + 1; // +1 for NUL |
| 49 |
} |
| 50 |
|
| 51 |
char *s = malloc(len); |
| 52 |
if (s == NULL) { |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
|
| 56 |
int offset = snprintf(s, len, "%s\n -- ", buf); |
| 57 |
assert(offset > 0); |
| 58 |
vsnprintf(s + offset, len - (size_t)offset, format, ap); |
| 59 |
|
| 60 |
return s; |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
exerror(const char* format, ...) |
| 65 |
{ |
| 66 |
if (expr.program->disc->errorf && !expr.program->errors) |
| 67 |
{ |
| 68 |
va_list ap; |
| 69 |
|
| 70 |
expr.program->errors = 1; |
| 71 |
va_start(ap, format); |
| 72 |
char *s = make_msg(format, ap); |
| 73 |
va_end(ap); |
| 74 |
expr.program->disc->errorf(expr.program, expr.program->disc, 2, "%s", |
| 75 |
s ? s : "out of space"); |
| 76 |
free(s); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
exwarn(const char *format, ...) |
| 82 |
{ |
| 83 |
if (expr.program->disc->errorf) { |
| 84 |
va_list ap; |
| 85 |
|
| 86 |
va_start(ap, format); |
| 87 |
char *s = make_msg(format, ap); |
| 88 |
va_end(ap); |
| 89 |
expr.program->disc->errorf(expr.program, expr.program->disc, |
| 90 |
ERROR_WARNING, "%s", s ? s : "out of space"); |
| 91 |
free(s); |
| 92 |
} |
| 93 |
} |