3ff6085ec943c1f300fa3494fc267b4a35e3dab7
[oweals/opkg-lede.git] / libopkg / libopkg.c
1 /* opkglib.c - the opkg package management system
2
3    Florian Boor
4
5    Copyright (C) 2003 kernel concepts
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 "includes.h"
19
20 #include "opkg_conf.h"
21 #include "opkg_cmd.h"
22 #include "file_util.h"
23 #include "args.h"
24 #include "opkg_download.h"
25
26 #include "opkg_message.h"
27
28 /* This is used for backwards compatibility */
29 int
30 opkg_op (int argc, char *argv[])
31 {
32         int opts;
33         char *cmd_name;
34         opkg_cmd_t *cmd;
35         int nocheckfordirorfile = 0;
36         int noreadfeedsfile = 0;
37
38         conf->verbosity = NOTICE;       
39
40         opts = args_parse (argc, argv);
41         if (opts == argc || opts < 0)
42         {
43                 args_usage ("opkg must have one sub-command argument");
44         }
45
46         cmd_name = argv[opts++];
47
48         if ( !strcmp(cmd_name,"print-architecture") ||
49              !strcmp(cmd_name,"print_architecture") ||
50              !strcmp(cmd_name,"print-installation-architecture") ||
51              !strcmp(cmd_name,"print_installation_architecture") )
52            nocheckfordirorfile = 1;
53
54         if ( !strcmp(cmd_name,"flag") ||
55              !strcmp(cmd_name,"configure") ||
56              !strcmp(cmd_name,"remove") ||
57              !strcmp(cmd_name,"files") ||
58              !strcmp(cmd_name,"search") ||
59              !strcmp(cmd_name,"compare_versions") ||
60              !strcmp(cmd_name,"compare-versions") ||
61              !strcmp(cmd_name,"list_installed") ||
62              !strcmp(cmd_name,"list-installed") ||
63              !strcmp(cmd_name,"status") )
64            noreadfeedsfile = 1;
65
66         cmd = opkg_cmd_find (cmd_name);
67         if (cmd == NULL)
68         {
69                 fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
70                          cmd_name);
71                 args_usage (NULL);
72         }
73
74         conf->pfm = cmd->pfm;
75
76         if(opkg_conf_init())
77                 goto err0;
78
79         if (!nocheckfordirorfile) {
80                 if (!noreadfeedsfile) {
81                         if (pkg_hash_load_feeds())
82                                 goto err1;
83                 }
84    
85                 if (pkg_hash_load_status_files())
86                         goto err1;
87         }
88
89         if (cmd->requires_args && opts == argc)
90         {
91                 fprintf (stderr,
92                          "%s: the ``%s'' command requires at least one argument\n",
93                          argv[0], cmd_name);
94                 args_usage (NULL);
95         }
96
97         if (opkg_cmd_exec (cmd, argc - opts, (const char **) (argv + opts)))
98                 goto err2;
99
100         return 0;
101
102 err2:
103 #ifdef HAVE_CURL
104         opkg_curl_cleanup();
105 #endif
106 err1:
107         opkg_conf_deinit ();
108
109 err0:
110         print_error_list();
111         free_error_list();
112
113         return -1;
114 }