2002-12-25 01:06:35 +08:00
|
|
|
/** \ingroup rpmio
|
|
|
|
* \file rpmio/argv.c
|
|
|
|
*/
|
|
|
|
|
2002-10-13 22:39:21 +08:00
|
|
|
#include "system.h"
|
|
|
|
|
2010-01-05 21:04:20 +08:00
|
|
|
#include <stdlib.h>
|
2007-12-08 20:02:32 +08:00
|
|
|
#include <rpm/argv.h>
|
2008-04-14 21:21:40 +08:00
|
|
|
#include <rpm/rpmstring.h>
|
2002-10-13 22:39:21 +08:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
2008-04-07 17:35:37 +08:00
|
|
|
void argvPrint(const char * msg, ARGV_const_t argv, FILE * fp)
|
2002-10-13 22:39:21 +08:00
|
|
|
{
|
2008-04-07 17:35:37 +08:00
|
|
|
ARGV_const_t av;
|
2002-10-13 22:39:21 +08:00
|
|
|
|
|
|
|
if (fp == NULL) fp = stderr;
|
|
|
|
|
|
|
|
if (msg)
|
|
|
|
fprintf(fp, "===================================== %s\n", msg);
|
|
|
|
|
2002-10-20 06:48:25 +08:00
|
|
|
if (argv)
|
|
|
|
for (av = argv; *av; av++)
|
|
|
|
fprintf(fp, "%s\n", *av);
|
2002-10-13 22:39:21 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-14 20:58:10 +08:00
|
|
|
ARGV_t argvNew(void)
|
|
|
|
{
|
|
|
|
ARGV_t argv = xcalloc(1, sizeof(*argv));
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2002-10-14 22:52:45 +08:00
|
|
|
ARGI_t argiFree(ARGI_t argi)
|
|
|
|
{
|
|
|
|
if (argi) {
|
|
|
|
argi->nvals = 0;
|
|
|
|
argi->vals = _free(argi->vals);
|
|
|
|
}
|
|
|
|
argi = _free(argi);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:48:54 +08:00
|
|
|
ARGV_t argvFree(ARGV_t argv)
|
2002-10-13 22:39:21 +08:00
|
|
|
{
|
|
|
|
ARGV_t av;
|
|
|
|
|
2002-10-14 22:52:45 +08:00
|
|
|
if (argv)
|
|
|
|
for (av = argv; *av; av++)
|
|
|
|
*av = _free(*av);
|
|
|
|
argv = _free(argv);
|
|
|
|
return NULL;
|
2002-10-13 22:39:21 +08:00
|
|
|
}
|
|
|
|
|
2008-04-07 17:34:07 +08:00
|
|
|
int argiCount(ARGI_const_t argi)
|
2002-10-20 06:48:25 +08:00
|
|
|
{
|
|
|
|
int nvals = 0;
|
|
|
|
if (argi)
|
|
|
|
nvals = argi->nvals;
|
|
|
|
return nvals;
|
|
|
|
}
|
|
|
|
|
2008-04-07 17:34:07 +08:00
|
|
|
ARGint_t argiData(ARGI_const_t argi)
|
2002-12-03 10:57:02 +08:00
|
|
|
{
|
|
|
|
ARGint_t vals = NULL;
|
|
|
|
if (argi && argi->nvals > 0)
|
|
|
|
vals = argi->vals;
|
|
|
|
return vals;
|
|
|
|
}
|
|
|
|
|
2008-04-07 17:11:16 +08:00
|
|
|
int argvCount(ARGV_const_t argv)
|
2002-10-13 22:39:21 +08:00
|
|
|
{
|
|
|
|
int argc = 0;
|
|
|
|
if (argv)
|
|
|
|
while (argv[argc] != NULL)
|
|
|
|
argc++;
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
2008-04-07 17:11:16 +08:00
|
|
|
ARGV_t argvData(ARGV_t argv)
|
2002-12-03 10:57:02 +08:00
|
|
|
{
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2002-10-13 22:39:21 +08:00
|
|
|
int argvCmp(const void * a, const void * b)
|
|
|
|
{
|
2008-04-07 14:31:21 +08:00
|
|
|
const char *astr = *(ARGV_t)a;
|
|
|
|
const char *bstr = *(ARGV_t)b;
|
2002-10-13 22:39:21 +08:00
|
|
|
return strcmp(astr, bstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int argvSort(ARGV_t argv, int (*compar)(const void *, const void *))
|
|
|
|
{
|
2002-10-14 22:52:45 +08:00
|
|
|
if (compar == NULL)
|
|
|
|
compar = argvCmp;
|
2002-10-13 22:39:21 +08:00
|
|
|
qsort(argv, argvCount(argv), sizeof(*argv), compar);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-07 17:11:16 +08:00
|
|
|
ARGV_t argvSearch(ARGV_const_t argv, const char *val,
|
2002-10-13 22:39:21 +08:00
|
|
|
int (*compar)(const void *, const void *))
|
|
|
|
{
|
2002-10-14 22:52:45 +08:00
|
|
|
if (argv == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (compar == NULL)
|
|
|
|
compar = argvCmp;
|
|
|
|
return bsearch(&val, argv, argvCount(argv), sizeof(*argv), compar);
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:48:54 +08:00
|
|
|
int argiAdd(ARGI_t * argip, int ix, int val)
|
2002-10-14 22:52:45 +08:00
|
|
|
{
|
|
|
|
ARGI_t argi;
|
|
|
|
|
|
|
|
if (argip == NULL)
|
|
|
|
return -1;
|
|
|
|
if (*argip == NULL)
|
|
|
|
*argip = xcalloc(1, sizeof(**argip));
|
|
|
|
argi = *argip;
|
2002-10-20 06:48:25 +08:00
|
|
|
if (ix < 0)
|
|
|
|
ix = argi->nvals;
|
2002-10-14 22:52:45 +08:00
|
|
|
if (ix >= argi->nvals) {
|
|
|
|
argi->vals = xrealloc(argi->vals, (ix + 1) * sizeof(*argi->vals));
|
|
|
|
memset(argi->vals + argi->nvals, 0,
|
|
|
|
(ix - argi->nvals) * sizeof(*argi->vals));
|
|
|
|
argi->nvals = ix + 1;
|
|
|
|
}
|
|
|
|
argi->vals[ix] = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-07 14:31:21 +08:00
|
|
|
int argvAdd(ARGV_t * argvp, const char *val)
|
2002-10-14 22:52:45 +08:00
|
|
|
{
|
|
|
|
ARGV_t argv;
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
if (argvp == NULL)
|
|
|
|
return -1;
|
|
|
|
argc = argvCount(*argvp);
|
|
|
|
*argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp));
|
|
|
|
argv = *argvp;
|
|
|
|
argv[argc++] = xstrdup(val);
|
|
|
|
argv[argc ] = NULL;
|
|
|
|
return 0;
|
2002-10-13 22:39:21 +08:00
|
|
|
}
|
|
|
|
|
2008-04-14 21:21:40 +08:00
|
|
|
int argvAddNum(ARGV_t *argvp, int val)
|
|
|
|
{
|
|
|
|
char *valstr = NULL;
|
|
|
|
int rc;
|
|
|
|
rasprintf(&valstr, "%d", val);
|
|
|
|
rc = argvAdd(argvp, valstr);
|
|
|
|
free(valstr);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-04-07 17:11:16 +08:00
|
|
|
int argvAppend(ARGV_t * argvp, ARGV_const_t av)
|
2002-10-13 22:39:21 +08:00
|
|
|
{
|
|
|
|
ARGV_t argv = *argvp;
|
|
|
|
int argc = argvCount(argv);
|
|
|
|
int ac = argvCount(av);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
|
|
|
|
for (i = 0; i < ac; i++)
|
|
|
|
argv[argc + i] = xstrdup(av[i]);
|
|
|
|
argv[argc + ac] = NULL;
|
|
|
|
*argvp = argv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-15 17:54:55 +08:00
|
|
|
ARGV_t argvSplitString(const char * str, const char * seps, argvFlags flags)
|
2002-10-13 22:39:21 +08:00
|
|
|
{
|
2008-04-28 21:42:04 +08:00
|
|
|
char *dest = xmalloc(strlen(str) + 1);
|
2002-10-13 22:39:21 +08:00
|
|
|
ARGV_t argv;
|
|
|
|
int argc = 1;
|
|
|
|
const char * s;
|
|
|
|
char * t;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
for (argc = 1, s = str, t = dest; (c = *s); s++, t++) {
|
|
|
|
if (strchr(seps, c)) {
|
|
|
|
argc++;
|
|
|
|
c = '\0';
|
|
|
|
}
|
|
|
|
*t = c;
|
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
argv = xmalloc( (argc + 1) * sizeof(*argv));
|
|
|
|
|
|
|
|
for (c = 0, s = dest; s < t; s+= strlen(s) + 1) {
|
2010-03-15 17:54:55 +08:00
|
|
|
if (*s == '\0' && (flags & ARGV_SKIPEMPTY))
|
2002-10-13 22:39:21 +08:00
|
|
|
continue;
|
|
|
|
argv[c] = xstrdup(s);
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
argv[c] = NULL;
|
2008-04-28 21:42:04 +08:00
|
|
|
free(dest);
|
2010-03-15 17:54:55 +08:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Backwards compatibility */
|
|
|
|
int argvSplit(ARGV_t * argvp, const char * str, const char * seps)
|
|
|
|
{
|
|
|
|
if (argvp) {
|
|
|
|
*argvp = argvSplitString(str, seps, ARGV_SKIPEMPTY);
|
|
|
|
}
|
2002-10-13 22:39:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-04-18 17:36:02 +08:00
|
|
|
|
|
|
|
char *argvJoin(ARGV_const_t argv, const char *sep)
|
|
|
|
{
|
|
|
|
char *dest = NULL;
|
|
|
|
char * const *arg;
|
|
|
|
|
2009-06-23 16:55:54 +08:00
|
|
|
for (arg = argv; arg && *arg; arg++) {
|
2008-04-18 17:36:02 +08:00
|
|
|
rstrscat(&dest, *arg, *(arg+1) ? sep : "", NULL);
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|