[Dctrl-tools-devel] [SCM] Debian control file query tools branch, master, updated. 2.14.2-20-ga83883f

Antti-Juhani Kaijanaho ajk at debian.org
Sat Sep 11 19:44:12 UTC 2010


The following commit has been merged in the master branch:
commit a83883f738f737a931bfe0b710896aef59437790
Author: Antti-Juhani Kaijanaho <ajk at debian.org>
Date:   Sat Sep 11 22:43:49 2010 +0300

    tbl-dctrl: Figure out the fields from the input if no '-c' is given.
    
    Closes: #596424 reported by myself
    
    Signed-off-by: Antti-Juhani Kaijanaho <ajk at debian.org>

diff --git a/debian/changelog b/debian/changelog
index ad435d8..588833c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,10 @@
 dctrl-tools (2.16) UNRELEASED; urgency=low
 
   * Merge 2.14.2
+  * tbl-dctrl: Figure out the fields from the input if no '-c' is given.
+    Closes: #596424 reported by myself
 
- -- Antti-Juhani Kaijanaho <ajk at debian.org>  Sat, 11 Sep 2010 11:35:20 +0300
+ -- Antti-Juhani Kaijanaho <ajk at debian.org>  Sat, 11 Sep 2010 22:36:36 +0300
 
 dctrl-tools (2.15) experimental; urgency=low
 
