/
tag.c
391 lines (345 loc) · 8.79 KB
/
tag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* tag.c */
/* This file contains functions and variables which are used by all tags
* programs, including elvis and ctags.
*/
#include "elvis.h"
#ifdef FEATURE_TAGS
# if USE_PROTOTYPES
static ELVBOOL tagbefore(TAG *t1, TAG *t2);
# endif
/* This array stores the (dynamically allocated) names of attributes. */
char *tagattrname[MAXATTR] = {"tagname", "tagfile", "tagaddress"};
/* This stores the search direction to be used with regular expressions */
ELVBOOL tagforward;
/* This variable stores a list of tags. Tags can be added to the list via
* the tagadd() function, and deleted via tagdelete()
*/
TAG *taglist;
/* Tag comparison function, returns non-zero if t1 should be inserted before t2.
*/
static ELVBOOL tagbefore(t1, t2)
TAG *t1, *t2;
{
long cmp;
char tmptime[20];
/* First compare them by tag name. If that doesn't resolve it, then
* compare them by how well they match previously accepted tags. As a
* last resort, compare them by timestamp
*/
cmp = strcmp(t1->TAGNAME, t2->TAGNAME);
if (cmp == 0)
{
cmp = t2->match - t1->match;
}
if (cmp == 0)
{
strcpy(tmptime, dirtime(t1->TAGFILE));
cmp = strcmp(dirtime(t2->TAGFILE), tmptime);
}
/* return the comparison result */
return (ELVBOOL)(cmp < 0);
}
/* This function frees any names in the tagattrname[] array. It should be
* called when switching to a different tag file, after the last TAG structure
* from the previous file has been freed.
*/
void tagnamereset()
{
int i;
for (i = 3; i < MAXATTR && tagattrname[i]; i++)
{
safefree(tagattrname[i]);
tagattrname[i] = NULL;
}
}
/* This function allocates a TAG structure, and fills in all fields with
* copies of the attributes from another tag. The string fields are also
* dynamically allocated.
*/
TAG *tagdup(tag)
TAG *tag; /* a tag to be duplicated */
{
TAG *ret; /* tag to be returned */
int i;
ret = (TAG *)safealloc(1, sizeof(TAG));
memset(ret, 0, sizeof(TAG));
for (i = 0; i < MAXATTR; i++)
{
if (tag->attr[i])
{
ret->attr[i] = safedup(tag->attr[i]);
}
}
ret->match = tag->match;
return ret;
}
/* This function adds a named attribute to a tag. Returns ElvTrue if successful,
* or ElvFalse if the tag already has too many named attributes.
*/
ELVBOOL tagattr(tag, name, value)
TAG *tag; /* the tag to receive the value */
char *name; /* name of the attribute */
char *value; /* value of the attribute */
{
int i;
/* search for the name in tagattrname[] */
for (i = 0; i < MAXATTR && tagattrname[i] && strcmp(name, tagattrname[i]); i++)
{
}
if (i >= MAXATTR)
return ElvFalse;
if (!tagattrname[i])
tagattrname[i] = safedup(name);
/* store the value */
tag->attr[i] = value;
return ElvTrue;
}
/* This function frees a tag. Returns the "next" field from the deleted
* tag, which can be handy in a loop.
*/
TAG *tagfree(tag)
TAG *tag; /* the tag to be freed */
{
TAG *next;
int i;
/* free this tag */
next = tag->next;
for (i = 0; i < MAXATTR; i++)
{
if (tag->attr[i]) safefree(tag->attr[i]);
}
safefree(tag);
/* return its "next" field */
return next;
}
/* This function deletes the first tag, or all tags, from the taglist.
* After the last tag has been deleted, the tag attribute names are reset.
*/
void tagdelete(all)
ELVBOOL all; /* if ElvTrue, delete all tags (else just first tag) */
{
/* delete the tag(s) */
if (taglist)
{
do
{
taglist = tagfree(taglist);
} while (all && taglist != NULL);
}
/* if taglist is empty, then we can discard the attribute names too */
if (!taglist)
{
tagnamereset();
}
}
/* This function inserts a tag into the tag list. The list is sorted primarily
* by name; within each name, they are sorted by the likelyhood factor (highest
* first). Returns ElvTrue of another tag with that name was previously in the
* list, or ElvFalse if the new tag's name is unique so far.
*
* NOTE: This uses a special type of list, which uses an extra pointer to boost
* its update effiency to be about the same as a binary tree. Consequently,
* it should be efficient, even for fairly large lists.
*/
void tagadd(tag)
TAG *tag;
{
TAG *scan;
/* if empty list, this is easy */
if (!taglist)
{
taglist = tag;
return;
}
/* if before head, then insert as new head */
if (tagbefore(tag, taglist))
{
tag->next = taglist;
tag->bighop = taglist->bighop;
taglist->bighop = NULL;
taglist = tag;
return;
}
/* search for the insertion point */
for (scan = taglist; scan->next && !tagbefore(tag, scan->next); )
{
/* if there is a bighop value, try it */
if (scan->bighop && !tagbefore(tag, scan->bighop))
{
scan = scan->bighop;
}
else
{
scan = scan->next;
}
}
/* insert the item after the "scan" item */
tag->next = scan->next;
scan->next = tag;
if (!scan->bighop)
scan->bighop = tag;
}
/* This function parses a line from a tag file, and returns the corresponding
* tag. Returns NULL if...
* + the tag name is 0 characters long, or
* + the file name is missing or 0 characters long, or
* + the third field is neither a number nor a regular expression, or
* If any tag attributes are malformed, or if there are too many of them, then
* the remainder of the line is ignored but the tag data up to that point is
* returned.
*
* THE LINE IS CLOBBERED! THE RETURNED TAG IS STATICALLY-ALLOCATED, AND
* WILL CONTAIN REFERENCES BACK TO THE LINE TEXT! You can use tagdup() to
* make a dynamically-allocated copy of the tag.
*/
TAG *tagparse(line)
char *line; /* a text line from a tags file */
{
char *tagname; /* name of tag (first field) */
char *filename; /* name of file (second field) */
char *address; /* address of the tag, as a string */
char *name, *value; /* an attribute of the tag */
char *build;
char c;
int quoted; /* 0=not quoted, 1=quoted, 2=backslash */
static TAG tag; /* the tag to be returned */
/* clobber any old data in "tag" */
memset(&tag, 0, sizeof tag);
/* parse the tag name */
tagname = line;
while (*line != '\t')
{
if (!*line || *line == '\n')
return NULL;
line++;
}
*line++ = '\0';
/* parse the file name */
filename = line;
while (*line != '\t')
{
if (!*line || *line == '\n')
return NULL;
line++;
}
*line++ = '\0';
/* parse the line address */
address = line;
if (elvdigit(*address))
{
/* number -- use all digits */
for (; elvdigit(*line); line++)
{
}
}
else if (*address == '?' || *address == '/')
{
/* regexp -- use chars up to EOL or next unquoted delimiter */
c = *line;
quoted = 0;
do
{
if (*line == '\0' || *line == '\n')
return NULL;
line++;
if (quoted > 0)
quoted--;
if (quoted == 0 && *line == '\\')
quoted = 2;
} while (*line != c
|| quoted != 0
|| (line[1] == ';' && (line[2] == '/' || line[2] == '?')));
line++;
}
else
{
/* bad line address */
return NULL;
}
/* allow an optional semicolon-quote after the address */
if (line[0] == ';' && line[1] == '"')
{
*line = '\0';
line += 2;
}
/* must be EOL or a tab after the address */
if (*line && *line != '\n' && *line != '\t')
return NULL;
/* stuff the standard attributes into the tag */
tag.attr[0] = tagname;
tag.attr[1] = filename;
tag.attr[2] = address;
/* If there are any extra attributes, stuff them into the tag */
for (c = *line; c == '\t'; )
{
/* mark the end of the previous field */
*line++ = '\0';
/* parse the name of the attribute */
name = line;
while (*line && *line != '\n' && *line != '\t' && *line != ':')
{
line++;
}
if (*line != ':')
{
/* use the "name" as the value of `kind' */
c = *line;
*line = '\0';
if (!tagattr(&tag, "kind", name))
{
break;
}
*line = c;
continue;
}
*line++ = '\0';
/* Parse the value of the attribute, and translate "\x" into
* the corresponding character.
*/
build = value = line;
while (*line && *line != '\n' && *line != '\t')
{
if (line[0] == '\\' && line[1] == 't')
{
*build++ = '\t';
line += 2;
}
else if (line[0] == '\\' && line[1] == 'n')
{
*build++ = '\n';
line += 2;
}
else if (line[0] == '\\' && line[1] == '\\')
{
*build++ = '\\';
line += 2;
}
else
{
*build++ = *line++;
}
}
/* Mark the end of the value. Before doing that, though,
* remember the last character scanned so far. We must
* remember that character before marking the end of the
* value, because it is possible that the end of the value
* will be at the same location as the last scanned char,
* and we don't want to clobber it until after we have a
* copy elsewhere.
*/
c = *line;
*build = '\0';
/* For "file" attribute with no value, assume filename */
if (!*value && !strcmp(name, "file"))
value = filename;
/* Store it. If too many hints, then skip the rest */
if (!tagattr(&tag, name, value))
{
break;
}
}
/* mark the end of the last field */
*line = '\0';
return &tag;
}
#endif /* FEATURE_TAGS */