readtags.c

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