diff --git a/grep-dctrl/grep-dctrl.c b/grep-dctrl/grep-dctrl.c
index 07c5ed6..4f26168 100644
--- a/grep-dctrl/grep-dctrl.c
+++ b/grep-dctrl/grep-dctrl.c
@@ -766,11 +766,14 @@ static void show_field(struct arguments *args,
                        struct paragraph *para,
                        struct field_attr *fa)
 {
-        struct fsaf_read_rv r 
-                = get_field(para, 
-                            fa->inx,
-                            GET_BACKUP_FIELD(fa->application_data));
-        
+        struct field_data *fd =
+                find_field_wr(para,
+                              fa->inx,
+                              GET_BACKUP_FIELD(fa->application_data));
+        if (fd == NULL) return;
+        struct fsaf_read_rv r =
+                fsaf_read(para->common->fp, fd->start, fd->end - fd->start);
+
         if (args->short_descr &&
             fa == description_attr) {
                 char * nl = memchr(r.b, '\n', r.len);
diff --git a/lib/para_bundle.c b/lib/para_bundle.c
index ade296f..f9e36c6 100644
--- a/lib/para_bundle.c
+++ b/lib/para_bundle.c
@@ -1,5 +1,5 @@
 /*  dctrl-tools - Debian control file inspection tools
-    Copyright © 2004 Antti-Juhani Kaijanaho
+    Copyright © 2004, 2010 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
@@ -19,7 +19,7 @@
 #include "msg.h"
 #include "para_bundle.h"
 
-void bundle_slurp(struct para_bundle * pb, struct ifile ifi)
+void bundle_slurp(struct para_bundle * pb, struct ifile ifi, _Bool reg)
 {
 	int fd = open_ifile(ifi);
 	if (fd == -1) {
@@ -39,7 +39,7 @@ void bundle_slurp(struct para_bundle * pb, struct ifile ifi)
 	sf->next = pb->files;
 	pb->files = sf;
 
-	para_parser_init(&sf->pp, f, false, false, false);
+	para_parser_init(&sf->pp, f, false, false, reg);
 	while (true) {
 		if (pb->num_paras == pb->max_num) {
 			size_t max_num = pb->max_num == 0 ? 256 :
diff --git a/lib/para_bundle.h b/lib/para_bundle.h
index 212032d..9f02c70 100644
--- a/lib/para_bundle.h
+++ b/lib/para_bundle.h
@@ -1,5 +1,5 @@
 /*  dctrl-tools - Debian control file inspection tools
-    Copyright © 2004 Antti-Juhani Kaijanaho
+    Copyright © 2004, 2010 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
@@ -51,7 +51,8 @@ void bundle_init(struct para_bundle * pb)
 	pb->files = 0;
 }
 
-void bundle_slurp(struct para_bundle *, struct ifile);
+void bundle_slurp(struct para_bundle *, struct ifile,
+                  _Bool register_unknown_fields);
 
 static inline
 size_t bundle_size(struct para_bundle * pb)
diff --git a/lib/paragraph.c b/lib/paragraph.c
index b30a3bc..8efbd6e 100644
--- a/lib/paragraph.c
+++ b/lib/paragraph.c
@@ -26,7 +26,7 @@ void para_parser_init(para_parser_t * pp, FSAF * fp,
                       bool register_unknown_fields)
 {
 	pp->fp = fp;
-	pp->eof = false;
+	pp->eof = 0;
 	pp->loc = 0;
 	pp->line = 1;
 	pp->invalidate_p = invalidate_p;
@@ -40,11 +40,17 @@ void para_init(para_parser_t * pp, para_t * para)
 	para->line = pp->line;
 	para->start = 0;
 	para->end = 0;
-	para->fields = 0;
-	para->nfields = 0;
 	para->nfields = fieldtrie_count();
-	para->fields = malloc(para->nfields * sizeof *para->fields);
-	if (para->fields == 0) fatal_enomem(0);
+	para->maxfields = para->nfields;
+        if (para->nfields > 0) {
+                para->fields = malloc(para->nfields * sizeof *para->fields);
+                if (para->fields == 0) fatal_enomem(0);
+                for (size_t i = 0; i < para->nfields; i++) {
+                        para->fields[i].present = 0;
+                }
+        } else {
+                para->fields = NULL;
+        }
 }
 
 void para_finalize(para_t * para)
@@ -55,6 +61,7 @@ void para_finalize(para_t * para)
 	if (para->fields != 0) {
 		free(para->fields);
 	}
+        para->maxfields = 0;
 	para->nfields = 0;
 	para->fields = 0;
 }
@@ -63,14 +70,18 @@ static struct field_data * register_field(para_t * para,
                                           char const * s, size_t slen)
 {
         size_t inx = fieldtrie_insert_n(s, slen)->inx;
-        if (inx >= para->nfields) {
-                assert(para->nfields > 0);
-                para->nfields = para->nfields * 2;
+        if (inx >= para->maxfields) {
+                if (para->maxfields == 0) para->maxfields = 2;
+                while (inx >= para->maxfields) {
+                        para->maxfields = para->maxfields * 2;
+                }
                 para->fields = realloc(para->fields,
-                                       para->nfields * sizeof(para->fields[0])
-                                      );
+                                       para->maxfields *
+                                       sizeof(para->fields[0]));
                 if (para->fields == 0) fatal_enomem(0);
         }
+        if (inx >= para->nfields) para->nfields = inx + 1;
+        assert(para->nfields <= para->maxfields);
         struct field_data *field_data = &para->fields[inx];
         return field_data;
 }
@@ -83,6 +94,7 @@ redo:
 	para->start = pp->loc;
 	para->line = pp->line;
 	for (size_t i = 0; i < para->nfields; i++) {
+                para->fields[i].present = 0;
 		para->fields[i].start = 0;
 		para->fields[i].end = 0;
 	}
@@ -159,18 +171,18 @@ FIELD_NAME:
                 if (attr == NULL) {
                         if (para->common->
                             register_unknown_fields) {
-                                field_data =
-                                        register_field(para,
-                                                       r.b,
-                                                       len);
-                        } else {
-                                field_data = 0;
+                                register_field(para, r.b, len);
                         }
+                }
+                attr = fieldtrie_lookup(r.b, len);
+                if (attr == NULL) {
+                        field_data = 0;
                 } else {
                         assert(attr->inx < para->nfields);
                         field_data = &para->fields[attr->inx];
                 }
                 if (field_data != NULL) {
+                        field_data->present = 1;
                         field_data->start = pos;
                         field_data->line = line;
                 }
diff --git a/lib/paragraph.h b/lib/paragraph.h
index 6b6ad5a..c785b8f 100644
--- a/lib/paragraph.h
+++ b/lib/paragraph.h
@@ -1,5 +1,5 @@
 /*  dctrl-tools - Debian control file inspection tools
-    Copyright © 2003, 2004, 2005 Antti-Juhani Kaijanaho
+    Copyright © 2003, 2004, 2005, 2010 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
@@ -26,6 +26,7 @@
 #include "fieldtrie.h"
 
 struct field_data {
+        _Bool present;
 	size_t line;
 	size_t start, end; /* offsets to the file; [start,end) is the body */
 };
@@ -45,9 +46,8 @@ struct paragraph {
 	size_t line;
 	struct paragraph_parser * common;
 	size_t start, end; /* offsets to the file; [start,end) is the paragraph */
-	size_t nfields;
+	size_t nfields, maxfields;
 	struct field_data * fields;
-	//struct field_data fields[MAX_FIELDS];
 };
 
 typedef struct paragraph_parser para_parser_t;
@@ -70,11 +70,37 @@ struct fsaf_read_rv get_whole_para(para_t * p)
 }
 
 static inline
