From: Nathan Wagner Date: Sat, 15 Sep 2018 09:03:00 +0000 (+0000) Subject: add zpm-parse program X-Git-Tag: v0.1.6~36 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=7e811bbe6fd035ebe52523434d55468e85f119fc add zpm-parse program --- diff --git a/Makefile b/Makefile index 2e29167..51cd85a 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ test: $(ZPKGBIN) t/ctap/prove PATH=$(curdir)/t:$(curdir):$(PATH) t/ctap/prove t/*.t programs: elftype zpm-soname zpm-soneed zpm-addfile zpm-extract zpm-init \ - zpm-vercmp zpm-findpkg zpm-merge + zpm-vercmp zpm-findpkg zpm-merge zpm-quote zpm-parse uncompress: uncompress.o $(CC) $(CFLAGS) -o $@ $+ -llzma @@ -129,6 +129,9 @@ zpm-foreach-path: zpm-foreach-path.o libzpm.a sqlite/sqlite3.h zpm-findpkg: zpm-findpkg.o libzpm.a $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf +zpm-parse: zpm-parse.o libzpm.a + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf + zpm-quote: zpm-quote.o libzpm.a $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf diff --git a/zpm-parse.c b/zpm-parse.c new file mode 100644 index 0000000..ec05356 --- /dev/null +++ b/zpm-parse.c @@ -0,0 +1,75 @@ +#define _POSIX_C_SOURCE 2 +#include +#include +#include + +#include "zpm.h" + +int main(int ac, char **av) { + int rel; + char name[256]; + char ver[256]; + int i; + int found; + int pname = 0; + int pver = 0; + int prel = 0; + int printed = 0; + int opt; + int pdefault = 1; + int eval = 0; + + while ((opt = getopt(ac, av, "nvrE")) != -1) { + switch (opt) { + case 'n': pdefault = 0; pname = 1; break; + case 'v': pdefault = 0; pver = 1; break; + case 'r': pdefault = 0; prel = 1; break; + case 'E': eval = 1; break; + default: + fprintf(stderr, "zpm parse [-nvrE] ...\n"); + exit(EXIT_FAILURE); + break; + } + } + + pname = pname || pdefault; + pver = pver || pdefault; + prel = prel || pdefault; + + /* TODO need to quote single quotes for eval'able output */ + for (i=optind; i= 1 && pname) { + if (eval) { + printf("name='%s'\n", name); + } else { + printf("%s", name); + printed++; + } + } + if (found >= 2 && pver) { + if (eval) { + printf("version='%s'\n", ver); + } else { + printf("%s%s", printed ? " " : "", ver); + printed++; + } + } + if (found >= 3 && prel) { + if (eval) { + printf("release='%d'\n", rel); + } else { + printf("%s%d", printed ? " " : "", rel); + printed++; + } + } + if (printed) { + putchar('\n'); + } + if (!found) { + exit(EXIT_FAILURE); + } + } + return 0; +}