Updated Spanish translation
[anjuta-git-plugin.git] / tagmanager / readtags.c
blobbfbcf95408dcc348998e77d78fa6f0424fc77fd0
1 /*
2 * $Id$
4 * Copyright (c) 1996-2003, Darren Hiebert
6 * This source code is released into the public domain.
8 * This module contains functions for reading tag files.
9 */
12 * INCLUDE FILES
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 */
21 #include "readtags.h"
24 * MACROS
26 #define TAB '\t'
30 * DATA DECLARATIONS
32 typedef struct {
33 size_t size;
34 char *buffer;
35 } vstring;
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 const 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;
89 * DATA DEFINITIONS
91 const char *const EmptyString = "";
92 const char *const PseudoTagPrefix = "!_";
95 * FUNCTION DEFINITIONS
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 !)
105 static int struppercmp (const char *s1, const char *s2)
107 int result;
110 result = toupper ((int) *s1) - toupper ((int) *s2);
111 } while (result == 0 && *s1++ != '\0' && *s2++ != '\0');
112 return result;
115 static int strnuppercmp (const char *s1, const char *s2, size_t n)
117 int result;
120 result = toupper ((int) *s1) - toupper ((int) *s2);
121 } while (result == 0 && --n > 0 && *s1++ != '\0' && *s2++ != '\0');
122 return result;
125 static int growString (vstring *s)
127 int result = 0;
128 size_t newLength;
129 char *newLine;
130 if (s->size == 0)
132 newLength = 128;
133 newLine = (char*) malloc (newLength);
134 *newLine = '\0';
136 else
138 newLength = 2 * s->size;
139 newLine = (char*) realloc (s->buffer, newLength);
141 if (newLine == NULL)
142 perror ("string too large");
143 else
145 s->buffer = newLine;
146 s->size = newLength;
147 result = 1;
149 return result;
152 /* Copy name of tag out of tag line */
153 static void copyName (tagFile *const file)
155 size_t length;
156 const char *end = strchr (file->line.buffer, '\t');
157 if (end == NULL)
159 end = strchr (file->line.buffer, '\n');
160 if (end == NULL)
161 end = strchr (file->line.buffer, '\r');
163 if (end != NULL)
164 length = end - file->line.buffer;
165 else
166 length = strlen (file->line.buffer);
167 while (length >= file->name.size)
168 growString (&file->name);
169 strncpy (file->name.buffer, file->line.buffer, length);
170 file->name.buffer [length] = '\0';
173 static int readTagLineRaw (tagFile *const file)
175 int result = 1;
176 int reReadLine;
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.
185 char *const pLastChar = file->line.buffer + file->line.size - 2;
186 char *line;
188 file->pos = ftell (file->fp);
189 reReadLine = 0;
190 *pLastChar = '\0';
191 line = fgets (file->line.buffer, (int) file->line.size, file->fp);
192 if (line == NULL)
194 /* read error */
195 if (! feof (file->fp))
196 perror ("readTagLine");
197 result = 0;
199 else if (*pLastChar != '\0' &&
200 *pLastChar != '\n' && *pLastChar != '\r')
202 /* buffer overflow */
203 growString (&file->line);
204 fseek (file->fp, file->pos, SEEK_SET);
205 reReadLine = 1;
207 else
209 size_t i = strlen (file->line.buffer);
210 while (i > 0 &&
211 (file->line.buffer [i - 1] == '\n' || file->line.buffer [i - 1] == '\r'))
213 file->line.buffer [i - 1] = '\0';
214 --i;
217 } while (reReadLine && result);
218 if (result)
219 copyName (file);
220 return result;
223 static int readTagLine (tagFile *const file)
225 int result;
228 result = readTagLineRaw (file);
229 } while (result && *file->name.buffer == '\0');
230 return result;
233 static tagResult growFields (tagFile *const file)
235 tagResult result = TagFailure;
236 unsigned short newCount = 2 * file->fields.max;
237 tagExtensionField *newFields = (tagExtensionField*)
238 realloc (file->fields.list, newCount * sizeof (tagExtensionField));
239 if (newFields == NULL)
240 perror ("too many extension fields");
241 else
243 file->fields.list = newFields;
244 file->fields.max = newCount;
245 result = TagSuccess;
247 return result;
250 static void parseExtensionFields (tagFile *const file, tagEntry *const entry,
251 char *const string)
253 char *p = string;
254 while (p != NULL && *p != '\0')
256 while (*p == TAB)
257 *p++ = '\0';
258 if (*p != '\0')
260 char *colon;
261 char *field = p;
262 p = strchr (p, TAB);
263 if (p != NULL)
264 *p++ = '\0';
265 colon = strchr (field, ':');
266 if (colon == NULL)
267 entry->kind = field;
268 else
270 const char *key = field;
271 const char *value = colon + 1;
272 *colon = '\0';
273 if (strcmp (key, "kind") == 0)
274 entry->kind = value;
275 else if (strcmp (key, "file") == 0)
276 entry->fileScope = 1;
277 else if (strcmp (key, "line") == 0)
278 entry->address.lineNumber = atol (value);
279 else
281 if (entry->fields.count == file->fields.max)
282 growFields (file);
283 file->fields.list [entry->fields.count].key = key;
284 file->fields.list [entry->fields.count].value = value;
285 ++entry->fields.count;
292 static void parseTagLine (tagFile *file, tagEntry *const entry)
294 int i;
295 char *p = file->line.buffer;
296 char *tab = strchr (p, TAB);
297 int fieldsPresent = 0;
299 entry->fields.list = NULL;
300 entry->fields.count = 0;
301 entry->kind = NULL;
302 entry->fileScope = 0;
304 entry->name = p;
305 if (tab != NULL)
307 *tab = '\0';
308 p = tab + 1;
309 entry->file = p;
310 tab = strchr (p, TAB);
311 if (tab != NULL)
313 *tab = '\0';
314 p = tab + 1;
315 if (*p == '/' || *p == '?')
317 /* parse pattern */
318 int delimiter = *(unsigned char*) p;
319 entry->address.lineNumber = 0;
320 entry->address.pattern = p;
323 p = strchr (p + 1, delimiter);
324 } while (p != NULL && *(p - 1) == '\\');
325 if (p == NULL)
327 /* invalid pattern */
329 else
330 ++p;
332 else if (isdigit ((int) *(unsigned char*) p))
334 /* parse line number */
335 entry->address.pattern = p;
336 entry->address.lineNumber = atol (p);
337 while (isdigit ((int) *(unsigned char*) p))
338 ++p;
340 else
342 /* invalid pattern */
344 fieldsPresent = (strncmp (p, ";\"", 2) == 0);
345 *p = '\0';
346 if (fieldsPresent)
347 parseExtensionFields (file, entry, p + 2);
350 if (entry->fields.count > 0)
351 entry->fields.list = file->fields.list;
352 for (i = entry->fields.count ; i < file->fields.max ; ++i)
354 file->fields.list [i].key = NULL;
355 file->fields.list [i].value = NULL;
359 static char *duplicate (const char *str)
361 char *result = NULL;
362 if (str != NULL)
364 result = (char*) malloc (strlen (str) + 1);
365 if (result == NULL)
366 perror (NULL);
367 else
368 strcpy (result, str);
370 return result;
373 static void readPseudoTags (tagFile *const file, tagFileInfo *const info)
375 fpos_t startOfLine;
376 const size_t prefixLength = strlen (PseudoTagPrefix);
377 if (info != NULL)
379 info->file.format = 1;
380 info->file.sort = TAG_UNSORTED;
381 info->program.author = NULL;
382 info->program.name = NULL;
383 info->program.url = NULL;
384 info->program.version = NULL;
386 while (1)
388 fgetpos (file->fp, &startOfLine);
389 if (! readTagLine (file))
390 break;
391 if (strncmp (file->line.buffer, PseudoTagPrefix, prefixLength) != 0)
392 break;
393 else
395 tagEntry entry;
396 const char *key, *value;
397 parseTagLine (file, &entry);
398 key = entry.name + prefixLength;
399 value = entry.file;
400 if (strcmp (key, "TAG_FILE_SORTED") == 0)
401 file->sortMethod = (sortType) atoi (value);
402 else if (strcmp (key, "TAG_FILE_FORMAT") == 0)
403 file->format = atoi (value);
404 else if (strcmp (key, "TAG_PROGRAM_AUTHOR") == 0)
405 file->program.author = duplicate (value);
406 else if (strcmp (key, "TAG_PROGRAM_NAME") == 0)
407 file->program.name = duplicate (value);
408 else if (strcmp (key, "TAG_PROGRAM_URL") == 0)
409 file->program.url = duplicate (value);
410 else if (strcmp (key, "TAG_PROGRAM_VERSION") == 0)
411 file->program.version = duplicate (value);
412 if (info != NULL)
414 info->file.format = file->format;
415 info->file.sort = file->sortMethod;
416 info->program.author = file->program.author;
417 info->program.name = file->program.name;
418 info->program.url = file->program.url;
419 info->program.version = file->program.version;
423 fsetpos (file->fp, &startOfLine);
426 static void gotoFirstLogicalTag (tagFile *const file)
428 fpos_t startOfLine;
429 const size_t prefixLength = strlen (PseudoTagPrefix);
430 rewind (file->fp);
431 while (1)
433 fgetpos (file->fp, &startOfLine);
434 if (! readTagLine (file))
435 break;
436 if (strncmp (file->line.buffer, PseudoTagPrefix, prefixLength) != 0)
437 break;
439 fsetpos (file->fp, &startOfLine);
442 static tagFile *initialize (const char *const filePath, tagFileInfo *const info)
444 tagFile *result = (tagFile*) malloc (sizeof (tagFile));
445 if (result != NULL)
447 memset (result, 0, sizeof (tagFile));
448 growString (&result->line);
449 growString (&result->name);
450 result->fields.max = 20;
451 result->fields.list = (tagExtensionField*) malloc (
452 result->fields.max * sizeof (tagExtensionField));
453 result->fp = fopen (filePath, "r");
454 if (result->fp == NULL)
456 free (result);
457 result = NULL;
458 info->status.error_number = errno;
460 else
462 fseek (result->fp, 0, SEEK_END);
463 result->size = ftell (result->fp);
464 rewind (result->fp);
465 readPseudoTags (result, info);
466 info->status.opened = 1;
467 result->initialized = 1;
470 return result;
473 static void terminate (tagFile *const file)
475 fclose (file->fp);
477 free (file->line.buffer);
478 free (file->name.buffer);
479 free (file->fields.list);
481 if (file->program.author != NULL)
482 free (file->program.author);
483 if (file->program.name != NULL)
484 free (file->program.name);
485 if (file->program.url != NULL)
486 free (file->program.url);
487 if (file->program.version != NULL)
488 free (file->program.version);
490 memset (file, 0, sizeof (tagFile));
492 free (file);
495 static tagResult readNext (tagFile *const file, tagEntry *const entry)
497 tagResult result = TagFailure;
498 if (file == NULL || ! file->initialized)
499 result = TagFailure;
500 else if (! readTagLine (file))
501 result = TagFailure;
502 else
504 if (entry != NULL)
505 parseTagLine (file, entry);
506 result = TagSuccess;
508 return result;
511 static const char *readFieldValue (
512 const tagEntry *const entry, const char *const key)
514 const char *result = NULL;
515 int i;
516 if (strcmp (key, "kind") == 0)
517 result = entry->kind;
518 else if (strcmp (key, "file") == 0)
519 result = EmptyString;
520 else for (i = 0 ; i < entry->fields.count && result == NULL ; ++i)
521 if (strcmp (entry->fields.list [i].key, key) == 0)
522 result = entry->fields.list [i].value;
523 return result;
526 static int readTagLineSeek (tagFile *const file, const off_t pos)
528 int result = 0;
529 if (fseek (file->fp, pos, SEEK_SET) == 0)
531 result = readTagLine (file); /* read probable partial line */
532 if (pos > 0 && result)
533 result = readTagLine (file); /* read complete line */
535 return result;
538 static int nameComparison (tagFile *const file)
540 int result;
541 if (file->search.ignorecase)
543 if (file->search.partial)
544 result = strnuppercmp (file->search.name, file->name.buffer,
545 file->search.nameLength);
546 else
547 result = struppercmp (file->search.name, file->name.buffer);
549 else
551 if (file->search.partial)
552 result = strncmp (file->search.name, file->name.buffer,
553 file->search.nameLength);
554 else
555 result = strcmp (file->search.name, file->name.buffer);
557 return result;
560 static void findFirstNonMatchBefore (tagFile *const file)
562 #define JUMP_BACK 512
563 int more_lines;
564 int comp;
565 off_t start = file->pos;
566 off_t pos = start;
569 if (pos < (off_t) JUMP_BACK)
570 pos = 0;
571 else
572 pos = pos - JUMP_BACK;
573 more_lines = readTagLineSeek (file, pos);
574 comp = nameComparison (file);
575 } while (more_lines && comp == 0 && pos > 0 && pos < start);
578 static tagResult findFirstMatchBefore (tagFile *const file)
580 tagResult result = TagFailure;
581 int more_lines;
582 off_t start = file->pos;
583 findFirstNonMatchBefore (file);
586 more_lines = readTagLine (file);
587 if (nameComparison (file) == 0)
588 result = TagSuccess;
589 } while (more_lines && result != TagSuccess && file->pos < start);
590 return result;
593 static tagResult findBinary (tagFile *const file)
595 tagResult result = TagFailure;
596 off_t lower_limit = 0;
597 off_t upper_limit = file->size;
598 off_t last_pos = 0;
599 off_t pos = upper_limit / 2;
600 while (result != TagSuccess)
602 if (! readTagLineSeek (file, pos))
604 /* in case we fell off end of file */
605 result = findFirstMatchBefore (file);
606 break;
608 else if (pos == last_pos)
610 /* prevent infinite loop if we backed up to beginning of file */
611 break;
613 else
615 const int comp = nameComparison (file);
616 last_pos = pos;
617 if (comp < 0)
619 upper_limit = pos;
620 pos = lower_limit + ((upper_limit - lower_limit) / 2);
622 else if (comp > 0)
624 lower_limit = pos;
625 pos = lower_limit + ((upper_limit - lower_limit) / 2);
627 else if (pos == 0)
628 result = TagSuccess;
629 else
630 result = findFirstMatchBefore (file);
633 return result;
636 static tagResult findSequential (tagFile *const file)
638 tagResult result = TagFailure;
639 if (file->initialized)
641 while (result == TagFailure && readTagLine (file))
643 if (nameComparison (file) == 0)
644 result = TagSuccess;
647 return result;
650 static tagResult find (tagFile *const file, tagEntry *const entry,
651 const char *const name, const int options)
653 tagResult result = TagFailure;
654 file->search.name = name;
655 file->search.nameLength = strlen (name);
656 file->search.partial = (options & TAG_PARTIALMATCH) != 0;
657 file->search.ignorecase = (options & TAG_IGNORECASE) != 0;
658 fseek (file->fp, 0, SEEK_END);
659 file->size = ftell (file->fp);
660 rewind (file->fp);
661 if ((file->sortMethod == TAG_SORTED && !file->search.ignorecase) ||
662 (file->sortMethod == TAG_FOLDSORTED && file->search.ignorecase))
664 #ifdef DEBUG
665 printf ("<performing binary search>\n");
666 #endif
667 result = findBinary (file);
669 else
671 #ifdef DEBUG
672 printf ("<performing sequential search>\n");
673 #endif
674 result = findSequential (file);
677 if (result != TagSuccess)
678 file->search.pos = file->size;
679 else
681 file->search.pos = file->pos;
682 if (entry != NULL)
683 parseTagLine (file, entry);
685 return result;
688 static tagResult findNext (tagFile *const file, tagEntry *const entry)
690 tagResult result = TagFailure;
691 if ((file->sortMethod == TAG_SORTED && !file->search.ignorecase) ||
692 (file->sortMethod == TAG_FOLDSORTED && file->search.ignorecase))
694 result = tagsNext (file, entry);
695 if (result == TagSuccess && nameComparison (file) != 0)
696 result = TagFailure;
698 else
700 result = findSequential (file);
701 if (result == TagSuccess && entry != NULL)
702 parseTagLine (file, entry);
704 return result;
708 * EXTERNAL INTERFACE
711 extern tagFile *tagsOpen (const char *const filePath, tagFileInfo *const info)
713 return initialize (filePath, info);
716 extern tagResult tagsSetSortType (tagFile *const file, const sortType type)
718 tagResult result = TagFailure;
719 if (file != NULL && file->initialized)
721 file->sortMethod = type;
722 result = TagSuccess;
724 return result;
727 extern tagResult tagsFirst (tagFile *const file, tagEntry *const entry)
729 tagResult result = TagFailure;
730 if (file != NULL && file->initialized)
732 gotoFirstLogicalTag (file);
733 result = readNext (file, entry);
735 return result;
738 extern tagResult tagsNext (tagFile *const file, tagEntry *const entry)
740 tagResult result = TagFailure;
741 if (file != NULL && file->initialized)
742 result = readNext (file, entry);
743 return result;
746 extern const char *tagsField (const tagEntry *const entry, const char *const key)
748 const char *result = NULL;
749 if (entry != NULL)
750 result = readFieldValue (entry, key);
751 return result;
754 extern tagResult tagsFind (tagFile *const file, tagEntry *const entry,
755 const char *const name, const int options)
757 tagResult result = TagFailure;
758 if (file != NULL && file->initialized)
759 result = find (file, entry, name, options);
760 return result;
763 extern tagResult tagsFindNext (tagFile *const file, tagEntry *const entry)
765 tagResult result = TagFailure;
766 if (file != NULL && file->initialized)
767 result = findNext (file, entry);
768 return result;
771 extern tagResult tagsClose (tagFile *const file)
773 tagResult result = TagFailure;
774 if (file != NULL && file->initialized)
776 terminate (file);
777 result = TagSuccess;
779 return result;
783 * TEST FRAMEWORK
786 #ifdef READTAGS_MAIN
788 static const char *TagFileName = "tags";
789 static const char *ProgramName;
790 static int extensionFields;
791 static int SortOverride;
792 static sortType SortMethod;
794 static void printTag (const tagEntry *entry)
796 int i;
797 int first = 1;
798 const char* separator = ";\"";
799 const char* const empty = "";
800 /* "sep" returns a value only the first time it is evaluated */
801 #define sep (first ? (first = 0, separator) : empty)
802 printf ("%s\t%s\t%s",
803 entry->name, entry->file, entry->address.pattern);
804 if (extensionFields)
806 if (entry->kind != NULL && entry->kind [0] != '\0')
807 printf ("%s\tkind:%s", sep, entry->kind);
808 if (entry->fileScope)
809 printf ("%s\tfile:", sep);
810 #if 0
811 if (entry->address.lineNumber > 0)
812 printf ("%s\tline:%lu", sep, entry->address.lineNumber);
813 #endif
814 for (i = 0 ; i < entry->fields.count ; ++i)
815 printf ("%s\t%s:%s", sep, entry->fields.list [i].key,
816 entry->fields.list [i].value);
818 putchar ('\n');
819 #undef sep
822 static void findTag (const char *const name, const int options)
824 tagFileInfo info;
825 tagEntry entry;
826 tagFile *const file = tagsOpen (TagFileName, &info);
827 if (file == NULL)
829 fprintf (stderr, "%s: cannot open tag file: %s: %s\n",
830 ProgramName, strerror (info.status.error_number), name);
831 exit (1);
833 else
835 if (SortOverride)
836 tagsSetSortType (file, SortMethod);
837 if (tagsFind (file, &entry, name, options) == TagSuccess)
841 printTag (&entry);
842 } while (tagsFindNext (file, &entry) == TagSuccess);
844 tagsClose (file);
848 static void listTags (void)
850 tagFileInfo info;
851 tagEntry entry;
852 tagFile *const file = tagsOpen (TagFileName, &info);
853 if (file == NULL)
855 fprintf (stderr, "%s: cannot open tag file: %s: %s\n",
856 ProgramName, strerror (info.status.error_number), TagFileName);
857 exit (1);
859 else
861 while (tagsNext (file, &entry) == TagSuccess)
862 printTag (&entry);
863 tagsClose (file);
867 const char *const Usage =
868 "Find tag file entries matching specified names.\n\n"
869 "Usage: %s [-ilp] [-s[0|1]] [-t file] [name(s)]\n\n"
870 "Options:\n"
871 " -e Include extension fields in output.\n"
872 " -i Perform case-insensitive matching.\n"
873 " -l List all tags.\n"
874 " -p Perform partial matching.\n"
875 " -s[0|1|2] Override sort detection of tag file.\n"
876 " -t file Use specified tag file (default: \"tags\").\n"
877 "Note that options are acted upon as encountered, so order is significant.\n";
879 extern int main (int argc, char **argv)
881 int options = 0;
882 int actionSupplied = 0;
883 int i;
884 ProgramName = argv [0];
885 if (argc == 1)
887 fprintf (stderr, Usage, ProgramName);
888 exit (1);
890 for (i = 1 ; i < argc ; ++i)
892 const char *const arg = argv [i];
893 if (arg [0] != '-')
895 findTag (arg, options);
896 actionSupplied = 1;
898 else
900 size_t j;
901 for (j = 1 ; arg [j] != '\0' ; ++j)
903 switch (arg [j])
905 case 'e': extensionFields = 1; break;
906 case 'i': options |= TAG_IGNORECASE; break;
907 case 'p': options |= TAG_PARTIALMATCH; break;
908 case 'l': listTags (); actionSupplied = 1; break;
910 case 't':
911 if (arg [j+1] != '\0')
913 TagFileName = arg + j + 1;
914 j += strlen (TagFileName);
916 else if (i + 1 < argc)
917 TagFileName = argv [++i];
918 else
920 fprintf (stderr, Usage, ProgramName);
921 exit (1);
923 break;
924 case 's':
925 SortOverride = 1;
926 ++j;
927 if (arg [j] == '\0')
928 SortMethod = TAG_SORTED;
929 else if (strchr ("012", arg[j]) != NULL)
930 SortMethod = (sortType) (arg[j] - '0');
931 else
933 fprintf (stderr, Usage, ProgramName);
934 exit (1);
936 break;
937 default:
938 fprintf (stderr, "%s: unknown option: %c\n",
939 ProgramName, arg[j]);
940 exit (1);
941 break;
946 if (! actionSupplied)
948 fprintf (stderr,
949 "%s: no action specified: specify tag name(s) or -l option\n",
950 ProgramName);
951 exit (1);
953 return 0;
956 #endif
958 /* vi:set tabstop=4 shiftwidth=4: */