1 /*- 2 * Copyright (c) 2010 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by David A. Holland. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <assert.h> 31 #include <stdarg.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <errno.h> 36 37 #include "utils.h" 38 #include "array.h" 39 #include "place.h" 40 41 struct placefile { 42 struct place includedfrom; 43 char *dir; 44 char *name; 45 int depth; 46 bool fromsystemdir; 47 }; 48 DECLARRAY(placefile, static UNUSED); 49 DEFARRAY(placefile, static); 50 51 static struct placefilearray placefiles; 52 static bool overall_failure; 53 54 static const char *myprogname; 55 56 static FILE *debuglogfile; 57 58 //////////////////////////////////////////////////////////// 59 // placefiles 60 61 static 62 struct placefile * 63 placefile_create(const struct place *from, const char *name, 64 bool fromsystemdir) 65 { 66 struct placefile *pf; 67 const char *s; 68 size_t len; 69 70 pf = domalloc(sizeof(*pf)); 71 pf->includedfrom = *from; 72 73 s = strrchr(name, '/'); 74 len = (s == NULL) ? 0 : s - name; 75 pf->dir = dostrndup(name, len); 76 77 pf->name = dostrdup(name); 78 pf->fromsystemdir = fromsystemdir; 79 80 if (from->file != NULL) { 81 pf->depth = from->file->depth + 1; 82 } else { 83 pf->depth = 1; 84 } 85 return pf; 86 } 87 88 static 89 void 90 placefile_destroy(struct placefile *pf) 91 { 92 dostrfree(pf->name); 93 dofree(pf, sizeof(*pf)); 94 } 95 96 DESTROYALL_ARRAY(placefile, ); 97 98 const char * 99 place_getparsedir(const struct place *place) 100 { 101 if (place->file == NULL) { 102 return "."; 103 } 104 return place->file->dir; 105 } 106 107 static 108 struct placefile * 109 placefile_find(const struct place *incfrom, const char *name) 110 { 111 unsigned i, num; 112 struct placefile *pf; 113 114 num = placefilearray_num(&placefiles); 115 for (i=0; i<num; i++) { 116 pf = placefilearray_get(&placefiles, i); 117 if (place_eq(incfrom, &pf->includedfrom) && 118 !strcmp(name, pf->name)) { 119 return pf; 120 } 121 } 122 return NULL; 123 } 124 125 void 126 place_changefile(struct place *p, const char *name) 127 { 128 struct placefile *pf; 129 130 assert(p->type == P_FILE); 131 if (!strcmp(name, p->file->name)) { 132 return; 133 } 134 pf = placefile_find(&p->file->includedfrom, name); 135 if (pf == NULL) { 136 pf = placefile_create(&p->file->includedfrom, name, 137 p->file->fromsystemdir); 138 placefilearray_add(&placefiles, pf, NULL); 139 } 140 p->file = pf; 141 } 142 143 const struct placefile * 144 place_addfile(const struct place *place, const char *file, bool issystem) 145 { 146 struct placefile *pf; 147 148 pf = placefile_create(place, file, issystem); 149 placefilearray_add(&placefiles, pf, NULL); 150 if (pf->depth > 120) { 151 complain(place, "Maximum include nesting depth exceeded"); 152 die(); 153 } 154 return pf; 155 } 156 157 //////////////////////////////////////////////////////////// 158 // places 159 160 void 161 place_setnowhere(struct place *p) 162 { 163 p->type = P_NOWHERE; 164 p->file = NULL; 165 p->line = 0; 166 p->column = 0; 167 } 168 169 void 170 place_setbuiltin(struct place *p, unsigned num) 171 { 172 p->type = P_BUILTIN; 173 p->file = NULL; 174 p->line = num; 175 p->column = 1; 176 } 177 178 void 179 place_setcommandline(struct place *p, unsigned line, unsigned column) 180 { 181 p->type = P_COMMANDLINE; 182 p->file = NULL; 183 p->line = line; 184 p->column = column; 185 } 186 187 void 188 place_setfilestart(struct place *p, const struct placefile *pf) 189 { 190 p->type = P_FILE; 191 p->file = pf; 192 p->line = 1; 193 p->column = 1; 194 } 195 196 void 197 place_addcolumns(struct place *p, unsigned cols) 198 { 199 unsigned newcol; 200 201 newcol = p->column + cols; 202 if (newcol < p->column) { 203 /* overflow (use the old place to complain) */ 204 complain(p, "Column numbering overflow"); 205 die(); 206 } 207 p->column = newcol; 208 } 209 210 void 211 place_addlines(struct place *p, unsigned lines) 212 { 213 unsigned nextline; 214 215 nextline = p->line + lines; 216 if (nextline < p->line) { 217 /* overflow (use the old place to complain) */ 218 complain(p, "Line numbering overflow"); 219 die(); 220 } 221 p->line = nextline; 222 } 223 224 const char * 225 place_getname(const struct place *p) 226 { 227 switch (p->type) { 228 case P_NOWHERE: return "<nowhere>"; 229 case P_BUILTIN: return "<built-in>"; 230 case P_COMMANDLINE: return "<command-line>"; 231 case P_FILE: return p->file->name; 232 } 233 assert(0); 234 return NULL; 235 } 236 237 bool 238 place_samefile(const struct place *a, const struct place *b) 239 { 240 if (a->type != b->type) { 241 return false; 242 } 243 if (a->file != b->file) { 244 return false; 245 } 246 return true; 247 } 248 249 bool 250 place_eq(const struct place *a, const struct place *b) 251 { 252 if (!place_samefile(a, b)) { 253 return false; 254 } 255 if (a->line != b->line || a->column != b->column) { 256 return false; 257 } 258 return true; 259 } 260 261 static 262 void 263 place_printfrom(const struct place *p) 264 { 265 const struct place *from; 266 267 if (p->file == NULL) { 268 return; 269 } 270 from = &p->file->includedfrom; 271 if (from->type != P_NOWHERE) { 272 place_printfrom(from); 273 fprintf(stderr, "In file included from %s:%u:%u:\n", 274 place_getname(from), from->line, from->column); 275 } 276 } 277 278 //////////////////////////////////////////////////////////// 279 // complaints 280 281 void 282 complain_init(const char *pn) 283 { 284 myprogname = pn; 285 } 286 287 void 288 complain(const struct place *p, const char *fmt, ...) 289 { 290 va_list ap; 291 292 if (p != NULL) { 293 place_printfrom(p); 294 fprintf(stderr, "%s:%u:%u: ", place_getname(p), 295 p->line, p->column); 296 } else { 297 fprintf(stderr, "%s: ", myprogname); 298 } 299 va_start(ap, fmt); 300 vfprintf(stderr, fmt, ap); 301 va_end(ap); 302 fprintf(stderr, "\n"); 303 } 304 305 void 306 complain_fail(void) 307 { 308 overall_failure = true; 309 } 310 311 bool 312 complain_failed(void) 313 { 314 return overall_failure; 315 } 316 317 //////////////////////////////////////////////////////////// 318 // debug logging 319 320 void 321 debuglog_open(const struct place *p, /*const*/ char *file) 322 { 323 assert(debuglogfile == NULL); 324 debuglogfile = fopen(file, "w"); 325 if (debuglogfile == NULL) { 326 complain(p, "%s: %s", file, strerror(errno)); 327 die(); 328 } 329 } 330 331 void 332 debuglog_close(void) 333 { 334 if (debuglogfile != NULL) { 335 fclose(debuglogfile); 336 debuglogfile = NULL; 337 } 338 } 339 340 PF(2, 3) void 341 debuglog(const struct place *p, const char *fmt, ...) 342 { 343 va_list ap; 344 345 if (debuglogfile == NULL) { 346 return; 347 } 348 349 fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line); 350 va_start(ap, fmt); 351 vfprintf(debuglogfile, fmt, ap); 352 va_end(ap); 353 fprintf(debuglogfile, "\n"); 354 fflush(debuglogfile); 355 } 356 357 PF(3, 4) void 358 debuglog2(const struct place *p, const struct place *p2, const char *fmt, ...) 359 { 360 va_list ap; 361 362 if (debuglogfile == NULL) { 363 return; 364 } 365 366 fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line); 367 if (place_samefile(p, p2)) { 368 fprintf(debuglogfile, "(block began at line %u) ", 369 p2->line); 370 } else { 371 fprintf(debuglogfile, "(block began at %s:%u)", 372 place_getname(p2), p2->line); 373 } 374 va_start(ap, fmt); 375 vfprintf(debuglogfile, fmt, ap); 376 va_end(ap); 377 fprintf(debuglogfile, "\n"); 378 fflush(debuglogfile); 379 } 380 381 //////////////////////////////////////////////////////////// 382 // module init and cleanup 383 384 void 385 place_init(void) 386 { 387 placefilearray_init(&placefiles); 388 } 389 390 void 391 place_cleanup(void) 392 { 393 placefilearray_destroyall(&placefiles); 394 placefilearray_cleanup(&placefiles); 395 } 396