opkg: remove redundant OPKG_LIB conditional code
[oweals/opkg-lede.git] / libopkg / libopkg.c
1 /* opkglib.c - the itsy 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 "opkg.h"
19 #include "includes.h"
20 #include "libopkg.h"
21
22 #include "args.h"
23 #include "opkg_conf.h"
24 #include "opkg_cmd.h"
25 #include "file_util.h"
26
27
28
29 opkg_message_callback opkg_cb_message = NULL;
30 opkg_response_callback opkg_cb_response = NULL;
31 opkg_status_callback opkg_cb_status = NULL;
32 opkg_list_callback opkg_cb_list = NULL;
33
34
35 int
36 opkg_init (opkg_message_callback mcall, 
37            opkg_response_callback rcall,
38            args_t * args)
39 {
40         opkg_cb_message = mcall;
41         opkg_cb_response = rcall;
42
43         args_init (args);
44
45         return 0;
46 }
47
48
49 int
50 opkg_deinit (args_t * args)
51 {
52         args_deinit (args);
53         opkg_cb_message = NULL;
54         opkg_cb_response = NULL;
55
56         /* place other cleanup stuff here */
57
58         return 0;
59 }
60
61
62 int
63 opkg_packages_list(args_t *args, 
64                    const char *packages, 
65                    opkg_list_callback cblist,
66                    void *userdata)
67 {
68         opkg_cmd_t *cmd;
69         opkg_conf_t opkg_conf;
70         int err;
71
72         err = opkg_conf_init (&opkg_conf, args);
73         if (err)
74         {
75                 return err;
76         }
77
78         opkg_cb_list = cblist;
79         /* we need to do this because of static declarations, 
80          * maybe a good idea to change */
81         cmd = opkg_cmd_find ("list");
82         if (packages)
83                 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
84         else
85                 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
86         opkg_cb_list = NULL;
87         opkg_conf_deinit (&opkg_conf);
88         return (err);
89 }
90
91
92 int
93 opkg_packages_status(args_t *args,
94                      const char *packages,
95                      opkg_status_callback cbstatus,
96                      void *userdata)
97 {
98         opkg_cmd_t *cmd;
99         opkg_conf_t opkg_conf;
100         int err;
101
102         err = opkg_conf_init (&opkg_conf, args);
103         if (err)
104         {
105                 return err;
106         }
107
108         opkg_cb_status = cbstatus;
109
110         /* we need to do this because of static declarations,
111          * maybe a good idea to change */
112         cmd = opkg_cmd_find ("status");
113         if (packages)
114                 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
115         else
116                 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
117
118         opkg_cb_status = NULL;
119         opkg_conf_deinit (&opkg_conf);
120         return (err);
121 }
122
123
124 int
125 opkg_packages_info(args_t *args,
126                    const char *packages,
127                    opkg_status_callback cbstatus,
128                    void *userdata)
129 {
130         opkg_cmd_t *cmd;
131         opkg_conf_t opkg_conf;
132         int err;
133
134         err = opkg_conf_init (&opkg_conf, args);
135         if (err)
136         {
137                 return err;
138         }
139
140         opkg_cb_status = cbstatus;
141
142         /* we need to do this because of static declarations,
143          * maybe a good idea to change */
144         cmd = opkg_cmd_find ("info");
145         if (packages)
146                 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
147         else
148                 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
149
150         opkg_cb_status = NULL;
151         opkg_conf_deinit (&opkg_conf);
152         return (err);
153 }
154
155
156 int
157 opkg_packages_install (args_t * args, const char *name)
158 {
159         opkg_cmd_t *cmd;
160         opkg_conf_t opkg_conf;
161         int err;
162
163         /* this error should be handled in application */
164         if (!name || !strlen (name))
165                 return (-1);
166
167         err = opkg_conf_init (&opkg_conf, args);
168         if (err)
169         {
170                 return err;
171         }
172
173         /* we need to do this because of static declarations,
174          * maybe a good idea to change */
175         cmd = opkg_cmd_find ("install");
176         err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
177
178         opkg_conf_deinit(&opkg_conf);
179         return (err);
180 }
181
182
183 int
184 opkg_packages_remove(args_t *args, const char *name, int purge)
185 {
186         opkg_cmd_t *cmd;
187         opkg_conf_t opkg_conf;
188         int err;
189
190         /* this error should be handled in application */
191         if (!name || !strlen (name))
192                 return (-1);
193
194         err = opkg_conf_init (&opkg_conf, args);
195         if (err)
196         {
197                 return err;
198         }
199
200         /* we need to do this because of static declarations, 
201          * maybe a good idea to change */
202         if (purge)
203                 cmd = opkg_cmd_find ("purge");
204         else
205                 cmd = opkg_cmd_find ("remove");
206
207         err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
208         
209         opkg_conf_deinit(&opkg_conf);
210         return (err);
211 }
212
213
214 int 
215 opkg_lists_update(args_t *args)
216 {
217         opkg_cmd_t *cmd;
218         opkg_conf_t opkg_conf;
219         int err;
220
221         err = opkg_conf_init (&opkg_conf, args);
222         if (err)
223         {
224                 return err;
225         }
226
227         /* we need to do this because of static declarations, 
228          * maybe a good idea to change */
229         cmd = opkg_cmd_find ("update");
230
231         err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, NULL);
232         
233         opkg_conf_deinit(&opkg_conf);
234         return (err);
235 }
236
237
238 int 
239 opkg_packages_upgrade(args_t *args)
240 {
241         opkg_cmd_t *cmd;
242         opkg_conf_t opkg_conf;
243         int err;
244
245         err = opkg_conf_init (&opkg_conf, args);
246         if (err)
247         {
248                 return err;
249         }
250
251         /* we need to do this because of static declarations, 
252          * maybe a good idea to change */
253         cmd = opkg_cmd_find ("upgrade");
254
255         err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, NULL);
256         
257         opkg_conf_deinit(&opkg_conf);
258         return (err);
259 }
260
261
262 int
263 opkg_packages_download (args_t * args, const char *name)
264 {
265         opkg_cmd_t *cmd;
266         opkg_conf_t opkg_conf;
267         int err;
268
269         /* this error should be handled in application */
270         if (!name || !strlen (name))
271                 return (-1);
272
273         err = opkg_conf_init (&opkg_conf, args);
274         if (err)
275         {
276                 return err;
277         }
278
279         /* we need to do this because of static declarations,
280          * maybe a good idea to change */
281         cmd = opkg_cmd_find ("download");
282         err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
283
284         opkg_conf_deinit(&opkg_conf);
285         return (err);
286 }
287
288
289 int
290 opkg_package_files(args_t *args, 
291                    const char *name, 
292                    opkg_list_callback cblist,
293                    void *userdata)
294 {
295         opkg_cmd_t *cmd;
296         opkg_conf_t opkg_conf;
297         int err;
298
299         /* this error should be handled in application */
300         if (!name || !strlen (name))
301                 return (-1);
302
303         err = opkg_conf_init (&opkg_conf, args);
304         if (err)
305         {
306                 return err;
307         }
308
309         opkg_cb_list = cblist;
310         
311         /* we need to do this because of static declarations, 
312          * maybe a good idea to change */
313         cmd = opkg_cmd_find ("files");
314
315         err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, userdata);
316         
317         opkg_cb_list = NULL;
318         opkg_conf_deinit(&opkg_conf);
319         return (err);
320 }
321
322
323 int 
324 opkg_file_search(args_t *args, 
325                 const char *file,
326                                 opkg_list_callback cblist,
327                 void *userdata)
328 {
329         opkg_cmd_t *cmd;
330         opkg_conf_t opkg_conf;
331         int err;
332         
333         /* this error should be handled in application */
334         if (!file || !strlen (file))
335                 return (-1);
336
337         err = opkg_conf_init (&opkg_conf, args);
338         if (err)
339         {
340                 return err;
341         }
342
343         opkg_cb_list = cblist;
344
345         /* we need to do this because of static declarations, 
346          * maybe a good idea to change */
347         cmd = opkg_cmd_find ("search");
348         err = opkg_cmd_exec (cmd, &opkg_conf, 1, &file, userdata);
349         
350         opkg_cb_list = NULL;
351         opkg_conf_deinit(&opkg_conf);
352         return(err);
353 }
354
355
356 int 
357 opkg_file_what(args_t *args, const char *file, const char* command)
358 {
359         opkg_cmd_t *cmd;
360         opkg_conf_t opkg_conf;
361         int err;
362         
363         /* this error should be handled in application */
364         if (!file || !strlen (file))
365                 return (-1);
366
367         err = opkg_conf_init (&opkg_conf, args);
368         if (err)
369         {
370                 return err;
371         }
372
373         /* we need to do this because of static declarations, 
374          * maybe a good idea to change */
375         cmd = opkg_cmd_find (command);
376         err = opkg_cmd_exec (cmd, &opkg_conf, 1, &file, NULL);
377         
378         opkg_conf_deinit(&opkg_conf);
379         return(err);
380 }
381
382 #define opkg_package_whatdepends(args,file) opkg_file_what(args,file,"whatdepends")
383 #define opkg_package_whatrecommends(args, file) opkg_file_what(args,file,"whatrecommends")
384 #define opkg_package_whatprovides(args, file) opkg_file_what(args,file,"whatprovides")
385 #define opkg_package_whatconflicts(args, file) opkg_file_what(args,file,"whatconflicts")
386 #define opkg_package_whatreplaces(args, file) opkg_file_what(args,file,"whatreplaces")
387
388
389 int default_opkg_message_callback(opkg_conf_t *conf, message_level_t level, 
390                                   char *msg)
391 {
392      if (conf && (conf->verbosity < level)) {
393           return 0;
394      } else {
395           if ( level == OPKG_ERROR ){
396              push_error_list(&error_list, msg); 
397           } else
398              printf(msg);
399      }
400      return 0;
401 }
402
403 int default_opkg_list_callback(char *name, char *desc, char *version, 
404                                pkg_state_status_t status, void *userdata)
405 {
406      if (desc)
407           printf("%s - %s - %s\n", name, version, desc);
408      else
409           printf("%s - %s\n", name, version);
410      return 0;
411 }
412
413 int default_opkg_files_callback(char *name, char *desc, char *version,
414                    pkg_state_status_t status, void *userdata)
415 {
416      if (desc)
417           printf("%s\n", desc);
418      return 0;
419 }
420
421 int default_opkg_status_callback(char *name, int istatus, char *desc,
422                                  void *userdata)
423 {
424      printf("%s\n", desc);
425      return 0;
426 }
427
428 char* default_opkg_response_callback(char *question)
429 {
430      char *response = NULL;
431      printf(question);
432      fflush(stdout);
433      do {
434           response = (char *)file_read_line_alloc(stdin);
435      } while (response == NULL);
436      return response;
437 }
438
439 /* This is used for backward compatibility */
440 int
441 opkg_op (int argc, char *argv[])
442 {
443         int err, optind;
444         args_t args;
445         char *cmd_name;
446         opkg_cmd_t *cmd;
447         opkg_conf_t opkg_conf;
448
449         args_init (&args);
450
451         optind = args_parse (&args, argc, argv);
452         if (optind == argc || optind < 0)
453         {
454                 args_usage ("opkg must have one sub-command argument");
455         }
456
457         cmd_name = argv[optind++];
458 /* Pigi: added a flag to disable the checking of structures if the command does not need to 
459          read anything from there.
460 */
461         if ( !strcmp(cmd_name,"print-architecture") ||
462              !strcmp(cmd_name,"print_architecture") ||
463              !strcmp(cmd_name,"print-installation-architecture") ||
464              !strcmp(cmd_name,"print_installation_architecture") )
465            args.nocheckfordirorfile = 1;
466
467 /* Pigi: added a flag to disable the reading of feed files  if the command does not need to 
468          read anything from there.
469 */
470         if ( !strcmp(cmd_name,"flag") ||
471              !strcmp(cmd_name,"configure") ||
472              !strcmp(cmd_name,"remove") ||
473              !strcmp(cmd_name,"files") ||
474              !strcmp(cmd_name,"search") ||
475              !strcmp(cmd_name,"compare_versions") ||
476              !strcmp(cmd_name,"compare-versions") ||
477              !strcmp(cmd_name,"list_installed") ||
478              !strcmp(cmd_name,"list-installed") ||
479              !strcmp(cmd_name,"status") )
480            args.noreadfeedsfile = 1;
481
482
483         err = opkg_conf_init (&opkg_conf, &args);
484         if (err)
485         {
486                 return err;
487         }
488
489         args_deinit (&args);
490
491         opkg_cb_message = default_opkg_message_callback;
492         opkg_cb_response = default_opkg_response_callback;
493         opkg_cb_status = default_opkg_status_callback;
494         if ( strcmp(cmd_name, "files")==0)
495              opkg_cb_list = default_opkg_files_callback;
496         else
497              opkg_cb_list = default_opkg_list_callback;
498
499         cmd = opkg_cmd_find (cmd_name);
500         if (cmd == NULL)
501         {
502                 fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
503                          cmd_name);
504                 args_usage (NULL);
505         }
506
507         if (cmd->requires_args && optind == argc)
508         {
509                 fprintf (stderr,
510                          "%s: the ``%s'' command requires at least one argument\n",
511                          __FUNCTION__, cmd_name);
512                 args_usage (NULL);
513         }
514
515         err = opkg_cmd_exec (cmd, &opkg_conf, argc - optind, (const char **) (argv + optind), NULL);
516
517         opkg_conf_deinit (&opkg_conf);
518
519         return err;
520 }