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