Debian Codegraph @

lib/atom.c

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 2011, 2012 Antti-Juhani Kaijanaho
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <regex.h>
20 #include <stdlib.h>
21 #include "atom.h"
22 #include "msg.h"
23 #include "version.h"
24
25 #define RE_PKG_BEGIN "(^| )"
26 #define RE_PKG_END "([, \\(]|$)"
27
28 void atom_finish(struct atom * atom)
29 {
30 char * regex_pat = NULL;
31 int regex_patlen = atom->patlen + strlen(RE_PKG_BEGIN)
32 + strlen(RE_PKG_END) + 1;
33 debug_message("predicate_finish_atom", 0);
34 if (atom->field_name != 0) {
35 char * repl = strchr(atom->field_name, ':');
36 if (repl != NULL) {
37 *repl++ = '\0';
38 atom->repl_inx = fieldtrie_insert(repl)->inx;
39 } else {
40 atom->repl_inx = -1;
41 }
42 atom->field_inx = fieldtrie_insert(atom->field_name)->inx;
43 }
44
45 if (atom->mode == M_REGEX || atom->mode == M_EREGEX) {
46 regex_pat = calloc(1, regex_patlen); /* rely on mem 0-ing */
47 if (regex_pat == 0) fatal_enomem(0);
48 if (atom->whole_pkg)
49 strncat(regex_pat, RE_PKG_BEGIN, strlen(RE_PKG_BEGIN));
50 strncat(regex_pat, atom->pat, atom->patlen);
51 if (atom->whole_pkg)
52 strncat(regex_pat, RE_PKG_END, strlen(RE_PKG_END));
53 debug_message("compiling:", 0);
54 debug_message(regex_pat, 0);
55 int rerr = regcomp(&atom->regex, regex_pat,
56 (atom->mode == M_EREGEX ? REG_EXTENDED : 0)
57 | REG_NOSUB
58 | (atom->ignore_case ? REG_ICASE : 0));
59 free(regex_pat);
60 if (rerr != 0) {
61 char * s;
62 s = get_regerror(rerr, &atom->regex);
63 if (s == 0) fatal_enomem(0);
64 message(L_FATAL, 0, "%s", s);
65 free(s);
66 fail();
67 }
68 }
69
70 }
71
72 static bool atom_field_verify(struct atom * atom, FSAF * fp,
73 size_t start, size_t end)
74 {
75 size_t len = end - start;
76 struct fsaf_read_rv r = fsaf_read(fp, start, len);
77 assert(r.len == len);
78 switch (atom->mode) {
79 case M_EXACT:
80 if (len != atom->patlen) return false;
81 if (atom->ignore_case) {
82 return strncasecmp(atom->pat, r.b, len) == 0;
83 } else {
84 return strncmp(atom->pat, r.b, len) == 0;
85 }
86 case M_SUBSTR: {
87 #if 0
88 if (atom->ignore_case) {
89 return strncasestr(r.b, atom->pat, len);
90 } else {
91 return strnstr(r.b, atom->pat, len);
92 }
93 #else
94 bool rv;
95 char * s = strndup(r.b, len);
96 if (s == 0) fatal_enomem(0);
97 if (atom->ignore_case) {
98 rv = strcasestr(s, atom->pat) != 0;
99 } else {
100 rv = strstr(s, atom->pat) != 0;
101 }
102 free(s);
103 return rv;
104 #endif
105 }
106 case M_REGEX: case M_EREGEX: {
107 char * s = strndup(r.b, len);
108 if (s == 0) fatal_enomem(0);
109 int regex_errcode = regexec(&atom->regex, s, 0, 0, 0);
110 free(s);
111 if (regex_errcode == 0 || regex_errcode == REG_NOMATCH) {
112 return (regex_errcode == 0);
113 }
114 /* Error handling be here. */
115 assert(regex_errcode != 0 && regex_errcode != REG_NOMATCH);
116 s = get_regerror (regex_errcode, &atom->regex);
117 if (s == 0) { enomem (0); return false; }
118 message(L_IMPORTANT, 0, "%s", s);
119 free(s);
120 return false;
121 }
122 case M_VER_EQ:case M_VER_LT:case M_VER_LE:case M_VER_GT:case M_VER_GE:
123 ;
124 char *pats = strndup(atom->pat, atom->patlen);
125 char *cands = strndup(r.b, len);
126 struct versionrevision pat, cand;
127 if (!parse_version(&pat, pats, atom->patlen)) {
128 free(pats);
129 free(cands);
130 return false;
131 }
132 if (!parse_version(&cand, cands, len)) {
133 free(pats);
134 free(cands);
135 return false;
136 }
137 int res = versioncompare(&cand, &pat);
138 free(pats);
139 free(cands);
140 switch (atom->mode) {
141 case M_VER_EQ:
142 return res == 0;
143 case M_VER_LT:
144 return res < 0;
145 case M_VER_LE:
146 return res <= 0;
147 case M_VER_GT:
148 return res > 0;
149 case M_VER_GE:
150 return res >= 0;
151 default:
152 assert(0);
153 }
154 }
155 assert(0);
156 }
157
158 bool atom_verify(struct atom * at, para_t * par)
159 {
160 FSAF * fp = par->common->fp;
161 if (at->field_inx == (size_t)-1) {
162 /* Take the full paragraph */
163 return atom_field_verify(at, fp, par->start, par->end);
164 }
165 /* Test field(s) */
166 struct field_data fds = find_field_wr(par, at->field_inx, at->repl_inx);
167 for (struct field_datum * fd = fds.first; fd != NULL; fd = fd->next) {
168 if (atom_field_verify(at, fp, fd->start, fd->end)) return true;
169 }
170 return false;
171 }