4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <fcntl.h> /* open */
29 #include <string.h> /* strdup */
31 #include <sys/types.h>
32 #include <sys/utsname.h> /* uname */
41 /* global config variable */
42 config_t
*config
= NULL
;
44 config_t
*config_new(void)
46 config_t
*newconfig
= calloc(1, sizeof(config_t
));
48 pm_fprintf(stderr
, ALPM_LOG_ERROR
,
49 _("malloc failure: could not allocate %zd bytes\n"),
53 /* defaults which may get overridden later */
54 newconfig
->op
= PM_OP_MAIN
;
55 newconfig
->logmask
= ALPM_LOG_ERROR
| ALPM_LOG_WARNING
;
56 newconfig
->configfile
= strdup(CONFFILE
);
57 if(alpm_capabilities() & ALPM_CAPABILITY_SIGNATURES
) {
58 newconfig
->siglevel
= ALPM_SIG_PACKAGE
| ALPM_SIG_PACKAGE_OPTIONAL
|
59 ALPM_SIG_DATABASE
| ALPM_SIG_DATABASE_OPTIONAL
;
65 int config_free(config_t
*oldconfig
)
67 if(oldconfig
== NULL
) {
71 FREELIST(oldconfig
->holdpkg
);
72 FREELIST(oldconfig
->syncfirst
);
73 FREELIST(oldconfig
->ignorepkg
);
74 FREELIST(oldconfig
->ignoregrp
);
75 FREELIST(oldconfig
->noupgrade
);
76 FREELIST(oldconfig
->noextract
);
77 free(oldconfig
->configfile
);
78 free(oldconfig
->rootdir
);
79 free(oldconfig
->dbpath
);
80 free(oldconfig
->logfile
);
81 free(oldconfig
->gpgdir
);
82 FREELIST(oldconfig
->cachedirs
);
83 free(oldconfig
->xfercommand
);
84 free(oldconfig
->print_format
);
85 free(oldconfig
->arch
);
92 /** Helper function for download_with_xfercommand() */
93 static char *get_filename(const char *url
) {
94 char *filename
= strrchr(url
, '/');
95 if(filename
!= NULL
) {
101 /** Helper function for download_with_xfercommand() */
102 static char *get_destfile(const char *path
, const char *filename
) {
104 /* len = localpath len + filename len + null */
105 size_t len
= strlen(path
) + strlen(filename
) + 1;
106 destfile
= calloc(len
, sizeof(char));
107 snprintf(destfile
, len
, "%s%s", path
, filename
);
112 /** Helper function for download_with_xfercommand() */
113 static char *get_tempfile(const char *path
, const char *filename
) {
115 /* len = localpath len + filename len + '.part' len + null */
116 size_t len
= strlen(path
) + strlen(filename
) + 6;
117 tempfile
= calloc(len
, sizeof(char));
118 snprintf(tempfile
, len
, "%s%s.part", path
, filename
);
123 /** External fetch callback */
124 static int download_with_xfercommand(const char *url
, const char *localpath
,
130 char *parsedcmd
,*tempcmd
;
131 char *destfile
, *tempfile
, *filename
;
133 if(!config
->xfercommand
) {
137 filename
= get_filename(url
);
141 destfile
= get_destfile(localpath
, filename
);
142 tempfile
= get_tempfile(localpath
, filename
);
144 if(force
&& stat(tempfile
, &st
) == 0) {
147 if(force
&& stat(destfile
, &st
) == 0) {
151 tempcmd
= strdup(config
->xfercommand
);
152 /* replace all occurrences of %o with fn.part */
153 if(strstr(tempcmd
, "%o")) {
155 parsedcmd
= strreplace(tempcmd
, "%o", tempfile
);
159 /* replace all occurrences of %u with the download URL */
160 parsedcmd
= strreplace(tempcmd
, "%u", url
);
163 /* save the cwd so we can restore it later */
165 cwdfd
= open(".", O_RDONLY
);
166 } while(cwdfd
== -1 && errno
== EINTR
);
168 pm_printf(ALPM_LOG_ERROR
, _("could not get current working directory\n"));
171 /* cwd to the download directory */
172 if(chdir(localpath
)) {
173 pm_printf(ALPM_LOG_WARNING
, _("could not chdir to download directory %s\n"), localpath
);
177 /* execute the parsed command via /bin/sh -c */
178 pm_printf(ALPM_LOG_DEBUG
, "running command: %s\n", parsedcmd
);
179 retval
= system(parsedcmd
);
182 pm_printf(ALPM_LOG_WARNING
, _("running XferCommand: fork failed!\n"));
184 } else if(retval
!= 0) {
185 /* download failed */
186 pm_printf(ALPM_LOG_DEBUG
, "XferCommand command returned non-zero status "
187 "code (%d)\n", retval
);
190 /* download was successful */
193 if(rename(tempfile
, destfile
)) {
194 pm_printf(ALPM_LOG_ERROR
, _("could not rename %s to %s (%s)\n"),
195 tempfile
, destfile
, strerror(errno
));
202 /* restore the old cwd if we have it */
204 if(fchdir(cwdfd
) != 0) {
205 pm_printf(ALPM_LOG_ERROR
, _("could not restore working directory (%s)\n"),
212 /* hack to let an user the time to cancel a download */
223 int config_set_arch(const char *arch
)
225 if(strcmp(arch
, "auto") == 0) {
228 config
->arch
= strdup(un
.machine
);
230 config
->arch
= strdup(arch
);
232 pm_printf(ALPM_LOG_DEBUG
, "config: arch: %s\n", config
->arch
);
237 * Parse a signature verification level line.
238 * @param values the list of parsed option values
239 * @param storage location to store the derived signature level; any existing
240 * value here is used as a starting point
241 * @param file path to the config file
242 * @param linenum current line number in file
243 * @return 0 on success, 1 on any parsing error
245 static int process_siglevel(alpm_list_t
*values
, alpm_siglevel_t
*storage
,
246 const char *file
, int linenum
)
248 alpm_siglevel_t level
= *storage
;
252 /* Collapse the option names into a single bitmasked value */
253 for(i
= values
; i
; i
= alpm_list_next(i
)) {
254 const char *original
= i
->data
, *value
;
255 int package
= 0, database
= 0;
257 if (strncmp(original
, "Package", strlen("Package")) == 0) {
258 /* only packages are affected, don't flip flags for databases */
259 value
= original
+ strlen("Package");
261 } else if (strncmp(original
, "Database", strlen("Database")) == 0) {
262 /* only databases are affected, don't flip flags for packages */
263 value
= original
+ strlen("Database");
266 /* no prefix, so anything found will affect both packages and dbs */
268 package
= database
= 1;
271 /* now parse out and store actual flag if it is valid */
272 if(strcmp(value
, "Never") == 0) {
274 level
&= ~ALPM_SIG_PACKAGE
;
277 level
&= ~ALPM_SIG_DATABASE
;
279 } else if(strcmp(value
, "Optional") == 0) {
281 level
|= ALPM_SIG_PACKAGE
;
282 level
|= ALPM_SIG_PACKAGE_OPTIONAL
;
285 level
|= ALPM_SIG_DATABASE
;
286 level
|= ALPM_SIG_DATABASE_OPTIONAL
;
288 } else if(strcmp(value
, "Required") == 0) {
290 level
|= ALPM_SIG_PACKAGE
;
291 level
&= ~ALPM_SIG_PACKAGE_OPTIONAL
;
294 level
|= ALPM_SIG_DATABASE
;
295 level
&= ~ALPM_SIG_DATABASE_OPTIONAL
;
297 } else if(strcmp(value
, "TrustedOnly") == 0) {
299 level
&= ~ALPM_SIG_PACKAGE_MARGINAL_OK
;
300 level
&= ~ALPM_SIG_PACKAGE_UNKNOWN_OK
;
303 level
&= ~ALPM_SIG_DATABASE_MARGINAL_OK
;
304 level
&= ~ALPM_SIG_DATABASE_UNKNOWN_OK
;
306 } else if(strcmp(value
, "TrustAll") == 0) {
308 level
|= ALPM_SIG_PACKAGE_MARGINAL_OK
;
309 level
|= ALPM_SIG_PACKAGE_UNKNOWN_OK
;
312 level
|= ALPM_SIG_DATABASE_MARGINAL_OK
;
313 level
|= ALPM_SIG_DATABASE_UNKNOWN_OK
;
316 pm_printf(ALPM_LOG_ERROR
,
317 _("config file %s, line %d: invalid value for '%s' : '%s'\n"),
318 file
, linenum
, "SigLevel", original
);
321 level
&= ~ALPM_SIG_USE_DEFAULT
;
324 /* ensure we have sig checking ability and are actually turning it on */
325 if(!(alpm_capabilities() & ALPM_CAPABILITY_SIGNATURES
) &&
326 level
& (ALPM_SIG_PACKAGE
| ALPM_SIG_DATABASE
)) {
327 pm_printf(ALPM_LOG_ERROR
,
328 _("config file %s, line %d: '%s' option invalid, no signature support\n"),
329 file
, linenum
, "SigLevel");
339 static int process_cleanmethods(alpm_list_t
*values
,
340 const char *file
, int linenum
)
343 for(i
= values
; i
; i
= alpm_list_next(i
)) {
344 const char *value
= i
->data
;
345 if(strcmp(value
, "KeepInstalled") == 0) {
346 config
->cleanmethod
|= PM_CLEAN_KEEPINST
;
347 } else if(strcmp(value
, "KeepCurrent") == 0) {
348 config
->cleanmethod
|= PM_CLEAN_KEEPCUR
;
350 pm_printf(ALPM_LOG_ERROR
,
351 _("config file %s, line %d: invalid value for '%s' : '%s'\n"),
352 file
, linenum
, "CleanMethod", value
);
359 /** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm
360 * settings. Refactored out of the parseconfig code since all of them did
361 * the exact same thing and duplicated code.
362 * @param ptr a pointer to the start of the multiple options
363 * @param option the string (friendly) name of the option, used for messages
364 * @param list the list to add the option to
366 static void setrepeatingoption(char *ptr
, const char *option
,
371 while((q
= strchr(ptr
, ' '))) {
373 *list
= alpm_list_add(*list
, strdup(ptr
));
374 pm_printf(ALPM_LOG_DEBUG
, "config: %s: %s\n", option
, ptr
);
378 *list
= alpm_list_add(*list
, strdup(ptr
));
379 pm_printf(ALPM_LOG_DEBUG
, "config: %s: %s\n", option
, ptr
);
382 static int _parse_options(const char *key
, char *value
,
383 const char *file
, int linenum
)
386 /* options without settings */
387 if(strcmp(key
, "UseSyslog") == 0) {
388 config
->usesyslog
= 1;
389 pm_printf(ALPM_LOG_DEBUG
, "config: usesyslog\n");
390 } else if(strcmp(key
, "ILoveCandy") == 0) {
392 pm_printf(ALPM_LOG_DEBUG
, "config: chomp\n");
393 } else if(strcmp(key
, "VerbosePkgLists") == 0) {
394 config
->verbosepkglists
= 1;
395 pm_printf(ALPM_LOG_DEBUG
, "config: verbosepkglists\n");
396 } else if(strcmp(key
, "UseDelta") == 0) {
397 config
->usedelta
= 1;
398 pm_printf(ALPM_LOG_DEBUG
, "config: usedelta\n");
399 } else if(strcmp(key
, "TotalDownload") == 0) {
400 config
->totaldownload
= 1;
401 pm_printf(ALPM_LOG_DEBUG
, "config: totaldownload\n");
402 } else if(strcmp(key
, "CheckSpace") == 0) {
403 config
->checkspace
= 1;
405 pm_printf(ALPM_LOG_WARNING
,
406 _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
407 file
, linenum
, key
, "options");
410 /* options with settings */
411 if(strcmp(key
, "NoUpgrade") == 0) {
412 setrepeatingoption(value
, "NoUpgrade", &(config
->noupgrade
));
413 } else if(strcmp(key
, "NoExtract") == 0) {
414 setrepeatingoption(value
, "NoExtract", &(config
->noextract
));
415 } else if(strcmp(key
, "IgnorePkg") == 0) {
416 setrepeatingoption(value
, "IgnorePkg", &(config
->ignorepkg
));
417 } else if(strcmp(key
, "IgnoreGroup") == 0) {
418 setrepeatingoption(value
, "IgnoreGroup", &(config
->ignoregrp
));
419 } else if(strcmp(key
, "HoldPkg") == 0) {
420 setrepeatingoption(value
, "HoldPkg", &(config
->holdpkg
));
421 } else if(strcmp(key
, "SyncFirst") == 0) {
422 setrepeatingoption(value
, "SyncFirst", &(config
->syncfirst
));
423 } else if(strcmp(key
, "CacheDir") == 0) {
424 setrepeatingoption(value
, "CacheDir", &(config
->cachedirs
));
425 } else if(strcmp(key
, "Architecture") == 0) {
427 config_set_arch(value
);
429 } else if(strcmp(key
, "DBPath") == 0) {
430 /* don't overwrite a path specified on the command line */
431 if(!config
->dbpath
) {
432 config
->dbpath
= strdup(value
);
433 pm_printf(ALPM_LOG_DEBUG
, "config: dbpath: %s\n", value
);
435 } else if(strcmp(key
, "RootDir") == 0) {
436 /* don't overwrite a path specified on the command line */
437 if(!config
->rootdir
) {
438 config
->rootdir
= strdup(value
);
439 pm_printf(ALPM_LOG_DEBUG
, "config: rootdir: %s\n", value
);
441 } else if(strcmp(key
, "GPGDir") == 0) {
442 if(!config
->gpgdir
) {
443 config
->gpgdir
= strdup(value
);
444 pm_printf(ALPM_LOG_DEBUG
, "config: gpgdir: %s\n", value
);
446 } else if(strcmp(key
, "LogFile") == 0) {
447 if(!config
->logfile
) {
448 config
->logfile
= strdup(value
);
449 pm_printf(ALPM_LOG_DEBUG
, "config: logfile: %s\n", value
);
451 } else if(strcmp(key
, "XferCommand") == 0) {
452 config
->xfercommand
= strdup(value
);
453 pm_printf(ALPM_LOG_DEBUG
, "config: xfercommand: %s\n", value
);
454 } else if(strcmp(key
, "CleanMethod") == 0) {
455 alpm_list_t
*methods
= NULL
;
456 setrepeatingoption(value
, "CleanMethod", &methods
);
457 if(process_cleanmethods(methods
, file
, linenum
)) {
462 } else if(strcmp(key
, "SigLevel") == 0) {
463 alpm_list_t
*values
= NULL
;
464 setrepeatingoption(value
, "SigLevel", &values
);
465 if(process_siglevel(values
, &config
->siglevel
, file
, linenum
)) {
471 pm_printf(ALPM_LOG_WARNING
,
472 _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
473 file
, linenum
, key
, "options");
480 static int _add_mirror(alpm_db_t
*db
, char *value
)
482 const char *dbname
= alpm_db_get_name(db
);
483 /* let's attempt a replacement for the current repo */
484 char *temp
= strreplace(value
, "$repo", dbname
);
485 /* let's attempt a replacement for the arch */
486 const char *arch
= config
->arch
;
489 server
= strreplace(temp
, "$arch", arch
);
492 if(strstr(temp
, "$arch")) {
494 pm_printf(ALPM_LOG_ERROR
,
495 _("mirror '%s' contains the '%s' variable, but no '%s' is defined.\n"),
496 value
, "$arch", "Architecture");
502 if(alpm_db_add_server(db
, server
) != 0) {
503 /* pm_errno is set by alpm_db_setserver */
504 pm_printf(ALPM_LOG_ERROR
, _("could not add server URL to database '%s': %s (%s)\n"),
505 dbname
, server
, alpm_strerror(alpm_errno(config
->handle
)));
514 /** Sets up libalpm global stuff in one go. Called after the command line
515 * and inital config file parsing. Once this is complete, we can see if any
516 * paths were defined. If a rootdir was defined and nothing else, we want all
517 * of our paths to live under the rootdir that was specified. Safe to call
518 * multiple times (will only do anything the first time).
520 static int setup_libalpm(void)
523 enum _alpm_errno_t err
;
524 alpm_handle_t
*handle
;
526 pm_printf(ALPM_LOG_DEBUG
, "setup_libalpm called\n");
528 /* Configure root path first. If it is set and dbpath/logfile were not
529 * set, then set those as well to reside under the root. */
530 if(config
->rootdir
) {
532 if(!config
->dbpath
) {
533 snprintf(path
, PATH_MAX
, "%s/%s", config
->rootdir
, DBPATH
+ 1);
534 config
->dbpath
= strdup(path
);
536 if(!config
->logfile
) {
537 snprintf(path
, PATH_MAX
, "%s/%s", config
->rootdir
, LOGFILE
+ 1);
538 config
->logfile
= strdup(path
);
541 config
->rootdir
= strdup(ROOTDIR
);
542 if(!config
->dbpath
) {
543 config
->dbpath
= strdup(DBPATH
);
547 /* initialize library */
548 handle
= alpm_initialize(config
->rootdir
, config
->dbpath
, &err
);
550 pm_printf(ALPM_LOG_ERROR
, _("failed to initialize alpm library (%s)\n"),
552 if(err
== ALPM_ERR_DB_VERSION
) {
553 pm_printf(ALPM_LOG_ERROR
, _(" try running pacman-db-upgrade\n"));
557 config
->handle
= handle
;
559 alpm_option_set_logcb(handle
, cb_log
);
560 alpm_option_set_dlcb(handle
, cb_dl_progress
);
561 alpm_option_set_eventcb(handle
, cb_event
);
562 alpm_option_set_questioncb(handle
, cb_question
);
563 alpm_option_set_progresscb(handle
, cb_progress
);
565 config
->logfile
= config
->logfile
? config
->logfile
: strdup(LOGFILE
);
566 ret
= alpm_option_set_logfile(handle
, config
->logfile
);
568 pm_printf(ALPM_LOG_ERROR
, _("problem setting logfile '%s' (%s)\n"),
569 config
->logfile
, alpm_strerror(alpm_errno(handle
)));
573 /* Set GnuPG's home directory. This is not relative to rootdir, even if
574 * rootdir is defined. Reasoning: gpgdir contains configuration data. */
575 config
->gpgdir
= config
->gpgdir
? config
->gpgdir
: strdup(GPGDIR
);
576 ret
= alpm_option_set_gpgdir(handle
, config
->gpgdir
);
578 pm_printf(ALPM_LOG_ERROR
, _("problem setting gpgdir '%s' (%s)\n"),
579 config
->gpgdir
, alpm_strerror(alpm_errno(handle
)));
583 /* add a default cachedir if one wasn't specified */
584 if(config
->cachedirs
== NULL
) {
585 alpm_option_add_cachedir(handle
, CACHEDIR
);
587 alpm_option_set_cachedirs(handle
, config
->cachedirs
);
590 alpm_option_set_default_siglevel(handle
, config
->siglevel
);
592 if(config
->xfercommand
) {
593 alpm_option_set_fetchcb(handle
, download_with_xfercommand
);
594 } else if(!(alpm_capabilities() & ALPM_CAPABILITY_DOWNLOADER
)) {
595 pm_printf(ALPM_LOG_WARNING
, _("no '%s' configured"), "XferCommand");
598 if(config
->totaldownload
) {
599 alpm_option_set_totaldlcb(handle
, cb_dl_total
);
602 alpm_option_set_arch(handle
, config
->arch
);
603 alpm_option_set_checkspace(handle
, config
->checkspace
);
604 alpm_option_set_usesyslog(handle
, config
->usesyslog
);
605 alpm_option_set_usedelta(handle
, config
->usedelta
);
607 alpm_option_set_ignorepkgs(handle
, config
->ignorepkg
);
608 alpm_option_set_ignoregroups(handle
, config
->ignoregrp
);
609 alpm_option_set_noupgrades(handle
, config
->noupgrade
);
610 alpm_option_set_noextracts(handle
, config
->noextract
);
616 * Allows parsing in advance of an entire config section before we start
617 * calling library methods.
620 /* useful for all sections */
623 /* db section option gathering */
624 alpm_siglevel_t siglevel
;
625 alpm_list_t
*servers
;
629 * Wrap up a section once we have reached the end of it. This should be called
630 * when a subsequent section is encountered, or when we have reached the end of
631 * the root config file. Once called, all existing saved config pieces on the
632 * section struct are freed.
633 * @param section the current parsed and saved section data
634 * @param parse_options whether we are parsing options or repo data
635 * @return 0 on success, 1 on failure
637 static int finish_section(struct section_t
*section
, int parse_options
)
643 pm_printf(ALPM_LOG_DEBUG
, "config: finish section '%s'\n", section
->name
);
645 /* parsing options (or nothing)- nothing to do except free the pieces */
646 if(!section
->name
|| parse_options
|| section
->is_options
) {
650 /* if we are not looking at options sections only, register a db */
651 db
= alpm_db_register_sync(config
->handle
, section
->name
, section
->siglevel
);
653 pm_printf(ALPM_LOG_ERROR
, _("could not register '%s' database (%s)\n"),
654 section
->name
, alpm_strerror(alpm_errno(config
->handle
)));
659 for(i
= section
->servers
; i
; i
= alpm_list_next(i
)) {
660 char *value
= alpm_list_getdata(i
);
661 if(_add_mirror(db
, value
) != 0) {
662 pm_printf(ALPM_LOG_ERROR
,
663 _("could not add mirror '%s' to database '%s' (%s)\n"),
664 value
, section
->name
, alpm_strerror(alpm_errno(config
->handle
)));
672 alpm_list_free(section
->servers
);
673 section
->servers
= NULL
;
674 section
->siglevel
= ALPM_SIG_USE_DEFAULT
;
676 section
->name
= NULL
;
680 /** The "real" parseconfig. Each "Include" directive will recall this method so
681 * recursion and stack depth are limited to 10 levels. The publicly visible
682 * parseconfig calls this with a NULL section argument so we can recall from
683 * within ourself on an include.
684 * @param file path to the config file
685 * @param section the current active section
686 * @param parse_options whether to parse and call methods for the options
687 * section; if 0, parse and call methods for the repos sections
688 * @param depth the current recursion depth
689 * @return 0 on success, 1 on failure
691 static int _parseconfig(const char *file
, struct section_t
*section
,
692 int parse_options
, int depth
)
698 const int max_depth
= 10;
700 if(depth
>= max_depth
) {
701 pm_printf(ALPM_LOG_ERROR
,
702 _("config parsing exceeded max recursion depth of %d.\n"), max_depth
);
707 pm_printf(ALPM_LOG_DEBUG
, "config: attempting to read file %s\n", file
);
708 fp
= fopen(file
, "r");
710 pm_printf(ALPM_LOG_ERROR
, _("config file %s could not be read.\n"), file
);
715 while(fgets(line
, PATH_MAX
, fp
)) {
716 char *key
, *value
, *ptr
;
721 line_len
= strlen(line
);
723 /* ignore whole line and end of line comments */
724 if(line_len
== 0 || line
[0] == '#') {
727 if((ptr
= strchr(line
, '#'))) {
731 if(line
[0] == '[' && line
[line_len
- 1] == ']') {
733 /* only possibility here is a line == '[]' */
735 pm_printf(ALPM_LOG_ERROR
, _("config file %s, line %d: bad section name.\n"),
740 /* new config section, skip the '[' */
741 name
= strdup(line
+ 1);
742 name
[line_len
- 2] = '\0';
743 /* we're at a new section; perform any post-actions for the prior */
744 if(finish_section(section
, parse_options
)) {
748 pm_printf(ALPM_LOG_DEBUG
, "config: new section '%s'\n", name
);
749 section
->name
= name
;
750 section
->is_options
= (strcmp(name
, "options") == 0);
755 /* strsep modifies the 'line' string: 'key \0 value' */
763 pm_printf(ALPM_LOG_ERROR
, _("config file %s, line %d: syntax error in config file- missing key.\n"),
768 /* For each directive, compare to the camelcase string. */
769 if(section
->name
== NULL
) {
770 pm_printf(ALPM_LOG_ERROR
, _("config file %s, line %d: All directives must belong to a section.\n"),
775 /* Include is allowed in both options and repo sections */
776 if(strcmp(key
, "Include") == 0) {
782 pm_printf(ALPM_LOG_ERROR
, _("config file %s, line %d: directive '%s' needs a value\n"),
787 /* Ignore include failures... assume non-critical */
788 globret
= glob(value
, GLOB_NOCHECK
, NULL
, &globbuf
);
791 pm_printf(ALPM_LOG_DEBUG
,
792 "config file %s, line %d: include globbing out of space\n",
796 pm_printf(ALPM_LOG_DEBUG
,
797 "config file %s, line %d: include globbing read error for %s\n",
798 file
, linenum
, value
);
801 pm_printf(ALPM_LOG_DEBUG
,
802 "config file %s, line %d: no include found for %s\n",
803 file
, linenum
, value
);
806 for(gindex
= 0; gindex
< globbuf
.gl_pathc
; gindex
++) {
807 pm_printf(ALPM_LOG_DEBUG
, "config file %s, line %d: including %s\n",
808 file
, linenum
, globbuf
.gl_pathv
[gindex
]);
809 _parseconfig(globbuf
.gl_pathv
[gindex
], section
, parse_options
, depth
+ 1);
816 if(parse_options
&& section
->is_options
) {
817 /* we are either in options ... */
818 if((ret
= _parse_options(key
, value
, file
, linenum
)) != 0) {
821 } else if (!parse_options
&& !section
->is_options
) {
822 /* ... or in a repo section */
823 if(strcmp(key
, "Server") == 0) {
825 pm_printf(ALPM_LOG_ERROR
, _("config file %s, line %d: directive '%s' needs a value\n"),
830 section
->servers
= alpm_list_add(section
->servers
, strdup(value
));
831 } else if(strcmp(key
, "SigLevel") == 0) {
832 alpm_list_t
*values
= NULL
;
833 setrepeatingoption(value
, "SigLevel", &values
);
835 if(section
->siglevel
== ALPM_SIG_USE_DEFAULT
) {
836 section
->siglevel
= config
->siglevel
;
838 if(process_siglevel(values
, §ion
->siglevel
, file
, linenum
)) {
846 pm_printf(ALPM_LOG_WARNING
,
847 _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
848 file
, linenum
, key
, section
->name
);
854 ret
= finish_section(section
, parse_options
);
861 pm_printf(ALPM_LOG_DEBUG
, "config: finished parsing %s\n", file
);
865 /** Parse a configuration file.
866 * @param file path to the config file
867 * @return 0 on success, non-zero on error
869 int parseconfig(const char *file
)
872 struct section_t section
;
873 memset(§ion
, 0, sizeof(struct section_t
));
874 section
.siglevel
= ALPM_SIG_USE_DEFAULT
;
875 /* the config parse is a two-pass affair. We first parse the entire thing for
876 * the [options] section so we can get all default and path options set.
877 * Next, we go back and parse everything but [options]. */
879 /* call the real parseconfig function with a null section & db argument */
880 pm_printf(ALPM_LOG_DEBUG
, "parseconfig: options pass\n");
881 if((ret
= _parseconfig(file
, §ion
, 1, 0))) {
884 if((ret
= setup_libalpm())) {
887 /* second pass, repo section parsing */
888 pm_printf(ALPM_LOG_DEBUG
, "parseconfig: repo pass\n");
889 return _parseconfig(file
, §ion
, 0, 0);
892 /* vim: set ts=2 sw=2 noet: */