=================================================================== RCS file: /cvs/docbook2mdoc/statistics.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -p -r1.1 -r1.2 --- docbook2mdoc/statistics.c 2019/03/29 15:55:28 1.1 +++ docbook2mdoc/statistics.c 2019/03/29 18:09:43 1.2 @@ -1,4 +1,4 @@ -/* $Id: statistics.c,v 1.1 2019/03/29 15:55:28 schwarze Exp $ */ +/* $Id: statistics.c,v 1.2 2019/03/29 18:09:43 schwarze Exp $ */ /* * Copyright (c) 2019 Ingo Schwarze * @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -54,18 +55,30 @@ static size_t stacki; /* * Count one instance of a parent-child relation. + * Before the special call table_add(NULL, NULL), + * mark relations to not be counted; + * in that phase, child can be NULL as a wildcard. */ static void table_add(const char *parent, const char *child) { - size_t i; + static int init_done; + size_t i; + if (parent == NULL && child == NULL) { + init_done = 1; + return; + } + /* If the table entry already exists, increment its count. */ for (i = 0; i < tablei; i++) { if (strcmp(parent, table[i].parent) == 0 && - strcmp(child, table[i].child) == 0) { - table[i].count++; + (child == NULL || table[i].child == NULL || + strcmp(child, table[i].child) == 0)) { + assert(init_done); + if (table[i].count != -1) + table[i].count++; return; } } @@ -83,9 +96,11 @@ table_add(const char *parent, const char *child) if ((table[tablei].parent = strdup(parent)) == NULL) err(1, NULL); - if ((table[tablei].child = strdup(child)) == NULL) + if (child == NULL) + table[tablei].child = NULL; + else if ((table[tablei].child = strdup(child)) == NULL) err(1, NULL); - table[tablei++].count = 1; + table[tablei++].count = init_done ? 1 : -1; } /* @@ -232,7 +247,7 @@ parse_file(int fd, char *fname) poff++; } else if (b[poff] != '!' && b[poff] != '?') { table_add(stacki > 0 ? - stack[stacki - 1] : "", + stack[stacki - 1] : "ROOT", b + poff); stack_push(b + poff); } @@ -258,12 +273,28 @@ main(int argc, char *argv[]) char *fname; size_t fsz, i; ssize_t rsz; - int fd; + int ch, fd, show_all; - fd = -1; - fname = NULL; + show_all = 0; + while ((ch = getopt(argc, argv, "a")) != -1) { + switch (ch) { + case 'a': + show_all = 1; + break; + default: + return 1; + } + } + /* Exclude relations that are already fully implemented. */ + if (show_all == 0) { + table_add("para", NULL); + } + table_add(NULL, NULL); + /* Loop over input files. */ + fd = -1; + fname = NULL; while ((rsz = getline(&fname, &fsz, stdin)) != -1) { if (fname[rsz - 1] == '\n') fname[--rsz] = '\0'; @@ -282,7 +313,8 @@ main(int argc, char *argv[]) /* Dump results. */ for (i = 0; i < tablei; i++) - printf("%d\t%s\t%s\n", table[i].count, - table[i].parent, table[i].child); + if (table[i].count != -1) + printf("%d\t%s\t%s\n", table[i].count, + table[i].parent, table[i].child); return 0; }