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