1 #define _POSIX_C_SOURCE 2
23 int (*callback)(void*,int,char**,char**);
27 printf("usage: zpm foreach-path [-fncC] args ...\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/zpm/local.db", 0, 0, 0, "", 0, 0, printpaths
206 if ((s = getenv("ZPMDB"))) {
207 /* TODO does this need to be copied ? */
213 * -f 'package database', otherwise regular default of env ZPMDB,
214 * or /var/lib/zpm/zpm.db, or die
216 * arg 1: pkgid triple, but will do a pkg find
217 * arg 2: program to run. equivalent of 'printf "%s\n" "$path"
221 while ((opt = getopt(ac, av, "f:c:n:C")) != -1) {
223 case 'f': conf.dbfile = optarg;
225 case 'c': conf.cmd = optarg;
226 conf.callback = run_shell;
229 conf.errcontinue = 1;
232 conf.nullstr = optarg;
242 if (!zpm_open(&pkg, conf.dbfile)) {
243 fprintf(stderr, "can't open zpm db %s\n", conf.dbfile);
249 conf.pkgid = av[argn];
250 // fprintf(stderr, "set pkgid to %s\n", conf.pkgid);
253 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, 0, 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;