Cleanup error_list stuff a bit more.
[oweals/opkg-lede.git] / libopkg / libopkg.c
1 /* opkglib.c - the opkg package management system
2
3    Florina 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 #include "libopkg.h"
20
21 #include "args.h"
22 #include "opkg_conf.h"
23 #include "opkg_cmd.h"
24 #include "file_util.h"
25
26 #include "opkg_message.h"
27 #include "opkg_error.h"
28
29 opkg_status_callback opkg_cb_status = NULL;
30 opkg_list_callback opkg_cb_list = NULL;
31
32 int default_opkg_message_callback(opkg_conf_t *conf, message_level_t level, 
33                                   char *msg)
34 {
35      if (conf && (conf->verbosity < level)) {
36           return 0;
37      } else {
38           if ( level == OPKG_ERROR ){
39              push_error_list(msg); 
40           } else
41             printf("%s",msg);
42      }
43      return 0;
44 }
45
46 int default_opkg_list_callback(char *name, char *desc, char *version, 
47                                pkg_state_status_t status, void *userdata)
48 {
49      if (desc)
50           printf("%s - %s - %s\n", name, version, desc);
51      else
52           printf("%s - %s\n", name, version);
53      return 0;
54 }
55
56 int default_opkg_files_callback(char *name, char *desc, char *version,
57                    pkg_state_status_t status, void *userdata)
58 {
59      if (desc)
60           printf("%s\n", desc);
61      return 0;
62 }
63
64 int default_opkg_status_callback(char *name, int istatus, char *desc,
65                                  void *userdata)
66 {
67      printf("%s\n", desc);
68      return 0;
69 }
70
71 char* default_opkg_response_callback(char *question)
72 {
73      char *response = NULL;
74      printf("%s",question);
75      fflush(stdout);
76      do {
77           response = (char *)file_read_line_alloc(stdin);
78      } while (response == NULL);
79      return response;
80 }
81
82 /* This is used for backward compatibility */
83 int
84 opkg_op (int argc, char *argv[])
85 {
86         int err, optind;
87         args_t args;
88         char *cmd_name;
89         opkg_cmd_t *cmd;
90         opkg_conf_t opkg_conf;
91
92         args_init (&args);
93
94         optind = args_parse (&args, argc, argv);
95         if (optind == argc || optind < 0)
96         {
97                 args_usage ("opkg must have one sub-command argument");
98         }
99
100         cmd_name = argv[optind++];
101 /* Pigi: added a flag to disable the checking of structures if the command does not need to 
102          read anything from there.
103 */
104         if ( !strcmp(cmd_name,"print-architecture") ||
105              !strcmp(cmd_name,"print_architecture") ||
106              !strcmp(cmd_name,"print-installation-architecture") ||
107              !strcmp(cmd_name,"print_installation_architecture") )
108            args.nocheckfordirorfile = 1;
109
110 /* Pigi: added a flag to disable the reading of feed files  if the command does not need to 
111          read anything from there.
112 */
113         if ( !strcmp(cmd_name,"flag") ||
114              !strcmp(cmd_name,"configure") ||
115              !strcmp(cmd_name,"remove") ||
116              !strcmp(cmd_name,"files") ||
117              !strcmp(cmd_name,"search") ||
118              !strcmp(cmd_name,"compare_versions") ||
119              !strcmp(cmd_name,"compare-versions") ||
120              !strcmp(cmd_name,"list_installed") ||
121              !strcmp(cmd_name,"list-installed") ||
122              !strcmp(cmd_name,"status") )
123            args.noreadfeedsfile = 1;
124
125         opkg_cb_message = default_opkg_message_callback;
126         opkg_cb_response = default_opkg_response_callback;
127         opkg_cb_status = default_opkg_status_callback;
128
129
130         err = opkg_conf_init (&opkg_conf, &args);
131         args_deinit (&args);
132         if (err)
133         {
134                 print_error_list();
135                 free_error_list();
136                 return err;
137         }
138
139         if ( strcmp(cmd_name, "files")==0)
140              opkg_cb_list = default_opkg_files_callback;
141         else
142              opkg_cb_list = default_opkg_list_callback;
143
144         cmd = opkg_cmd_find (cmd_name);
145         if (cmd == NULL)
146         {
147                 fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
148                          cmd_name);
149                 args_usage (NULL);
150         }
151
152         if (cmd->requires_args && optind == argc)
153         {
154                 fprintf (stderr,
155                          "%s: the ``%s'' command requires at least one argument\n",
156                          __FUNCTION__, cmd_name);
157                 args_usage (NULL);
158         }
159
160         err = opkg_cmd_exec (cmd, &opkg_conf, argc - optind, (const char **) (argv + optind), NULL);
161
162         opkg_conf_deinit (&opkg_conf);
163
164         return err;
165 }