2010-11-20 12:29:18 +02:00
|
|
|
/*
|
|
|
|
* qpkg.c - Quick package database query
|
|
|
|
*
|
|
|
|
* Written 2010 by Werner Almesberger
|
|
|
|
* Copyright 2010 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-11-19 19:00:15 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2010-11-21 13:11:40 +02:00
|
|
|
#include <assert.h>
|
2010-11-19 19:00:15 +02:00
|
|
|
|
2010-11-20 18:11:41 +02:00
|
|
|
#include "jrb.h"
|
2010-11-19 19:00:15 +02:00
|
|
|
#include "id.h"
|
|
|
|
#include "prereq.h"
|
|
|
|
#include "gobble.h"
|
|
|
|
#include "qpkg.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct tree *packages = NULL;
|
|
|
|
struct tree *versions = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static void list_all_packages(void)
|
|
|
|
{
|
2010-11-20 18:11:41 +02:00
|
|
|
const struct jrb *n;
|
2010-11-19 19:00:15 +02:00
|
|
|
|
2010-11-20 18:11:41 +02:00
|
|
|
for (n = jrb_first(packages->root); n; n = jrb_next(n)) {
|
|
|
|
const struct id *id = n->key;
|
|
|
|
struct pkg *pkg = n->val;
|
2010-11-19 19:00:15 +02:00
|
|
|
|
|
|
|
printf("%.*s", (int) id->len, id->s);
|
|
|
|
if (!pkg)
|
|
|
|
printf(" (virtual)");
|
|
|
|
else {
|
|
|
|
if (pkg->version)
|
|
|
|
printf(" (%.*s)",
|
|
|
|
(int) pkg->version->len, pkg->version->s);
|
|
|
|
if (pkg->more)
|
|
|
|
printf(" +++");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void list_one_package(const char *name)
|
|
|
|
{
|
2010-11-20 18:11:41 +02:00
|
|
|
const struct jrb *n = find_id(packages, name, strlen(name));
|
2010-11-19 19:00:15 +02:00
|
|
|
const struct pkg *pkg;
|
|
|
|
|
2010-11-20 18:11:41 +02:00
|
|
|
if (!n) {
|
2010-11-19 19:00:15 +02:00
|
|
|
fprintf(stderr, "no such package \"%s\"\n", name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-11-20 18:11:41 +02:00
|
|
|
for (pkg = n->val; pkg; pkg = pkg->more)
|
2010-11-19 19:00:15 +02:00
|
|
|
printf("%.*s\n", (int) pkg->version->len, pkg->version->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void find_prereq(const char *name, const char *version)
|
|
|
|
{
|
2010-11-20 18:11:41 +02:00
|
|
|
const struct jrb *n = find_id(packages, name, strlen(name));
|
2010-11-19 19:00:15 +02:00
|
|
|
struct pkg *pkg;
|
|
|
|
struct pkg **pkgs;
|
|
|
|
|
2010-11-20 18:11:41 +02:00
|
|
|
if (!n) {
|
2010-11-19 19:00:15 +02:00
|
|
|
fprintf(stderr, "no such package \"%s\"\n", name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-11-20 18:11:41 +02:00
|
|
|
pkg = n->val;
|
2010-11-19 19:00:15 +02:00
|
|
|
if (!pkg) {
|
|
|
|
fprintf(stderr, "package %s is a ghost\n", name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (version) {
|
2010-11-20 18:11:41 +02:00
|
|
|
n = find_id(versions, version, strlen(version));
|
|
|
|
if (!n) {
|
2010-11-19 19:00:15 +02:00
|
|
|
fprintf(stderr, "no such version\"%s\"\n", version);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-11-20 18:11:41 +02:00
|
|
|
while (pkg && pkg->version != n->key)
|
2010-11-19 19:00:15 +02:00
|
|
|
pkg = pkg->more;
|
|
|
|
if (pkg) {
|
|
|
|
fprintf(stderr, "no %s version\"%s\" found\n",
|
|
|
|
name, version);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgs = prereq(pkg);
|
|
|
|
if (!pkgs) {
|
|
|
|
fprintf(stderr, "can't resolve %s%s%s\n",
|
|
|
|
name, version ? " version " : "", version ? version : "");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while (*pkgs) {
|
|
|
|
const char *file = (*pkgs)->filename;
|
|
|
|
const char *end;
|
|
|
|
|
2010-11-21 13:11:40 +02:00
|
|
|
assert(file);
|
|
|
|
end = strchr(file, '\n');
|
|
|
|
printf("%.*s\n", (int) (end-file), file);
|
2010-11-19 19:00:15 +02:00
|
|
|
pkgs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [pkg-list ...] list [pkg]\n"
|
|
|
|
" %s [pkg-list ...] prereq pkg [version]\n"
|
|
|
|
, name, name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int arg;
|
|
|
|
|
|
|
|
packages = make_tree(comp_id);
|
|
|
|
versions = make_tree(comp_id);
|
2010-11-21 08:53:19 +02:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
usage(*argv);
|
|
|
|
|
2010-11-19 19:00:15 +02:00
|
|
|
for (arg = 1; arg != argc; arg++) {
|
2010-11-19 19:21:55 +02:00
|
|
|
if (*argv[arg] == '-')
|
|
|
|
usage(*argv);
|
2010-11-19 19:00:15 +02:00
|
|
|
if (!strcmp(argv[arg], "list")) {
|
|
|
|
switch (argc-arg) {
|
|
|
|
case 1:
|
|
|
|
list_all_packages();
|
|
|
|
return 0;
|
|
|
|
case 2:
|
|
|
|
list_one_package(argv[arg+1]);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[arg], "prereq")) {
|
|
|
|
switch (argc-arg) {
|
|
|
|
case 2:
|
|
|
|
find_prereq(argv[arg+1], NULL);
|
|
|
|
return 0;
|
|
|
|
case 3:
|
|
|
|
find_prereq(argv[arg+1], argv[arg+2]);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gobble(argv[arg]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|