From c05e6d5dbb521b93d69ee953c643bb83b0721533 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 21 Nov 2010 22:53:27 -0300 Subject: [PATCH] qpkg: sort package version list such that highest comes first - Makefile (OBJS_qpkg): added fixup.o - fixup.h, fixup.c (sort_versions): sort package versions - qpkg.c (do_fixups, main): do fixups when done gobbling - qpkg.h: need to include id.h to be self-contained - test/sortver: test version sorting --- qpkg/Makefile | 2 +- qpkg/fixup.c | 42 +++++++++++++ qpkg/fixup.h | 18 ++++++ qpkg/qpkg.c | 9 +++ qpkg/qpkg.h | 3 + qpkg/test/sortver | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 qpkg/fixup.c create mode 100644 qpkg/fixup.h create mode 100755 qpkg/test/sortver diff --git a/qpkg/Makefile b/qpkg/Makefile index e482a38..62afe7b 100644 --- a/qpkg/Makefile +++ b/qpkg/Makefile @@ -12,7 +12,7 @@ SHELL = /bin/bash -OBJS_qpkg = gobble.o id.o prereq.o qpkg.o jrb.o +OBJS_qpkg = fixup.o gobble.o id.o prereq.o qpkg.o jrb.o OBJS_rbtest = rbtest.o jrb.o OBJS = $(OBJS_qpkg) $(OBJS_rbtest) diff --git a/qpkg/fixup.c b/qpkg/fixup.c new file mode 100644 index 0000000..55e3d76 --- /dev/null +++ b/qpkg/fixup.c @@ -0,0 +1,42 @@ +/* + * fixup.c - Adjust things after parsing is done + * + * 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. + */ + + +#include +#include "jrb.h" + +#include "qpkg.h" +#include "fixup.h" + + +void sort_versions(void) +{ + const struct jrb *n; + struct pkg **a, **b; + struct pkg *tmp; + + for (n = jrb_first(packages->root); n != jrb_nil(packages->root); + n = jrb_next(n)) + for (a = (struct pkg **) &n->val; *a; a = &(*a)->more) + for (b = &(*a)->more; *b;) + if (comp_versions((*a)->version, + (*b)->version) >= 0) + b = &(*b)->more; + else { + tmp = *a; + *a = *b; + *b = tmp; + tmp = (*a)->more; + (*a)->more = (*b)->more; + (*b)->more = tmp; + } +} diff --git a/qpkg/fixup.h b/qpkg/fixup.h new file mode 100644 index 0000000..7671fe7 --- /dev/null +++ b/qpkg/fixup.h @@ -0,0 +1,18 @@ +/* + * fixup.c - Adjust things after parsing is done + * + * 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. + */ + +#ifndef FIXUP_H +#define FIXUP_H + +void sort_versions(void); + +#endif /* !FIXUP_H */ diff --git a/qpkg/qpkg.c b/qpkg/qpkg.c index a4799b7..ce428de 100644 --- a/qpkg/qpkg.c +++ b/qpkg/qpkg.c @@ -20,6 +20,7 @@ #include "id.h" #include "prereq.h" #include "gobble.h" +#include "fixup.h" #include "qpkg.h" @@ -116,6 +117,12 @@ static void find_prereq(const char *name, const char *version) } +static void do_fixups(void) +{ + sort_versions(); +} + + static void usage(const char *name) { fprintf(stderr, @@ -145,6 +152,7 @@ int main(int argc, char **argv) if (*argv[arg] == '-') usage(*argv); if (!strcmp(argv[arg], "list")) { + do_fixups(); switch (argc-arg) { case 1: list_all_packages(); @@ -156,6 +164,7 @@ int main(int argc, char **argv) usage(*argv); } } else if (!strcmp(argv[arg], "prereq")) { + do_fixups(); switch (argc-arg) { case 2: find_prereq(argv[arg+1], NULL); diff --git a/qpkg/qpkg.h b/qpkg/qpkg.h index 96e862e..d0a1593 100644 --- a/qpkg/qpkg.h +++ b/qpkg/qpkg.h @@ -13,6 +13,9 @@ #ifndef QPKG_H #define QPKG_H +#include "id.h" + + enum flags { /* parse-time flags */ QPKG_INSTALLED = 1 << 0, /* installed on target */ diff --git a/qpkg/test/sortver b/qpkg/test/sortver new file mode 100755 index 0000000..7badd93 --- /dev/null +++ b/qpkg/test/sortver @@ -0,0 +1,146 @@ +#!/bin/sh +. ./Common + +############################################################################### + +qpkg "sort versions 1 2 3" list <