Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * Hypervisor Software File: search.c | |
5 | * | |
6 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
7 | * | |
8 | * - Do no alter or remove copyright notices | |
9 | * | |
10 | * - Redistribution and use of this software in source and binary forms, with | |
11 | * or without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistribution of source code must retain the above copyright notice, | |
15 | * this list of conditions and the following disclaimer. | |
16 | * | |
17 | * - Redistribution in binary form must reproduce the above copyright notice, | |
18 | * this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * | |
21 | * Neither the name of Sun Microsystems, Inc. or the names of contributors | |
22 | * may be used to endorse or promote products derived from this software | |
23 | * without specific prior written permission. | |
24 | * | |
25 | * This software is provided "AS IS," without a warranty of any kind. | |
26 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
27 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
28 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
29 | * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
30 | * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
31 | * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
32 | * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
33 | * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
34 | * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
35 | * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
36 | * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
37 | * | |
38 | * You acknowledge that this software is not designed, licensed or | |
39 | * intended for use in the design, construction, operation or maintenance of | |
40 | * any nuclear facility. | |
41 | * | |
42 | * ========== Copyright Header End ============================================ | |
43 | */ | |
44 | /* | |
45 | * @(#)search.c 1.1 02/05/02 | |
46 | * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved | |
47 | * Copyright Use is subject to license terms. | |
48 | */ | |
49 | #include <stdio.h> | |
50 | #include <stdlib.h> | |
51 | #include <unistd.h> | |
52 | #include <string.h> | |
53 | #include <regexpr.h> | |
54 | ||
55 | #include "fscope.h" | |
56 | ||
57 | void | |
58 | free_searchlist(search_t *list) | |
59 | { | |
60 | search_t *next; | |
61 | ||
62 | while (list != NULL) { | |
63 | next = list->next; | |
64 | free(list); | |
65 | list = next; | |
66 | } | |
67 | } | |
68 | ||
69 | search_t * | |
70 | new_search_t(void) | |
71 | { | |
72 | search_t *new; | |
73 | ||
74 | new = malloc(sizeof (search_t)); | |
75 | new->private = NULL; | |
76 | new->notify = NULL; | |
77 | new->next = NULL; | |
78 | new->prev = NULL; | |
79 | new->xref = NULL; | |
80 | return (new); | |
81 | } | |
82 | ||
83 | static search_t * | |
84 | regexp_search(extract_t *info, search_t *root, int restrict) | |
85 | { | |
86 | search_t *new; | |
87 | xref_t *cref; | |
88 | char *regexpbuf; | |
89 | int i; | |
90 | char *symbol; | |
91 | int rlen; | |
92 | ||
93 | symbol = info->symbol; | |
94 | regexpbuf = compile(symbol, NULL, NULL); | |
95 | if (regexpbuf == NULL) { | |
96 | printf("Regexp error: %d\n", regerrno); | |
97 | return (NULL); | |
98 | } | |
99 | for (i = 0; i < 128; i++) { | |
100 | cref = info->state->all_refs[i]; | |
101 | while (cref != NULL) { | |
102 | int match; | |
103 | if (cref->flags & restrict) { | |
104 | match = step(cref->name, regexpbuf); | |
105 | } else { | |
106 | match = 0; | |
107 | } | |
108 | if (match) { | |
109 | new = new_search_t(); | |
110 | new->xref = cref; | |
111 | new->next = root; | |
112 | root = new; | |
113 | } | |
114 | cref = cref->next_def; | |
115 | } | |
116 | } | |
117 | free(regexpbuf); | |
118 | return (root); | |
119 | } | |
120 | ||
121 | static search_t * | |
122 | exact_search(extract_t *info, search_t *root, int restrict) | |
123 | { | |
124 | search_t *new; | |
125 | xref_t *cref; | |
126 | char *symbol; | |
127 | ||
128 | symbol = info->symbol; | |
129 | cref = info->state->all_refs[(unsigned int)symbol[0]]; | |
130 | while (cref != NULL) { | |
131 | if ((cref->flags & restrict) && | |
132 | (strcmp(symbol, cref->name) == 0)) { | |
133 | new = new_search_t(); | |
134 | new->xref = cref; | |
135 | new->next = root; | |
136 | root = new; | |
137 | } | |
138 | cref = cref->next_def; | |
139 | } | |
140 | return (root); | |
141 | } | |
142 | ||
143 | search_t * | |
144 | build_searchlist(extract_t *info, search_t *root, int restrict) | |
145 | { | |
146 | if (info->flags & FLAG_REGEXP) { | |
147 | return (regexp_search(info, root, restrict)); | |
148 | } | |
149 | return (exact_search(info, root, restrict)); | |
150 | } | |
151 | ||
152 | search_t * | |
153 | search_list(extract_t *info, int prune, search_t *list, | |
154 | int (match)(extract_t *info, search_t *item)) | |
155 | { | |
156 | search_t *root = NULL; | |
157 | search_t *new, *next, *prev; | |
158 | search_t *newlist = NULL; | |
159 | int hit; | |
160 | ||
161 | prev = NULL; | |
162 | newlist = list; | |
163 | while (newlist != NULL) { | |
164 | next = newlist->next; | |
165 | hit = match(info, newlist); | |
166 | if (prune) { | |
167 | if (!hit) { | |
168 | /* prune it away */ | |
169 | if (prev != NULL) { | |
170 | prev->next = next; | |
171 | } else { | |
172 | list = next; | |
173 | } | |
174 | if (newlist->notify != NULL) { | |
175 | newlist->notify(newlist, 1); | |
176 | } | |
177 | free(newlist); | |
178 | } else { | |
179 | prev = newlist; | |
180 | } | |
181 | } else { | |
182 | if (hit) { | |
183 | new = new_search_t(); | |
184 | if (newlist->notify != NULL) { | |
185 | new->private = | |
186 | newlist->notify(newlist, 0); | |
187 | } else { | |
188 | new->private = newlist->private; | |
189 | } | |
190 | new->xref = newlist->xref; | |
191 | new->next = root; | |
192 | root = new; | |
193 | } | |
194 | prev = newlist; | |
195 | } | |
196 | newlist = next; | |
197 | } | |
198 | if (prune) | |
199 | return (list); | |
200 | return (root); | |
201 | } | |
202 | ||
203 | void | |
204 | iterate_list(extract_t *info, search_t *list, | |
205 | void (fn)(extract_t *info, search_t *item)) | |
206 | { | |
207 | while (list != NULL) { | |
208 | fn(info, list); | |
209 | list = list->next; | |
210 | } | |
211 | } |