]> pd.if.org Git - zpackage/blob - src/parse.c
move C source files into src
[zpackage] / src / parse.c
1 #define _POSIX_C_SOURCE 2
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "zpm.h"
7
8 int main(int ac, char **av) {
9         int rel;
10         char name[256];
11         char ver[256];
12         int i;
13         int found;
14         int pname = 0;
15         int pver = 0;
16         int prel = 0;
17         int printed = 0;
18         int opt;
19         int pdefault = 1;
20         int eval = 0;
21
22         while ((opt = getopt(ac, av, "nvrE")) != -1) {
23                 switch (opt) {
24                         case 'n': pdefault = 0; pname = 1; break;
25                         case 'v': pdefault = 0; pver = 1; break;
26                         case 'r': pdefault = 0; prel = 1; break;
27                         case 'E': eval = 1; break;
28                         default:
29                                   fprintf(stderr, "zpm parse [-nvrE] ...\n");
30                                   exit(EXIT_FAILURE);
31                                   break;
32                 }
33         }
34
35         pname = pname || pdefault;
36         pver = pver || pdefault;
37         prel = prel || pdefault;
38
39         for (i=optind; i<ac; i++) {
40                 printed = 0;
41                 found = zpm_parse_package(av[i], name, ver, &rel);
42                 /* single quotes aren't allowed, so a simple single quote
43                  * is sufficient for eval */
44                 if (found >= 1 && pname) {
45                         if (eval) {
46                                 printf("name='%s'\n", name);
47                         } else {
48                                 printf("%s", name);
49                                 printed++;
50                         }
51                 }
52                 if (found >= 2 && pver) {
53                         if (eval) {
54                                 printf("version='%s'\n", ver);
55                         } else {
56                                 printf("%s%s", printed ? " " : "", ver);
57                                 printed++;
58                         }
59                 }
60                 if (found >= 3 && prel) {
61                         if (eval) {
62                                 printf("release='%d'\n", rel);
63                         } else {
64                                 printf("%s%d", printed ? " " : "", rel);
65                                 printed++;
66                         }
67                 }
68                 if (printed) {
69                         putchar('\n');
70                 }
71                 if (!found) {
72                         exit(EXIT_FAILURE);
73                 }
74         }
75         return 0;
76 }