[Dctrl-tools-devel] [SCM] Debian control file query tools branch, master, updated. 2.21.1-8-gb2a4f15
Antti-Juhani Kaijanaho
ajk at debian.org
Sun Apr 22 19:16:07 UTC 2012
The following commit has been merged in the master branch:
commit 72505e09d0e04ee3ed2ac9fd6d9ded688ad88f38
Author: Antti-Juhani Kaijanaho <ajk at debian.org>
Date: Sun Apr 22 21:42:50 2012 +0300
Move the fields array from parse_prim to a library (strlist).
Signed-off-by: Antti-Juhani Kaijanaho <ajk at debian.org>
diff --git a/grep-dctrl/grep-dctrl.c b/grep-dctrl/grep-dctrl.c
index f7ebd05..526ad61 100644
--- a/grep-dctrl/grep-dctrl.c
+++ b/grep-dctrl/grep-dctrl.c
@@ -38,6 +38,7 @@
#include "msg.h"
#include "paragraph.h"
#include "predicate.h"
+#include "strlist.h"
#include "util.h"
const char * argp_program_version = "grep-dctrl (dctrl-tools) " VERSION;
@@ -532,8 +533,8 @@ static struct predicate * parse_prim(struct arguments * args)
}
char *pattern = 0;
- char *fields[MAX_FIELDS];
- size_t num_fields = 0;
+ struct strlist *fields = strlist_new();
+ if (fields == 0) enomem(0);
enum matching_mode mm = M_SUBSTR;
bool ignore_case = false;
bool whole_pkg = false;
@@ -543,11 +544,9 @@ static struct predicate * parse_prim(struct arguments * args)
debug("tok = %s, mm = %d", tokdescr(peek_token(args)), mm);
switch (peek_token(args)) {
case TOK_FIELD:
- if (num_fields >= MAX_FIELDS) {
- message(L_FATAL, 0, _("too many field names"));
- fail();
+ if (!strlist_append(fields, get_string(args))) {
+ enomem(0);
}
- fields[num_fields++] = get_string(args);
break;
case TOK_ERGEX:
if (mm != M_SUBSTR) goto failmode;
@@ -615,40 +614,42 @@ static struct predicate * parse_prim(struct arguments * args)
goto loop_done;
}
nonempty = true;
- } loop_done:
-
- if (!nonempty) {
- unexpected(get_token(args));
- }
+ } loop_done:
- if (pattern == 0) {
- message(L_FATAL, 0, _("A pattern is mandatory"));
- fail();
- }
-
- if (num_fields == 0) {
- num_fields = 1;
- fields[0] = 0;
- }
-
- struct predicate *rv = 0;
- for (size_t i = 0; i < num_fields; i++) {
- struct atom * atom = malloc(sizeof *atom);
- if (atom == 0) enomem(0);
- atom->field_name = fields[i];
- atom->field_inx = -1;
- atom->mode = mm;
- atom->ignore_case = ignore_case;
- atom->whole_pkg = whole_pkg;
- atom->pat = pattern;
- atom->patlen = strlen(pattern);
- atom_finish(atom);
- struct predicate *tmp = predicate_ATOM(atom);
- rv = rv != 0 ? predicate_OR(rv, tmp) : tmp;
+ if (!nonempty) {
+ unexpected(get_token(args));
+ }
+
+ if (pattern == 0) {
+ message(L_FATAL, 0, _("A pattern is mandatory"));
+ fail();
+ }
+
+ if (strlist_is_empty(fields)) {
+ strlist_append(fields, 0);
}
-
+
+ struct predicate *rv = 0;
+ for (struct strlist_iterator it = strlist_begin(fields);
+ !strlist_iterator_at_end(it); strlist_iterator_next(&it)) {
+ struct atom * atom = malloc(sizeof *atom);
+ if (atom == 0) enomem(0);
+ atom->field_name = strlist_iterator_get(it);
+ atom->field_inx = -1;
+ atom->mode = mm;
+ atom->ignore_case = ignore_case;
+ atom->whole_pkg = whole_pkg;
+ atom->pat = pattern;
+ atom->patlen = strlen(pattern);
+ atom_finish(atom);
+ struct predicate *tmp = predicate_ATOM(atom);
+ rv = rv != 0 ? predicate_OR(rv, tmp) : tmp;
+ }
+
+ strlist_free(fields);
return rv;
failmode:
+ strlist_free(fields);
message(L_FATAL, 0, _("inconsistent modifiers of simple filters"));
fail();
return 0;
diff --git a/lib/strlist.c b/lib/strlist.c
new file mode 100644
index 0000000..c21a410
--- /dev/null
+++ b/lib/strlist.c
@@ -0,0 +1,87 @@
+/* dctrl-tools - Debian control file inspection tools
+ Copyright © 2012 Antti-Juhani Kaijanaho
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include "strlist.h"
+
+struct strlist {
+ const char **strs;
+ size_t n;
+ size_t maxn;
+};
+
+struct strlist *strlist_new(void)
+{
+ struct strlist *rv = malloc(sizeof *rv);
+ if (rv == 0) return 0;
+ rv->strs = 0;
+ rv->n = 0;
+ rv->maxn = 0;
+ return rv;
+}
+
+bool strlist_append(struct strlist *sl, const char *s)
+{
+ if (sl->n + 1 >= sl->maxn) {
+ size_t newmax = sl->maxn * 2;
+ if (newmax == 0) newmax = 2;
+ const char **newstrs = realloc(sl->strs,
+ newmax * sizeof *newstrs);
+ if (newstrs == 0) return false;
+ sl->maxn = newmax;
+ sl->strs = newstrs;
+ }
+ assert(sl->n < sl->maxn);
+ sl->strs[sl->n++] = s;
+ return true;
+}
+bool strlist_is_empty(struct strlist *sl)
+{
+ return sl->n == 0;
+}
+
+void strlist_free(struct strlist *sl)
+{
+ free(sl->strs);
+ free(sl);
+}
+
+struct strlist_iterator strlist_begin(struct strlist *sl)
+{
+ struct strlist_iterator it = {
+ .sl = sl,
+ .i = 0
+ };
+ return it;
+}
+bool strlist_iterator_at_end(struct strlist_iterator it)
+{
+ return it.i >= it.sl->n;
+}
+
+const char *strlist_iterator_get(struct strlist_iterator it)
+{
+ return it.sl->strs[it.i];
+}
+
+void strlist_iterator_next(struct strlist_iterator * it)
+{
+ it->i++;
+}
diff --git a/lib/ifile.h b/lib/strlist.h
similarity index 56%
copy from lib/ifile.h
copy to lib/strlist.h
index fa92ae8..da64623 100644
--- a/lib/ifile.h
+++ b/lib/strlist.h
@@ -1,5 +1,5 @@
/* dctrl-tools - Debian control file inspection tools
- Copyright © 2004, 2011 Antti-Juhani Kaijanaho
+ Copyright © 2012 Antti-Juhani Kaijanaho
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -16,25 +16,23 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef IFILE_H
-#define IFILE_H
+#ifndef GUARD_LIB_STRLIST_H
+#define GUARD_LIB_STRLIST_H
-#include <stdbool.h>
-
-struct ifile {
- enum ifile_mode { m_error, m_read, m_exec } mode;
- char const * s;
+struct strlist;
+struct strlist_iterator {
+ struct strlist *sl;
+ size_t i;
};
-extern char const * const ifile_modes[];
-
-// returns fd
-int open_ifile(struct ifile f);
-
-// must be used on ifile-opened fd's
-void close_ifile(struct ifile f, int fd);
+struct strlist *strlist_new(void);
+_Bool strlist_append(struct strlist *, const char *);
+_Bool strlist_is_empty(struct strlist *);
+void strlist_free(struct strlist *);
-// check if a safe file to read from
-bool chk_ifile(struct ifile fname, int fd);
+struct strlist_iterator strlist_begin(struct strlist *);
+_Bool strlist_iterator_at_end(struct strlist_iterator);
+const char *strlist_iterator_get(struct strlist_iterator);
+void strlist_iterator_next(struct strlist_iterator *);
-#endif /* IFILE_H */
+#endif /* GUARD_LIB_STRLIST_H */
--
Debian control file query tools
More information about the Dctrl-tools-devel
mailing list