| 1 | #include <errno.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include <mLib/dstr.h> |
| 6 | #include <mLib/macros.h> |
| 7 | #include <mLib/quis.h> |
| 8 | #include <mLib/report.h> |
| 9 | |
| 10 | struct field { |
| 11 | const char *p; |
| 12 | size_t n; |
| 13 | }; |
| 14 | |
| 15 | int main(int argc, char *argv[]) |
| 16 | { |
| 17 | int i, j; |
| 18 | FILE *fp; |
| 19 | dstr d = DSTR_INIT, dd = DSTR_INIT; |
| 20 | const char *fn, *p, *q; |
| 21 | size_t tagsz; |
| 22 | struct field f[7]; |
| 23 | |
| 24 | ego(argv[0]); |
| 25 | |
| 26 | for (i = 1; i < argc; i++) { |
| 27 | fn = argv[i]; |
| 28 | if ((fp = fopen(fn, "r")) == 0) |
| 29 | die(1, "fopen(%s): %s", fn, strerror(errno)); |
| 30 | q = strrchr(fn, '.'); tagsz = q ? q - fn : strlen(fn); |
| 31 | |
| 32 | for (;;) { |
| 33 | DRESET(&d); DRESET(&dd); |
| 34 | if (dstr_putline(&d, fp) == EOF) break; |
| 35 | |
| 36 | if (!d.len || d.buf[0] == '*' || d.buf[0] == '[') continue; |
| 37 | |
| 38 | for (p = d.buf, j = 0; j < N(f); j++) { |
| 39 | while (*p == ' ') p++; |
| 40 | if (!*p) break; |
| 41 | f[j].p = p; |
| 42 | if (j == N(f) - 1) { |
| 43 | f[j++].n = d.buf + d.len - p; |
| 44 | break; |
| 45 | } |
| 46 | while (*p && *p != ' ') p++; |
| 47 | if (!*p) break; |
| 48 | f[j].n = p - f[j].p; |
| 49 | } |
| 50 | if (j < N(f)) die(1, "failed to parse input"); |
| 51 | |
| 52 | DPUTM(&dd, f[0].p, f[0].n); DPUTC(&dd, ' '); |
| 53 | DPUTM(&dd, f[2].p, f[2].n); DPUTC(&dd, ' '); |
| 54 | DPUTM(&dd, f[3].p, f[3].n); DPUTC(&dd, ' '); |
| 55 | DPUTM(&dd, f[4].p, f[4].n); DPUTC(&dd, ' '); |
| 56 | DPUTM(&dd, f[5].p, f[5].n); DPUTC(&dd, '|'); |
| 57 | DPUTM(&dd, fn, tagsz); DPUTC(&dd, '|'); |
| 58 | DPUTM(&dd, f[6].p, f[6].n); DPUTC(&dd, '\n'); |
| 59 | fwrite(dd.buf, 1, dd.len, stdout); |
| 60 | } |
| 61 | |
| 62 | if (ferror(fp)) die(1, "ferror(%s)", fn); |
| 63 | fclose(fp); |
| 64 | } |
| 65 | |
| 66 | if (ferror(stdout) || fflush(stdout) || fclose(stdout)) |
| 67 | die(1, "stdout: %s", strerror(errno)); |
| 68 | |
| 69 | return (0); |
| 70 | } |