Remove str_util.{c,h}
[oweals/opkg-lede.git] / libopkg / opkg_conf.c
1 /* opkg_conf.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
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 "opkg_conf.h"
20 #include "opkg_error.h"
21
22 #include "xregex.h"
23 #include "sprintf_alloc.h"
24 #include "args.h"
25 #include "opkg_message.h"
26 #include "file_util.h"
27 #include "opkg_defines.h"
28 #include "libbb/libbb.h"
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <glob.h>
35
36 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
37                                 pkg_src_list_t *pkg_src_list,
38                                 nv_pair_list_t *tmp_dest_nv_pair_list);
39 static int opkg_conf_set_option(const opkg_option_t *options,
40                                 const char *name, const char *value);
41 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
42                                       const char *default_dest_name);
43 static int set_and_load_pkg_src_list(opkg_conf_t *conf,
44                                      pkg_src_list_t *nv_pair_list);
45 static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
46                                       nv_pair_list_t *nv_pair_list);
47
48 void opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
49 {
50      opkg_option_t tmp[] = {
51           { "cache", OPKG_OPT_TYPE_STRING, &conf->cache},
52           { "force_defaults", OPKG_OPT_TYPE_BOOL, &conf->force_defaults },
53           { "force_maintainer", OPKG_OPT_TYPE_BOOL, &conf->force_maintainer }, 
54           { "force_depends", OPKG_OPT_TYPE_BOOL, &conf->force_depends },
55           { "force_overwrite", OPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
56           { "force_downgrade", OPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
57           { "force_reinstall", OPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
58           { "force_space", OPKG_OPT_TYPE_BOOL, &conf->force_space },
59           { "check_signature", OPKG_OPT_TYPE_INT, &conf->check_signature }, 
60           { "ftp_proxy", OPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
61           { "http_proxy", OPKG_OPT_TYPE_STRING, &conf->http_proxy },
62           { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
63           { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
64           { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
65           { "nodeps", OPKG_OPT_TYPE_BOOL, &conf->nodeps },
66           { "offline_root", OPKG_OPT_TYPE_STRING, &conf->offline_root },
67           { "offline_root_path", OPKG_OPT_TYPE_STRING, &conf->offline_root_path },
68           { "offline_root_post_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
69           { "offline_root_pre_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
70           { "proxy_passwd", OPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
71           { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
72           { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
73           { "tmp_dir", OPKG_OPT_TYPE_STRING, &conf->tmp_dir },
74           { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
75 #if defined(HAVE_OPENSSL)
76           { "signature_ca_file", OPKG_OPT_TYPE_STRING, &conf->signature_ca_file },
77           { "signature_ca_path", OPKG_OPT_TYPE_STRING, &conf->signature_ca_path },
78 #endif
79 #if defined(HAVE_PATHFINDER)
80           { "check_x509_path", OPKG_OPT_TYPE_INT, &conf->check_x509_path }, 
81 #endif
82 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
83           { "ssl_engine", OPKG_OPT_TYPE_STRING, &conf->ssl_engine },
84           { "ssl_cert", OPKG_OPT_TYPE_STRING, &conf->ssl_cert },
85           { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &conf->ssl_cert_type },
86           { "ssl_key", OPKG_OPT_TYPE_STRING, &conf->ssl_key },
87           { "ssl_key_type", OPKG_OPT_TYPE_STRING, &conf->ssl_key_type },
88           { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &conf->ssl_key_passwd },
89           { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_file },
90           { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_path },
91           { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &conf->ssl_dont_verify_peer },
92 #endif
93           { NULL }
94      };
95
96      *options = xcalloc(1, sizeof(tmp));
97      memcpy(*options, tmp, sizeof(tmp));
98 };
99
100 static void opkg_conf_override_string(char **conf_str, char *arg_str) 
101 {
102      if (arg_str) {
103           if (*conf_str) {
104                free(*conf_str);
105           }
106           *conf_str = xstrdup(arg_str);
107      }
108 }
109
110 static void opkg_conf_free_string(char **conf_str)
111 {
112      if (*conf_str) {
113           free(*conf_str);
114           *conf_str = NULL;
115      }
116 }
117
118 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
119 {
120      int err;
121      int errno_copy;
122      char *tmp_dir_base, *tmp2;
123      nv_pair_list_t tmp_dest_nv_pair_list;
124      char *lock_file = NULL;
125      glob_t globbuf;
126      char *etc_opkg_conf_pattern;
127      char *offline_root = NULL;
128
129      memset(conf, 0, sizeof(opkg_conf_t));
130
131 #if defined(HAVE_PATHFINDER)
132      conf->check_x509_path = 1;
133 #endif
134
135      pkg_src_list_init(&conf->pkg_src_list);
136
137      nv_pair_list_init(&tmp_dest_nv_pair_list);
138      pkg_dest_list_init(&conf->pkg_dest_list);
139
140      nv_pair_list_init(&conf->arch_list);
141
142      conf->restrict_to_default_dest = 0;
143      conf->default_dest = NULL;
144
145      if (args->conf_file) {
146           struct stat stat_buf;
147           err = stat(args->conf_file, &stat_buf);
148           if (err == 0)
149                if (opkg_conf_parse_file(conf, args->conf_file,
150                                     &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
151                    /* Memory leakage from opkg_conf_parse-file */
152                    return OPKG_CONF_ERR_PARSE;
153                }
154      }
155
156      opkg_conf_override_string(&conf->offline_root, args->offline_root);
157      offline_root = conf->offline_root;
158
159      if (conf->offline_root)
160           sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root);
161      else {
162           char *conf_file_dir = getenv("OPKG_CONF_DIR");
163           if (conf_file_dir == NULL)
164                   conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
165           sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
166      }
167      memset(&globbuf, 0, sizeof(globbuf));
168      err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
169      free (etc_opkg_conf_pattern);
170      if (!err) {
171           int i;
172           for (i = 0; i < globbuf.gl_pathc; i++) {
173                if (globbuf.gl_pathv[i]) 
174                     if (args->conf_file &&
175                                 !strcmp(args->conf_file, globbuf.gl_pathv[i]))
176                             continue;
177                     if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
178                                          &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
179                         /* Memory leakage from opkg_conf_parse-file */
180                         return OPKG_CONF_ERR_PARSE;
181                     }
182                     if (offline_root != conf->offline_root) {
183                         opkg_message(conf, OPKG_ERROR,
184                                         "Config file %s, within an offline "
185                                         "root contains option offline_root.\n",
186                                        globbuf.gl_pathv[i]);
187                         return OPKG_CONF_ERR_PARSE;
188                     }
189           }
190      }
191      globfree(&globbuf);
192
193      opkg_conf_override_string(&conf->offline_root_path, 
194                                args->offline_root_path);
195      opkg_conf_override_string(&conf->offline_root_pre_script_cmd, 
196                                args->offline_root_pre_script_cmd);
197      opkg_conf_override_string(&conf->offline_root_post_script_cmd, 
198                                args->offline_root_post_script_cmd);
199
200      opkg_conf_override_string(&conf->cache, args->cache);
201      opkg_conf_override_string(&conf->tmp_dir, args->tmp_dir);
202
203      /* check for lock file */
204      if (conf->offline_root)
205        sprintf_alloc (&lock_file, "%s/%s/lock", conf->offline_root, OPKG_STATE_DIR_PREFIX);
206      else
207        sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
208
209      err = conf->lock_fd = creat (lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
210      if (err != -1)
211        err = lockf (conf->lock_fd, F_TLOCK, 0);
212      errno_copy = errno;
213
214      if (err) {
215        opkg_message (conf, OPKG_ERROR, "Could not lock %s: %s\n",
216                  lock_file, strerror(errno_copy));
217        free(lock_file);
218        return OPKG_CONF_ERR_LOCK;
219      }
220      free(lock_file);
221
222      if (conf->tmp_dir)
223           tmp_dir_base = conf->tmp_dir;
224      else 
225           tmp_dir_base = getenv("TMPDIR");
226      sprintf_alloc(&tmp2, "%s/%s",
227                    tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
228                    OPKG_CONF_TMP_DIR_SUFFIX);
229      if (conf->tmp_dir)
230              free(conf->tmp_dir);
231      conf->tmp_dir = mkdtemp(tmp2);
232      if (conf->tmp_dir == NULL) {
233           opkg_message(conf, OPKG_ERROR,
234                           "%s: Creating temp dir %s failed: %s\n",
235                           __FUNCTION__, tmp2, strerror(errno));
236           return OPKG_CONF_ERR_TMP_DIR;
237      }
238
239      pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
240      hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
241      hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN/16);
242
243      if (conf->lists_dir == NULL)
244         conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
245
246      if (conf->offline_root) {
247             char *tmp;
248             sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir);
249             free(conf->lists_dir);
250             conf->lists_dir = tmp;
251      }
252
253      /* if no architectures were defined, then default all, noarch, and host architecture */
254      if (nv_pair_list_empty(&conf->arch_list)) {
255           nv_pair_list_append(&conf->arch_list, "all", "1");
256           nv_pair_list_append(&conf->arch_list, "noarch", "1");
257           nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
258      }
259
260      /* Even if there is no conf file, we'll need at least one dest. */
261      if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
262           nv_pair_list_append(&tmp_dest_nv_pair_list,
263                               OPKG_CONF_DEFAULT_DEST_NAME,
264                               OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
265      }
266
267      /* After parsing the file, set options from command-line, (so that
268         command-line arguments take precedence) */
269      /* XXX: CLEANUP: The interaction between args.c and opkg_conf.c
270         really needs to be cleaned up. There is so much duplication
271         right now it is ridiculous. Maybe opkg_conf_t should just save
272         a pointer to args_t (which could then not be freed), rather
273         than duplicating every field here? */
274      if (args->autoremove) {
275           conf->autoremove = 1;
276      }
277      if (args->force_depends) {
278           conf->force_depends = 1;
279      }
280      if (args->force_defaults) {
281           conf->force_defaults = 1;
282      }
283      if (args->force_maintainer) {
284           conf->force_maintainer = 1;
285      }
286      if (args->force_overwrite) {
287           conf->force_overwrite = 1;
288      }
289      if (args->force_downgrade) {
290           conf->force_downgrade = 1;
291      }
292      if (args->force_space) {
293           conf->force_space = 1;
294      }
295      if (args->force_reinstall) {
296           conf->force_reinstall = 1;
297      }
298      if (args->force_removal_of_dependent_packages) {
299           conf->force_removal_of_dependent_packages = 1;
300      }
301      if (args->force_removal_of_essential_packages) {
302           conf->force_removal_of_essential_packages = 1;
303      }
304      if (args->nodeps) {
305           conf->nodeps = 1;
306      }
307      if (args->noaction) {
308           conf->noaction = 1;
309      }
310      if (args->query_all) {
311           conf->query_all = 1;
312      }
313      if (args->verbosity != conf->verbosity) {
314           conf->verbosity = args->verbosity;
315      } 
316
317 /* Pigi: added a flag to disable the checking of structures if the command does not need to 
318          read anything from there.
319 */
320      if (!(args->nocheckfordirorfile)) {
321
322         if (!(args->noreadfeedsfile)) {
323            if (set_and_load_pkg_src_list(conf, &conf->pkg_src_list)) {
324                nv_pair_list_deinit(&tmp_dest_nv_pair_list);
325                return -1;
326            }
327         }
328    
329         /* Now that we have resolved conf->offline_root, we can commit to
330            the directory names for the dests and load in all the package
331            lists. */
332         if (set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list)) {
333                nv_pair_list_deinit(&tmp_dest_nv_pair_list);
334                return -1;
335         }
336    
337         if (args->dest) {
338              err = opkg_conf_set_default_dest(conf, args->dest);
339              if (err) {
340                   nv_pair_list_deinit(&tmp_dest_nv_pair_list);
341                   return OPKG_CONF_ERR_DEFAULT_DEST;
342              }
343         }
344      }
345      nv_pair_list_deinit(&tmp_dest_nv_pair_list);
346
347      return 0;
348 }
349
350 void opkg_conf_deinit(opkg_conf_t *conf)
351 {
352      rm_r(conf->tmp_dir);
353
354      free(conf->tmp_dir);
355      free(conf->lists_dir);
356
357      pkg_src_list_deinit(&conf->pkg_src_list);
358      pkg_dest_list_deinit(&conf->pkg_dest_list);
359      nv_pair_list_deinit(&conf->arch_list);
360
361      opkg_conf_free_string(&conf->cache);
362
363      opkg_conf_free_string(&conf->ftp_proxy);
364      opkg_conf_free_string(&conf->http_proxy);
365      opkg_conf_free_string(&conf->no_proxy);
366
367      opkg_conf_free_string(&conf->offline_root);
368      opkg_conf_free_string(&conf->offline_root_path);
369      opkg_conf_free_string(&conf->offline_root_pre_script_cmd);
370      opkg_conf_free_string(&conf->offline_root_post_script_cmd);
371
372      opkg_conf_free_string(&conf->proxy_passwd);
373      opkg_conf_free_string(&conf->proxy_user);
374
375 #if defined(HAVE_OPENSSL)
376      opkg_conf_free_string(&conf->signature_ca_file);
377      opkg_conf_free_string(&conf->signature_ca_path);
378 #endif
379
380 #if defined(HAVE_SSLCURL)
381      opkg_conf_free_string(&conf->ssl_engine);
382      opkg_conf_free_string(&conf->ssl_cert);
383      opkg_conf_free_string(&conf->ssl_cert_type);
384      opkg_conf_free_string(&conf->ssl_key);
385      opkg_conf_free_string(&conf->ssl_key_type);
386      opkg_conf_free_string(&conf->ssl_key_passwd);
387      opkg_conf_free_string(&conf->ssl_ca_file);
388      opkg_conf_free_string(&conf->ssl_ca_path);
389 #endif
390
391      if (conf->verbosity >= OPKG_DEBUG) { 
392         hash_print_stats(&conf->pkg_hash);
393         hash_print_stats(&conf->file_hash);
394         hash_print_stats(&conf->obs_file_hash);
395      }
396
397      if (&conf->pkg_hash)
398                     pkg_hash_deinit(&conf->pkg_hash);
399      if (&conf->file_hash)
400                     hash_table_deinit(&conf->file_hash);
401      if (&conf->obs_file_hash)
402                     hash_table_deinit(&conf->obs_file_hash);
403
404      /* lockf maybe defined with warn_unused_result */
405      if(lockf(conf->lock_fd, F_ULOCK, 0) != 0){
406               opkg_message(conf, OPKG_DEBUG, "%s: unlock failed: %s\n",
407                               __FUNCTION__, 
408                               strerror(errno));
409      }
410      close(conf->lock_fd);
411 }
412
413 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
414                                       const char *default_dest_name)
415 {
416      pkg_dest_list_elt_t *iter;
417      pkg_dest_t *dest;
418
419      for (iter = void_list_first(&conf->pkg_dest_list); iter; iter = void_list_next(&conf->pkg_dest_list, iter)) {
420           dest = (pkg_dest_t *)iter->data;
421           if (strcmp(dest->name, default_dest_name) == 0) {
422                conf->default_dest = dest;
423                conf->restrict_to_default_dest = 1;
424                return 0;
425           }
426      }
427
428      fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
429
430      return 1;
431 }
432
433 static int
434 set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
435 {
436      pkg_src_list_elt_t *iter;
437      pkg_src_t *src;
438      char *list_file;
439
440      for (iter = void_list_first(pkg_src_list); iter; iter = void_list_next(pkg_src_list, iter)) {
441           src = (pkg_src_t *)iter->data;
442           if (src == NULL) {
443                continue;
444           }
445
446           sprintf_alloc(&list_file, "%s/%s", 
447                           conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
448                           src->name);
449
450           if (file_exists(list_file)) {
451                if (pkg_hash_add_from_file(conf, list_file, src, NULL, 0)) {
452                     free(list_file);
453                     return -1;
454                }
455           }
456           free(list_file);
457      }
458
459      return 0;
460 }
461
462 static int
463 set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list)
464 {
465      nv_pair_list_elt_t *iter;
466      nv_pair_t *nv_pair;
467      pkg_dest_t *dest;
468      char *root_dir;
469
470      for (iter = nv_pair_list_first(nv_pair_list); iter; iter = nv_pair_list_next(nv_pair_list, iter)) {
471           nv_pair = (nv_pair_t *)iter->data;
472
473           if (conf->offline_root) {
474                sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
475           } else {
476                root_dir = xstrdup(nv_pair->value);
477           }
478           dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
479           free(root_dir);
480           if (dest == NULL) {
481                continue;
482           }
483           if (conf->default_dest == NULL) {
484                conf->default_dest = dest;
485           }
486           if (file_exists(dest->status_file_name)) {
487                if (pkg_hash_add_from_file(conf, dest->status_file_name,
488                                       NULL, dest, 1))
489                        return -1;
490           }
491      }
492
493      return 0;
494 }
495
496 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
497                                 pkg_src_list_t *pkg_src_list,
498                                 nv_pair_list_t *tmp_dest_nv_pair_list)
499 {
500      int err;
501      opkg_option_t * options;
502      FILE *file;
503      regex_t valid_line_re, comment_re;
504 #define regmatch_size 12
505      regmatch_t regmatch[regmatch_size];
506
507      opkg_init_options_array(conf, &options);
508
509      file = fopen(filename, "r");
510      if (file == NULL) {
511           fprintf(stderr, "%s: failed to open %s: %s\n",
512                   __FUNCTION__, filename, strerror(errno));
513           free(options);
514           return -1;
515      }
516      opkg_message(conf, OPKG_NOTICE, "loading conf file %s\n", filename);
517
518      err = xregcomp(&comment_re, 
519                     "^[[:space:]]*(#.*|[[:space:]]*)$",
520                     REG_EXTENDED);
521      if (err) {
522           free(options);
523           return -1;
524      }
525      err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
526      if (err) {
527           free(options);
528           return -1;
529      }
530
531      while(1) {
532           int line_num = 0;
533           char *line;
534           char *type, *name, *value, *extra;
535
536           line = file_read_line_alloc(file);
537           line_num++;
538           if (line == NULL) {
539                break;
540           }
541
542           if (regexec(&comment_re, line, 0, 0, 0) == 0) {
543                goto NEXT_LINE;
544           }
545
546           if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
547                fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
548                        filename, line_num, line);
549                goto NEXT_LINE;
550           }
551
552           /* This has to be so ugly to deal with optional quotation marks */
553           if (regmatch[2].rm_so > 0) {
554                type = xstrndup(line + regmatch[2].rm_so,
555                               regmatch[2].rm_eo - regmatch[2].rm_so);
556           } else {
557                type = xstrndup(line + regmatch[3].rm_so,
558                               regmatch[3].rm_eo - regmatch[3].rm_so);
559           }
560           if (regmatch[5].rm_so > 0) {
561                name = xstrndup(line + regmatch[5].rm_so,
562                               regmatch[5].rm_eo - regmatch[5].rm_so);
563           } else {
564                name = xstrndup(line + regmatch[6].rm_so,
565                               regmatch[6].rm_eo - regmatch[6].rm_so);
566           }
567           if (regmatch[8].rm_so > 0) {
568                value = xstrndup(line + regmatch[8].rm_so,
569                                regmatch[8].rm_eo - regmatch[8].rm_so);
570           } else {
571                value = xstrndup(line + regmatch[9].rm_so,
572                                regmatch[9].rm_eo - regmatch[9].rm_so);
573           }
574           extra = NULL;
575           if (regmatch[11].rm_so > 0) {
576                extra = xstrndup (line + regmatch[11].rm_so,
577                                 regmatch[11].rm_eo - regmatch[11].rm_so);
578           }
579
580           /* We use the tmp_dest_nv_pair_list below instead of
581              conf->pkg_dest_list because we might encounter an
582              offline_root option later and that would invalidate the
583              directories we would have computed in
584              pkg_dest_list_init. (We do a similar thing with
585              tmp_src_nv_pair_list for sake of symmetry.) */
586           if (strcmp(type, "option") == 0) {
587                opkg_conf_set_option(options, name, value);
588           } else if (strcmp(type, "src") == 0) {
589                if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
590                     pkg_src_list_append (pkg_src_list, name, value, extra, 0);
591                } else {
592                     opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
593                                  name, value);
594                }
595           } else if (strcmp(type, "src/gz") == 0) {
596                if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
597                     pkg_src_list_append (pkg_src_list, name, value, extra, 1);
598                } else {
599                     opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
600                                  name, value);
601                }
602           } else if (strcmp(type, "dest") == 0) {
603                nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
604           } else if (strcmp(type, "lists_dir") == 0) {
605                conf->lists_dir = xstrdup(value);
606           } else if (strcmp(type, "arch") == 0) {
607                opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
608                if (!value) {
609                     opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
610                     value = xstrdup("10");
611                }
612                nv_pair_list_append(&conf->arch_list, name, value);
613           } else {
614                fprintf(stderr, "WARNING: Ignoring unknown configuration "
615                        "parameter: %s %s %s\n", type, name, value);
616                free(options);
617                return -1;
618           }
619
620           free(type);
621           free(name);
622           free(value);
623           if (extra)
624                free (extra);
625
626      NEXT_LINE:
627           free(line);
628      }
629
630      free(options);
631      regfree(&comment_re);
632      regfree(&valid_line_re);
633      fclose(file);
634
635      return 0;
636 }
637
638 static int opkg_conf_set_option(const opkg_option_t *options,
639                                 const char *name, const char *value)
640 {
641      int i = 0;
642      while (options[i].name) {
643           if (strcmp(options[i].name, name) == 0) {
644                switch (options[i].type) {
645                case OPKG_OPT_TYPE_BOOL:
646                     *((int *)options[i].value) = 1;
647                     return 0;
648                case OPKG_OPT_TYPE_INT:
649                     if (value) {
650                          *((int *)options[i].value) = atoi(value);
651                          return 0;
652                     } else {
653                          printf("%s: Option %s need an argument\n",
654                                 __FUNCTION__, name);
655                          return EINVAL;
656                     }               
657                case OPKG_OPT_TYPE_STRING:
658                     if (value) {
659                          *((char **)options[i].value) = xstrdup(value);
660                          return 0;
661                     } else {
662                          printf("%s: Option %s need an argument\n",
663                                 __FUNCTION__, name);
664                          return EINVAL;
665                     }
666                }
667           }
668           i++;
669      }
670     
671      fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
672              __FUNCTION__, name, value);
673      return EINVAL;
674 }
675
676 int opkg_conf_write_status_files(opkg_conf_t *conf)
677 {
678      pkg_dest_list_elt_t *iter;
679      pkg_dest_t *dest;
680      pkg_vec_t *all;
681      pkg_t *pkg;
682      int i, ret = 0;
683
684      if (conf->noaction)
685           return 0;
686
687      list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
688           dest = (pkg_dest_t *)iter->data;
689
690           dest->status_fp = fopen(dest->status_file_name, "w");
691           if (dest->status_fp == NULL) {
692                fprintf(stderr, "%s: Can't open status file: %s: %s\n",
693                     __FUNCTION__, dest->status_file_name, strerror(errno));
694                ret = -1;
695           }
696      }
697
698      all = pkg_vec_alloc();
699      pkg_hash_fetch_available(&conf->pkg_hash, all);
700
701      for(i = 0; i < all->len; i++) {
702           pkg = all->pkgs[i];
703           /* We don't need most uninstalled packages in the status file */
704           if (pkg->state_status == SS_NOT_INSTALLED
705               && (pkg->state_want == SW_UNKNOWN
706                   || pkg->state_want == SW_DEINSTALL
707                   || pkg->state_want == SW_PURGE)) {
708                continue;
709           }
710           if (pkg->dest == NULL) {
711                fprintf(stderr, "%s: ERROR: Can't write status for "
712                        "package %s since it has a NULL dest\n",
713                        __FUNCTION__, pkg->name);
714                continue;
715           }
716           if (pkg->dest->status_fp)
717                pkg_print_status(pkg, pkg->dest->status_fp);
718      }
719
720      pkg_vec_free(all);
721
722      list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
723           dest = (pkg_dest_t *)iter->data;
724           fclose(dest->status_fp);
725      }
726
727      return ret;
728 }
729
730
731 char *root_filename_alloc(opkg_conf_t *conf, char *filename)
732 {
733      char *root_filename;
734      sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
735      return root_filename;
736 }