| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : readtags.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
/* |
| 26 |
* $Id: readtags.c,v 1.1 2005/11/21 18:00:11 eran Exp $ |
| 27 |
* |
| 28 |
* Copyright (c) 1996-2003, Darren Hiebert |
| 29 |
* |
| 30 |
* This source code is released into the public domain. |
| 31 |
* |
| 32 |
* This module contains functions for reading tag files. |
| 33 |
*/ |
| 34 |
|
| 35 |
/* |
| 36 |
* INCLUDE FILES |
| 37 |
*/ |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <ctype.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <errno.h> |
| 43 |
#include <sys/types.h> /* to declare off_t */ |
| 44 |
|
| 45 |
#include "readtags.h" |
| 46 |
|
| 47 |
/* |
| 48 |
* MACROS |
| 49 |
*/ |
| 50 |
#define TAB '\t' |
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
* DATA DECLARATIONS |
| 55 |
*/ |
| 56 |
typedef struct { |
| 57 |
size_t size; |
| 58 |
char *buffer; |
| 59 |
} vstring; |
| 60 |
|
| 61 |
/** Information about current tag file */ |
| 62 |
struct sTagFile { |
| 63 |
/** has the file been opened and this structure initialized? */ |
| 64 |
short initialized; |
| 65 |
/** format of tag file */ |
| 66 |
short format; |
| 67 |
/** how is the tag file sorted? */ |
| 68 |
sortType sortMethod; |
| 69 |
/** pointer to file structure */ |
| 70 |
FILE* fp; |
| 71 |
/** file position of first character of `line' */ |
| 72 |
off_t pos; |
| 73 |
/** size of tag file in seekable positions */ |
| 74 |
off_t size; |
| 75 |
/** last line read */ |
| 76 |
vstring line; |
| 77 |
/** name of tag in last line read */ |
| 78 |
vstring name; |
| 79 |
/** defines tag search state */ |
| 80 |
struct { |
| 81 |
/** file position of last match for tag */ |
| 82 |
off_t pos; |
| 83 |
/** name of tag last searched for */ |
| 84 |
const char *name; |
| 85 |
/** length of name for partial matches */ |
| 86 |
size_t nameLength; |
| 87 |
/** peforming partial match */ |
| 88 |
short partial; |
| 89 |
/** ignoring case */ |
| 90 |
short ignorecase; |
| 91 |
} search; |
| 92 |
/** miscellaneous extension fields */ |
| 93 |
struct { |
| 94 |
/** number of entries in `list' */ |
| 95 |
unsigned short max; |
| 96 |
/** list of key value pairs */ |
| 97 |
tagExtensionField *list; |
| 98 |
} fields; |
| 99 |
/** buffers to be freed at close */ |
| 100 |
struct { |
| 101 |
/** name of program author */ |
| 102 |
char *author; |
| 103 |
/** name of program */ |
| 104 |
char *name; |
| 105 |
/** URL of distribution */ |
| 106 |
char *url; |
| 107 |
/** program version */ |
| 108 |
char *version; |
| 109 |
} program; |
| 110 |
}; |
| 111 |
|
| 112 |
/* |
| 113 |
* DATA DEFINITIONS |
| 114 |
*/ |
| 115 |
const char *const EmptyString = ""; |
| 116 |
const char *const PseudoTagPrefix = "!_"; |
| 117 |
|
| 118 |
/* |
| 119 |
* FUNCTION DEFINITIONS |
| 120 |
*/ |
| 121 |
|
| 122 |
/** |
| 123 |
* Compare two strings, ignoring case. |
| 124 |
* Return 0 for match, < 0 for smaller, > 0 for bigger |
| 125 |
* Make sure case is folded to uppercase in comparison (like for 'sort -f') |
| 126 |
* This makes a difference when one of the chars lies between upper and lower |
| 127 |
* ie. one of the chars [ \ ] ^ _ ` for ascii. (The '_' in particular !) |
| 128 |
*/ |
| 129 |
static int struppercmp (const char *s1, const char *s2) |
| 130 |
{ |
| 131 |
int result; |
| 132 |
do |
| 133 |
{ |
| 134 |
result = toupper ((int) *s1) - toupper ((int) *s2); |
| 135 |
} while (result == 0 && *s1++ != '\0' && *s2++ != '\0'); |
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
static int strnuppercmp (const char *s1, const char *s2, size_t n) |
| 140 |
{ |
| 141 |
int result; |
| 142 |
do |
| 143 |
{ |
| 144 |
result = toupper ((int) *s1) - toupper ((int) *s2); |
| 145 |
} while (result == 0 && --n > 0 && *s1++ != '\0' && *s2++ != '\0'); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
static int growString (vstring *s) |
| 150 |
{ |
| 151 |
int result = 0; |
| 152 |
size_t newLength; |
| 153 |
char *newLine; |
| 154 |
if (s->size == 0) |
| 155 |
{ |
| 156 |
newLength = 128; |
| 157 |
newLine = (char*) malloc (newLength); |
| 158 |
*newLine = '\0'; |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
newLength = 2 * s->size; |
| 163 |
newLine = (char*) realloc (s->buffer, newLength); |
| 164 |
} |
| 165 |
if (newLine == NULL) |
| 166 |
perror ("string too large"); |
| 167 |
else |
| 168 |
{ |
| 169 |
s->buffer = newLine; |
| 170 |
s->size = newLength; |
| 171 |
result = 1; |
| 172 |
} |
| 173 |
return result; |
| 174 |
} |
| 175 |
|
| 176 |
/** Copy name of tag out of tag line */ |
| 177 |
static void copyName (tagFile *const file) |
| 178 |
{ |
| 179 |
size_t length; |
| 180 |
const char *end = strchr (file->line.buffer, '\t'); |
| 181 |
if (end == NULL) |
| 182 |
{ |
| 183 |
end = strchr (file->line.buffer, '\n'); |
| 184 |
if (end == NULL) |
| 185 |
end = strchr (file->line.buffer, '\r'); |
| 186 |
} |
| 187 |
if (end != NULL) |
| 188 |
length = end - file->line.buffer; |
| 189 |
else |
| 190 |
length = strlen (file->line.buffer); |
| 191 |
while (length >= file->name.size) |
| 192 |
growString (&file->name); |
| 193 |
strncpy (file->name.buffer, file->line.buffer, length); |
| 194 |
file->name.buffer [length] = '\0'; |
| 195 |
} |
| 196 |
|
| 197 |
static int readTagLineRaw (tagFile *const file) |
| 198 |
{ |
| 199 |
int result = 1; |
| 200 |
int reReadLine; |
| 201 |
|
| 202 |
/* If reading the line places any character other than a null or a |
| 203 |
* newline at the last character position in the buffer (one less than |
| 204 |
* the buffer size), then we must resize the buffer and reattempt to read |
| 205 |
* the line. |
| 206 |
*/ |
| 207 |
do |
| 208 |
{ |
| 209 |
char *const pLastChar = file->line.buffer + file->line.size - 2; |
| 210 |
char *line; |
| 211 |
|
| 212 |
file->pos = ftell (file->fp); |
| 213 |
reReadLine = 0; |
| 214 |
*pLastChar = '\0'; |
| 215 |
line = fgets (file->line.buffer, (int) file->line.size, file->fp); |
| 216 |
if (line == NULL) |
| 217 |
{ |
| 218 |
/* read error */ |
| 219 |
if (! feof (file->fp)) |
| 220 |
perror ("readTagLine"); |
| 221 |
result = 0; |
| 222 |
} |
| 223 |
else if (*pLastChar != '\0' && |
| 224 |
*pLastChar != '\n' && *pLastChar != '\r') |
| 225 |
{ |
| 226 |
/* buffer overflow */ |
| 227 |
growString (&file->line); |
| 228 |
fseek (file->fp, file->pos, SEEK_SET); |
| 229 |
reReadLine = 1; |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
size_t i = strlen (file->line.buffer); |
| 234 |
while (i > 0 && |
| 235 |
(file->line.buffer [i - 1] == '\n' || file->line.buffer [i - 1] == '\r')) |
| 236 |
{ |
| 237 |
file->line.buffer [i - 1] = '\0'; |
| 238 |
--i; |
| 239 |
} |
| 240 |
} |
| 241 |
} while (reReadLine && result); |
| 242 |
if (result) |
| 243 |
copyName (file); |
| 244 |
return result; |
| 245 |
} |
| 246 |
|
| 247 |
static int readTagLine (tagFile *const file) |
| 248 |
{ |
| 249 |
int result; |
| 250 |
do |
| 251 |
{ |
| 252 |
result = readTagLineRaw (file); |
| 253 |
} while (result && *file->name.buffer == '\0'); |
| 254 |
return result; |
| 255 |
} |
| 256 |
|
| 257 |
static tagResult growFields (tagFile *const file) |
| 258 |
{ |
| 259 |
tagResult result = TagFailure; |
| 260 |
unsigned short newCount = 2 * file->fields.max; |
| 261 |
tagExtensionField *newFields = (tagExtensionField*) |
| 262 |
realloc (file->fields.list, newCount * sizeof (tagExtensionField)); |
| 263 |
if (newFields == NULL) |
| 264 |
perror ("too many extension fields"); |
| 265 |
else |
| 266 |
{ |
| 267 |
file->fields.list = newFields; |
| 268 |
file->fields.max = newCount; |
| 269 |
result = TagSuccess; |
| 270 |
} |
| 271 |
return result; |
| 272 |
} |
| 273 |
|
| 274 |
static void parseExtensionFields (tagFile *const file, tagEntry *const entry, |
| 275 |
char *const string) |
| 276 |
{ |
| 277 |
char *p = string; |
| 278 |
while (p != NULL && *p != '\0') |
| 279 |
{ |
| 280 |
while (*p == TAB) |
| 281 |
*p++ = '\0'; |
| 282 |
if (*p != '\0') |
| 283 |
{ |
| 284 |
char *colon; |
| 285 |
char *field = p; |
| 286 |
p = strchr (p, TAB); |
| 287 |
if (p != NULL) |
| 288 |
*p++ = '\0'; |
| 289 |
colon = strchr (field, ':'); |
| 290 |
if (colon == NULL) |
| 291 |
entry->kind = field; |
| 292 |
else |
| 293 |
{ |
| 294 |
const char *key = field; |
| 295 |
const char *value = colon + 1; |
| 296 |
*colon = '\0'; |
| 297 |
if (strcmp (key, "kind") == 0) |
| 298 |
entry->kind = value; |
| 299 |
else if (strcmp (key, "file") == 0) |
| 300 |
entry->fileScope = 1; |
| 301 |
else if (strcmp (key, "line") == 0) |
| 302 |
entry->address.lineNumber = atol (value); |
| 303 |
else |
| 304 |
{ |
| 305 |
if (entry->fields.count == file->fields.max) |
| 306 |
growFields (file); |
| 307 |
file->fields.list [entry->fields.count].key = key; |
| 308 |
file->fields.list [entry->fields.count].value = value; |
| 309 |
++entry->fields.count; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
static void parseTagLine (tagFile *file, tagEntry *const entry) |
| 317 |
{ |
| 318 |
int i; |
| 319 |
char *p = file->line.buffer; |
| 320 |
char *tab = strchr (p, TAB); |
| 321 |
int fieldsPresent = 0; |
| 322 |
|
| 323 |
entry->fields.list = NULL; |
| 324 |
entry->fields.count = 0; |
| 325 |
entry->kind = NULL; |
| 326 |
entry->fileScope = 0; |
| 327 |
|
| 328 |
entry->name = p; |
| 329 |
if (tab != NULL) |
| 330 |
{ |
| 331 |
*tab = '\0'; |
| 332 |
p = tab + 1; |
| 333 |
entry->file = p; |
| 334 |
tab = strchr (p, TAB); |
| 335 |
if (tab != NULL) |
| 336 |
{ |
| 337 |
*tab = '\0'; |
| 338 |
p = tab + 1; |
| 339 |
if (*p == '/' || *p == '?') |
| 340 |
{ |
| 341 |
/* parse pattern */ |
| 342 |
int delimiter = *(unsigned char*) p; |
| 343 |
entry->address.lineNumber = 0; |
| 344 |
entry->address.pattern = p; |
| 345 |
do |
| 346 |
{ |
| 347 |
p = strchr (p + 1, delimiter); |
| 348 |
} while (p != NULL && *(p - 1) == '\\'); |
| 349 |
if (p == NULL) |
| 350 |
{ |
| 351 |
/* invalid pattern */ |
| 352 |
} |
| 353 |
else |
| 354 |
++p; |
| 355 |
} |
| 356 |
else if (isdigit ((int) *(unsigned char*) p)) |
| 357 |
{ |
| 358 |
/* parse line number */ |
| 359 |
entry->address.pattern = p; |
| 360 |
entry->address.lineNumber = atol (p); |
| 361 |
while (isdigit ((int) *(unsigned char*) p)) |
| 362 |
++p; |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
/* invalid pattern */ |
| 367 |
} |
| 368 |
fieldsPresent = (strncmp (p, ";\"", 2) == 0); |
| 369 |
*p = '\0'; |
| 370 |
if (fieldsPresent) |
| 371 |
parseExtensionFields (file, entry, p + 2); |
| 372 |
} |
| 373 |
} |
| 374 |
if (entry->fields.count > 0) |
| 375 |
entry->fields.list = file->fields.list; |
| 376 |
for (i = entry->fields.count ; i < file->fields.max ; ++i) |
| 377 |
{ |
| 378 |
file->fields.list [i].key = NULL; |
| 379 |
file->fields.list [i].value = NULL; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
static char *duplicate (const char *str) |
| 384 |
{ |
| 385 |
char *result = NULL; |
| 386 |
if (str != NULL) |
| 387 |
{ |
| 388 |
result = (char*) malloc (strlen (str) + 1); |
| 389 |
if (result == NULL) |
| 390 |
perror (NULL); |
| 391 |
else |
| 392 |
strcpy (result, str); |
| 393 |
} |
| 394 |
return result; |
| 395 |
} |
| 396 |
|
| 397 |
static void readPseudoTags (tagFile *const file, tagFileInfo *const info) |
| 398 |
{ |
| 399 |
fpos_t startOfLine; |
| 400 |
const size_t prefixLength = strlen (PseudoTagPrefix); |
| 401 |
if (info != NULL) |
| 402 |
{ |
| 403 |
info->file.format = 1; |
| 404 |
info->file.sort = TAG_UNSORTED; |
| 405 |
info->program.author = NULL; |
| 406 |
info->program.name = NULL; |
| 407 |
info->program.url = NULL; |
| 408 |
info->program.version = NULL; |
| 409 |
} |
| 410 |
for( ;; ) |
| 411 |
{ |
| 412 |
fgetpos (file->fp, &startOfLine); |
| 413 |
if (! readTagLine (file)) |
| 414 |
break; |
| 415 |
if (strncmp (file->line.buffer, PseudoTagPrefix, prefixLength) != 0) |
| 416 |
break; |
| 417 |
else |
| 418 |
{ |
| 419 |
tagEntry entry; |
| 420 |
const char *key, *value; |
| 421 |
parseTagLine (file, &entry); |
| 422 |
key = entry.name + prefixLength; |
| 423 |
value = entry.file; |
| 424 |
if (strcmp (key, "TAG_FILE_SORTED") == 0) |
| 425 |
file->sortMethod = (sortType) atoi (value); |
| 426 |
else if (strcmp (key, "TAG_FILE_FORMAT") == 0) |
| 427 |
file->format = static_cast<short>(atoi (value)); |
| 428 |
else if (strcmp (key, "TAG_PROGRAM_AUTHOR") == 0) |
| 429 |
file->program.author = duplicate (value); |
| 430 |
else if (strcmp (key, "TAG_PROGRAM_NAME") == 0) |
| 431 |
file->program.name = duplicate (value); |
| 432 |
else if (strcmp (key, "TAG_PROGRAM_URL") == 0) |
| 433 |
file->program.url = duplicate (value); |
| 434 |
else if (strcmp (key, "TAG_PROGRAM_VERSION") == 0) |
| 435 |
file->program.version = duplicate (value); |
| 436 |
if (info != NULL) |
| 437 |
{ |
| 438 |
info->file.format = file->format; |
| 439 |
info->file.sort = file->sortMethod; |
| 440 |
info->program.author = file->program.author; |
| 441 |
info->program.name = file->program.name; |
| 442 |
info->program.url = file->program.url; |
| 443 |
info->program.version = file->program.version; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
fsetpos (file->fp, &startOfLine); |
| 448 |
} |
| 449 |
|
| 450 |
static void gotoFirstLogicalTag (tagFile *const file) |
| 451 |
{ |
| 452 |
fpos_t startOfLine; |
| 453 |
const size_t prefixLength = strlen (PseudoTagPrefix); |
| 454 |
rewind (file->fp); |
| 455 |
for ( ;; ) |
| 456 |
{ |
| 457 |
fgetpos (file->fp, &startOfLine); |
| 458 |
if (! readTagLine (file)) |
| 459 |
break; |
| 460 |
if (strncmp (file->line.buffer, PseudoTagPrefix, prefixLength) != 0) |
| 461 |
break; |
| 462 |
} |
| 463 |
fsetpos (file->fp, &startOfLine); |
| 464 |
} |
| 465 |
|
| 466 |
static tagFile *initialize (const char *const filePath, tagFileInfo *const info) |
| 467 |
{ |
| 468 |
tagFile *result = (tagFile*) malloc (sizeof (tagFile)); |
| 469 |
if (result != NULL) |
| 470 |
{ |
| 471 |
memset (result, 0, sizeof (tagFile)); |
| 472 |
growString (&result->line); |
| 473 |
growString (&result->name); |
| 474 |
result->fields.max = 20; |
| 475 |
result->fields.list = (tagExtensionField*) malloc ( |
| 476 |
result->fields.max * sizeof (tagExtensionField)); |
| 477 |
result->fp = fopen (filePath, "r"); |
| 478 |
if (result->fp == NULL) |
| 479 |
{ |
| 480 |
free (result); |
| 481 |
result = NULL; |
| 482 |
if(info) { |
| 483 |
info->status.error_number = errno; |
| 484 |
} |
| 485 |
} |
| 486 |
else |
| 487 |
{ |
| 488 |
fseek (result->fp, 0, SEEK_END); |
| 489 |
result->size = ftell (result->fp); |
| 490 |
rewind (result->fp); |
| 491 |
if(info) { |
| 492 |
readPseudoTags (result, info); |
| 493 |
info->status.opened = 1; |
| 494 |
} |
| 495 |
result->initialized = 1; |
| 496 |
} |
| 497 |
} |
| 498 |
return result; |
| 499 |
} |
| 500 |
|
| 501 |
static void terminate (tagFile *const file) |
| 502 |
{ |
| 503 |
fclose (file->fp); |
| 504 |
|
| 505 |
free (file->line.buffer); |
| 506 |
free (file->name.buffer); |
| 507 |
free (file->fields.list); |
| 508 |
|
| 509 |
if (file->program.author != NULL) |
| 510 |
free (file->program.author); |
| 511 |
if (file->program.name != NULL) |
| 512 |
free (file->program.name); |
| 513 |
if (file->program.url != NULL) |
| 514 |
free (file->program.url); |
| 515 |
if (file->program.version != NULL) |
| 516 |
free (file->program.version); |
| 517 |
|
| 518 |
memset (file, 0, sizeof (tagFile)); |
| 519 |
|
| 520 |
free (file); |
| 521 |
} |
| 522 |
|
| 523 |
static tagResult readNext (tagFile *const file, tagEntry *const entry) |
| 524 |
{ |
| 525 |
tagResult result = TagFailure; |
| 526 |
if (file == NULL || ! file->initialized) |
| 527 |
result = TagFailure; |
| 528 |
else if (! readTagLine (file)) |
| 529 |
result = TagFailure; |
| 530 |
else |
| 531 |
{ |
| 532 |
if (entry != NULL) |
| 533 |
parseTagLine (file, entry); |
| 534 |
result = TagSuccess; |
| 535 |
} |
| 536 |
return result; |
| 537 |
} |
| 538 |
|
| 539 |
static const char *readFieldValue ( |
| 540 |
const tagEntry *const entry, const char *const key) |
| 541 |
{ |
| 542 |
const char *result = NULL; |
| 543 |
int i; |
| 544 |
if (strcmp (key, "kind") == 0) |
| 545 |
result = entry->kind; |
| 546 |
else if (strcmp (key, "file") == 0) |
| 547 |
result = EmptyString; |
| 548 |
else for (i = 0 ; i < entry->fields.count && result == NULL ; ++i) |
| 549 |
if (strcmp (entry->fields.list [i].key, key) == 0) |
| 550 |
result = entry->fields.list [i].value; |
| 551 |
return result; |
| 552 |
} |
| 553 |
|
| 554 |
static int readTagLineSeek (tagFile *const file, const off_t pos) |
| 555 |
{ |
| 556 |
int result = 0; |
| 557 |
if (fseek (file->fp, pos, SEEK_SET) == 0) |
| 558 |
{ |
| 559 |
result = readTagLine (file); /* read probable partial line */ |
| 560 |
if (pos > 0 && result) |
| 561 |
result = readTagLine (file); /* read complete line */ |
| 562 |
} |
| 563 |
return result; |
| 564 |
} |
| 565 |
|
| 566 |
static int nameComparison (tagFile *const file) |
| 567 |
{ |
| 568 |
int result; |
| 569 |
if (file->search.ignorecase) |
| 570 |
{ |
| 571 |
if (file->search.partial) |
| 572 |
result = strnuppercmp (file->search.name, file->name.buffer, |
| 573 |
file->search.nameLength); |
| 574 |
else |
| 575 |
result = struppercmp (file->search.name, file->name.buffer); |
| 576 |
} |
| 577 |
else |
| 578 |
{ |
| 579 |
if (file->search.partial) |
| 580 |
result = strncmp (file->search.name, file->name.buffer, |
| 581 |
file->search.nameLength); |
| 582 |
else |
| 583 |
result = strcmp (file->search.name, file->name.buffer); |
| 584 |
} |
| 585 |
return result; |
| 586 |
} |
| 587 |
|
| 588 |
static void findFirstNonMatchBefore (tagFile *const file) |
| 589 |
{ |
| 590 |
#define JUMP_BACK 512 |
| 591 |
int more_lines; |
| 592 |
int comp; |
| 593 |
off_t start = file->pos; |
| 594 |
off_t pos = start; |
| 595 |
do |
| 596 |
{ |
| 597 |
if (pos < (off_t) JUMP_BACK) |
| 598 |
pos = 0; |
| 599 |
else |
| 600 |
pos = pos - JUMP_BACK; |
| 601 |
more_lines = readTagLineSeek (file, pos); |
| 602 |
comp = nameComparison (file); |
| 603 |
} while (more_lines && comp == 0 && pos > 0 && pos < start); |
| 604 |
} |
| 605 |
|
| 606 |
static tagResult findFirstMatchBefore (tagFile *const file) |
| 607 |
{ |
| 608 |
tagResult result = TagFailure; |
| 609 |
int more_lines; |
| 610 |
off_t start = file->pos; |
| 611 |
findFirstNonMatchBefore (file); |
| 612 |
do |
| 613 |
{ |
| 614 |
more_lines = readTagLine (file); |
| 615 |
if (nameComparison (file) == 0) |
| 616 |
result = TagSuccess; |
| 617 |
} while (more_lines && result != TagSuccess && file->pos < start); |
| 618 |
return result; |
| 619 |
} |
| 620 |
|
| 621 |
static tagResult findBinary (tagFile *const file) |
| 622 |
{ |
| 623 |
tagResult result = TagFailure; |
| 624 |
off_t lower_limit = 0; |
| 625 |
off_t upper_limit = file->size; |
| 626 |
off_t last_pos = 0; |
| 627 |
off_t pos = upper_limit / 2; |
| 628 |
while (result != TagSuccess) |
| 629 |
{ |
| 630 |
if (! readTagLineSeek (file, pos)) |
| 631 |
{ |
| 632 |
/* in case we fell off end of file */ |
| 633 |
result = findFirstMatchBefore (file); |
| 634 |
break; |
| 635 |
} |
| 636 |
else if (pos == last_pos) |
| 637 |
{ |
| 638 |
/* prevent infinite loop if we backed up to beginning of file */ |
| 639 |
break; |
| 640 |
} |
| 641 |
else |
| 642 |
{ |
| 643 |
const int comp = nameComparison (file); |
| 644 |
last_pos = pos; |
| 645 |
if (comp < 0) |
| 646 |
{ |
| 647 |
upper_limit = pos; |
| 648 |
pos = lower_limit + ((upper_limit - lower_limit) / 2); |
| 649 |
} |
| 650 |
else if (comp > 0) |
| 651 |
{ |
| 652 |
lower_limit = pos; |
| 653 |
pos = lower_limit + ((upper_limit - lower_limit) / 2); |
| 654 |
} |
| 655 |
else if (pos == 0) |
| 656 |
result = TagSuccess; |
| 657 |
else |
| 658 |
result = findFirstMatchBefore (file); |
| 659 |
} |
| 660 |
} |
| 661 |
return result; |
| 662 |
} |
| 663 |
|
| 664 |
static tagResult findSequential (tagFile *const file) |
| 665 |
{ |
| 666 |
tagResult result = TagFailure; |
| 667 |
if (file->initialized) |
| 668 |
{ |
| 669 |
while (result == TagFailure && readTagLine (file)) |
| 670 |
{ |
| 671 |
if (nameComparison (file) == 0) |
| 672 |
result = TagSuccess; |
| 673 |
} |
| 674 |
} |
| 675 |
return result; |
| 676 |
} |
| 677 |
|
| 678 |
static tagResult find (tagFile *const file, tagEntry *const entry, |
| 679 |
const char *const name, const int options) |
| 680 |
{ |
| 681 |
tagResult result = TagFailure; |
| 682 |
file->search.name = name; |
| 683 |
file->search.nameLength = strlen (name); |
| 684 |
file->search.partial = (options & TAG_PARTIALMATCH) != 0; |
| 685 |
file->search.ignorecase = (options & TAG_IGNORECASE) != 0; |
| 686 |
fseek (file->fp, 0, SEEK_END); |
| 687 |
file->size = ftell (file->fp); |
| 688 |
rewind (file->fp); |
| 689 |
if ((file->sortMethod == TAG_SORTED && !file->search.ignorecase) || |
| 690 |
(file->sortMethod == TAG_FOLDSORTED && file->search.ignorecase)) |
| 691 |
{ |
| 692 |
#ifdef DEBUG |
| 693 |
printf ("<performing binary search>\n"); |
| 694 |
#endif |
| 695 |
result = findBinary (file); |
| 696 |
} |
| 697 |
else |
| 698 |
{ |
| 699 |
#ifdef DEBUG |
| 700 |
printf ("<performing sequential search>\n"); |
| 701 |
#endif |
| 702 |
result = findSequential (file); |
| 703 |
} |
| 704 |
|
| 705 |
if (result != TagSuccess) |
| 706 |
file->search.pos = file->size; |
| 707 |
else |
| 708 |
{ |
| 709 |
file->search.pos = file->pos; |
| 710 |
if (entry != NULL) |
| 711 |
parseTagLine (file, entry); |
| 712 |
} |
| 713 |
return result; |
| 714 |
} |
| 715 |
|
| 716 |
static tagResult findNext (tagFile *const file, tagEntry *const entry) |
| 717 |
{ |
| 718 |
tagResult result = TagFailure; |
| 719 |
if ((file->sortMethod == TAG_SORTED && !file->search.ignorecase) || |
| 720 |
(file->sortMethod == TAG_FOLDSORTED && file->search.ignorecase)) |
| 721 |
{ |
| 722 |
result = tagsNext (file, entry); |
| 723 |
if (result == TagSuccess && nameComparison (file) != 0) |
| 724 |
result = TagFailure; |
| 725 |
} |
| 726 |
else |
| 727 |
{ |
| 728 |
result = findSequential (file); |
| 729 |
if (result == TagSuccess && entry != NULL) |
| 730 |
parseTagLine (file, entry); |
| 731 |
} |
| 732 |
return result; |
| 733 |
} |
| 734 |
|
| 735 |
/* |
| 736 |
* EXTERNAL INTERFACE |
| 737 |
*/ |
| 738 |
|
| 739 |
extern tagFile *tagsOpen (const char *const filePath, tagFileInfo *const info) |
| 740 |
{ |
| 741 |
return initialize (filePath, info); |
| 742 |
} |
| 743 |
|
| 744 |
extern tagResult tagsSetSortType (tagFile *const file, const sortType type) |
| 745 |
{ |
| 746 |
tagResult result = TagFailure; |
| 747 |
if (file != NULL && file->initialized) |
| 748 |
{ |
| 749 |
file->sortMethod = type; |
| 750 |
result = TagSuccess; |
| 751 |
} |
| 752 |
return result; |
| 753 |
} |
| 754 |
|
| 755 |
extern tagResult tagsFirst (tagFile *const file, tagEntry *const entry) |
| 756 |
{ |
| 757 |
tagResult result = TagFailure; |
| 758 |
if (file != NULL && file->initialized) |
| 759 |
{ |
| 760 |
gotoFirstLogicalTag (file); |
| 761 |
result = readNext (file, entry); |
| 762 |
} |
| 763 |
return result; |
| 764 |
} |
| 765 |
|
| 766 |
extern tagResult tagsNext (tagFile *const file, tagEntry *const entry) |
| 767 |
{ |
| 768 |
tagResult result = TagFailure; |
| 769 |
if (file != NULL && file->initialized) |
| 770 |
result = readNext (file, entry); |
| 771 |
return result; |
| 772 |
} |
| 773 |
|
| 774 |
extern const char *tagsField (const tagEntry *const entry, const char *const key) |
| 775 |
{ |
| 776 |
const char *result = NULL; |
| 777 |
if (entry != NULL) |
| 778 |
result = readFieldValue (entry, key); |
| 779 |
return result; |
| 780 |
} |
| 781 |
|
| 782 |
extern tagResult tagsFind (tagFile *const file, tagEntry *const entry, |
| 783 |
const char *const name, const int options) |
| 784 |
{ |
| 785 |
tagResult result = TagFailure; |
| 786 |
if (file != NULL && file->initialized) |
| 787 |
result = find (file, entry, name, options); |
| 788 |
return result; |
| 789 |
} |
| 790 |
|
| 791 |
extern tagResult tagsFindNext (tagFile *const file, tagEntry *const entry) |
| 792 |
{ |
| 793 |
tagResult result = TagFailure; |
| 794 |
if (file != NULL && file->initialized) |
| 795 |
result = findNext (file, entry); |
| 796 |
return result; |
| 797 |
} |
| 798 |
|
| 799 |
extern tagResult tagsClose (tagFile *const file) |
| 800 |
{ |
| 801 |
tagResult result = TagFailure; |
| 802 |
if (file != NULL && file->initialized) |
| 803 |
{ |
| 804 |
terminate (file); |
| 805 |
result = TagSuccess; |
| 806 |
} |
| 807 |
return result; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
/* |
| 812 |
* TEST FRAMEWORK |
| 813 |
*/ |
| 814 |
|
| 815 |
#ifdef READTAGS_MAIN |
| 816 |
|
| 817 |
|
| 818 |
static const char *TagFileName = "tags"; |
| 819 |
static const char *ProgramName; |
| 820 |
static int extensionFields; |
| 821 |
static int SortOverride; |
| 822 |
static sortType SortMethod; |
| 823 |
|
| 824 |
static void printTag (const tagEntry *entry) |
| 825 |
{ |
| 826 |
int i; |
| 827 |
int first = 1; |
| 828 |
const char* separator = ";\""; |
| 829 |
const char* const empty = ""; |
| 830 |
/* "sep" returns a value only the first time it is evaluated */ |
| 831 |
#define sep (first ? (first = 0, separator) : empty) |
| 832 |
printf ("%s\t%s\t%s", |
| 833 |
entry->name, entry->file, entry->address.pattern); |
| 834 |
if (extensionFields) |
| 835 |
{ |
| 836 |
if (entry->kind != NULL && entry->kind [0] != '\0') |
| 837 |
printf ("%s\tkind:%s", sep, entry->kind); |
| 838 |
if (entry->fileScope) |
| 839 |
printf ("%s\tfile:", sep); |
| 840 |
#if 0 |
| 841 |
if (entry->address.lineNumber > 0) |
| 842 |
printf ("%s\tline:%lu", sep, entry->address.lineNumber); |
| 843 |
#endif |
| 844 |
for (i = 0 ; i < entry->fields.count ; ++i) |
| 845 |
printf ("%s\t%s:%s", sep, entry->fields.list [i].key, |
| 846 |
entry->fields.list [i].value); |
| 847 |
} |
| 848 |
putchar ('\n'); |
| 849 |
#undef sep |
| 850 |
} |
| 851 |
|
| 852 |
static void findTag (const char *const name, const int options) |
| 853 |
{ |
| 854 |
tagFileInfo info; |
| 855 |
tagEntry entry; |
| 856 |
tagFile *const file = tagsOpen (TagFileName, &info); |
| 857 |
if (file == NULL) |
| 858 |
{ |
| 859 |
fprintf (stderr, "%s: cannot open tag file: %s: %s\n", |
| 860 |
ProgramName, strerror (info.status.error_number), name); |
| 861 |
exit (1); |
| 862 |
} |
| 863 |
else |
| 864 |
{ |
| 865 |
if (SortOverride) |
| 866 |
tagsSetSortType (file, SortMethod); |
| 867 |
if (tagsFind (file, &entry, name, options) == TagSuccess) |
| 868 |
{ |
| 869 |
do |
| 870 |
{ |
| 871 |
printTag (&entry); |
| 872 |
} while (tagsFindNext (file, &entry) == TagSuccess); |
| 873 |
} |
| 874 |
tagsClose (file); |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
static void listTags (void) |
| 879 |
{ |
| 880 |
tagFileInfo info; |
| 881 |
tagEntry entry; |
| 882 |
tagFile *const file = tagsOpen (TagFileName, &info); |
| 883 |
if (file == NULL) |
| 884 |
{ |
| 885 |
fprintf (stderr, "%s: cannot open tag file: %s: %s\n", |
| 886 |
ProgramName, strerror (info.status.error_number), TagFileName); |
| 887 |
exit (1); |
| 888 |
} |
| 889 |
else |
| 890 |
{ |
| 891 |
while (tagsNext (file, &entry) == TagSuccess) |
| 892 |
printTag (&entry); |
| 893 |
tagsClose (file); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
const char *const Usage = |
| 898 |
"Find tag file entries matching specified names.\n\n" |
| 899 |
"Usage: %s [-ilp] [-s[0|1]] [-t file] [name(s)]\n\n" |
| 900 |
"Options:\n" |
| 901 |
" -e Include extension fields in output.\n" |
| 902 |
" -i Perform case-insensitive matching.\n" |
| 903 |
" -l List all tags.\n" |
| 904 |
" -p Perform partial matching.\n" |
| 905 |
" -s[0|1|2] Override sort detection of tag file.\n" |
| 906 |
" -t file Use specified tag file (default: \"tags\").\n" |
| 907 |
"Note that options are acted upon as encountered, so order is significant.\n"; |
| 908 |
|
| 909 |
extern int main (int argc, char **argv) |
| 910 |
{ |
| 911 |
int options = 0; |
| 912 |
int actionSupplied = 0; |
| 913 |
int i; |
| 914 |
ProgramName = argv [0]; |
| 915 |
if (argc == 1) |
| 916 |
{ |
| 917 |
fprintf (stderr, Usage, ProgramName); |
| 918 |
exit (1); |
| 919 |
} |
| 920 |
for (i = 1 ; i < argc ; ++i) |
| 921 |
{ |
| 922 |
const char *const arg = argv [i]; |
| 923 |
if (arg [0] != '-') |
| 924 |
{ |
| 925 |
findTag (arg, options); |
| 926 |
actionSupplied = 1; |
| 927 |
} |
| 928 |
else |
| 929 |
{ |
| 930 |
size_t j; |
| 931 |
for (j = 1 ; arg [j] != '\0' ; ++j) |
| 932 |
{ |
| 933 |
switch (arg [j]) |
| 934 |
{ |
| 935 |
case 'e': extensionFields = 1; break; |
| 936 |
case 'i': options |= TAG_IGNORECASE; break; |
| 937 |
case 'p': options |= TAG_PARTIALMATCH; break; |
| 938 |
case 'l': listTags (); actionSupplied = 1; break; |
| 939 |
|
| 940 |
case 't': |
| 941 |
if (arg [j+1] != '\0') |
| 942 |
{ |
| 943 |
TagFileName = arg + j + 1; |
| 944 |
j += strlen (TagFileName); |
| 945 |
} |
| 946 |
else if (i + 1 < argc) |
| 947 |
TagFileName = argv [++i]; |
| 948 |
else |
| 949 |
{ |
| 950 |
fprintf (stderr, Usage, ProgramName); |
| 951 |
exit (1); |
| 952 |
} |
| 953 |
break; |
| 954 |
case 's': |
| 955 |
SortOverride = 1; |
| 956 |
++j; |
| 957 |
if (arg [j] == '\0') |
| 958 |
SortMethod = TAG_SORTED; |
| 959 |
else if (strchr ("012", arg[j]) != NULL) |
| 960 |
SortMethod = (sortType) (arg[j] - '0'); |
| 961 |
else |
| 962 |
{ |
| 963 |
fprintf (stderr, Usage, ProgramName); |
| 964 |
exit (1); |
| 965 |
} |
| 966 |
break; |
| 967 |
default: |
| 968 |
fprintf (stderr, "%s: unknown option: %c\n", |
| 969 |
ProgramName, arg[j]); |
| 970 |
exit (1); |
| 971 |
break; |
| 972 |
} |
| 973 |
} |
| 974 |
} |
| 975 |
} |
| 976 |
if (! actionSupplied) |
| 977 |
{ |
| 978 |
fprintf (stderr, |
| 979 |
"%s: no action specified: specify tag name(s) or -l option\n", |
| 980 |
ProgramName); |
| 981 |
exit (1); |
| 982 |
} |
| 983 |
return 0; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
#endif |
| 988 |
|
| 989 |
/* vi:set tabstop=8 shiftwidth=4: */ |