opkg: correct the name of the state changed callback and run it when appropriate
[project/opkg-lede.git] / 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 #ifdef OPKG_LIB
19
20 #include "opkg.h"
21 #include "includes.h"
22 #include "libopkg.h"
23
24 #include "args.h"
25 #include "opkg_conf.h"
26 #include "opkg_cmd.h"
27 #include "file_util.h"
28
29
30
31 opkg_message_callback opkg_cb_message = NULL;
32 opkg_response_callback opkg_cb_response = NULL;
33 opkg_status_callback opkg_cb_status = NULL;
34 opkg_list_callback opkg_cb_list = NULL;
35
36
37 int
38 opkg_init (opkg_message_callback mcall,
39 opkg_response_callback rcall,
40 args_t * args)
41 {
42 opkg_cb_message = mcall;
43 opkg_cb_response = rcall;
44
45 args_init (args);
46
47 return 0;
48 }
49
50
51 int
52 opkg_deinit (args_t * args)
53 {
54 args_deinit (args);
55 opkg_cb_message = NULL;
56 opkg_cb_response = NULL;
57
58 /* place other cleanup stuff here */
59
60 return 0;
61 }
62
63
64 int
65 opkg_packages_list(args_t *args,
66 const char *packages,
67 opkg_list_callback cblist,
68 void *userdata)
69 {
70 opkg_cmd_t *cmd;
71 opkg_conf_t opkg_conf;
72 int err;
73
74 err = opkg_conf_init (&opkg_conf, args);
75 if (err)
76 {
77 return err;
78 }
79
80 opkg_cb_list = cblist;
81 /* we need to do this because of static declarations,
82 * maybe a good idea to change */
83 cmd = opkg_cmd_find ("list");
84 if (packages)
85 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
86 else
87 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
88 opkg_cb_list = NULL;
89 opkg_conf_deinit (&opkg_conf);
90 return (err);
91 }
92
93
94 int
95 opkg_packages_status(args_t *args,
96 const char *packages,
97 opkg_status_callback cbstatus,
98 void *userdata)
99 {
100 opkg_cmd_t *cmd;
101 opkg_conf_t opkg_conf;
102 int err;
103
104 err = opkg_conf_init (&opkg_conf, args);
105 if (err)
106 {
107 return err;
108 }
109
110 opkg_cb_status = cbstatus;
111
112 /* we need to do this because of static declarations,
113 * maybe a good idea to change */
114 cmd = opkg_cmd_find ("status");
115 if (packages)
116 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
117 else
118 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
119
120 opkg_cb_status = NULL;
121 opkg_conf_deinit (&opkg_conf);
122 return (err);
123 }
124
125
126 int
127 opkg_packages_info(args_t *args,
128 const char *packages,
129 opkg_status_callback cbstatus,
130 void *userdata)
131 {
132 opkg_cmd_t *cmd;
133 opkg_conf_t opkg_conf;
134 int err;
135
136 err = opkg_conf_init (&opkg_conf, args);
137 if (err)
138 {
139 return err;
140 }
141
142 opkg_cb_status = cbstatus;
143
144 /* we need to do this because of static declarations,
145 * maybe a good idea to change */
146 cmd = opkg_cmd_find ("info");
147 if (packages)
148 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
149 else
150 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
151
152 opkg_cb_status = NULL;
153 opkg_conf_deinit (&opkg_conf);
154 return (err);
155 }
156
157
158 int
159 opkg_packages_install (args_t * args, const char *name)
160 {
161 opkg_cmd_t *cmd;
162 opkg_conf_t opkg_conf;
163 int err;
164
165 /* this error should be handled in application */
166 if (!name || !strlen (name))
167 return (-1);
168
169 err = opkg_conf_init (&opkg_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 = opkg_cmd_find ("install");
178 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
179
180 opkg_conf_deinit(&opkg_conf);
181 return (err);
182 }
183
184
185 int
186 opkg_packages_remove(args_t *args, const char *name, int purge)
187 {
188 opkg_cmd_t *cmd;
189 opkg_conf_t opkg_conf;
190 int err;
191
192 /* this error should be handled in application */
193 if (!name || !strlen (name))
194 return (-1);
195
196 err = opkg_conf_init (&opkg_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 = opkg_cmd_find ("purge");
206 else
207 cmd = opkg_cmd_find ("remove");
208
209 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
210
211 opkg_conf_deinit(&opkg_conf);
212 return (err);
213 }
214
215
216 int
217 opkg_lists_update(args_t *args)
218 {
219 opkg_cmd_t *cmd;
220 opkg_conf_t opkg_conf;
221 int err;
222
223 err = opkg_conf_init (&opkg_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 = opkg_cmd_find ("update");
232
233 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, NULL);
234
235 opkg_conf_deinit(&opkg_conf);
236 return (err);
237 }
238
239
240 int
241 opkg_packages_upgrade(args_t *args)
242 {
243 opkg_cmd_t *cmd;
244 opkg_conf_t opkg_conf;
245 int err;
246
247 err = opkg_conf_init (&opkg_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 = opkg_cmd_find ("upgrade");
256
257 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, NULL);
258
259 opkg_conf_deinit(&opkg_conf);
260 return (err);
261 }
262
263
264 int
265 opkg_packages_download (args_t * args, const char *name)
266 {
267 opkg_cmd_t *cmd;
268 opkg_conf_t opkg_conf;
269 int err;
270
271 /* this error should be handled in application */
272 if (!name || !strlen (name))
273 return (-1);
274
275 err = opkg_conf_init (&opkg_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 = opkg_cmd_find ("download");
284 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
285
286 opkg_conf_deinit(&opkg_conf);
287 return (err);
288 }
289
290
291 int
292 opkg_package_files(args_t *args,
293 const char *name,
294 opkg_list_callback cblist,
295 void *userdata)
296 {
297 opkg_cmd_t *cmd;
298 opkg_conf_t opkg_conf;
299 int err;
300
301 /* this error should be handled in application */
302 if (!name || !strlen (name))
303 return (-1);
304
305 err = opkg_conf_init (&opkg_conf, args);
306 if (err)
307 {
308 return err;
309 }
310
311 opkg_cb_list = cblist;
312
313 /* we need to do this because of static declarations,
314 * maybe a good idea to change */
315 cmd = opkg_cmd_find ("files");
316
317 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, userdata);
318
319 opkg_cb_list = NULL;
320 opkg_conf_deinit(&opkg_conf);
321 return (err);
322 }
323
324
325 int
326 opkg_file_search(args_t *args,
327 const char *file,
328 opkg_list_callback cblist,
329 void *userdata)
330 {
331 opkg_cmd_t *cmd;
332 opkg_conf_t opkg_conf;
333 int err;
334
335 /* this error should be handled in application */
336 if (!file || !strlen (file))
337 return (-1);
338
339 err = opkg_conf_init (&opkg_conf, args);
340 if (err)
341 {
342 return err;
343 }
344
345 opkg_cb_list = cblist;
346
347 /* we need to do this because of static declarations,
348 * maybe a good idea to change */
349 cmd = opkg_cmd_find ("search");
350 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &file, userdata);
351
352 opkg_cb_list = NULL;
353 opkg_conf_deinit(&opkg_conf);
354 return(err);
355 }
356
357
358 int
359 opkg_file_what(args_t *args, const char *file, const char* command)
360 {
361 opkg_cmd_t *cmd;
362 opkg_conf_t opkg_conf;
363 int err;
364
365 /* this error should be handled in application */
366 if (!file || !strlen (file))
367 return (-1);
368
369 err = opkg_conf_init (&opkg_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 = opkg_cmd_find (command);
378 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &file, NULL);
379
380 opkg_conf_deinit(&opkg_conf);
381 return(err);
382 }
383
384 #define opkg_package_whatdepends(args,file) opkg_file_what(args,file,"whatdepends")
385 #define opkg_package_whatrecommends(args, file) opkg_file_what(args,file,"whatrecommends")
386 #define opkg_package_whatprovides(args, file) opkg_file_what(args,file,"whatprovides")
387 #define opkg_package_whatconflicts(args, file) opkg_file_what(args,file,"whatconflicts")
388 #define opkg_package_whatreplaces(args, file) opkg_file_what(args,file,"whatreplaces")
389
390
391 int default_opkg_message_callback(opkg_conf_t *conf, message_level_t level,
392 char *msg)
393 {
394 if (conf && (conf->verbosity < level)) {
395 return 0;
396 } else {
397 #ifdef OPKG_LIB
398 if ( level == OPKG_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_opkg_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_opkg_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_opkg_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_opkg_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 opkg_op (int argc, char *argv[])
447 {
448 int err, optind;
449 args_t args;
450 char *cmd_name;
451 opkg_cmd_t *cmd;
452 opkg_conf_t opkg_conf;
453
454 args_init (&args);
455
456 optind = args_parse (&args, argc, argv);
457 if (optind == argc || optind < 0)
458 {
459 args_usage ("opkg 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 = opkg_conf_init (&opkg_conf, &args);
489 if (err)
490 {
491 return err;
492 }
493
494 args_deinit (&args);
495
496 opkg_cb_message = default_opkg_message_callback;
497 opkg_cb_response = default_opkg_response_callback;
498 opkg_cb_status = default_opkg_status_callback;
499 if ( strcmp(cmd_name, "files")==0)
500 opkg_cb_list = default_opkg_files_callback;
501 else
502 opkg_cb_list = default_opkg_list_callback;
503
504 cmd = opkg_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 = opkg_cmd_exec (cmd, &opkg_conf, argc - optind, (const char **) (argv + optind), NULL);
521
522 opkg_conf_deinit (&opkg_conf);
523
524 return err;
525 }
526
527 #endif /* OPKG_LIB */