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