Allow $repo expansion in 'Server' config lines
Small change (addition of a 'strreplace' function) which replaces any $repo tokens found in a server line with the name of the repo or section being processed. While this is more simplistic than suggestions on flyspray, it works and I think it is cleaner. Merits can be discussed further. Ref: FS#6389 Signed-off-by: Aaron Griffin <aaronmgriffin@gmail.com>
This commit is contained in:
parent
1381b58ceb
commit
ebad199614
3 changed files with 72 additions and 22 deletions
|
@ -1059,11 +1059,15 @@ int SYMEXPORT alpm_parse_config(char *file, alpm_cb_db_register callback, const
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(strcmp(origkey, "Server") == 0 || strcmp(key, "SERVER") == 0) {
|
if(strcmp(origkey, "Server") == 0 || strcmp(key, "SERVER") == 0) {
|
||||||
/* add to the list */
|
/* let's attempt a replacement for the current repo */
|
||||||
if(alpm_db_setserver(db, ptr) != 0) {
|
char *server = _alpm_strreplace(ptr, "$repo", section);
|
||||||
|
|
||||||
|
if(alpm_db_setserver(db, server) != 0) {
|
||||||
/* pm_errno is set by alpm_db_setserver */
|
/* pm_errno is set by alpm_db_setserver */
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(server);
|
||||||
} else {
|
} else {
|
||||||
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -205,6 +205,51 @@ char *_alpm_strtrim(char *str)
|
||||||
return(str);
|
return(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Helper function for _alpm_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 *_alpm_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Create a lock file
|
/* Create a lock file
|
||||||
*/
|
*/
|
||||||
int _alpm_lckmk(const char *file)
|
int _alpm_lckmk(const char *file)
|
||||||
|
|
|
@ -48,6 +48,7 @@ int _alpm_makepath(const char *path);
|
||||||
int _alpm_copyfile(const char *src, const char *dest);
|
int _alpm_copyfile(const char *src, const char *dest);
|
||||||
char *_alpm_strtoupper(char *str);
|
char *_alpm_strtoupper(char *str);
|
||||||
char *_alpm_strtrim(char *str);
|
char *_alpm_strtrim(char *str);
|
||||||
|
char *_alpm_strreplace(const char *str, const char *needle, const char *replace);
|
||||||
int _alpm_lckmk(const char *file);
|
int _alpm_lckmk(const char *file);
|
||||||
int _alpm_lckrm(const char *file);
|
int _alpm_lckrm(const char *file);
|
||||||
int _alpm_unpack(const char *archive, const char *prefix, const char *fn);
|
int _alpm_unpack(const char *archive, const char *prefix, const char *fn);
|
||||||
|
|
Loading…
Add table
Reference in a new issue