Root/qpkg/qpkg.c

Source at commit a9f12d56661a8e6def5a2b32519c3efd55e38d31 created 8 years 11 months ago.
By Werner Almesberger, qpkg: converted ID comparison from "struct id *" to "void *"
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5#include "id.h"
6#include "prereq.h"
7#include "gobble.h"
8#include "qpkg.h"
9
10
11struct tree *packages = NULL;
12struct tree *versions = NULL;
13
14
15static void list_all_packages(void)
16{
17    const struct id *id;
18
19    for (id = first_id(packages); id; id = next_id(id)) {
20        struct pkg *pkg = id->value;
21
22        printf("%.*s", (int) id->len, id->s);
23        if (!pkg)
24            printf(" (virtual)");
25        else {
26            if (pkg->version)
27                printf(" (%.*s)",
28                    (int) pkg->version->len, pkg->version->s);
29            if (pkg->more)
30                printf(" +++");
31        }
32        printf("\n");
33    }
34}
35
36
37static void list_one_package(const char *name)
38{
39    const struct id *id = find_id(packages, name, strlen(name));
40    const struct pkg *pkg;
41
42    if (!id) {
43        fprintf(stderr, "no such package \"%s\"\n", name);
44        exit(1);
45    }
46    for (pkg = id->value; pkg; pkg = pkg->more)
47        printf("%.*s\n", (int) pkg->version->len, pkg->version->s);
48}
49
50
51static void find_prereq(const char *name, const char *version)
52{
53    const struct id *id = find_id(packages, name, strlen(name));
54    struct pkg *pkg;
55    struct pkg **pkgs;
56
57    if (!id) {
58        fprintf(stderr, "no such package \"%s\"\n", name);
59        exit(1);
60    }
61    pkg = id->value;
62    if (!pkg) {
63        fprintf(stderr, "package %s is a ghost\n", name);
64        exit(1);
65    }
66    if (version) {
67        id = find_id(versions, version, strlen(version));
68        if (!id) {
69            fprintf(stderr, "no such version\"%s\"\n", version);
70            exit(1);
71        }
72        while (pkg && pkg->version != id)
73            pkg = pkg->more;
74        if (pkg) {
75            fprintf(stderr, "no %s version\"%s\" found\n",
76                name, version);
77            exit(1);
78        }
79    }
80
81    pkgs = prereq(pkg);
82    if (!pkgs) {
83        fprintf(stderr, "can't resolve %s%s%s\n",
84            name, version ? " version " : "", version ? version : "");
85        exit(1);
86    }
87    while (*pkgs) {
88        const char *file = (*pkgs)->filename;
89        const char *end;
90
91        if (file) {
92            end = strchr(file, '\n');
93            printf("%.*s\n", (int) (end-file), file);
94        }
95        pkgs++;
96    }
97}
98
99
100static void usage(const char *name)
101{
102    fprintf(stderr,
103"usage: %s [pkg-list ...] list [pkg]\n"
104" %s [pkg-list ...] prereq pkg [version]\n"
105    , name, name);
106    exit(1);
107}
108
109
110int main(int argc, char **argv)
111{
112    int arg;
113
114    if (argc == 1)
115        usage(*argv);
116    packages = make_tree(comp_id);
117    versions = make_tree(comp_id);
118    for (arg = 1; arg != argc; arg++) {
119        if (*argv[arg] == '-')
120            usage(*argv);
121        if (!strcmp(argv[arg], "list")) {
122            switch (argc-arg) {
123            case 1:
124                list_all_packages();
125                return 0;
126            case 2:
127                list_one_package(argv[arg+1]);
128                return 0;
129            default:
130                usage(*argv);
131            }
132        } else if (!strcmp(argv[arg], "prereq")) {
133            switch (argc-arg) {
134            case 2:
135                find_prereq(argv[arg+1], NULL);
136                return 0;
137            case 3:
138                find_prereq(argv[arg+1], argv[arg+2]);
139                return 0;
140            default:
141                usage(*argv);
142            }
143        } else {
144            gobble(argv[arg]);
145        }
146    }
147    return 0;
148}
149

Archive Download this file

Branches:
master



interactive