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 {
|
||||
if(strcmp(origkey, "Server") == 0 || strcmp(key, "SERVER") == 0) {
|
||||
/* add to the list */
|
||||
if(alpm_db_setserver(db, ptr) != 0) {
|
||||
/* let's attempt a replacement for the current repo */
|
||||
char *server = _alpm_strreplace(ptr, "$repo", section);
|
||||
|
||||
if(alpm_db_setserver(db, server) != 0) {
|
||||
/* pm_errno is set by alpm_db_setserver */
|
||||
return(-1);
|
||||
}
|
||||
|
||||
free(server);
|
||||
} else {
|
||||
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
||||
}
|
||||
|
|
|
@ -161,7 +161,7 @@ int _alpm_copyfile(const char *src, const char *dest)
|
|||
}
|
||||
|
||||
/* Convert a string to uppercase
|
||||
*/
|
||||
*/
|
||||
char *_alpm_strtoupper(char *str)
|
||||
{
|
||||
char *ptr = str;
|
||||
|
@ -174,7 +174,7 @@ char *_alpm_strtoupper(char *str)
|
|||
}
|
||||
|
||||
/* Trim whitespace and newlines from a string
|
||||
*/
|
||||
*/
|
||||
char *_alpm_strtrim(char *str)
|
||||
{
|
||||
char *pch = str;
|
||||
|
@ -205,8 +205,53 @@ char *_alpm_strtrim(char *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
|
||||
*/
|
||||
*/
|
||||
int _alpm_lckmk(const char *file)
|
||||
{
|
||||
int fd, count = 0;
|
||||
|
@ -234,7 +279,7 @@ int _alpm_lckmk(const char *file)
|
|||
}
|
||||
|
||||
/* Remove a lock file
|
||||
*/
|
||||
*/
|
||||
int _alpm_lckrm(const char *file)
|
||||
{
|
||||
if(unlink(file) == -1 && errno != ENOENT) {
|
||||
|
@ -244,7 +289,7 @@ int _alpm_lckrm(const char *file)
|
|||
}
|
||||
|
||||
/* Compression functions
|
||||
*/
|
||||
*/
|
||||
|
||||
int _alpm_unpack(const char *archive, const char *prefix, const char *fn)
|
||||
{
|
||||
|
|
|
@ -48,6 +48,7 @@ int _alpm_makepath(const char *path);
|
|||
int _alpm_copyfile(const char *src, const char *dest);
|
||||
char *_alpm_strtoupper(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_lckrm(const char *file);
|
||||
int _alpm_unpack(const char *archive, const char *prefix, const char *fn);
|
||||
|
|
Loading…
Add table
Reference in a new issue