20d38720edb5323c9c0aad549ceb502b812e5685
[oweals/opkg-lede.git] / opkg_cmd.c
1 /* opkg_cmd.c - the itsy package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include <string.h>
19
20 #include "opkg.h"
21 #include <libgen.h>
22 #include <glob.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <dirent.h>
29
30 #include "opkg_conf.h"
31 #include "opkg_cmd.h"
32 #include "opkg_message.h"
33 #include "pkg.h"
34 #include "pkg_dest.h"
35 #include "pkg_parse.h"
36 #include "sprintf_alloc.h"
37 #include "pkg.h"
38 #include "file_util.h"
39 #include "str_util.h"
40 #include "libbb/libbb.h"
41
42 #include <fnmatch.h>
43
44
45 #include "opkg_download.h"
46 #include "opkg_install.h"
47 #include "opkg_upgrade.h"
48 #include "opkg_remove.h"
49 #include "opkg_configure.h"
50 #include "opkg_message.h"
51
52 #ifdef OPKG_LIB
53 #include "libopkg.h"
54 static void *p_userdata = NULL;
55 #endif
56
57 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv);
58 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv);
59 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv);
60 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv);
61 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv);
62 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv);
63 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv);
64 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv);
65 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv);
66 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv);
67 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv);
68 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv);
69 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv);
70 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv);
71 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv);
72 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv);
73 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv);
74 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv);
75 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv);
76 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv);
77 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv);
78 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv);
79 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv);
80 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv);
81 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv);
82
83 /* XXX: CLEANUP: The usage strings should be incorporated into this
84    array for easier maintenance */
85 static opkg_cmd_t cmds[] = {
86      {"update", 0, (opkg_cmd_fun_t)opkg_update_cmd}, 
87      {"upgrade", 0, (opkg_cmd_fun_t)opkg_upgrade_cmd},
88      {"list", 0, (opkg_cmd_fun_t)opkg_list_cmd},
89      {"list_installed", 0, (opkg_cmd_fun_t)opkg_list_installed_cmd},
90      {"info", 0, (opkg_cmd_fun_t)opkg_info_cmd},
91      {"flag", 1, (opkg_cmd_fun_t)opkg_flag_cmd},
92      {"status", 0, (opkg_cmd_fun_t)opkg_status_cmd},
93      {"install_pending", 0, (opkg_cmd_fun_t)opkg_install_pending_cmd},
94      {"install", 1, (opkg_cmd_fun_t)opkg_install_cmd},
95      {"remove", 1, (opkg_cmd_fun_t)opkg_remove_cmd},
96      {"purge", 1, (opkg_cmd_fun_t)opkg_purge_cmd},
97      {"configure", 0, (opkg_cmd_fun_t)opkg_configure_cmd},
98      {"files", 1, (opkg_cmd_fun_t)opkg_files_cmd},
99      {"search", 1, (opkg_cmd_fun_t)opkg_search_cmd},
100      {"download", 1, (opkg_cmd_fun_t)opkg_download_cmd},
101      {"compare_versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
102      {"compare-versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
103      {"print-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
104      {"print_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
105      {"print-installation-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
106      {"print_installation_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
107      {"depends", 1, (opkg_cmd_fun_t)opkg_depends_cmd},
108      {"whatdepends", 1, (opkg_cmd_fun_t)opkg_whatdepends_cmd},
109      {"whatdependsrec", 1, (opkg_cmd_fun_t)opkg_whatdepends_recursively_cmd},
110      {"whatrecommends", 1, (opkg_cmd_fun_t)opkg_whatrecommends_cmd},
111      {"whatsuggests", 1, (opkg_cmd_fun_t)opkg_whatsuggests_cmd},
112      {"whatprovides", 1, (opkg_cmd_fun_t)opkg_whatprovides_cmd},
113      {"whatreplaces", 1, (opkg_cmd_fun_t)opkg_whatreplaces_cmd},
114      {"whatconflicts", 1, (opkg_cmd_fun_t)opkg_whatconflicts_cmd},
115 };
116
117 int opkg_state_changed;
118 static void write_status_files_if_changed(opkg_conf_t *conf)
119 {
120      if (opkg_state_changed && !conf->noaction) {
121           opkg_message(conf, OPKG_INFO,
122                        "  writing status file\n");
123           opkg_conf_write_status_files(conf);
124           pkg_write_changed_filelists(conf);
125      } else { 
126           opkg_message(conf, OPKG_NOTICE, "Nothing to be done\n");
127      }
128 }
129
130
131 static int num_cmds = sizeof(cmds) / sizeof(opkg_cmd_t);
132
133 opkg_cmd_t *opkg_cmd_find(const char *name)
134 {
135      int i;
136      opkg_cmd_t *cmd;
137
138      for (i=0; i < num_cmds; i++) {
139           cmd = &cmds[i];
140           if (strcmp(name, cmd->name) == 0) {
141                return cmd;
142           }
143      }
144
145      return NULL;
146 }
147
148 #ifdef OPKG_LIB
149 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
150 {
151         int result;
152         p_userdata = userdata;
153       
154
155         result = (cmd->fun)(conf, argc, argv);
156
157         if ( result != 0 ) {
158            opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
159         }
160
161         if ( error_list ) {
162            reverse_error_list(&error_list);
163
164            opkg_message(conf, OPKG_NOTICE, "Collected errors:\n");
165            /* Here we print the errors collected and free the list */
166            while (error_list != NULL) {
167                  opkg_message(conf, OPKG_NOTICE, "%s",error_list->errmsg);
168                  error_list = error_list->next;
169
170            }
171            free_error_list(&error_list);
172
173         }
174    
175         p_userdata = NULL;
176         return result;
177 }
178 #else
179 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv)
180 {
181      return (cmd->fun)(conf, argc, argv);
182 }
183 #endif
184
185 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
186 {
187      int err;
188      int failures;
189      char *lists_dir;
190      pkg_src_list_elt_t *iter;
191      pkg_src_t *src;
192
193  
194     sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
195  
196     if (! file_is_dir(lists_dir)) {
197           if (file_exists(lists_dir)) {
198                opkg_message(conf, OPKG_ERROR,
199                             "%s: ERROR: %s exists, but is not a directory\n",
200                             __FUNCTION__, lists_dir);
201                free(lists_dir);
202                return EINVAL;
203           }
204           err = file_mkdir_hier(lists_dir, 0755);
205           if (err) {
206                opkg_message(conf, OPKG_ERROR,
207                             "%s: ERROR: failed to make directory %s: %s\n",
208                             __FUNCTION__, lists_dir, strerror(errno));
209                free(lists_dir);
210                return EINVAL;
211           }     
212      } 
213
214      failures = 0;
215      for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
216           char *url, *list_file_name;
217
218           src = iter->data;
219
220           if (src->extra_data)  /* debian style? */
221               sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, 
222                             src->gzip ? "Packages.gz" : "Packages");
223           else
224               sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
225
226           sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
227           if (src->gzip) {
228               char *tmp;
229               char *tmp_file_name;
230               FILE *in, *out;
231
232               tmp = strdup ("/tmp/opkg.XXXXXX");
233
234               if (mkdtemp (tmp) == NULL) {
235                   perror ("mkdtemp");
236                   failures++;
237                   continue;
238               }
239               
240               sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
241               err = opkg_download(conf, url, tmp_file_name);
242               if (err == 0) {
243                    opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url);
244                    in = fopen (tmp_file_name, "r");
245                    out = fopen (list_file_name, "w");
246                    if (in && out)
247                         unzip (in, out);
248                    else
249                         err = 1;
250                    if (in)
251                         fclose (in);
252                    if (out)
253                         fclose (out);
254                    unlink (tmp_file_name);
255                    rmdir (tmp);
256                    free (tmp);
257               }
258           } else
259               err = opkg_download(conf, url, list_file_name);
260           if (err) {
261                failures++;
262           } else {
263                opkg_message(conf, OPKG_NOTICE,
264                             "Updated list of available packages in %s\n",
265                             list_file_name);
266           }
267           free(url);
268
269           /* download detached signitures to verify the package lists */
270           /* get the url for the sig file */
271           if (src->extra_data)  /* debian style? */
272               sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
273                             "Packages.sig");
274           else
275               sprintf_alloc(&url, "%s/%s", src->value, "Packages.sig");
276
277           /* create temporary dir for it */
278           char *tmp, *tmp_file_name;
279           tmp = strdup ("/tmp/opkg.XXXXXX");
280           if (mkdtemp (tmp) == NULL) {
281                 perror ("mkdtemp");
282                 failures++;
283                 continue;
284           }
285           sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
286
287           err = opkg_download(conf, url, tmp_file_name);
288           if (err) {
289             failures++;
290           } else {
291             int err;
292             err = opkg_verify_file (list_file_name, tmp_file_name);
293             if (err == 0)
294                 opkg_message (conf, OPKG_NOTICE, "Signature check passed\n");
295             else
296                 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
297           }
298           unlink (tmp_file_name);
299           free (tmp_file_name);
300
301           free (url);
302           free(list_file_name);
303      }
304      free(lists_dir);
305
306 #ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
307 #warning here
308      /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
309       * this is a hack to work around poor bookkeeping in old opkg upgrade code 
310       * -Jamey 3/1/03
311       */
312      {
313           int i;
314           int changed = 0;
315           pkg_vec_t *available = pkg_vec_alloc();
316           pkg_hash_fetch_available(&conf->pkg_hash, available);
317           opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
318           for (i = 0; i < available->len; i++) {
319                pkg_t *pkg = available->pkgs[i];
320                if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
321                     opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
322                     pkg->state_want = SW_UNKNOWN;
323                     changed = 1;
324                }
325           }
326           pkg_vec_free(available);
327           if (changed) {
328                write_status_files_if_changed(conf);
329           }
330      }
331 #endif
332
333      return failures;
334 }
335
336
337 /* scan the args passed and cache the local filenames of the packages */
338 int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
339 {
340      int i;
341      int err;
342     
343      /* 
344       * First scan through package names/urls
345       * For any urls, download the packages and install in database.
346       * For any files, install package info in database.
347       */
348      for (i = 0; i < argc; i ++) {
349           char *filename = argv [i];
350           //char *tmp = basename (tmp);
351           //int tmplen = strlen (tmp);
352
353           //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
354           //     continue;
355           //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
356           //     continue;
357         
358           opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s  \n",filename );
359
360           err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
361           if (err)
362             return err;
363      }
364      return 0;
365 }
366
367 struct opkg_intercept
368 {
369     char *oldpath;
370     char *statedir;
371 };
372
373 typedef struct opkg_intercept *opkg_intercept_t;
374
375 opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
376 {
377     opkg_intercept_t ctx;
378     char *newpath;
379     int gen;
380
381     ctx = malloc (sizeof (*ctx));
382     ctx->oldpath = strdup (getenv ("PATH"));
383
384     sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
385     setenv ("PATH", newpath, 1);
386     free (newpath);
387     
388     gen = 0;
389  retry:
390     sprintf_alloc (&ctx->statedir, "/tmp/opkg-intercept-%d-%d", getpid (), gen);
391     if (mkdir (ctx->statedir, 0770) < 0) {
392         if (errno == EEXIST) {
393             free (ctx->statedir);
394             gen++;
395             goto retry;
396         }
397         perror (ctx->statedir);
398         return NULL;
399     }
400     setenv ("OPKG_INTERCEPT_DIR", ctx->statedir, 1);
401     return ctx;
402 }
403
404 int opkg_finalize_intercepts(opkg_intercept_t ctx)
405 {
406     char *cmd;
407     DIR *dir;
408     int err = 0;
409
410     setenv ("PATH", ctx->oldpath, 1);
411     free (ctx->oldpath);
412
413     dir = opendir (ctx->statedir);
414     if (dir) {
415         struct dirent *de;
416         while (de = readdir (dir), de != NULL) {
417             char *path;
418             
419             if (de->d_name[0] == '.')
420                 continue;
421             
422             sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
423             if (access (path, X_OK) == 0) {
424                 if (system (path)) {
425                     err = errno;
426                     perror (de->d_name);
427                 }
428             }
429             free (path);
430         }
431     } else
432         perror (ctx->statedir);
433         
434     sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
435     system (cmd);
436     free (cmd);
437
438     free (ctx->statedir);
439     free (ctx);
440
441     return err;
442 }
443
444 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
445 {
446      pkg_vec_t *all;
447      int i;
448      pkg_t *pkg;
449      opkg_intercept_t ic;
450      int r, err = 0;
451
452      opkg_message(conf, OPKG_INFO,
453                   "Configuring unpacked packages\n");
454      fflush( stdout );
455
456      all = pkg_vec_alloc();
457      pkg_hash_fetch_available(&conf->pkg_hash, all);
458
459      ic = opkg_prep_intercepts (conf);
460     
461      for(i = 0; i < all->len; i++) {
462           pkg = all->pkgs[i];
463
464           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
465                continue;
466
467           if (pkg->state_status == SS_UNPACKED) {
468                opkg_message(conf, OPKG_NOTICE,
469                             "Configuring %s\n", pkg->name);
470                fflush( stdout );
471                r = opkg_configure(conf, pkg);
472                if (r == 0) {
473                     pkg->state_status = SS_INSTALLED;
474                     pkg->parent->state_status = SS_INSTALLED;
475                     pkg->state_flag &= ~SF_PREFER;
476                } else {
477                     if (!err)
478                         err = r;
479                }
480           }
481      }
482
483      r = opkg_finalize_intercepts (ic);
484      if (r && !err)
485          err = r;
486
487      pkg_vec_free(all);
488      return err;
489 }
490
491 static opkg_conf_t *global_conf;
492
493 static void sigint_handler(int sig)
494 {
495      signal(sig, SIG_DFL);
496      opkg_message(NULL, OPKG_NOTICE,
497                   "opkg: interrupted. writing out status database\n");
498      write_status_files_if_changed(global_conf);
499      exit(128 + sig);
500 }
501
502 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv)
503 {
504      int i;
505      char *arg;
506      int err=0;
507
508      global_conf = conf;
509      signal(SIGINT, sigint_handler);
510
511      /*
512       * Now scan through package names and install
513       */
514      for (i=0; i < argc; i++) {
515           arg = argv[i];
516
517           opkg_message(conf, OPKG_DEBUG2, "Debug install_cmd: %s  \n",arg );
518           err = opkg_prepare_url_for_install(conf, arg, &argv[i]);
519           if (err != EINVAL && err != 0)
520               return err;
521      }
522      pkg_info_preinstall_check(conf);
523
524      for (i=0; i < argc; i++) {
525           arg = argv[i];
526           if (conf->multiple_providers)
527                err = opkg_install_multi_by_name(conf, arg);
528           else{
529                err = opkg_install_by_name(conf, arg);
530           }
531           if (err == OPKG_PKG_HAS_NO_CANDIDATE) {
532                opkg_message(conf, OPKG_ERROR,
533                             "Cannot find package %s.\n"
534                             "Check the spelling or perhaps run 'opkg update'\n",
535                             arg);
536           }
537      }
538
539      /* recheck to verify that all dependences are satisfied */
540      if (0) opkg_satisfy_all_dependences(conf);
541
542      opkg_configure_packages(conf, NULL);
543
544      write_status_files_if_changed(conf);
545
546      return err;
547 }
548
549 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv)
550 {
551      int i;
552      pkg_t *pkg;
553      int err;
554
555      global_conf = conf;
556      signal(SIGINT, sigint_handler);
557
558      if (argc) {
559           for (i=0; i < argc; i++) {
560                char *arg = argv[i];
561
562                err = opkg_prepare_url_for_install(conf, arg, &arg);
563                if (err != EINVAL && err != 0)
564                    return err;
565           }
566           pkg_info_preinstall_check(conf);
567
568           for (i=0; i < argc; i++) {
569                char *arg = argv[i];
570                if (conf->restrict_to_default_dest) {
571                     pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
572                                                                 argv[i],
573                                                                 conf->default_dest);
574                     if (pkg == NULL) {
575                          opkg_message(conf, OPKG_NOTICE,
576                                       "Package %s not installed in %s\n",
577                                       argv[i], conf->default_dest->name);
578                          continue;
579                     }
580                } else {
581                     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
582                                                            argv[i]);
583                }
584                if (pkg)
585                     opkg_upgrade_pkg(conf, pkg);
586                else {
587                     opkg_install_by_name(conf, arg);
588                }
589           }
590      } else {
591           pkg_vec_t *installed = pkg_vec_alloc();
592
593           pkg_info_preinstall_check(conf);
594
595           pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
596           for (i = 0; i < installed->len; i++) {
597                pkg = installed->pkgs[i];
598                opkg_upgrade_pkg(conf, pkg);
599           }
600           pkg_vec_free(installed);
601      }
602
603      /* recheck to verify that all dependences are satisfied */
604      if (0) opkg_satisfy_all_dependences(conf);
605
606      opkg_configure_packages(conf, NULL);
607
608      write_status_files_if_changed(conf);
609
610      return 0;
611 }
612
613 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv)
614 {
615      int i, err;
616      char *arg;
617      pkg_t *pkg;
618
619      pkg_info_preinstall_check(conf);
620      for (i = 0; i < argc; i++) {
621           arg = argv[i];
622
623           pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
624           if (pkg == NULL) {
625                opkg_message(conf, OPKG_ERROR,
626                             "Cannot find package %s.\n"
627                             "Check the spelling or perhaps run 'opkg update'\n",
628                             arg);
629                continue;
630           }
631
632           err = opkg_download_pkg(conf, pkg, ".");
633
634           if (err) {
635                opkg_message(conf, OPKG_ERROR,
636                             "Failed to download %s\n", pkg->name);
637           } else {
638                opkg_message(conf, OPKG_NOTICE,
639                             "Downloaded %s as %s\n",
640                             pkg->name, pkg->local_filename);
641           }
642      }
643
644      return 0;
645 }
646
647
648 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv)
649 {
650      int i ;
651      pkg_vec_t *available;
652      pkg_t *pkg;
653      char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
654      char *newline;
655      char *pkg_name = NULL;
656      char *version_str;
657
658      if (argc > 0) {
659           pkg_name = argv[0];
660      }
661      available = pkg_vec_alloc();
662      pkg_hash_fetch_available(&conf->pkg_hash, available);
663      for (i=0; i < available->len; i++) {
664           pkg = available->pkgs[i];
665           /* if we have package name or pattern and pkg does not match, then skip it */
666           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
667                continue;
668           if (pkg->description) {
669                strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
670           } else {
671                desc_short[0] = '\0';
672           }
673           desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
674           newline = strchr(desc_short, '\n');
675           if (newline) {
676                *newline = '\0';
677           }
678 #ifndef OPKG_LIB
679           printf("%s - %s\n", pkg->name, desc_short);
680 #else
681           if (opkg_cb_list) {
682                 version_str = pkg_version_str_alloc(pkg);
683                 opkg_cb_list(pkg->name,desc_short,
684                                              version_str,
685                                          pkg->state_status,
686                                          p_userdata);
687                 free(version_str);
688           }
689 #endif
690      }
691      pkg_vec_free(available);
692
693      return 0;
694 }
695
696
697 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv)
698 {
699      int i ;
700      pkg_vec_t *available;
701      pkg_t *pkg;
702      char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
703      char *newline;
704      char *pkg_name = NULL;
705      char *version_str;
706
707      if (argc > 0) {
708           pkg_name = argv[0];
709      }
710      available = pkg_vec_alloc();
711      pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
712      for (i=0; i < available->len; i++) {
713           pkg = available->pkgs[i];
714           /* if we have package name or pattern and pkg does not match, then skip it */
715           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
716                continue;
717           if (pkg->description) {
718                strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
719           } else {
720                desc_short[0] = '\0';
721           }
722           desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
723           newline = strchr(desc_short, '\n');
724           if (newline) {
725                *newline = '\0';
726           }
727 #ifndef OPKG_LIB
728           printf("%s - %s\n", pkg->name, desc_short);
729 #else
730           if (opkg_cb_list) {
731                 version_str = pkg_version_str_alloc(pkg);
732                 opkg_cb_list(pkg->name,desc_short,
733                                              version_str,
734                                          pkg->state_status,
735                                          p_userdata);
736                 free(version_str);
737           }
738 #endif
739      }
740
741      return 0;
742 }
743
744 static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int installed_only)
745 {
746      int i;
747      pkg_vec_t *available;
748      pkg_t *pkg;
749      char *pkg_name = NULL;
750      char **pkg_fields = NULL;
751      int n_fields = 0;
752      char *buff ; // = (char *)malloc(1);
753
754      if (argc > 0) {
755           pkg_name = argv[0];
756      }
757      if (argc > 1) {
758           pkg_fields = &argv[1];
759           n_fields = argc - 1;
760      }
761
762      available = pkg_vec_alloc();
763      if (installed_only)
764           pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
765      else
766           pkg_hash_fetch_available(&conf->pkg_hash, available);
767      for (i=0; i < available->len; i++) {
768           pkg = available->pkgs[i];
769           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
770                continue;
771           }
772 #ifndef OPKG_LIB
773           if (n_fields) {
774                for (j = 0; j < n_fields; j++)
775                     pkg_print_field(pkg, stdout, pkg_fields[j]);
776           } else {
777                pkg_print_info(pkg, stdout);
778           }
779 #else
780
781           buff = pkg_formatted_info(pkg);
782           if ( buff ) {
783                if (opkg_cb_status) opkg_cb_status(pkg->name,
784                                                   pkg->state_status,
785                                                   buff,
786                                                   p_userdata);
787 /* 
788    We should not forget that actually the pointer is allocated. 
789    We need to free it :)  ( Thanks florian for seeing the error )
790 */
791                free(buff);
792           }
793 #endif
794           if (conf->verbosity > 1) {
795                conffile_list_elt_t *iter;
796                for (iter = pkg->conffiles.head; iter; iter = iter->next) {
797                     conffile_t *cf = iter->data;
798                     int modified = conffile_has_been_modified(conf, cf);
799                     opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
800                                  cf->name, cf->value, modified);
801                }
802           }
803      }
804 #ifndef OPKG_LIB
805      if (buff)
806           free(buff);
807 #endif
808      pkg_vec_free(available);
809
810      return 0;
811 }
812
813 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv)
814 {
815      return opkg_info_status_cmd(conf, argc, argv, 0);
816 }
817
818 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv)
819 {
820      return opkg_info_status_cmd(conf, argc, argv, 1);
821 }
822
823 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv)
824 {
825      
826      int err;
827      if (argc > 0) {
828           char *pkg_name = NULL;
829
830           pkg_name = argv[0];
831
832           err = opkg_configure_packages (conf, pkg_name);
833      
834      } else {
835           err = opkg_configure_packages (conf, NULL);
836      }
837
838      write_status_files_if_changed(conf);
839
840      return err;
841 }
842
843 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv)
844 {
845      int i, err;
846      char *globpattern;
847      glob_t globbuf;
848     
849      sprintf_alloc(&globpattern, "%s/*" OPKG_PKG_EXTENSION, conf->pending_dir);
850      err = glob(globpattern, 0, NULL, &globbuf);
851      free(globpattern);
852      if (err) {
853           return 0;
854      }
855
856      opkg_message(conf, OPKG_NOTICE,
857                   "The following packages in %s will now be installed.\n",
858                   conf->pending_dir);
859      for (i = 0; i < globbuf.gl_pathc; i++) {
860           opkg_message(conf, OPKG_NOTICE,
861                        "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
862      }
863      opkg_message(conf, OPKG_NOTICE, "\n");
864      for (i = 0; i < globbuf.gl_pathc; i++) {
865           err = opkg_install_from_file(conf, globbuf.gl_pathv[i]);
866           if (err == 0) {
867                err = unlink(globbuf.gl_pathv[i]);
868                if (err) {
869                     opkg_message(conf, OPKG_ERROR,
870                                  "%s: ERROR: failed to unlink %s: %s\n",
871                                  __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
872                     return err;
873                }
874           }
875      }
876      globfree(&globbuf);
877
878      return err;
879 }
880
881 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
882 {
883      int i,a,done;
884      pkg_t *pkg;
885      pkg_t *pkg_to_remove;
886      pkg_vec_t *available;
887      char *pkg_name = NULL;
888      global_conf = conf;
889      signal(SIGINT, sigint_handler);
890
891 // ENH: Add the "no pkg removed" just in case.
892
893     done = 0;
894
895      available = pkg_vec_alloc();
896      pkg_info_preinstall_check(conf);
897      if ( argc > 0 ) {
898         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
899         for (i=0; i < argc; i++) {
900            pkg_name = malloc(strlen(argv[i])+2);
901            strcpy(pkg_name,argv[i]);
902            for (a=0; a < available->len; a++) {
903                pkg = available->pkgs[a];
904                if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
905                   continue;
906                }
907                if (conf->restrict_to_default_dest) {
908                     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
909                                                                 pkg->name,
910                                                                 conf->default_dest);
911                } else {
912                     pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
913                }
914         
915                if (pkg == NULL) {
916                     opkg_message(conf, OPKG_ERROR, "Package %s is not installed.\n", pkg->name);
917                     continue;
918                }
919                if (pkg->state_status == SS_NOT_INSTALLED) {    // Added the control, so every already removed package could be skipped
920                     opkg_message(conf, OPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
921                     continue;
922                }
923                opkg_remove_pkg(conf, pkg_to_remove,0);
924                done = 1;
925            }
926            free (pkg_name);
927         }
928         pkg_vec_free(available);
929      } else {
930           pkg_vec_t *installed_pkgs = pkg_vec_alloc();
931           int i;
932           int flagged_pkg_count = 0;
933           int removed;
934
935           pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
936
937           for (i = 0; i < installed_pkgs->len; i++) {
938                pkg_t *pkg = installed_pkgs->pkgs[i];
939                if (pkg->state_flag & SF_USER) {
940                     flagged_pkg_count++;
941                } else {
942                     if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
943                          opkg_message(conf, OPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
944                }
945           }
946           if (!flagged_pkg_count) {
947                opkg_message(conf, OPKG_NOTICE, "No packages flagged as installed by user, \n"
948                             "so refusing to uninstall unflagged non-leaf packages\n");
949                return 0;
950           }
951
952           /* find packages not flagged SF_USER (i.e., installed to
953            * satisfy a dependence) and not having any dependents, and
954            * remove them */
955           do {
956                removed = 0;
957                for (i = 0; i < installed_pkgs->len; i++) {
958                     pkg_t *pkg = installed_pkgs->pkgs[i];
959                     if (!(pkg->state_flag & SF_USER)
960                         && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
961                          removed++;
962                          opkg_message(conf, OPKG_NOTICE, "Removing non-user leaf package %s\n");
963                          opkg_remove_pkg(conf, pkg,0);
964                          done = 1;
965                     }
966                }
967           } while (removed);
968           pkg_vec_free(installed_pkgs);
969      }
970
971      if ( done == 0 ) 
972         opkg_message(conf, OPKG_NOTICE, "No packages removed.\n");
973
974      write_status_files_if_changed(conf);
975      return 0;
976 }
977
978 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv)
979 {
980      int i;
981      pkg_t *pkg;
982
983      global_conf = conf;
984      signal(SIGINT, sigint_handler);
985
986      pkg_info_preinstall_check(conf);
987
988      for (i=0; i < argc; i++) {
989           if (conf->restrict_to_default_dest) {
990                pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
991                                                            argv[i],
992                                                            conf->default_dest);
993           } else {
994                pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
995           }
996
997           if (pkg == NULL) {
998                opkg_message(conf, OPKG_ERROR,
999                             "Package %s is not installed.\n", argv[i]);
1000                continue;
1001           }
1002           opkg_purge_pkg(conf, pkg);
1003      }
1004
1005      write_status_files_if_changed(conf);
1006      return 0;
1007 }
1008
1009 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv)
1010 {
1011      int i;
1012      pkg_t *pkg;
1013      const char *flags = argv[0];
1014     
1015      global_conf = conf;
1016      signal(SIGINT, sigint_handler);
1017
1018      for (i=1; i < argc; i++) {
1019           if (conf->restrict_to_default_dest) {
1020                pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1021                                                            argv[i],
1022                                                            conf->default_dest);
1023           } else {
1024                pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1025           }
1026
1027           if (pkg == NULL) {
1028                opkg_message(conf, OPKG_ERROR,
1029                             "Package %s is not installed.\n", argv[i]);
1030                continue;
1031           }
1032           if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1033               ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1034               pkg->state_flag = pkg_state_flag_from_str(flags);
1035           }
1036 /* pb_ asked this feature 03292004 */
1037 /* Actually I will use only this two, but this is an open for various status */
1038           if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1039               pkg->state_status = pkg_state_status_from_str(flags);
1040           }
1041           opkg_state_changed++;
1042           opkg_message(conf, OPKG_NOTICE,
1043                        "Setting flags for package %s to %s\n",
1044                        pkg->name, flags);
1045      }
1046
1047      write_status_files_if_changed(conf);
1048      return 0;
1049 }
1050
1051 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv)
1052 {
1053      pkg_t *pkg;
1054      str_list_t *installed_files;
1055      str_list_elt_t *iter;
1056      char *pkg_version;
1057      size_t buff_len = 8192;
1058      size_t used_len;
1059      char *buff ;
1060
1061      buff = (char *)malloc(buff_len);
1062      if ( buff == NULL ) {
1063         fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1064         return ENOMEM;
1065      }
1066  
1067      if (argc < 1) {
1068           return EINVAL;
1069      }
1070
1071      pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1072                                             argv[0]);
1073      if (pkg == NULL) {
1074           opkg_message(conf, OPKG_ERROR,
1075                        "Package %s not installed.\n", argv[0]);
1076           return 0;
1077      }
1078
1079      installed_files = pkg_get_installed_files(pkg);
1080      pkg_version = pkg_version_str_alloc(pkg);
1081
1082 #ifndef OPKG_LIB
1083      printf("Package %s (%s) is installed on %s and has the following files:\n",
1084             pkg->name, pkg_version, pkg->dest->name);
1085      for (iter = installed_files->head; iter; iter = iter->next) {
1086           puts(iter->data);
1087      }
1088 #else
1089      if (buff) {
1090      try_again:
1091           used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1092                               pkg->name, pkg_version, pkg->dest->name) + 1;
1093           if (used_len > buff_len) {
1094                buff_len *= 2;
1095                buff = realloc (buff, buff_len);
1096                goto try_again;
1097           }
1098           for (iter = installed_files->head; iter; iter = iter->next) {
1099                used_len += strlen (iter->data) + 1;
1100                while (buff_len <= used_len) {
1101                     buff_len *= 2;
1102                     buff = realloc (buff, buff_len);
1103                }
1104                strncat(buff, iter->data, buff_len);
1105                strncat(buff, "\n", buff_len);
1106           } 
1107           if (opkg_cb_list) opkg_cb_list(pkg->name,
1108                                          buff,
1109                                          pkg_version_str_alloc(pkg),
1110                                          pkg->state_status,
1111                                          p_userdata);
1112           free(buff);
1113      }
1114 #endif
1115
1116      free(pkg_version);
1117      pkg_free_installed_files(pkg);
1118
1119      return 0;
1120 }
1121
1122 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv)
1123 {
1124
1125      if (argc > 0) {
1126           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1127           const char *rel_str = "depends on";
1128           int i;
1129      
1130           pkg_info_preinstall_check(conf);
1131
1132           if (conf->query_all)
1133                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1134           else
1135                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1136           for (i = 0; i < argc; i++) {
1137                const char *target = argv[i];
1138                int j;
1139
1140                opkg_message(conf, OPKG_ERROR, "target=%s\n", target);
1141
1142                for (j = 0; j < available_pkgs->len; j++) {
1143                     pkg_t *pkg = available_pkgs->pkgs[j];
1144                     if (fnmatch(target, pkg->name, 0) == 0) {
1145                          int k;
1146                          int count = pkg->depends_count + pkg->pre_depends_count;
1147                          opkg_message(conf, OPKG_ERROR, "What %s (arch=%s) %s\n",
1148                                       target, pkg->architecture, rel_str);
1149                          for (k = 0; k < count; k++) {
1150                               compound_depend_t *cdepend = &pkg->depends[k];
1151                               int l;
1152                               for (l = 0; l < cdepend->possibility_count; l++) {
1153                                    depend_t *possibility = cdepend->possibilities[l];
1154                                    opkg_message(conf, OPKG_ERROR, "    %s", possibility->pkg->name);
1155                                    if (conf->verbosity > 0) {
1156                                         // char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 
1157                                         opkg_message(conf, OPKG_NOTICE, " %s", possibility->version);
1158                                         if (possibility->version) {
1159                                              char *typestr = NULL;
1160                                              switch (possibility->constraint) {
1161                                              case NONE: typestr = "none"; break;
1162                                              case EARLIER: typestr = "<"; break;
1163                                              case EARLIER_EQUAL: typestr = "<="; break;
1164                                              case EQUAL: typestr = "="; break;
1165                                              case LATER_EQUAL: typestr = ">="; break;
1166                                              case LATER: typestr = ">"; break;
1167                                              }
1168                                              opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1169                                         }
1170                                         // free(ver);
1171                                    }
1172                                    opkg_message(conf, OPKG_ERROR, "\n");
1173                               }
1174                          }
1175                     }
1176                }
1177           }
1178           pkg_vec_free(available_pkgs);
1179      }
1180      return 0;
1181 }
1182
1183 enum what_field_type {
1184   WHATDEPENDS,
1185   WHATCONFLICTS,
1186   WHATPROVIDES,
1187   WHATREPLACES,
1188   WHATRECOMMENDS,
1189   WHATSUGGESTS
1190 };
1191
1192 static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1193 {
1194
1195      if (argc > 0) {
1196           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1197           const char *rel_str = NULL;
1198           int i;
1199           int changed;
1200
1201           switch (what_field_type) {
1202           case WHATDEPENDS: rel_str = "depends on"; break;
1203           case WHATCONFLICTS: rel_str = "conflicts with"; break;
1204           case WHATSUGGESTS: rel_str = "suggests"; break;
1205           case WHATRECOMMENDS: rel_str = "recommends"; break;
1206           case WHATPROVIDES: rel_str = "provides"; break;
1207           case WHATREPLACES: rel_str = "replaces"; break;
1208           }
1209      
1210           if (conf->query_all)
1211                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1212           else
1213                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1214
1215           /* mark the root set */
1216           pkg_vec_clear_marks(available_pkgs);
1217           opkg_message(conf, OPKG_NOTICE, "Root set:\n");
1218           for (i = 0; i < argc; i++) {
1219                const char *dependee_pattern = argv[i];
1220                pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1221           }
1222           for (i = 0; i < available_pkgs->len; i++) {
1223                pkg_t *pkg = available_pkgs->pkgs[i];
1224                if (pkg->state_flag & SF_MARKED) {
1225                     /* mark the parent (abstract) package */
1226                     pkg_mark_provides(pkg);
1227                     opkg_message(conf, OPKG_NOTICE, "  %s\n", pkg->name);
1228                }
1229           }
1230
1231           opkg_message(conf, OPKG_NOTICE, "What %s root set\n", rel_str);
1232           do {
1233                int j;
1234                changed = 0;
1235
1236                for (j = 0; j < available_pkgs->len; j++) {
1237                     pkg_t *pkg = available_pkgs->pkgs[j];
1238                     int k;
1239                     int count = ((what_field_type == WHATCONFLICTS)
1240                                  ? pkg->conflicts_count
1241                                  : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1242                     /* skip this package if it is already marked */
1243                     if (pkg->parent->state_flag & SF_MARKED) {
1244                          continue;
1245                     }
1246                     for (k = 0; k < count; k++) {
1247                          compound_depend_t *cdepend = 
1248                               (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1249                          int l;
1250                          for (l = 0; l < cdepend->possibility_count; l++) {
1251                               depend_t *possibility = cdepend->possibilities[l];
1252                               if (possibility->pkg->state_flag & SF_MARKED) {
1253                                    /* mark the depending package so we won't visit it again */
1254                                    pkg->state_flag |= SF_MARKED;
1255                                    pkg_mark_provides(pkg);
1256                                    changed++;
1257
1258                                    opkg_message(conf, OPKG_NOTICE, "    %s", pkg->name);
1259                                    if (conf->verbosity > 0) {
1260                                         char *ver = pkg_version_str_alloc(pkg); 
1261                                         opkg_message(conf, OPKG_NOTICE, " %s", ver);
1262                                         opkg_message(conf, OPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1263                                         if (possibility->version) {
1264                                              char *typestr = NULL;
1265                                              switch (possibility->constraint) {
1266                                              case NONE: typestr = "none"; break;
1267                                              case EARLIER: typestr = "<"; break;
1268                                              case EARLIER_EQUAL: typestr = "<="; break;
1269                                              case EQUAL: typestr = "="; break;
1270                                              case LATER_EQUAL: typestr = ">="; break;
1271                                              case LATER: typestr = ">"; break;
1272                                              }
1273                                              opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1274                                         }
1275                                         free(ver);
1276                                         if (!pkg_dependence_satisfiable(conf, possibility))
1277                                              opkg_message(conf, OPKG_NOTICE, " unsatisfiable");
1278                                    }
1279                                    opkg_message(conf, OPKG_NOTICE, "\n");
1280                                    goto next_package;
1281                               }
1282                          }
1283                     }
1284                next_package:
1285                     ;
1286                }
1287           } while (changed && recursive);
1288           pkg_vec_free(available_pkgs);
1289      }
1290
1291      return 0;
1292 }
1293
1294 int pkg_mark_provides(pkg_t *pkg)
1295 {
1296      int provides_count = pkg->provides_count;
1297      abstract_pkg_t **provides = pkg->provides;
1298      int i;
1299      pkg->parent->state_flag |= SF_MARKED;
1300      for (i = 0; i < provides_count; i++) {
1301           provides[i]->state_flag |= SF_MARKED;
1302      }
1303      return 0;
1304 }
1305
1306 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv)
1307 {
1308      return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1309 }
1310 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv)
1311 {
1312      return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1313 }
1314
1315 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv)
1316 {
1317      return opkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1318 }
1319
1320 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv)
1321 {
1322      return opkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1323 }
1324
1325 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv)
1326 {
1327      return opkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1328 }
1329
1330 static int opkg_what_provides_replaces_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1331 {
1332
1333      if (argc > 0) {
1334           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1335           const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1336           int i;
1337      
1338           pkg_info_preinstall_check(conf);
1339
1340           if (conf->query_all)
1341                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1342           else
1343                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1344           for (i = 0; i < argc; i++) {
1345                const char *target = argv[i];
1346                int j;
1347
1348                opkg_message(conf, OPKG_ERROR, "What %s %s\n",
1349                             rel_str, target);
1350                for (j = 0; j < available_pkgs->len; j++) {
1351                     pkg_t *pkg = available_pkgs->pkgs[j];
1352                     int k;
1353                     int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1354                     for (k = 0; k < count; k++) {
1355                          abstract_pkg_t *apkg = 
1356                               ((what_field_type == WHATPROVIDES) 
1357                                ? pkg->provides[k]
1358                                : pkg->replaces[k]);
1359                          if (fnmatch(target, apkg->name, 0) == 0) {
1360                               opkg_message(conf, OPKG_ERROR, "    %s", pkg->name);
1361                               if (strcmp(target, apkg->name) != 0)
1362                                    opkg_message(conf, OPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1363                               opkg_message(conf, OPKG_ERROR, "\n");
1364                          }
1365                     }
1366                }
1367           }
1368           pkg_vec_free(available_pkgs);
1369      }
1370      return 0;
1371 }
1372
1373 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv)
1374 {
1375      return opkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1376 }
1377
1378 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv)
1379 {
1380      return opkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1381 }
1382
1383 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv)
1384 {
1385      int i;
1386
1387      pkg_vec_t *installed;
1388      pkg_t *pkg;
1389      str_list_t *installed_files;
1390      str_list_elt_t *iter;
1391      char *installed_file;
1392
1393      if (argc < 1) {
1394           return EINVAL;
1395      }
1396  
1397      installed = pkg_vec_alloc();
1398      pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1399
1400      for (i=0; i < installed->len; i++) {
1401           pkg = installed->pkgs[i];
1402
1403           installed_files = pkg_get_installed_files(pkg);
1404
1405           for (iter = installed_files->head; iter; iter = iter->next) {
1406                installed_file = iter->data;
1407                if (fnmatch(argv[0], installed_file, 0)==0)  {
1408 #ifndef OPKG_LIB
1409                     printf("%s: %s\n", pkg->name, installed_file);
1410 #else
1411                         if (opkg_cb_list) opkg_cb_list(pkg->name, 
1412                                                        installed_file, 
1413                                                        pkg_version_str_alloc(pkg), 
1414                                                        pkg->state_status, p_userdata);
1415 #endif                     
1416                }                
1417           }
1418
1419           pkg_free_installed_files(pkg);
1420      }
1421
1422      /* XXX: CLEANUP: It's not obvious from the name of
1423         pkg_hash_fetch_all_installed that we need to call
1424         pkg_vec_free to avoid a memory leak. */
1425      pkg_vec_free(installed);
1426
1427      return 0;
1428 }
1429
1430 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv)
1431 {
1432      if (argc == 3) {
1433           /* this is a bit gross */
1434           struct pkg p1, p2;
1435           parseVersion(&p1, argv[0]); 
1436           parseVersion(&p2, argv[2]); 
1437           return pkg_version_satisfied(&p1, &p2, argv[1]);
1438      } else {
1439           opkg_message(conf, OPKG_ERROR,
1440                        "opkg compare_versions <v1> <op> <v2>\n"
1441                        "<op> is one of <= >= << >> =\n");
1442           return -1;
1443      }
1444 }
1445
1446 #ifndef HOST_CPU_STR
1447 #define HOST_CPU_STR__(X) #X
1448 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1449 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1450 #endif
1451
1452 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv)
1453 {
1454      nv_pair_list_elt_t *l;
1455
1456      l = conf->arch_list.head;
1457      while (l) {
1458           nv_pair_t *nv = l->data;
1459           printf("arch %s %s\n", nv->name, nv->value);
1460           l = l->next;
1461      }
1462      return 0;
1463 }
1464
1465