pacman.c

Go to the documentation of this file.
00001 /*
00002  *  pacman.c
00003  *
00004  *  Copyright (c) 2002-2008 by Judd Vinet <jvinet@zeroflux.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include "config.h"
00021 
00022 #include <stdlib.h> /* atoi */
00023 #include <stdio.h>
00024 #include <limits.h>
00025 #include <getopt.h>
00026 #include <string.h>
00027 #include <signal.h>
00028 #include <unistd.h>
00029 #include <sys/types.h>
00030 #include <sys/utsname.h> /* uname */
00031 #include <locale.h> /* setlocale */
00032 #include <time.h> /* time_t */
00033 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
00034 #include <mcheck.h> /* debug tracing (mtrace) */
00035 #endif
00036 
00037 /* alpm */
00038 #include <alpm.h>
00039 #include <alpm_list.h>
00040 
00041 /* pacman */
00042 #include "pacman.h"
00043 #include "util.h"
00044 #include "callback.h"
00045 #include "conf.h"
00046 #include "package.h"
00047 
00048 pmdb_t *db_local;
00049 /* list of targets specified on command line */
00050 static alpm_list_t *pm_targets;
00051 
00052 /** Display usage/syntax for the specified operation.
00053  * @param op     the operation code requested
00054  * @param myname basename(argv[0])
00055  */
00056 static void usage(int op, const char * const myname)
00057 {
00058     /* prefetch some strings for usage below, which moves a lot of calls
00059      * out of gettext. */
00060     char const * const str_opt = _("options");
00061     char const * const str_file = _("file");
00062     char const * const str_pkg = _("package");
00063     char const * const str_usg = _("usage");
00064     char const * const str_opr = _("operation");
00065 
00066     if(op == PM_OP_MAIN) {
00067         printf("%s:  %s <%s> [...]\n", str_usg, myname, str_opr);
00068         printf("%s:\n", str_opt);
00069         printf("    %s {-h --help}\n", myname);
00070         printf("    %s {-V --version}\n", myname);
00071         printf("    %s {-A --add}     [%s] <%s>\n", myname, str_opt, str_file);
00072         printf("    %s {-Q --query}   [%s] [%s]\n", myname, str_opt, str_pkg);
00073         printf("    %s {-R --remove}  [%s] <%s>\n", myname, str_opt, str_pkg);
00074         printf("    %s {-S --sync}    [%s] [%s]\n", myname, str_opt, str_pkg);
00075         printf("    %s {-U --upgrade} [%s] <%s>\n", myname, str_opt, str_file);
00076         printf(_("\nuse '%s --help' with other options for more syntax\n"), myname);
00077     } else {
00078         if(op == PM_OP_ADD) {
00079             printf("%s:  %s {-A --add} [%s] <%s>\n", str_usg, myname, str_opt, str_file);
00080             printf("%s:\n", str_opt);
00081             printf(_("      --asdeps         install packages as non-explicitly installed\n"));
00082             printf(_("  -d, --nodeps         skip dependency checks\n"));
00083             printf(_("  -f, --force          force install, overwrite conflicting files\n"));
00084         } else if(op == PM_OP_REMOVE) {
00085             printf("%s:  %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg);
00086             printf("%s:\n", str_opt);
00087             printf(_("  -c, --cascade        remove packages and all packages that depend on them\n"));
00088             printf(_("  -d, --nodeps         skip dependency checks\n"));
00089             printf(_("  -k, --dbonly         only remove database entry, do not remove files\n"));
00090             printf(_("  -n, --nosave         remove configuration files as well\n"));
00091             printf(_("  -s, --recursive      remove dependencies also (that won't break packages)\n"));
00092         } else if(op == PM_OP_UPGRADE) {
00093             printf("%s:  %s {-U --upgrade} [%s] <%s>\n", str_usg, myname, str_opt, str_file);
00094             printf("%s:\n", str_opt);
00095             printf(_("      --asdeps         install packages as non-explicitly installed\n"));
00096             printf(_("  -d, --nodeps         skip dependency checks\n"));
00097             printf(_("  -f, --force          force install, overwrite conflicting files\n"));
00098         } else if(op == PM_OP_QUERY) {
00099             printf("%s:  %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
00100             printf("%s:\n", str_opt);
00101             printf(_("  -c, --changelog      view the changelog of a package\n"));
00102             printf(_("  -d, --deps           list all packages installed as dependencies\n"));
00103             printf(_("  -e, --explicit       list all packages explicitly installed\n"));
00104             printf(_("  -g, --groups         view all members of a package group\n"));
00105             printf(_("  -i, --info           view package information (-ii for backup files)\n"));
00106             printf(_("  -l, --list           list the contents of the queried package\n"));
00107             printf(_("  -m, --foreign        list installed packages not found in sync db(s)\n"));
00108             printf(_("  -o, --owns <file>    query the package that owns <file>\n"));
00109             printf(_("  -p, --file <package> query a package file instead of the database\n"));
00110             printf(_("  -s, --search <regex> search locally-installed packages for matching strings\n"));
00111             printf(_("  -t, --unrequired     list all packages not required by any package\n"));
00112             printf(_("  -u, --upgrades       list all packages that can be upgraded\n"));
00113             printf(_("  -q, --quiet          show less information for query and search\n"));
00114         } else if(op == PM_OP_SYNC) {
00115             printf("%s:  %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
00116             printf("%s:\n", str_opt);
00117             printf(_("      --asdeps         install packages as non-explicitly installed\n"));
00118             printf(_("  -c, --clean          remove old packages from cache directory (-cc for all)\n"));
00119             printf(_("  -d, --nodeps         skip dependency checks\n"));
00120             printf(_("  -e, --dependsonly    install dependencies only\n"));
00121             printf(_("  -f, --force          force install, overwrite conflicting files\n"));
00122             printf(_("  -g, --groups         view all members of a package group\n"));
00123             printf(_("  -i, --info           view package information\n"));
00124             printf(_("  -l, --list <repo>    view a list of packages in a repo\n"));
00125             printf(_("  -p, --print-uris     print out URIs for given packages and their dependencies\n"));
00126             printf(_("  -s, --search <regex> search remote repositories for matching strings\n"));
00127             printf(_("  -u, --sysupgrade     upgrade all packages that are out of date\n"));
00128             printf(_("  -w, --downloadonly   download packages but do not install/upgrade anything\n"));
00129             printf(_("  -y, --refresh        download fresh package databases from the server\n"));
00130             printf(_("      --needed         only upgrade outdated or not yet installed packages\n"));
00131             printf(_("      --ignore <pkg>   ignore a package upgrade (can be used more than once)\n"));
00132             printf(_("      --ignoregroup <grp>\n"
00133                      "                       ignore a group upgrade (can be used more than once)\n"));
00134             printf(_("  -q  --quiet          show less information for query and search\n"));
00135         }
00136         printf(_("      --config <path>  set an alternate configuration file\n"));
00137         printf(_("      --logfile <path> set an alternate log file\n"));
00138         printf(_("      --noconfirm      do not ask for any confirmation\n"));
00139         printf(_("      --noprogressbar  do not show a progress bar when downloading files\n"));
00140         printf(_("      --noscriptlet    do not execute the install scriptlet if one exists\n"));
00141         printf(_("  -v, --verbose        be verbose\n"));
00142         printf(_("  -r, --root <path>    set an alternate installation root\n"));
00143         printf(_("  -b, --dbpath <path>  set an alternate database location\n"));
00144         printf(_("      --cachedir <dir> set an alternate package cache location\n"));
00145     }
00146 }
00147 
00148 /** Output pacman version and copyright.
00149  */
00150 static void version(void)
00151 {
00152     printf("\n");
00153     printf(" .--.                  Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, LIB_VERSION);
00154     printf("/ _.-' .-.  .-.  .-.   Copyright (C) 2002-2008 Judd Vinet <jvinet@zeroflux.org>\n");
00155     printf("\\  '-. '-'  '-'  '-'\n");
00156     printf(" '--'\n");
00157     printf(_("                       This program may be freely redistributed under\n"
00158              "                       the terms of the GNU General Public License\n"));
00159     printf("\n");
00160 }
00161 
00162 /** Sets up gettext localization. Safe to call multiple times.
00163  */
00164 /* Inspired by the monotone function localize_monotone. */
00165 #if defined(ENABLE_NLS)
00166 static void localize(void)
00167 {
00168     static int init = 0;
00169     if (!init) {
00170         setlocale(LC_ALL, "");
00171         bindtextdomain(PACKAGE, LOCALEDIR);
00172         textdomain(PACKAGE);
00173         init = 1;
00174     }
00175 }
00176 #endif
00177 
00178 /** Set user agent environment variable.
00179  */
00180 static void setuseragent(void)
00181 {
00182     char agent[101];
00183     struct utsname un;
00184 
00185     uname(&un);
00186     snprintf(agent, 100, "pacman/" PACKAGE_VERSION " (%s %s) libalpm/" LIB_VERSION,
00187             un.sysname, un.machine);
00188     setenv("HTTP_USER_AGENT", agent, 0);
00189 }
00190 
00191 /** Catches thrown signals. Performs necessary cleanup to ensure database is
00192  * in a consistant state.
00193  * @param signum the thrown signal
00194  */
00195 static void cleanup(int signum)
00196 {
00197     if(signum==SIGSEGV)
00198     {
00199         /* write a log message and write to stderr */
00200         pm_printf(PM_LOG_ERROR, "segmentation fault\n");
00201         pm_fprintf(stderr, PM_LOG_ERROR, "Internal pacman error: Segmentation fault.\n"
00202                 "Please submit a full bug report with --debug if appropriate.\n");
00203         exit(signum);
00204     } else if((signum == SIGINT)) {
00205         if(alpm_trans_interrupt() == 0) {
00206             /* a transaction is being interrupted, don't exit pacman yet. */
00207             return;
00208         }
00209         /* no commiting transaction, we can release it now and then exit pacman */
00210         alpm_trans_release();
00211         /* output a newline to be sure we clear any line we may be on */
00212         printf("\n");
00213     }
00214 
00215     /* free alpm library resources */
00216     if(alpm_release() == -1) {
00217         pm_printf(PM_LOG_ERROR, alpm_strerrorlast());
00218     }
00219 
00220     /* free memory */
00221     FREELIST(pm_targets);
00222     if(config) {
00223         config_free(config);
00224         config = NULL;
00225     }
00226 
00227     exit(signum);
00228 }
00229 
00230 /** Sets all libalpm required paths in one go. Called after the command line and
00231  * inital config file parsing. Once this is complete, we can see if any paths were
00232  * defined. If a rootdir was defined and nothing else, we want all of our paths to
00233  * live under the rootdir that was specified. Safe to call multiple times (will only
00234  * do anything the first time).
00235  */
00236 static void setlibpaths(void)
00237 {
00238     static int init = 0;
00239     if (!init) {
00240         int ret = 0;
00241 
00242         pm_printf(PM_LOG_DEBUG, "setlibpaths() called\n");
00243         if(config->rootdir) {
00244             char path[PATH_MAX];
00245             ret = alpm_option_set_root(config->rootdir);
00246             if(ret != 0) {
00247                 pm_printf(PM_LOG_ERROR, _("problem setting rootdir '%s' (%s)\n"),
00248                         config->rootdir, alpm_strerrorlast());
00249                 cleanup(ret);
00250             }
00251             if(!config->dbpath) {
00252                 snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), DBPATH);
00253                 config->dbpath = strdup(path);
00254             }
00255             if(!config->logfile) {
00256                 snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), LOGFILE);
00257                 ret = alpm_option_set_dbpath(path);
00258                 config->logfile = strdup(path);
00259             }
00260         }
00261         if(config->dbpath) {
00262             ret = alpm_option_set_dbpath(config->dbpath);
00263             if(ret != 0) {
00264                 pm_printf(PM_LOG_ERROR, _("problem setting dbpath '%s' (%s)\n"),
00265                         config->dbpath, alpm_strerrorlast());
00266                 cleanup(ret);
00267             }
00268         }
00269         if(config->logfile) {
00270             ret = alpm_option_set_logfile(config->logfile);
00271             if(ret != 0) {
00272                 pm_printf(PM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"),
00273                         config->logfile, alpm_strerrorlast());
00274                 cleanup(ret);
00275             }
00276         }
00277 
00278         /* add a default cachedir if one wasn't specified */
00279         if(alpm_option_get_cachedirs() == NULL) {
00280             alpm_option_add_cachedir(CACHEDIR);
00281         }
00282         init = 1;
00283     }
00284 }
00285 
00286 /** Parse command-line arguments for each operation.
00287  * @param argc argc
00288  * @param argv argv
00289  * @return 0 on success, 1 on error
00290  */
00291 static int parseargs(int argc, char *argv[])
00292 {
00293     int opt;
00294     int option_index = 0;
00295     static struct option opts[] =
00296     {
00297         {"add",        no_argument,       0, 'A'},
00298         {"query",      no_argument,       0, 'Q'},
00299         {"remove",     no_argument,       0, 'R'},
00300         {"sync",       no_argument,       0, 'S'},
00301         {"deptest",    no_argument,       0, 'T'}, /* used by makepkg */
00302         {"upgrade",    no_argument,       0, 'U'},
00303         {"version",    no_argument,       0, 'V'},
00304         {"dbpath",     required_argument, 0, 'b'},
00305         {"cascade",    no_argument,       0, 'c'},
00306         {"changelog",  no_argument,       0, 'c'},
00307         {"clean",      no_argument,       0, 'c'},
00308         {"nodeps",     no_argument,       0, 'd'},
00309         {"deps",       no_argument,       0, 'd'},
00310         {"dependsonly",no_argument,       0, 'e'},
00311         {"explicit",   no_argument,       0, 'e'},
00312         {"force",      no_argument,       0, 'f'},
00313         {"groups",     no_argument,       0, 'g'},
00314         {"help",       no_argument,       0, 'h'},
00315         {"info",       no_argument,       0, 'i'},
00316         {"dbonly",     no_argument,       0, 'k'},
00317         {"list",       no_argument,       0, 'l'},
00318         {"foreign",    no_argument,       0, 'm'},
00319         {"nosave",     no_argument,       0, 'n'},
00320         {"owns",       no_argument,       0, 'o'},
00321         {"file",       no_argument,       0, 'p'},
00322         {"print-uris", no_argument,       0, 'p'},
00323         {"quiet",      no_argument,       0, 'q'},
00324         {"root",       required_argument, 0, 'r'},
00325         {"recursive",  no_argument,       0, 's'},
00326         {"search",     no_argument,       0, 's'},
00327         {"unrequired", no_argument,       0, 't'},
00328         {"upgrades",   no_argument,       0, 'u'},
00329         {"sysupgrade", no_argument,       0, 'u'},
00330         {"verbose",    no_argument,       0, 'v'},
00331         {"downloadonly", no_argument,     0, 'w'},
00332         {"refresh",    no_argument,       0, 'y'},
00333         {"noconfirm",  no_argument,       0, 1000},
00334         {"config",     required_argument, 0, 1001},
00335         {"ignore",     required_argument, 0, 1002},
00336         {"debug",      optional_argument, 0, 1003},
00337         {"noprogressbar", no_argument,    0, 1004},
00338         {"noscriptlet", no_argument,      0, 1005},
00339         {"cachedir",   required_argument, 0, 1007},
00340         {"asdeps",     no_argument,       0, 1008},
00341         {"logfile",    required_argument, 0, 1009},
00342         {"ignoregroup", required_argument, 0, 1010},
00343         {"needed",     no_argument,       0, 1011},
00344         {0, 0, 0, 0}
00345     };
00346 
00347     while((opt = getopt_long(argc, argv, "ARUFQSTr:b:vkhscVfmnoldepqituwygz", opts, &option_index))) {
00348         alpm_list_t *list = NULL, *item = NULL; /* lists for splitting strings */
00349 
00350         if(opt < 0) {
00351             break;
00352         }
00353         switch(opt) {
00354             case 0: break;
00355             case 1000: config->noconfirm = 1; break;
00356             case 1001:
00357                 if(config->configfile) {
00358                     free(config->configfile);
00359                 }
00360                 config->configfile = strndup(optarg, PATH_MAX);
00361                 break;
00362             case 1002:
00363                 list = strsplit(optarg, ',');
00364                 for(item = list; item; item = alpm_list_next(item)) {
00365                     alpm_option_add_ignorepkg((char *)alpm_list_getdata(item));
00366                 }
00367                 FREELIST(list);
00368                 break;
00369             case 1003:
00370                 /* debug levels are made more 'human readable' than using a raw logmask
00371                  * here, error and warning are set in config_new, though perhaps a
00372                  * --quiet option will remove these later */
00373                 if(optarg) {
00374                     unsigned short debug = atoi(optarg);
00375                     switch(debug) {
00376                         case 2:
00377                             config->logmask |= PM_LOG_FUNCTION; /* fall through */
00378                         case 1:
00379                             config->logmask |= PM_LOG_DEBUG;
00380                             break;
00381                         default:
00382                           pm_printf(PM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
00383                                     optarg);
00384                             return(1);
00385                     }
00386                 } else {
00387                     config->logmask |= PM_LOG_DEBUG;
00388                 }
00389                 /* progress bars get wonky with debug on, shut them off */
00390                 config->noprogressbar = 1;
00391                 break;
00392             case 1004: config->noprogressbar = 1; break;
00393             case 1005: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
00394             case 1007:
00395                 if(alpm_option_add_cachedir(optarg) != 0) {
00396                     pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
00397                             optarg, alpm_strerrorlast());
00398                     return(1);
00399                 }
00400                 break;
00401             case 1008:
00402                 config->flags |= PM_TRANS_FLAG_ALLDEPS;
00403                 break;
00404             case 1009:
00405                 config->logfile = strdup(optarg);
00406                 break;
00407             case 1010:
00408                 list = strsplit(optarg, ',');
00409                 for(item = list; item; item = alpm_list_next(item)) {
00410                     alpm_option_add_ignoregrp((char *)alpm_list_getdata(item));
00411                 }
00412                 FREELIST(list);
00413                 break;
00414             case 1011: config->flags |= PM_TRANS_FLAG_NEEDED; break;
00415             case 'A': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_ADD); break;
00416             case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
00417             case 'R': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
00418             case 'S': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
00419             case 'T': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
00420             case 'U': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
00421             case 'V': config->version = 1; break;
00422             case 'b':
00423                 config->dbpath = strdup(optarg);
00424                 break;
00425             case 'c':
00426                 (config->op_s_clean)++;
00427                 config->flags |= PM_TRANS_FLAG_CASCADE;
00428                 config->op_q_changelog = 1;
00429                 break;
00430             case 'd':
00431                 config->op_q_deps = 1;
00432                 config->flags |= PM_TRANS_FLAG_NODEPS;
00433                 break;
00434             case 'e':
00435                 config->op_q_explicit = 1;
00436                 config->flags |= PM_TRANS_FLAG_DEPENDSONLY;
00437                 break;
00438             case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
00439             case 'g': (config->group)++; break;
00440             case 'h': config->help = 1; break;
00441             case 'i': (config->op_q_info)++; (config->op_s_info)++; break;
00442             case 'k': config->flags |= PM_TRANS_FLAG_DBONLY; break;
00443             case 'l': config->op_q_list = 1; break;
00444             case 'm': config->op_q_foreign = 1; break;
00445             case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
00446             case 'o': config->op_q_owns = 1; break;
00447             case 'p':
00448                 config->op_q_isfile = 1;
00449                 config->flags |= PM_TRANS_FLAG_PRINTURIS;
00450                 break;
00451             case 'q':
00452                 config->quiet = 1;
00453                 break;
00454             case 'r':
00455                 config->rootdir = strdup(optarg);
00456                 break;
00457             case 's':
00458                 config->op_s_search = 1;
00459                 config->op_q_search = 1;
00460                 config->flags |= PM_TRANS_FLAG_RECURSE;
00461                 break;
00462             case 't':
00463                 config->op_q_unrequired = 1;
00464                 break;
00465             case 'u':
00466                 config->op_s_upgrade = 1;
00467                 config->op_q_upgrade = 1;
00468                 break;
00469             case 'v': (config->verbose)++; break;
00470             case 'w':
00471                 config->op_s_downloadonly = 1;
00472                 config->flags |= PM_TRANS_FLAG_DOWNLOADONLY;
00473                 config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
00474                 break;
00475             case 'y': (config->op_s_sync)++; break;
00476             case '?': return(1);
00477             default: return(1);
00478         }
00479     }
00480 
00481     if(config->op == 0) {
00482         pm_printf(PM_LOG_ERROR, _("only one operation may be used at a time\n"));
00483         return(1);
00484     }
00485 
00486     if(config->help) {
00487         usage(config->op, mbasename(argv[0]));
00488         return(2);
00489     }
00490     if(config->version) {
00491         version();
00492         return(2);
00493     }
00494 
00495     while(optind < argc) {
00496         /* add the target to our target array */
00497         pm_targets = alpm_list_add(pm_targets, strdup(argv[optind]));
00498         optind++;
00499     }
00500 
00501     return(0);
00502 }
00503 
00504 /** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm
00505  * settings. Refactored out of the parseconfig code since all of them did
00506  * the exact same thing and duplicated code.
00507  * @param ptr a pointer to the start of the multiple options
00508  * @param option the string (friendly) name of the option, used for messages
00509  * @param optionfunc a function pointer to an alpm_option_add_* function
00510  */
00511 static void setrepeatingoption(const char *ptr, const char *option,
00512         void (*optionfunc)(const char*))
00513 {
00514     char *p = (char*)ptr;
00515     char *q;
00516 
00517     while((q = strchr(p, ' '))) {
00518         *q = '\0';
00519         (*optionfunc)(p);
00520         pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, p);
00521         p = q;
00522         p++;
00523     }
00524     (*optionfunc)(p);
00525     pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, p);
00526 }
00527 
00528 /* The real parseconfig. Called with a null section argument by the publicly
00529  * visible parseconfig so we can recall from within ourself on an include */
00530 static int _parseconfig(const char *file, const char *givensection,
00531                         pmdb_t * const givendb)
00532 {
00533     FILE *fp = NULL;
00534     char line[PATH_MAX+1];
00535     int linenum = 0;
00536     char *ptr, *section = NULL;
00537     pmdb_t *db = NULL;
00538 
00539     pm_printf(PM_LOG_DEBUG, "config: attempting to read file %s\n", file);
00540     fp = fopen(file, "r");
00541     if(fp == NULL) {
00542         pm_printf(PM_LOG_ERROR, _("config file %s could not be read.\n"), file);
00543         return(1);
00544     }
00545 
00546     /* if we are passed a section, use it as our starting point */
00547     if(givensection != NULL) {
00548         section = strdup(givensection);
00549     }
00550     /* if we are passed a db, use it as our starting point */
00551     if(givendb != NULL) {
00552         db = givendb;
00553     }
00554 
00555     while(fgets(line, PATH_MAX, fp)) {
00556         linenum++;
00557         strtrim(line);
00558 
00559         /* ignore whole line and end of line comments */
00560         if(strlen(line) == 0 || line[0] == '#') {
00561             continue;
00562         }
00563         if((ptr = strchr(line, '#'))) {
00564             *ptr = '\0';
00565         }
00566 
00567         if(line[0] == '[' && line[strlen(line)-1] == ']') {
00568             /* new config section, skip the '[' */
00569             ptr = line;
00570             ptr++;
00571             if(section) {
00572                 free(section);
00573             }
00574             section = strdup(ptr);
00575             section[strlen(section)-1] = '\0';
00576             pm_printf(PM_LOG_DEBUG, "config: new section '%s'\n", section);
00577             if(!strlen(section)) {
00578                 pm_printf(PM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
00579                         file, linenum);
00580                 return(1);
00581             }
00582             /* if we are not looking at the options section, register a db and also
00583              * ensure we have set all of our library paths as the library is too stupid
00584              * at the moment to do lazy opening of the databases */
00585             if(strcmp(section, "options") != 0) {
00586                 setlibpaths();
00587                 db = alpm_db_register_sync(section);
00588             }
00589         } else {
00590             /* directive */
00591             char *key, *upperkey;
00592             /* strsep modifies the 'line' string: 'key \0 ptr' */
00593             key = line;
00594             ptr = line;
00595             strsep(&ptr, "=");
00596             strtrim(key);
00597             strtrim(ptr);
00598 
00599             if(key == NULL) {
00600                 pm_printf(PM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
00601                         file, linenum);
00602                 return(1);
00603             }
00604             /* For each directive, compare to the uppercase and camelcase string.
00605              * This prevents issues with certain locales where characters don't
00606              * follow the toupper() rules we may expect, e.g. tr_TR where i != I.
00607              */
00608             upperkey = strtoupper(strdup(key));
00609             if(section == NULL) {
00610                 pm_printf(PM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"),
00611                         file, linenum);
00612                 return(1);
00613             }
00614             if(ptr == NULL && strcmp(section, "options") == 0) {
00615                 /* directives without settings, all in [options] */
00616                 if(strcmp(key, "NoPassiveFTP") == 0 || strcmp(upperkey, "NOPASSIVEFTP") == 0) {
00617                     alpm_option_set_nopassiveftp(1);
00618                     pm_printf(PM_LOG_DEBUG, "config: nopassiveftp\n");
00619                 } else if(strcmp(key, "UseSyslog") == 0 || strcmp(upperkey, "USESYSLOG") == 0) {
00620                     alpm_option_set_usesyslog(1);
00621                     pm_printf(PM_LOG_DEBUG, "config: usesyslog\n");
00622                 } else if(strcmp(key, "ILoveCandy") == 0 || strcmp(upperkey, "ILOVECANDY") == 0) {
00623                     config->chomp = 1;
00624                     pm_printf(PM_LOG_DEBUG, "config: chomp\n");
00625                 } else if(strcmp(key, "UseColor") == 0 || strcmp(upperkey, "USECOLOR") == 0) {
00626                     config->usecolor = 1;
00627                     pm_printf(PM_LOG_DEBUG, "config: usecolor\n");
00628                 } else if(strcmp(key, "ShowSize") == 0 || strcmp(upperkey, "SHOWSIZE") == 0) {
00629                     config->showsize = 1;
00630                     pm_printf(PM_LOG_DEBUG, "config: showsize\n");
00631                 } else if(strcmp(key, "UseDelta") == 0 || strcmp(upperkey, "USEDELTA") == 0) {
00632                     alpm_option_set_usedelta(1);
00633                     pm_printf(PM_LOG_DEBUG, "config: usedelta\n");
00634                 } else if(strcmp(key, "TotalDownload") == 0 || strcmp(upperkey, "TOTALDOWNLOAD") == 0) {
00635                     config->totaldownload = 1;
00636                     pm_printf(PM_LOG_DEBUG, "config: totaldownload\n");
00637                 } else {
00638                     pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"),
00639                             file, linenum, key);
00640                     return(1);
00641                 }
00642             } else {
00643                 /* directives with settings */
00644                 if(strcmp(key, "Include") == 0 || strcmp(upperkey, "INCLUDE") == 0) {
00645                     pm_printf(PM_LOG_DEBUG, "config: including %s\n", ptr);
00646                     _parseconfig(ptr, section, db);
00647                     /* Ignore include failures... assume non-critical */
00648                 } else if(strcmp(section, "options") == 0) {
00649                     if(strcmp(key, "NoUpgrade") == 0
00650                             || strcmp(upperkey, "NOUPGRADE") == 0) {
00651                         setrepeatingoption(ptr, "NoUpgrade", alpm_option_add_noupgrade);
00652                     } else if(strcmp(key, "NoExtract") == 0
00653                             || strcmp(upperkey, "NOEXTRACT") == 0) {
00654                         setrepeatingoption(ptr, "NoExtract", alpm_option_add_noextract);
00655                     } else if(strcmp(key, "IgnorePkg") == 0
00656                             || strcmp(upperkey, "IGNOREPKG") == 0) {
00657                         setrepeatingoption(ptr, "IgnorePkg", alpm_option_add_ignorepkg);
00658                     } else if(strcmp(key, "IgnoreGroup") == 0
00659                             || strcmp(upperkey, "IGNOREGROUP") == 0) {
00660                         setrepeatingoption(ptr, "IgnoreGroup", alpm_option_add_ignoregrp);
00661                     } else if(strcmp(key, "HoldPkg") == 0
00662                             || strcmp(upperkey, "HOLDPKG") == 0) {
00663                         setrepeatingoption(ptr, "HoldPkg", alpm_option_add_holdpkg);
00664                     } else if(strcmp(key, "DBPath") == 0 || strcmp(upperkey, "DBPATH") == 0) {
00665                         /* don't overwrite a path specified on the command line */
00666                         if(!config->dbpath) {
00667                             config->dbpath = strdup(ptr);
00668                             pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", ptr);
00669                         }
00670                     } else if(strcmp(key, "CacheDir") == 0 || strcmp(upperkey, "CACHEDIR") == 0) {
00671                         if(alpm_option_add_cachedir(ptr) != 0) {
00672                             pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
00673                                     ptr, alpm_strerrorlast());
00674                             return(1);
00675                         }
00676                         pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", ptr);
00677                     } else if(strcmp(key, "RootDir") == 0 || strcmp(upperkey, "ROOTDIR") == 0) {
00678                         /* don't overwrite a path specified on the command line */
00679                         if(!config->rootdir) {
00680                             config->rootdir = strdup(ptr);
00681                             pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", ptr);
00682                         }
00683                     } else if (strcmp(key, "LogFile") == 0 || strcmp(upperkey, "LOGFILE") == 0) {
00684                         if(!config->logfile) {
00685                             config->logfile = strdup(ptr);
00686                             pm_printf(PM_LOG_DEBUG, "config: logfile: %s\n", ptr);
00687                         }
00688                     } else if (strcmp(key, "XferCommand") == 0 || strcmp(upperkey, "XFERCOMMAND") == 0) {
00689                         alpm_option_set_xfercommand(ptr);
00690                         pm_printf(PM_LOG_DEBUG, "config: xfercommand: %s\n", ptr);
00691                     } else {
00692                         pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"),
00693                                 file, linenum, key);
00694                         return(1);
00695                     }
00696                 } else if(strcmp(key, "Server") == 0 || strcmp(upperkey, "SERVER") == 0) {
00697                     /* let's attempt a replacement for the current repo */
00698                     char *server = strreplace(ptr, "$repo", section);
00699 
00700                     if(alpm_db_setserver(db, server) != 0) {
00701                         /* pm_errno is set by alpm_db_setserver */
00702                         return(1);
00703                     }
00704 
00705                     free(server);
00706                 } else {
00707                     pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"),
00708                             file, linenum, key);
00709                     return(1);
00710                 }
00711             }
00712             free(upperkey);
00713         }
00714     }
00715     fclose(fp);
00716     if(section){
00717         free(section);
00718     }
00719 
00720     /* call setlibpaths here to ensure we have called it at least once */
00721     setlibpaths();
00722     pm_printf(PM_LOG_DEBUG, "config: finished parsing %s\n", file);
00723     return(0);
00724 }
00725 
00726 /** Parse a configuration file.
00727  * @param file path to the config file.
00728  * @return 0 on success, non-zero on error
00729  */
00730 static int parseconfig(const char *file)
00731 {
00732     /* call the real parseconfig function with a null section & db argument */
00733     return(_parseconfig(file, NULL, NULL));
00734 }
00735 
00736 /** Main function.
00737  * @param argc argc
00738  * @param argv argv
00739  * @return A return code indicating success, failure, etc.
00740  */
00741 int main(int argc, char *argv[])
00742 {
00743     int ret = 0;
00744 #if defined(HAVE_GETEUID)
00745     /* geteuid undefined in CYGWIN */
00746     uid_t myuid = geteuid();
00747 #endif
00748 
00749 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
00750     /*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
00751     mtrace();
00752 #endif
00753 
00754     /* set signal handlers */
00755     signal(SIGINT, cleanup);
00756     signal(SIGTERM, cleanup);
00757     signal(SIGSEGV, cleanup);
00758 
00759     /* i18n init */
00760 #if defined(ENABLE_NLS)
00761     localize();
00762 #endif
00763 
00764     /* set user agent for downloading */
00765     setuseragent();
00766 
00767     /* init config data */
00768     config = config_new();
00769 
00770     /* disable progressbar if the output is redirected */
00771     if(!isatty(1)) {
00772         config->noprogressbar = 1;
00773     }
00774 
00775     /* initialize library */
00776     if(alpm_initialize() == -1) {
00777         pm_printf(PM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"),
00778                 alpm_strerrorlast());
00779         cleanup(EXIT_FAILURE);
00780     }
00781 
00782     /* Setup logging as soon as possible, to print out maximum debugging info */
00783     alpm_option_set_logcb(cb_log);
00784     alpm_option_set_dlcb(cb_dl_progress);
00785     /* define paths to reasonable defaults */
00786     alpm_option_set_root(ROOTDIR);
00787     alpm_option_set_dbpath(DBPATH);
00788     alpm_option_set_logfile(LOGFILE);
00789 
00790     /* Priority of options:
00791      * 1. command line
00792      * 2. config file
00793      * 3. compiled-in defaults
00794      * However, we have to parse the command line first because a config file
00795      * location can be specified here, so we need to make sure we prefer these
00796      * options over the config file coming second.
00797      */
00798 
00799     /* parse the command line */
00800     ret = parseargs(argc, argv);
00801     if(ret != 0) {
00802         cleanup(ret);
00803     }
00804 
00805     /* parse the config file */
00806     ret = parseconfig(config->configfile);
00807     if(ret != 0) {
00808         cleanup(ret);
00809     }
00810 
00811 #if defined(HAVE_GETEUID)
00812     /* check if we have sufficient permission for the requested operation */
00813     if(myuid > 0 && needs_transaction()) {
00814         pm_printf(PM_LOG_ERROR, _("you cannot perform this operation unless you are root.\n"));
00815         cleanup(EXIT_FAILURE);
00816     }
00817 #endif
00818 
00819     if(config->verbose > 0) {
00820         alpm_list_t *i;
00821         printf("Root      : %s\n", alpm_option_get_root());
00822         printf("Conf File : %s\n", config->configfile);
00823         printf("DB Path   : %s\n", alpm_option_get_dbpath());
00824         printf("Cache Dirs: ");
00825         for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
00826             printf("%s  ", (char*)alpm_list_getdata(i));
00827         }
00828         printf("\n");
00829         printf("Lock File : %s\n", alpm_option_get_lockfile());
00830         printf("Log File  : %s\n", alpm_option_get_logfile());
00831         list_display("Targets   :", pm_targets);
00832     }
00833 
00834     /* Opening local database */
00835     db_local = alpm_db_register_local();
00836     if(db_local == NULL) {
00837         pm_printf(PM_LOG_ERROR, _("could not register 'local' database (%s)\n"),
00838                 alpm_strerrorlast());
00839         cleanup(EXIT_FAILURE);
00840     }
00841 
00842     /* start the requested operation */
00843     switch(config->op) {
00844         case PM_OP_ADD:
00845             ret = pacman_add(pm_targets);
00846             break;
00847         case PM_OP_REMOVE:
00848             ret = pacman_remove(pm_targets);
00849             break;
00850         case PM_OP_UPGRADE:
00851             ret = pacman_upgrade(pm_targets);
00852             break;
00853         case PM_OP_QUERY:
00854             ret = pacman_query(pm_targets);
00855             break;
00856         case PM_OP_SYNC:
00857             ret = pacman_sync(pm_targets);
00858             break;
00859         case PM_OP_DEPTEST:
00860             ret = pacman_deptest(pm_targets);
00861             break;
00862         default:
00863             pm_printf(PM_LOG_ERROR, _("no operation specified (use -h for help)\n"));
00864             ret = EXIT_FAILURE;
00865     }
00866 
00867     cleanup(ret);
00868     /* not reached */
00869     return(EXIT_SUCCESS);
00870 }
00871 
00872 /* vim: set ts=2 sw=2 noet: */

Generated on Mon Jan 14 23:53:40 2008 for libalpm by  doxygen 1.5.4