From 65662315b6d180f7066f6578c55a9feec7250114 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Mon, 4 Jun 2007 12:01:53 -0400 Subject: Add a parseconfig to the pacman frontend that compiles Warning: this compiles but may not work as intended quite yet. :) Signed-off-by: Dan McGee --- lib/libalpm/alpm.c | 2 +- lib/libalpm/handle.c | 53 +++++++------ src/pacman/pacman.c | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/pacman/util.c | 57 +++++++++++++- src/pacman/util.h | 2 +- 5 files changed, 298 insertions(+), 28 deletions(-) diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c index 0d5655e6..e3aacd27 100644 --- a/lib/libalpm/alpm.c +++ b/lib/libalpm/alpm.c @@ -190,7 +190,7 @@ int alpm_db_unregister(pmdb_t *db) * @param url url of the server * @return 0 on success, -1 on error (pm_errno is set accordingly) */ -int alpm_db_setserver(pmdb_t *db, const char *url) +int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url) { int found = 0; diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index d2e3dd0d..858578b5 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -122,24 +122,24 @@ void _alpm_handle_free(pmhandle_t *handle) FREE(handle); } -alpm_cb_log alpm_option_get_logcb() { return (handle ? handle->logcb : NULL); } -alpm_cb_download alpm_option_get_dlcb() { return (handle ? handle->dlcb : NULL); } +alpm_cb_log SYMEXPORT alpm_option_get_logcb() { return (handle ? handle->logcb : NULL); } +alpm_cb_download SYMEXPORT alpm_option_get_dlcb() { return (handle ? handle->dlcb : NULL); } unsigned short SYMEXPORT alpm_option_get_logmask() { return handle->logmask; } const char SYMEXPORT *alpm_option_get_root() { return handle->root; } const char SYMEXPORT *alpm_option_get_dbpath() { return handle->dbpath; } const char SYMEXPORT *alpm_option_get_cachedir() { return handle->cachedir; } const char SYMEXPORT *alpm_option_get_logfile() { return handle->logfile; } const char SYMEXPORT *alpm_option_get_lockfile() { return handle->lockfile; } -unsigned short alpm_option_get_usesyslog() { return handle->usesyslog; } -alpm_list_t *alpm_option_get_noupgrades() { return handle->noupgrade; } -alpm_list_t *alpm_option_get_noextracts() { return handle->noextract; } -alpm_list_t *alpm_option_get_ignorepkgs() { return handle->ignorepkg; } -alpm_list_t *alpm_option_get_holdpkgs() { return handle->holdpkg; } -time_t alpm_option_get_upgradedelay() { return handle->upgradedelay; } -const char *alpm_option_get_xfercommand() { return handle->xfercommand; } -unsigned short alpm_option_get_nopassiveftp() { return handle->nopassiveftp; } +unsigned short SYMEXPORT alpm_option_get_usesyslog() { return handle->usesyslog; } +alpm_list_t SYMEXPORT *alpm_option_get_noupgrades() { return handle->noupgrade; } +alpm_list_t SYMEXPORT *alpm_option_get_noextracts() { return handle->noextract; } +alpm_list_t SYMEXPORT *alpm_option_get_ignorepkgs() { return handle->ignorepkg; } +alpm_list_t SYMEXPORT *alpm_option_get_holdpkgs() { return handle->holdpkg; } +time_t SYMEXPORT alpm_option_get_upgradedelay() { return handle->upgradedelay; } +const char SYMEXPORT *alpm_option_get_xfercommand() { return handle->xfercommand; } +unsigned short SYMEXPORT alpm_option_get_nopassiveftp() { return handle->nopassiveftp; } unsigned short SYMEXPORT alpm_option_get_chomp() { return handle->chomp; } -unsigned short alpm_option_get_usecolor() { return handle->use_color; } +unsigned short SYMEXPORT alpm_option_get_usecolor() { return handle->use_color; } unsigned short SYMEXPORT alpm_option_get_showsize() { return handle->showsize; } pmdb_t SYMEXPORT *alpm_option_get_localdb() { return handle->db_local; } @@ -215,7 +215,7 @@ void SYMEXPORT alpm_option_set_cachedir(const char *cachedir) } } -void alpm_option_set_logfile(const char *logfile) +void SYMEXPORT alpm_option_set_logfile(const char *logfile) { ALPM_LOG_FUNC; @@ -240,27 +240,27 @@ void SYMEXPORT alpm_option_set_lockfile(const char *lockfile) } } -void alpm_option_set_usesyslog(unsigned short usesyslog) +void SYMEXPORT alpm_option_set_usesyslog(unsigned short usesyslog) { handle->usesyslog = usesyslog; } -void alpm_option_add_noupgrade(char *pkg) +void SYMEXPORT alpm_option_add_noupgrade(char *pkg) { handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg)); } -void alpm_option_set_noupgrades(alpm_list_t *noupgrade) +void SYMEXPORT alpm_option_set_noupgrades(alpm_list_t *noupgrade) { if(handle->noupgrade) FREELIST(handle->noupgrade); if(noupgrade) handle->noupgrade = noupgrade; } -void alpm_option_add_noextract(char *pkg) +void SYMEXPORT alpm_option_add_noextract(char *pkg) { handle->noextract = alpm_list_add(handle->noextract, strdup(pkg)); } -void alpm_option_set_noextracts(alpm_list_t *noextract) +void SYMEXPORT alpm_option_set_noextracts(alpm_list_t *noextract) { if(handle->noextract) FREELIST(handle->noextract); if(noextract) handle->noextract = noextract; @@ -276,40 +276,43 @@ void alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs) if(ignorepkgs) handle->ignorepkg = ignorepkgs; } -void alpm_option_add_holdpkg(char *pkg) +void SYMEXPORT alpm_option_add_holdpkg(char *pkg) { handle->holdpkg = alpm_list_add(handle->holdpkg, strdup(pkg)); } -void alpm_option_set_holdpkgs(alpm_list_t *holdpkgs) +void SYMEXPORT alpm_option_set_holdpkgs(alpm_list_t *holdpkgs) { if(handle->holdpkg) FREELIST(handle->holdpkg); if(holdpkgs) handle->holdpkg = holdpkgs; } -void alpm_option_set_upgradedelay(time_t delay) +void SYMEXPORT alpm_option_set_upgradedelay(time_t delay) { handle->upgradedelay = delay; } -void alpm_option_set_xfercommand(const char *cmd) +void SYMEXPORT alpm_option_set_xfercommand(const char *cmd) { if(handle->xfercommand) FREE(handle->xfercommand); if(cmd) handle->xfercommand = strdup(cmd); } -void alpm_option_set_nopassiveftp(unsigned short nopasv) +void SYMEXPORT alpm_option_set_nopassiveftp(unsigned short nopasv) { handle->nopassiveftp = nopasv; } -void alpm_option_set_chomp(unsigned short chomp) { handle->chomp = chomp; } +void SYMEXPORT alpm_option_set_chomp(unsigned short chomp) +{ + handle->chomp = chomp; +} -void alpm_option_set_usecolor(unsigned short usecolor) +void SYMEXPORT alpm_option_set_usecolor(unsigned short usecolor) { handle->use_color = usecolor; } -void alpm_option_set_showsize(unsigned short showsize) +void SYMEXPORT alpm_option_set_showsize(unsigned short showsize) { handle->showsize = showsize; } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 0b95ac15..e53eb2f8 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -456,6 +456,218 @@ static int parseargs(int argc, char *argv[]) return(0); } +/* The real parseconfig. Called with a null section argument by the publicly + * visible parseconfig so we can recall from within ourself on an include */ +static int _parseconfig(const char *file, const char *givensection) +{ + FILE *fp = NULL; + char line[PATH_MAX+1]; + int linenum = 0; + char *ptr, *section = NULL; + pmdb_t *db = NULL; + + fp = fopen(file, "r"); + if(fp == NULL) { + return(1); + } + + if(givensection != NULL) { + section = strdup(givensection); + } + + while(fgets(line, PATH_MAX, fp)) { + linenum++; + strtrim(line); + + /* ignore whole line and end of line comments */ + if(strlen(line) == 0 || line[0] == '#') { + continue; + } + if((ptr = strchr(line, '#'))) { + *ptr = '\0'; + } + + if(line[0] == '[' && line[strlen(line)-1] == ']') { + /* new config section, skip the '[' */ + ptr = &line[1]; + if(section) { + free(section); + } + section = strdup(ptr); + section[strlen(section)-1] = '\0'; + printf(_("config: new section '%s'"), section); + if(!strlen(section)) { + printf("PM_ERR_CONF_BAD_SECTION"); + return(1); + } + if(!strcmp(section, "local")) { + printf("PM_ERR_CONF_LOCAL"); + return(1); + } + } else { + /* directive */ + char *key; + const char *upperkey; + + /* strsep modifies the 'line' string: 'key \0 ptr' */ + key = line; + ptr = line; + strsep(&ptr, "="); + strtrim(key); + strtrim(ptr); + + if(key == NULL) { + printf("PM_ERR_CONF_BAD_SYNTAX"); + return(1); + } + upperkey = strtoupper(strdup(key)); + if(!strlen(section) && strcmp(key, "INCLUDE")) { + printf("PM_ERR_CONF_DIRECTIVE_OUTSIDE_SECTION"); + return(1); + } + if(ptr == NULL) { + /* directives without settings */ + /* TODO shouldn't we check if these are in the [options] section? */ + if(strcmp(key, "NoPassiveFTP") == 0 || strcmp(upperkey, "NOPASSIVEFTP") == 0) { + alpm_option_set_nopassiveftp(1); + printf(_("config: nopassiveftp")); + } else if(strcmp(key, "UseSyslog") == 0 || strcmp(upperkey, "USESYSLOG") == 0) { + alpm_option_set_usesyslog(1); + printf(_("config: usesyslog")); + } else if(strcmp(key, "ILoveCandy") == 0 || strcmp(upperkey, "ILOVECANDY") == 0) { + alpm_option_set_chomp(1); + printf(_("config: chomp")); + } else if(strcmp(key, "UseColor") == 0 || strcmp(upperkey, "USECOLOR") == 0) { + alpm_option_set_usecolor(1); + printf(_("config: usecolor")); + } else if(strcmp(key, "ShowSize") == 0 || strcmp(upperkey, "SHOWSIZE") == 0) { + alpm_option_set_showsize(1); + printf(_("config: showsize")); + } else { + printf("PM_ERR_CONF_BAD_SYNTAX"); + return(1); + } + } else { + /* directives with settings */ + if(strcmp(key, "Include") == 0 || strcmp(upperkey, "INCLUDE") == 0) { + printf(_("config: including %s"), ptr); + _parseconfig(ptr, section); + } else if(strcmp(section, "options") == 0) { + if(strcmp(key, "NoUpgrade") == 0 || strcmp(upperkey, "NOUPGRADE") == 0) { + /* TODO functionalize this */ + char *p = ptr; + char *q; + + while((q = strchr(p, ' '))) { + *q = '\0'; + alpm_option_add_noupgrade(p); + printf(_("config: noupgrade: %s"), p); + p = q; + p++; + } + alpm_option_add_noupgrade(p); + printf(_("config: noupgrade: %s"), p); + } else if(strcmp(key, "NoExtract") == 0 || strcmp(upperkey, "NOEXTRACT") == 0) { + char *p = ptr; + char *q; + + while((q = strchr(p, ' '))) { + *q = '\0'; + alpm_option_add_noextract(p); + printf(_("config: noextract: %s"), p); + p = q; + p++; + } + alpm_option_add_noextract(p); + printf(_("config: noextract: %s"), p); + } else if(strcmp(key, "IgnorePkg") == 0 || strcmp(upperkey, "IGNOREPKG") == 0) { + char *p = ptr; + char *q; + + while((q = strchr(p, ' '))) { + *q = '\0'; + alpm_option_add_ignorepkg(p); + printf(_("config: ignorepkg: %s"), p); + p = q; + p++; + } + alpm_option_add_ignorepkg(p); + printf(_("config: ignorepkg: %s"), p); + } else if(strcmp(key, "HoldPkg") == 0 || strcmp(upperkey, "HOLDPKG") == 0) { + char *p = ptr; + char *q; + + while((q = strchr(p, ' '))) { + *q = '\0'; + alpm_option_add_holdpkg(p); + printf(_("config: holdpkg: %s"), p); + p = q; + p++; + } + alpm_option_add_holdpkg(p); + printf(_("config: holdpkg: %s"), p); + } else if(strcmp(key, "DBPath") == 0 || strcmp(upperkey, "DBPATH") == 0) { + alpm_option_set_dbpath(ptr); + printf(_("config: dbpath: %s"), ptr); + } else if(strcmp(key, "CacheDir") == 0 || strcmp(upperkey, "CACHEDIR") == 0) { + alpm_option_set_cachedir(ptr); + printf(_("config: cachedir: %s"), ptr); + } else if(strcmp(key, "RootDir") == 0 || strcmp(upperkey, "ROOTDIR") == 0) { + alpm_option_set_root(ptr); + printf(_("config: rootdir: %s"), ptr); + } else if (strcmp(key, "LogFile") == 0 || strcmp(upperkey, "LOGFILE") == 0) { + alpm_option_set_logfile(ptr); + printf(_("config: logfile: %s"), ptr); + } else if (strcmp(key, "LockFile") == 0 || strcmp(upperkey, "LOCKFILE") == 0) { + alpm_option_set_lockfile(ptr); + printf(_("config: lockfile: %s"), ptr); + } else if (strcmp(key, "XferCommand") == 0 || strcmp(upperkey, "XFERCOMMAND") == 0) { + alpm_option_set_xfercommand(ptr); + printf(_("config: xfercommand: %s"), ptr); + } else if (strcmp(key, "UpgradeDelay") == 0 || strcmp(upperkey, "UPGRADEDELAY") == 0) { + /* The config value is in days, we use seconds */ + time_t ud = atol(ptr) * 60 * 60 *24; + alpm_option_set_upgradedelay(ud); + printf(_("config: upgradedelay: %d"), (int)ud); + } else { + printf("PM_ERR_CONF_BAD_SYNTAX"); + return(1); + } + } else { + if(strcmp(key, "Server") == 0 || strcmp(upperkey, "SERVER") == 0) { + /* let's attempt a replacement for the current repo */ + char *server = strreplace(ptr, "$repo", section); + + if(alpm_db_setserver(db, server) != 0) { + /* pm_errno is set by alpm_db_setserver */ + return(-1); + } + + free(server); + } else { + printf("PM_ERR_CONF_BAD_SYNTAX"); + return(1); + } + } + line[0] = '\0'; + } + } + } + fclose(fp); + + return(0); +} + +/** Parse a configuration file. + * @param file path to the config file. + * @return 0 on success, non-zero on error + */ +int parseconfig(const char *file) +{ + /* call the real parseconfig function with a null section argument */ + return(_parseconfig(file, NULL)); +} + /** * @brief Main function. * diff --git a/src/pacman/util.c b/src/pacman/util.c index a5876122..406145e3 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -209,6 +209,12 @@ char *strtoupper(char *str) char *strtrim(char *str) { char *pch = str; + + if(*str == '\0') { + /* string is empty, so we're done. */ + return(str); + } + while(isspace(*pch)) { pch++; } @@ -216,13 +222,62 @@ char *strtrim(char *str) memmove(str, pch, (strlen(pch) + 1)); } + /* check if there wasn't anything but whitespace in the string. */ + if(*str == '\0') { + return(str); + } + pch = (str + (strlen(str) - 1)); while(isspace(*pch)) { pch--; } *++pch = '\0'; - return str; + return(str); +} + +/* Helper function for strreplace */ +static void _strnadd(char **str, const char *append, unsigned int count) +{ + if(*str) { + *str = realloc(*str, strlen(*str) + count + 1); + } else { + *str = calloc(sizeof(char), count + 1); + } + + strncat(*str, append, count); +} + +/* Replace all occurances of 'needle' with 'replace' in 'str', returning + * a new string (must be free'd) */ +char *strreplace(const char *str, const char *needle, const char *replace) +{ + const char *p, *q; + p = q = str; + + char *newstr = NULL; + unsigned int needlesz = strlen(needle), + replacesz = strlen(replace); + + while (1) { + q = strstr(p, needle); + if(!q) { /* not found */ + if(*p) { + /* add the rest of 'p' */ + _strnadd(&newstr, p, strlen(p)); + } + break; + } else { /* found match */ + if(q > p){ + /* add chars between this occurance and last occurance, if any */ + _strnadd(&newstr, p, q - p); + } + _strnadd(&newstr, replace, replacesz); + p = q + needlesz; + } + } + + return newstr; } void list_display(const char *title, alpm_list_t *list) diff --git a/src/pacman/util.h b/src/pacman/util.h index 755b30e1..80d1b02c 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -43,7 +43,7 @@ int rmrf(const char *path); void indentprint(const char *str, int indent); char *strtoupper(char *str); char *strtrim(char *str); -int reg_match(char *string, char *pattern); +char *strreplace(const char *str, const char *needle, const char *replace); void list_display(const char *title, alpm_list_t *list); void display_targets(alpm_list_t *syncpkgs); int yesno(char *fmt, ...); -- cgit v1.2.3-70-g09d2