commit c0a508241b974e32485a86191d363ee29fe64cb1 from: Sven M. Hallberg date: Sun Jul 02 10:16:31 2023 UTC support multiple oids on the command line commit - 5772a2bc0cde0d65bafcc61ae81f5d156fb8e217 commit + c0a508241b974e32485a86191d363ee29fe64cb1 blob - 7f54083301b8555c1ce140580b03944de320b6d8 blob + 7c59c8704db10ae825fbbacf4672e7a63a4304aa --- pdf.1.mdoc +++ pdf.1.mdoc @@ -7,10 +7,10 @@ .Sh SYNOPSIS .Nm pdf .Op Fl qsv -.Op Fl d Ar what +.Op Fl d Ar type .Op Fl x Ar txtfile .Ar input.pdf -.Op Ar oid +.Op Ar oid ... .Sh DESCRIPTION The .Nm @@ -19,8 +19,8 @@ It prints the resulting AST to standard output using a .Pp The optional .Ar oid -argument selects a specific object to be printed instead of the whole document. -It is expected to be of the form +arguments select specific objects to be printed instead of the whole document. +Each is expected to be of the form .Dq Va n . Ns Va g where .Va n blob - b461f20777868ac50849218b64b8aeabeb02e9e0 blob + 047745e413075672abecbc5108fe1f15345288b6 --- pdf.c +++ pdf.c @@ -4743,7 +4743,7 @@ const char *progname; const char *infile; const char *xfile; int qflag, vflag, dflag; -int onum = -1, ogen = -1; /* -1 means unspecified */ +int *onum, *ogen; /* helper: parse 's' as a natural number. returns -1 on error. */ int @@ -4766,7 +4766,8 @@ strtonat(const char *s) void usage(void) { - fprintf(stderr, "usage: %s [-qsv] [-d what] [-x out.txt] file [oid]\n", + fprintf(stderr, + "usage: %s [-qsv] [-d type] [-x out.txt] file [oid ...]\n", progname); exit(2); } @@ -4906,11 +4907,11 @@ main(int argc, char *argv[]) { struct Env aux; HParseResult *res = NULL; - const HParsedToken *obj = NULL; + const HParsedToken **obj = NULL; const uint8_t *input; off_t sz; int fd; - int ch, n; + int ch, i, n; /* command line handling */ progname = argv[0]; @@ -4918,7 +4919,7 @@ main(int argc, char *argv[]) switch(ch) { case 'd': /* dump object */ if (strlen(optarg) != 1 || - strchr("s", optarg[0]) == NULL) + strchr("sc", optarg[0]) == NULL) errx(2, "-d: invalid argument"); dflag = optarg[0]; break; @@ -4952,21 +4953,31 @@ main(int argc, char *argv[]) } argc -= optind; argv += optind; - if (argc < 1 || argc > 2) + if (argc < 1) usage(); infile = argv[0]; - if (dflag && argc < 2) + argc--; + argv++; + if (dflag && argc < 1) errx(2, "-d requires an oid argument"); - if (argc > 1) { + if (argc > 0) { + onum = calloc(argc, sizeof *onum); + ogen = calloc(argc, sizeof *ogen); + obj = calloc(argc, sizeof *obj); + if (onum == NULL || ogen == NULL) + err(2, "object id arguments"); + } + for (i = 0; i < argc; i++) { char *a, *b; - a = strtok(argv[1], ". "); + ogen[i] = -1; /* -1 = unspecified */ + a = strtok(argv[i], ". "); b = strtok(NULL, ". "); - if ((onum = strtonat(a)) == -1 || - (b != NULL && (ogen = strtonat(b)) == -1)) + if ((onum[i] = strtonat(a)) == -1 || + (b != NULL && (ogen[i] = strtonat(b)) == -1)) errx(2, "invalid oid argument; expected num[.gen]"); - if (onum < 1) - errx(2, "invalid object number %d", onum); + if (onum[i] < 1) + errx(2, "invalid object number %d", onum[i]); } /* configure output verbosity */ @@ -5025,16 +5036,18 @@ main(int argc, char *argv[]) return 1; } - /* find the requested object */ - if (onum != -1) { - obj = findobj(res->ast, onum, ogen); - if (obj == NULL) { - if (ogen == -1) { - errx(2, "%s: object %d not found", infile, - onum); - } else { - errx(2, "%s: object %d.%d not found", infile, - onum, ogen); + /* find the requested objects */ + if (obj != NULL) { + for (i = 0; i < argc; i++) { + obj[i] = findobj(res->ast, onum[i], ogen[i]); + if (obj[i] == NULL) { + if (ogen[i] == -1) { + errx(2, "%s: object %d not found", + infile, onum[i]); + } else { + errx(2, "%s: object %d.%d not found", + infile, onum[i], ogen[i]); + } } } } @@ -5042,9 +5055,11 @@ main(int argc, char *argv[]) /* print desired output */ if (!qflag) { if (dflag == 's') - dumpstream(stdout, obj); + for (i = 0; i < argc; i++) + dumpstream(stdout, obj[i]); else if (obj != NULL) - h_pprintln(stdout, obj); + for (i = 0; i < argc; i++) + h_pprintln(stdout, obj[i]); else h_pprintln(stdout, res->ast); }