1 #define _POSIX_C_SOURCE 2
23 int (*callback)(void*,int,char**,char**);
27 printf("usage: ...\n");
30 static int run_program(void *f, int ncols, char **vals, char **cols) {
31 struct config *conf = f;
38 argv[0] = conf->program;
40 for (i=0;i<ncols;i++) {
41 argv[i+1] = vals[i] ? vals[i] : conf->nullstr;
48 perror("cannot fork");
54 rv = execvp(conf->program, argv);
56 perror("cannot exec");
63 perror("error waiting for child");
66 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
68 } else if (conf->errcontinue) {
75 static int run_shell(void *f, int ncols, char **vals, char **cols) {
76 struct config *conf = f;
88 for (i=0;i<ncols;i++) {
89 argv[i+4] = vals[i] ? vals[i] : conf->nullstr;
96 perror("cannot fork");
102 rv = execvp("sh", argv);
104 perror("cannot exec");
111 perror("error waiting for child");
113 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
115 } else if (conf->errcontinue) {
122 static int printpaths(void *f, int ncols, char **vals, char **cols) {
124 /* suppress unused warnings */
127 fprintf(stderr, "can't find path names\n");
130 // printf("path cols %d %p %p\n", ncols, vals, cols);
131 fprintf(stdout, "%s\n", vals[3]);
137 fprintf(stderr, "sqlite can't get column names\n");
139 for (i=0;i<ncols;i++) {
140 // if (i>3) fprintf(out, "\t");
141 fprintf(stdout, "%s\n", vals[i]);
147 void parse_package(char *pstr, char *name, char *ver, int *rel) {
152 /* string - ver - rel */
153 /* rel is all digits */
155 * ^(.+)-([0-9][^-]*)-([\d+])$
156 * ^(.+)-([0-9][^-]*)$
158 * The main problem in parsing is that the package name itself
159 * can contain a '-', so you can't just split on '-'
160 * Also, the version can be just digits.
163 /* everything up to the first '-' is in the name */
165 if (*pstr == '-' && isdigit(*(pstr+1))) {
177 while (*pstr && *pstr != '-') {
193 #define PATHLEN PATH_MAX
198 int main(int ac, char **av){
203 struct config conf = { "/var/lib/db.zpm", 0, 0, 0, "", 0, 0, printpaths
207 if ((s = getenv("ZPMDB"))) {
208 /* TODO does this need to be copied ? */
214 * -f 'package database', otherwise regular default of env ZPMDB,
215 * or /var/lib/zpm/zpm.db, or die
217 * arg 1: pkgid triple, but will do a pkg find
218 * arg 2: program to run. equivalent of 'printf "%s\n" "$path"
222 while ((opt = getopt(ac, av, "f:c:n:C")) != -1) {
224 case 'f': conf.dbfile = optarg;
226 case 'c': conf.cmd = optarg;
227 conf.callback = run_shell;
230 conf.errcontinue = 1;
233 conf.nullstr = optarg;
243 if (!zpm_open(&pkg, conf.dbfile)) {
244 fprintf(stderr, "can't open zpm db %s\n", conf.dbfile);
250 conf.pkgid = av[argn];
251 // fprintf(stderr, "set pkgid to %s\n", conf.pkgid);
254 fprintf(stderr, "must specify pkgid\n");
258 /* TODO lookup pkgid via zpm-findpkg equivalent */
261 conf.program = av[argn];
262 conf.callback = run_program;
264 /* TODO set conf.args to remaining arguments */
267 /* install a collation function */
268 // zpm_addvercmp(&pkg);
270 if (!zpm_foreach_path(&pkg, conf.pkgid, conf.callback, &conf, &errmsg)) {
272 fprintf(stderr, "database error: %s\n", errmsg);
275 if (pkg.error == 1) {
276 fprintf(stderr, "unable to allocate memory\n");
282 return conf.errors ? 1 : 0;