- int err;
- char *tmp_dir_base;
- nv_pair_list_t tmp_dest_nv_pair_list;
- char * lists_dir =NULL;
- glob_t globbuf;
- char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
- char *pending_dir =NULL;
-
- memset(conf, 0, sizeof(opkg_conf_t));
-
- pkg_src_list_init(&conf->pkg_src_list);
-
- nv_pair_list_init(&tmp_dest_nv_pair_list);
- pkg_dest_list_init(&conf->pkg_dest_list);
-
- nv_pair_list_init(&conf->arch_list);
-
- conf->restrict_to_default_dest = 0;
- conf->default_dest = NULL;
-
-
- if (args->tmp_dir)
- tmp_dir_base = args->tmp_dir;
- else
- tmp_dir_base = getenv("TMPDIR");
- sprintf_alloc(&conf->tmp_dir, "%s/%s",
- tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
- OPKG_CONF_TMP_DIR_SUFFIX);
- conf->tmp_dir = mkdtemp(conf->tmp_dir);
- if (conf->tmp_dir == NULL) {
- fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
- __FUNCTION__, conf->tmp_dir, strerror(errno));
- return errno;
- }
-
- conf->force_depends = 0;
- conf->force_defaults = 0;
- conf->force_overwrite = 0;
- conf->force_downgrade = 0;
- conf->force_reinstall = 0;
- conf->force_space = 0;
- conf->force_removal_of_essential_packages = 0;
- conf->force_removal_of_dependent_packages = 0;
- conf->nodeps = 0;
- conf->verbose_wget = 0;
- conf->offline_root = NULL;
- conf->offline_root_pre_script_cmd = NULL;
- conf->offline_root_post_script_cmd = NULL;
- conf->multiple_providers = 0;
- conf->verbosity = 1;
- conf->noaction = 0;
-
- conf->http_proxy = NULL;
- conf->ftp_proxy = NULL;
- conf->no_proxy = NULL;
- conf->proxy_user = NULL;
- conf->proxy_passwd = NULL;
-
- pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
- hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
- hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
- lists_dir=(char *)malloc(1);
- lists_dir[0]='\0';
- if (args->conf_file) {
- struct stat stat_buf;
- err = stat(args->conf_file, &stat_buf);
- if (err == 0)
- if (opkg_conf_parse_file(conf, args->conf_file,
- &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
- /* Memory leakage from opkg_conf_parse-file */
- return -1;
- }
-
- }
-
- /* if (!lists_dir ){*/
- if (strlen(lists_dir)<=1 ){
- lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
- sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
- }
-
- if (args->offline_root) {
- char *tmp = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
- sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
- free(lists_dir);
- lists_dir = tmp;
- }
-
- pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
- snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
-
- conf->lists_dir = strdup(lists_dir);
- conf->pending_dir = strdup(pending_dir);
-
- if (args->offline_root)
- sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root);
- memset(&globbuf, 0, sizeof(globbuf));
- err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
- if (!err) {
- int i;
- for (i = 0; i < globbuf.gl_pathc; i++) {
- if (globbuf.gl_pathv[i])
- if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i],
- &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
- /* Memory leakage from opkg_conf_parse-file */
- return -1;
- }
- }
- }
- globfree(&globbuf);
-
- /* if no architectures were defined, then default all, noarch, and host architecture */
- if (nv_pair_list_empty(&conf->arch_list)) {
- nv_pair_list_append(&conf->arch_list, "all", "1");
- nv_pair_list_append(&conf->arch_list, "noarch", "1");
- nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
- }
-
- /* Even if there is no conf file, we'll need at least one dest. */
- if (tmp_dest_nv_pair_list.head == NULL) {
- nv_pair_list_append(&tmp_dest_nv_pair_list,
- OPKG_CONF_DEFAULT_DEST_NAME,
- OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
- }
-
- /* After parsing the file, set options from command-line, (so that
- command-line arguments take precedence) */
- /* XXX: CLEANUP: The interaction between args.c and opkg_conf.c
- really needs to be cleaned up. There is so much duplication
- right now it is ridiculous. Maybe opkg_conf_t should just save
- a pointer to args_t (which could then not be freed), rather
- than duplicating every field here? */
- if (args->autoremove) {
- conf->autoremove = 1;
- }
- if (args->force_depends) {
- conf->force_depends = 1;
- }
- if (args->force_defaults) {
- conf->force_defaults = 1;
- }
- if (args->force_overwrite) {
- conf->force_overwrite = 1;
- }
- if (args->force_downgrade) {
- conf->force_downgrade = 1;
- }
- if (args->force_reinstall) {
- conf->force_reinstall = 1;
- }
- if (args->force_removal_of_dependent_packages) {
- conf->force_removal_of_dependent_packages = 1;
- }
- if (args->force_removal_of_essential_packages) {
- conf->force_removal_of_essential_packages = 1;
- }
- if (args->nodeps) {
- conf->nodeps = 1;
- }
- if (args->noaction) {
- conf->noaction = 1;
- }
- if (args->query_all) {
- conf->query_all = 1;
- }
- if (args->verbose_wget) {
- conf->verbose_wget = 1;
- }
- if (args->multiple_providers) {
- conf->multiple_providers = 1;
- }
- if (args->verbosity != conf->verbosity) {
- conf->verbosity = args->verbosity;
- }
-
- opkg_conf_override_string(&conf->offline_root,
- args->offline_root);
- opkg_conf_override_string(&conf->offline_root_pre_script_cmd,
- args->offline_root_pre_script_cmd);
- opkg_conf_override_string(&conf->offline_root_post_script_cmd,
- args->offline_root_post_script_cmd);
-
-/* Pigi: added a flag to disable the checking of structures if the command does not need to
- read anything from there.
-*/
- if ( !(args->nocheckfordirorfile)){
- /* need to run load the source list before dest list -Jamey */
- if ( !(args->noreadfeedsfile))
- set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
-
- /* Now that we have resolved conf->offline_root, we can commit to
- the directory names for the dests and load in all the package
- lists. */
- set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
-
- if (args->dest) {
- err = opkg_conf_set_default_dest(conf, args->dest);
- if (err) {
- return err;
- }
- }
- }
- nv_pair_list_deinit(&tmp_dest_nv_pair_list);
- free(lists_dir);
- free(pending_dir);
-
- return 0;
+ int line_num = 0;
+ int err = 0;
+ FILE *file;
+ regex_t valid_line_re, comment_re;
+#define regmatch_size 14
+ regmatch_t regmatch[regmatch_size];
+
+ file = fopen(filename, "r");
+ if (file == NULL) {
+ opkg_perror(ERROR, "Failed to open %s", filename);
+ err = -1;
+ goto err0;
+ }
+
+ opkg_msg(INFO, "Loading conf file %s.\n", filename);
+
+ err = xregcomp(&comment_re,
+ "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED);
+ if (err)
+ goto err1;
+
+ err = xregcomp(&valid_line_re,
+ "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
+ "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
+ "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
+ "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
+ REG_EXTENDED);
+ if (err)
+ goto err2;
+
+ while (1) {
+ char *line;
+ char *type, *name, *value, *extra;
+
+ line_num++;
+
+ line = file_read_line_alloc(file);
+ if (line == NULL)
+ break;
+
+ if (regexec(&comment_re, line, 0, 0, 0) == 0)
+ goto NEXT_LINE;
+
+ if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) ==
+ REG_NOMATCH) {
+ opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
+ filename, line_num, line);
+ goto NEXT_LINE;
+ }
+
+ /* This has to be so ugly to deal with optional quotation marks */
+ if (regmatch[2].rm_so > 0) {
+ type = xstrndup(line + regmatch[2].rm_so,
+ regmatch[2].rm_eo - regmatch[2].rm_so);
+ } else {
+ type = xstrndup(line + regmatch[3].rm_so,
+ regmatch[3].rm_eo - regmatch[3].rm_so);
+ }
+
+ if (regmatch[5].rm_so > 0) {
+ name = xstrndup(line + regmatch[5].rm_so,
+ regmatch[5].rm_eo - regmatch[5].rm_so);
+ } else {
+ name = xstrndup(line + regmatch[6].rm_so,
+ regmatch[6].rm_eo - regmatch[6].rm_so);
+ }
+
+ if (regmatch[8].rm_so > 0) {
+ value = xstrndup(line + regmatch[8].rm_so,
+ regmatch[8].rm_eo - regmatch[8].rm_so);
+ } else {
+ value = xstrndup(line + regmatch[9].rm_so,
+ regmatch[9].rm_eo - regmatch[9].rm_so);
+ }
+
+ extra = NULL;
+ if (regmatch[11].rm_so > 0) {
+ if (regmatch[13].rm_so > 0
+ && regmatch[13].rm_so != regmatch[13].rm_eo)
+ extra =
+ xstrndup(line + regmatch[11].rm_so,
+ regmatch[13].rm_eo -
+ regmatch[11].rm_so);
+ else
+ extra = xstrndup(line + regmatch[11].rm_so,
+ regmatch[11].rm_eo -
+ regmatch[11].rm_so);
+ }
+
+ if (regmatch[13].rm_so != regmatch[13].rm_eo
+ && strncmp(type, "dist", 4) != 0) {
+ opkg_msg(ERROR,
+ "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
+ filename, line_num, line);
+ } else {
+
+ /* We use the conf->tmp_dest_list below instead of
+ conf->pkg_dest_list because we might encounter an
+ offline_root option later and that would invalidate the
+ directories we would have computed in
+ pkg_dest_list_init. (We do a similar thing with
+ tmp_src_nv_pair_list for sake of symmetry.) */
+ if (strcmp(type, "option") == 0) {
+ opkg_conf_set_option(name, value);
+ } else if (strcmp(type, "dist") == 0) {
+ if (!nv_pair_list_find
+ ((nv_pair_list_t *) dist_src_list, name)) {
+ pkg_src_list_append(dist_src_list, name,
+ value, extra, 0);
+ } else {
+ opkg_msg(ERROR,
+ "Duplicate dist declaration (%s %s). "
+ "Skipping.\n", name, value);
+ }
+ } else if (strcmp(type, "dist/gz") == 0) {
+ if (!nv_pair_list_find
+ ((nv_pair_list_t *) dist_src_list, name)) {
+ pkg_src_list_append(dist_src_list, name,
+ value, extra, 1);
+ } else {
+ opkg_msg(ERROR,
+ "Duplicate dist declaration (%s %s). "
+ "Skipping.\n", name, value);
+ }
+ } else if (strcmp(type, "src") == 0) {
+ if (!nv_pair_list_find
+ ((nv_pair_list_t *) pkg_src_list, name)) {
+ pkg_src_list_append(pkg_src_list, name,
+ value, extra, 0);
+ } else {
+ opkg_msg(ERROR,
+ "Duplicate src declaration (%s %s). "
+ "Skipping.\n", name, value);
+ }
+ } else if (strcmp(type, "src/gz") == 0) {
+ if (!nv_pair_list_find
+ ((nv_pair_list_t *) pkg_src_list, name)) {
+ pkg_src_list_append(pkg_src_list, name,
+ value, extra, 1);
+ } else {
+ opkg_msg(ERROR,
+ "Duplicate src declaration (%s %s). "
+ "Skipping.\n", name, value);
+ }
+ } else if (strcmp(type, "dest") == 0) {
+ nv_pair_list_append(&conf->tmp_dest_list, name,
+ value);
+ } else if (strcmp(type, "lists_dir") == 0) {
+ conf->lists_dir = xstrdup(value);
+ } else if (strcmp(type, "arch") == 0) {
+ opkg_msg(INFO,
+ "Supported arch %s priority (%s)\n",
+ name, value);
+ if (!value) {
+ opkg_msg(NOTICE,
+ "No priority given for architecture %s,"
+ "defaulting to 10\n", name);
+ value = xstrdup("10");
+ }
+ nv_pair_list_append(&conf->arch_list, name,
+ value);
+ } else {
+ opkg_msg(ERROR,
+ "%s:%d: Ignoring invalid line: `%s'\n",
+ filename, line_num, line);
+ }
+
+ }
+
+ free(type);
+ free(name);
+ free(value);
+ if (extra)
+ free(extra);
+
+NEXT_LINE:
+ free(line);
+ }
+
+ regfree(&valid_line_re);
+err2:
+ regfree(&comment_re);
+err1:
+ if (fclose(file) == EOF) {
+ opkg_perror(ERROR, "Couldn't close %s", filename);
+ err = -1;
+ }
+err0:
+ return err;