| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <stdio.h> | ||
| 14 | #include <stdarg.h> | ||
| 15 | |||
| 16 | #include <netlink.h> | ||
| 17 | #include <gmputil.h> | ||
| 18 | #include <erec.h> | ||
| 19 | |||
| 20 | static const struct input_descriptor internal_indesc = { | ||
| 21 | .type = INDESC_INTERNAL, | ||
| 22 | .name = "internal", | ||
| 23 | }; | ||
| 24 | |||
| 25 | const struct location internal_location = { | ||
| 26 | .indesc = &internal_indesc, | ||
| 27 | }; | ||
| 28 | |||
| 29 | static const char * const error_record_names[] = { | ||
| 30 | [EREC_INFORMATIONAL] = NULL, | ||
| 31 | [EREC_WARNING] = "Warning", | ||
| 32 | [EREC_ERROR] = "Error" | ||
| 33 | }; | ||
| 34 | |||
| 35 | 3336 | void erec_add_location(struct error_record *erec, const struct location *loc) | |
| 36 | { | ||
| 37 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 3336 times.
|
3336 | assert(erec->num_locations < EREC_LOCATIONS_MAX); |
| 38 | 3336 | erec->locations[erec->num_locations] = *loc; | |
| 39 | 6672 | erec->locations[erec->num_locations].indesc = loc->indesc ? | |
| 40 |
2/2✓ Branch 0 (4→5) taken 3333 times.
✓ Branch 1 (4→6) taken 3 times.
|
3336 | : &internal_indesc; |
| 41 | 3336 | erec->num_locations++; | |
| 42 | 3336 | } | |
| 43 | |||
| 44 | 3251 | void erec_destroy(struct error_record *erec) | |
| 45 | { | ||
| 46 | 3251 | free(erec->msg); | |
| 47 | 3251 | free(erec); | |
| 48 | 3251 | } | |
| 49 | |||
| 50 | __attribute__((format(printf, 3, 0))) | ||
| 51 | 3251 | struct error_record *erec_vcreate(enum error_record_types type, | |
| 52 | const struct location *loc, | ||
| 53 | const char *fmt, va_list ap) | ||
| 54 | { | ||
| 55 | struct error_record *erec; | ||
| 56 | |||
| 57 | 3251 | erec = xmalloc(sizeof(*erec)); | |
| 58 | 3251 | erec->type = type; | |
| 59 | 3251 | erec->num_locations = 0; | |
| 60 | 3251 | erec_add_location(erec, loc); | |
| 61 | |||
| 62 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 3251 times.
|
3251 | if (vasprintf(&erec->msg, fmt, ap) < 0) |
| 63 | ✗ | erec->msg = NULL; | |
| 64 | |||
| 65 | 3251 | return erec; | |
| 66 | } | ||
| 67 | |||
| 68 | __attribute__((format(printf, 3, 4))) | ||
| 69 | 451 | struct error_record *erec_create(enum error_record_types type, | |
| 70 | const struct location *loc, | ||
| 71 | const char *fmt, ...) | ||
| 72 | { | ||
| 73 | struct error_record *erec; | ||
| 74 | va_list ap; | ||
| 75 | |||
| 76 | 451 | va_start(ap, fmt); | |
| 77 | 451 | erec = erec_vcreate(type, loc, fmt, ap); | |
| 78 | 451 | va_end(ap); | |
| 79 | 451 | return erec; | |
| 80 | } | ||
| 81 | |||
| 82 | 3328 | void print_location(FILE *f, const struct input_descriptor *indesc, | |
| 83 | const struct location *loc) | ||
| 84 | { | ||
| 85 | const struct input_descriptor *tmp; | ||
| 86 | const struct location *iloc; | ||
| 87 | |||
| 88 |
2/2✓ Branch 0 (2→3) taken 154 times.
✓ Branch 1 (2→8) taken 3174 times.
|
3328 | if (indesc->location.indesc != NULL) { |
| 89 | 154 | const char *prefix = "In file included from"; | |
| 90 | 154 | iloc = &indesc->location; | |
| 91 | 154 | for (tmp = iloc->indesc; | |
| 92 |
3/4✓ Branch 0 (6→7) taken 177 times.
✗ Branch 1 (6→8) not taken.
✓ Branch 2 (7→4) taken 23 times.
✓ Branch 3 (7→8) taken 154 times.
|
177 | tmp != NULL && tmp->type != INDESC_INTERNAL; |
| 93 | 23 | tmp = iloc->indesc) { | |
| 94 | 23 | fprintf(f, "%s %s:%u:%u-%u:\n", prefix, | |
| 95 | 23 | tmp->name, | |
| 96 | 23 | iloc->first_line, iloc->first_column, | |
| 97 | 23 | iloc->last_column); | |
| 98 | 23 | prefix = " from"; | |
| 99 | 23 | iloc = &tmp->location; | |
| 100 | } | ||
| 101 | } | ||
| 102 |
3/4✓ Branch 0 (8→9) taken 476 times.
✓ Branch 1 (8→11) taken 2852 times.
✓ Branch 2 (9→10) taken 476 times.
✗ Branch 3 (9→11) not taken.
|
3328 | if (indesc->type != INDESC_BUFFER && indesc->name) { |
| 103 | 476 | fprintf(f, "%s:%u:%u-%u: ", indesc->name, | |
| 104 | 476 | loc->first_line, loc->first_column, | |
| 105 | 476 | loc->last_column); | |
| 106 | } | ||
| 107 | 3328 | } | |
| 108 | |||
| 109 | 154 | const char *line_location(const struct input_descriptor *indesc, | |
| 110 | const struct location *loc, char *buf, size_t bufsiz) | ||
| 111 | { | ||
| 112 | 154 | const char *line = NULL; | |
| 113 | FILE *f; | ||
| 114 | |||
| 115 | 154 | f = fopen(indesc->name, "r"); | |
| 116 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 154 times.
|
154 | if (!f) |
| 117 | ✗ | return NULL; | |
| 118 | |||
| 119 |
2/4✓ Branch 0 (6→7) taken 154 times.
✗ Branch 1 (6→10) not taken.
✓ Branch 2 (8→9) taken 154 times.
✗ Branch 3 (8→10) not taken.
|
308 | if (!fseek(f, loc->line_offset, SEEK_SET) && |
| 120 | 154 | fread(buf, 1, bufsiz - 1, f) > 0) { | |
| 121 | 154 | *strchrnul(buf, '\n') = '\0'; | |
| 122 | 154 | line = buf; | |
| 123 | } | ||
| 124 | 154 | fclose(f); | |
| 125 | |||
| 126 | 154 | return line; | |
| 127 | } | ||
| 128 | |||
| 129 | 3232 | void erec_print(struct output_ctx *octx, const struct error_record *erec, | |
| 130 | unsigned int debug_mask) | ||
| 131 | { | ||
| 132 | 3232 | const struct location *loc = erec->locations; | |
| 133 | 3232 | const struct input_descriptor *indesc = loc->indesc; | |
| 134 | 3232 | const char *line = NULL; | |
| 135 | 3232 | char buf[1024] = {}; | |
| 136 | 3232 | char *pbuf = NULL; | |
| 137 | unsigned int i, end; | ||
| 138 | FILE *f; | ||
| 139 | int l; | ||
| 140 | |||
| 141 |
4/5✓ Branch 0 (2→3) taken 2852 times.
✓ Branch 1 (2→4) taken 135 times.
✓ Branch 2 (2→5) taken 154 times.
✓ Branch 3 (2→6) taken 91 times.
✗ Branch 4 (2→7) not taken.
|
3232 | switch (indesc->type) { |
| 142 | 2852 | case INDESC_BUFFER: | |
| 143 | case INDESC_CLI: | ||
| 144 | 2852 | line = indesc->data; | |
| 145 | 2852 | *strchrnul(line, '\n') = '\0'; | |
| 146 | 2852 | break; | |
| 147 | 135 | case INDESC_STDIN: | |
| 148 | 135 | line = indesc->data; | |
| 149 | 135 | line += loc->line_offset; | |
| 150 | 135 | *strchrnul(line, '\n') = '\0'; | |
| 151 | 135 | break; | |
| 152 | 154 | case INDESC_FILE: | |
| 153 | 154 | line = line_location(indesc, loc, buf, sizeof(buf)); | |
| 154 | 154 | break; | |
| 155 | 91 | case INDESC_INTERNAL: | |
| 156 | case INDESC_NETLINK: | ||
| 157 | 91 | break; | |
| 158 | ✗ | default: | |
| 159 | ✗ | BUG("invalid input descriptor type %u", indesc->type); | |
| 160 | } | ||
| 161 | |||
| 162 | 3232 | f = octx->error_fp; | |
| 163 | |||
| 164 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→22) taken 3232 times.
|
3232 | if (indesc->type == INDESC_NETLINK) { |
| 165 | ✗ | fprintf(f, "%s: ", indesc->name); | |
| 166 | ✗ | if (error_record_names[erec->type]) | |
| 167 | ✗ | fprintf(f, "%s: ", error_record_names[erec->type]); | |
| 168 | ✗ | fprintf(f, "%s\n", erec->msg); | |
| 169 | ✗ | for (l = 0; l < (int)erec->num_locations; l++) { | |
| 170 | ✗ | loc = &erec->locations[l]; | |
| 171 | ✗ | if (!loc->nle) | |
| 172 | ✗ | continue; | |
| 173 | ✗ | netlink_dump_expr(loc->nle, f, debug_mask); | |
| 174 | } | ||
| 175 | ✗ | return; | |
| 176 | } | ||
| 177 | |||
| 178 | 3232 | print_location(f, indesc, loc); | |
| 179 | |||
| 180 |
2/2✓ Branch 0 (23→24) taken 3190 times.
✓ Branch 1 (23→25) taken 42 times.
|
3232 | if (error_record_names[erec->type]) |
| 181 | 3190 | fprintf(f, "%s: ", error_record_names[erec->type]); | |
| 182 | 3232 | fprintf(f, "%s\n", erec->msg); | |
| 183 | |||
| 184 |
2/2✓ Branch 0 (26→27) taken 3141 times.
✓ Branch 1 (26→49) taken 91 times.
|
3232 | if (line) { |
| 185 | 3141 | fprintf(f, "%s\n", line); | |
| 186 | |||
| 187 | 3141 | end = 0; | |
| 188 |
2/2✓ Branch 0 (30→29) taken 3226 times.
✓ Branch 1 (30→31) taken 3141 times.
|
6367 | for (l = erec->num_locations - 1; l >= 0; l--) { |
| 189 | 3226 | loc = &erec->locations[l]; | |
| 190 | 3226 | end = max(end, loc->last_column); | |
| 191 | } | ||
| 192 | 3141 | pbuf = xmalloc(end + 1); | |
| 193 | 3141 | memset(pbuf, ' ', end + 1); | |
| 194 |
4/4✓ Branch 0 (36→37) taken 277027 times.
✓ Branch 1 (36→38) taken 2962 times.
✓ Branch 2 (37→33) taken 276848 times.
✓ Branch 3 (37→38) taken 179 times.
|
279989 | for (i = 0; i < end && line[i]; i++) { |
| 195 |
2/2✓ Branch 0 (33→34) taken 1782 times.
✓ Branch 1 (33→35) taken 275066 times.
|
276848 | if (line[i] == '\t') |
| 196 | 1782 | pbuf[i] = '\t'; | |
| 197 | } | ||
| 198 |
2/2✓ Branch 0 (46→39) taken 3226 times.
✓ Branch 1 (46→47) taken 3141 times.
|
6367 | for (l = erec->num_locations - 1; l >= 0; l--) { |
| 199 | 3226 | loc = &erec->locations[l]; | |
| 200 | 3226 | for (i = loc->first_column ? loc->first_column - 1 : 0; | |
| 201 |
2/2✓ Branch 0 (44→40) taken 59642 times.
✓ Branch 1 (44→45) taken 3226 times.
|
62868 | i < loc->last_column; i++) |
| 202 |
2/2✓ Branch 0 (40→41) taken 864 times.
✓ Branch 1 (40→42) taken 58778 times.
|
59642 | pbuf[i] = l ? '~' : '^'; |
| 203 | } | ||
| 204 | 3141 | pbuf[end] = '\0'; | |
| 205 | 3141 | fprintf(f, "%s", pbuf); | |
| 206 | 3141 | free(pbuf); | |
| 207 | } | ||
| 208 | 3232 | fprintf(f, "\n"); | |
| 209 | } | ||
| 210 | |||
| 211 | 56359 | void erec_print_list(struct output_ctx *octx, struct list_head *list, | |
| 212 | unsigned int debug_mask) | ||
| 213 | { | ||
| 214 | struct error_record *erec, *next; | ||
| 215 | |||
| 216 |
2/2✓ Branch 0 (7→3) taken 3190 times.
✓ Branch 1 (7→8) taken 56359 times.
|
59549 | list_for_each_entry_safe(erec, next, list, list) { |
| 217 | 3190 | list_del(&erec->list); | |
| 218 | 3190 | erec_print(octx, erec, debug_mask); | |
| 219 | 3190 | erec_destroy(erec); | |
| 220 | } | ||
| 221 | 56359 | } | |
| 222 | |||
| 223 | − | int __fmtstring(4, 5) __stmt_binary_error(struct eval_ctx *ctx, | |
| 224 | const struct location *l1, | ||
| 225 | const struct location *l2, | ||
| 226 | const char *fmt, ...) | ||
| 227 | { | ||
| 228 | struct error_record *erec; | ||
| 229 | va_list ap; | ||
| 230 | |||
| 231 | − | va_start(ap, fmt); | |
| 232 | − | erec = erec_vcreate(EREC_ERROR, l1, fmt, ap); | |
| 233 | − | if (l2 != NULL) | |
| 234 | − | erec_add_location(erec, l2); | |
| 235 | − | va_end(ap); | |
| 236 | − | erec_queue(erec, ctx->msgs); | |
| 237 | − | return -1; | |
| 238 | } | ||
| 239 |