+struct field_data * find_field(const para_t *p, size_t fld_inx)
+{
+	struct field_data * fd = fld_inx < p->nfields
+                ? &p->fields[fld_inx]
+                : NULL;
+        if (fd != NULL && !fd->present) fd = NULL;
+        return fd;
+}
+
+static inline
+struct field_data *find_field_wr(const para_t *p,
+                                 size_t fld_inx,
+                                 size_t repl_inx)
+{
+	struct field_data * fd = find_field(p, fld_inx);
+        if ((fd == NULL || fd->start == fd->end) && repl_inx != (size_t)(-1)) {
+                fd = find_field(p, repl_inx);
+        }
+        return fd;
+}
+
+static inline
 struct fsaf_read_rv get_field(para_t * p, size_t fld_inx, size_t repl_inx)
 {
-	struct field_data * fd = &p->fields[fld_inx];
-        if (fd->start == fd->end && repl_inx != (size_t)(-1)) {
-                 fd = &p->fields[repl_inx];
+        struct field_data *fd = find_field_wr(p, fld_inx, repl_inx);
+        if (fd == NULL) {
+                const struct fsaf_read_rv fail = {
+                        .b = NULL,
+                        .len = 0
+                };
+                return fail;
         }
 	return fsaf_read(p->common->fp, fd->start, fd->end - fd->start);
 }
@@ -82,7 +108,8 @@ struct fsaf_read_rv get_field(para_t * p, size_t fld_inx, size_t repl_inx)
 static inline
 char * get_field_as(const para_t * p, size_t fld_inx)
 {
-	struct field_data * fd = &p->fields[fld_inx];
+	struct field_data * fd = find_field(p, fld_inx);
+        if (fd == NULL) return strdup("");
 	return fsaf_getas(p->common->fp, fd->start, fd->end - fd->start);
 }
 
diff --git a/lib/predicate.c b/lib/predicate.c
index 94a916b..5c213b2 100644
--- a/lib/predicate.c
+++ b/lib/predicate.c
@@ -1,5 +1,5 @@
 /*  dctrl-tools - Debian control file inspection tools
-    Copyright © 2003, 2004, 2008 Antti-Juhani Kaijanaho
+    Copyright © 2003, 2004, 2008, 2010 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
@@ -93,19 +93,21 @@ void predicate_finish_atom(struct predicate * p)
 
 static bool verify_atom(struct atom * atom, para_t * para)
 {
-	size_t start, end;
+	size_t start = 0;
+        size_t end = 0;
 	if (atom->field_inx == -1) {
 		/* Take the full paragraph */
 		start = para->start;
 		end = para->end;
 	} else {
 		/* Take the field */
-		struct field_data * fd = &para->fields[atom->field_inx];
-                if (fd->start == fd->end && atom->repl_inx != (size_t)(-1)) {
-                        fd = &para->fields[atom->repl_inx];
-                }
-		start = fd->start;
-		end = fd->end;
+		struct field_data * fd = find_field_wr(para,
+                                                       atom->field_inx,
+                                                       atom->repl_inx);
+                if (fd != NULL) {
+                        start = fd->start;
+                        end = fd->end;
+                }                        
 	}
 	size_t len = end - start;
 	struct fsaf_read_rv r = fsaf_read(para->common->fp, start, len);
diff --git a/man/tbl-dctrl.1 b/man/tbl-dctrl.1
index ee22498..88a9247 100644
--- a/man/tbl-dctrl.1
+++ b/man/tbl-dctrl.1
@@ -1,5 +1,5 @@
 .TH tbl\-dctrl 1 2006-04-02 "Debian Project" "Debian administrator's manual"
-\" Copyright (C) 2005  Antti-Juhani Kaijanaho <ajk at debian.org>
+\" Copyright (C) 2005, 2010  Antti-Juhani Kaijanaho <ajk at debian.org>
 \"      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
@@ -20,9 +20,10 @@ tbl\-dctrl \- generate tabular representations of data in dctrl format
 .B tbl\-dctrl
 [
 .I options
-]
+] [
 .B \-c
 .IR column\-specification " ..."
+]
 [
 .I filename
 ] ...
