| 1 | /* |
| 2 | * Copyright (c) 1988 Mark Nudleman |
| 3 | * Copyright (c) 1988 Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This code is derived from software contributed to Berkeley by |
| 7 | * Mark Nudleman. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms are permitted |
| 10 | * provided that the above copyright notice and this paragraph are |
| 11 | * duplicated in all such forms and that any documentation, |
| 12 | * advertising materials, and other materials related to such |
| 13 | * distribution and use acknowledge that the software was developed |
| 14 | * by the University of California, Berkeley. The name of the |
| 15 | * University may not be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 20 | */ |
| 21 | |
| 22 | #ifndef lint |
| 23 | static char sccsid[] = "@(#)tags.c 5.1 (Berkeley) %G%"; |
| 24 | #endif /* not lint */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include "less.h" |
| 28 | |
| 29 | #define WHITESP(c) ((c)==' ' || (c)=='\t') |
| 30 | |
| 31 | #if TAGS |
| 32 | |
| 33 | public char *tagfile; |
| 34 | public char *tagpattern; |
| 35 | |
| 36 | static char *tags = "tags"; |
| 37 | |
| 38 | extern int linenums; |
| 39 | extern int sigs; |
| 40 | extern char *line; |
| 41 | |
| 42 | /* |
| 43 | * Find a tag in the "tags" file. |
| 44 | * Sets "tagfile" to the name of the file containing the tag, |
| 45 | * and "tagpattern" to the search pattern which should be used |
| 46 | * to find the tag. |
| 47 | */ |
| 48 | public int |
| 49 | findtag(tag) |
| 50 | register char *tag; |
| 51 | { |
| 52 | register char *p; |
| 53 | register FILE *f; |
| 54 | register int taglen; |
| 55 | int search_char; |
| 56 | static char tline[200]; |
| 57 | |
| 58 | if ((f = fopen(tags, "r")) == NULL) |
| 59 | { |
| 60 | error("No tags file"); |
| 61 | tagfile = NULL; |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | taglen = strlen(tag); |
| 66 | |
| 67 | /* |
| 68 | * Search the tags file for the desired tag. |
| 69 | */ |
| 70 | while (fgets(tline, sizeof(tline), f) != NULL) |
| 71 | { |
| 72 | if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen])) |
| 73 | continue; |
| 74 | |
| 75 | /* |
| 76 | * Found it. |
| 77 | * The line contains the tag, the filename and the |
| 78 | * pattern, separated by white space. |
| 79 | * The pattern is surrounded by a pair of identical |
| 80 | * search characters. |
| 81 | * Parse the line and extract these parts. |
| 82 | */ |
| 83 | tagfile = tagpattern = NULL; |
| 84 | |
| 85 | /* |
| 86 | * Skip over the whitespace after the tag name. |
| 87 | */ |
| 88 | for (p = tline; !WHITESP(*p) && *p != '\0'; p++) |
| 89 | continue; |
| 90 | while (WHITESP(*p)) |
| 91 | p++; |
| 92 | if (*p == '\0') |
| 93 | /* File name is missing! */ |
| 94 | continue; |
| 95 | |
| 96 | /* |
| 97 | * Save the file name. |
| 98 | * Skip over the whitespace after the file name. |
| 99 | */ |
| 100 | tagfile = p; |
| 101 | while (!WHITESP(*p) && *p != '\0') |
| 102 | p++; |
| 103 | *p++ = '\0'; |
| 104 | while (WHITESP(*p)) |
| 105 | p++; |
| 106 | if (*p == '\0') |
| 107 | /* Pattern is missing! */ |
| 108 | continue; |
| 109 | |
| 110 | /* |
| 111 | * Save the pattern. |
| 112 | * Skip to the end of the pattern. |
| 113 | * Delete the initial "^" and the final "$" from the pattern. |
| 114 | */ |
| 115 | search_char = *p++; |
| 116 | if (*p == '^') |
| 117 | p++; |
| 118 | tagpattern = p; |
| 119 | while (*p != search_char && *p != '\0') |
| 120 | p++; |
| 121 | if (p[-1] == '$') |
| 122 | p--; |
| 123 | *p = '\0'; |
| 124 | |
| 125 | fclose(f); |
| 126 | return; |
| 127 | } |
| 128 | fclose(f); |
| 129 | error("No such tag in tags file"); |
| 130 | tagfile = NULL; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Search for a tag. |
| 135 | * This is a stripped-down version of search(). |
| 136 | * We don't use search() for several reasons: |
| 137 | * - We don't want to blow away any search string we may have saved. |
| 138 | * - The various regular-expression functions (from different systems: |
| 139 | * regcmp vs. re_comp) behave differently in the presence of |
| 140 | * parentheses (which are almost always found in a tag). |
| 141 | */ |
| 142 | public int |
| 143 | tagsearch() |
| 144 | { |
| 145 | POSITION pos, linepos; |
| 146 | int linenum; |
| 147 | |
| 148 | pos = (POSITION)0; |
| 149 | linenum = find_linenum(pos); |
| 150 | |
| 151 | for (;;) |
| 152 | { |
| 153 | /* |
| 154 | * Get lines until we find a matching one or |
| 155 | * until we hit end-of-file. |
| 156 | */ |
| 157 | if (sigs) |
| 158 | return (1); |
| 159 | |
| 160 | /* |
| 161 | * Read the next line, and save the |
| 162 | * starting position of that line in linepos. |
| 163 | */ |
| 164 | linepos = pos; |
| 165 | pos = forw_raw_line(pos); |
| 166 | if (linenum != 0) |
| 167 | linenum++; |
| 168 | |
| 169 | if (pos == NULL_POSITION) |
| 170 | { |
| 171 | /* |
| 172 | * We hit EOF without a match. |
| 173 | */ |
| 174 | error("Tag not found"); |
| 175 | return (1); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * If we're using line numbers, we might as well |
| 180 | * remember the information we have now (the position |
| 181 | * and line number of the current line). |
| 182 | */ |
| 183 | if (linenums) |
| 184 | add_lnum(linenum, pos); |
| 185 | |
| 186 | /* |
| 187 | * Test the line to see if we have a match. |
| 188 | */ |
| 189 | if (strcmp(tagpattern, line) == 0) |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | jump_loc(linepos); |
| 194 | return (0); |
| 195 | } |
| 196 | |
| 197 | #endif |