@@ -58,7 +59,11 @@ In such a case,
 will include in the output a column whose title is the literal column
 specification and whose data is drawn from fields with that name.
 .
-At least one column must be requested.
+If no
+.B \-c
+options are given,
+.B tbl\-dctrl
+will use all fields in the input in the order in which they first appear.
 .PP
 There are two optional additions one can make to a column
 specification.
diff --git a/sort-dctrl/sort-dctrl.c b/sort-dctrl/sort-dctrl.c
index 852b89b..5e20cc8 100644
--- a/sort-dctrl/sort-dctrl.c
+++ b/sort-dctrl/sort-dctrl.c
@@ -1,5 +1,5 @@
 /*  dctrl-tools - Debian control file inspection tools
-    Copyright © 2004, 2005, 2006, 2007, 2008, 2009 Antti-Juhani Kaijanaho
+    Copyright © 2004, 2005, 2006, 2007, 2008, 2009, 2010 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
@@ -174,7 +174,7 @@ int main(int argc, char * argv[])
 	bundle_init(&pb);
 
 	for (size_t i = 0; i < args.num_fnames; i++) {
-		bundle_slurp(&pb, args.fname[i]);
+		bundle_slurp(&pb, args.fname[i], false);
 	}
 
 	sort_bundle(&args.keys, &pb);
diff --git a/tbl-dctrl/tbl-dctrl.c b/tbl-dctrl/tbl-dctrl.c
index fa4f8fc..b90a578 100644
--- a/tbl-dctrl/tbl-dctrl.c
+++ b/tbl-dctrl/tbl-dctrl.c
@@ -1,5 +1,5 @@
 /*  dctrl-tools - Debian control file inspection tools
-    Copyright © 2005, 2006, 2007, 2008, 2009 Antti-Juhani Kaijanaho
+    Copyright © 2005, 2006, 2007, 2008, 2009, 2010 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
@@ -385,23 +385,40 @@ int main(int argc, char * argv[])
 		};
 	}
 
-	if (args.need_preprocessing) {
+	if (args.num_columns == 0 || args.need_preprocessing) {
 		struct para_bundle pb;
 
 		bundle_init(&pb);
 		
 		for (size_t i = 0; i < args.num_fnames; i++) {
-			bundle_slurp(&pb, args.fname[i]);
+			bundle_slurp(&pb, args.fname[i],
+                                     args.num_columns == 0);
 		}
 
-		size_t n = fieldtrie_count();
+		const size_t n = fieldtrie_count();
+
+                if (args.num_columns == 0) {
+                        size_t m = n <= MAX_COLUMNS ? n : MAX_COLUMNS;
+                        for (size_t i = 0; i < m; i++) {
+                                struct field_attr *fa = fieldtrie_get(i);
+                                args.columns[i].heading =
+                                        strndup(fa->name, fa->namelen);
+                                if (args.columns[i].heading == NULL)
+                                        fatal_enomem(NULL);
+                                args.columns[i].field_inx = fa->inx;
+                                args.columns[i].column_width = -1;
+                        }
+                        args.num_columns = m;
+                }
+
 		size_t lens[n];
 		for (size_t i = 0; i < n; i++) lens[i] = 0;
 
 		for (size_t i = 0; i < pb.num_paras; i++) {
 			struct paragraph *p = pb.paras[i];
-			assert(p->nfields == n);
+			assert(p->nfields <= n);
 			for (size_t j = 0; j < p->nfields; j++) {
+                                if (!p->fields[j].present) continue;
 				struct fsaf_read_rv r = get_field(p, j, -1);
 				size_t len = mbs_len(r.b, r.len,
 						     p->common->fp->fname);
diff --git a/tests/0005.out b/tests/bug596424.in
similarity index 100%
copy from tests/0005.out
copy to tests/bug596424.in
diff --git a/tests/0003.out b/tests/bug596424.out
similarity index 100%
copy from tests/0003.out
copy to tests/bug596424.out
diff --git a/tests/bug596424.sh b/tests/bug596424.sh
new file mode 100644
index 0000000..fcce49f
--- /dev/null
+++ b/tests/bug596424.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+set -e
+
+$TBL_DCTRL bug596424.in

-- 
Debian control file query tools



More information about the Dctrl-tools-devel mailing list