util.c revision 8b420102e0d91edb2acbd2e8a40fc7d9995de2ba
/*
Authors:
Simo Sorce <ssorce@redhat.com>
Copyright (C) 2009 Red Hat
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include "talloc.h"
/* split a string into an allocated array of strings.
* the separator is a string, and is case-sensitive.
* optionally single values can be trimmed of of spaces and tabs */
{
const char *t, *p, *n;
char **list, **r;
t = str;
l = 0;
/* trim leading whitespace */
if (trim)
while (isspace(*t)) t++;
/* find substrings separated by the separator */
len = p - t;
n = p + 1; /* save next string starting point */
if (trim) {
/* strip whitespace after the separator
* so it's not in the next token */
while (isspace(*t)) {
t++;
len--;
if (len == 0) break;
}
p--;
/* strip whitespace before the separator
* so it's not in the current token */
len--;
p--;
}
}
/* Add the token to the array, +2 b/c of the trailing NULL */
if (!r) {
return ENOMEM;
} else {
list = r;
}
if (len == 0) {
} else {
}
if (!list[l]) {
return ENOMEM;
}
l++;
t = n; /* move to next string */
}
/* Handle the last remaining token */
if (t) {
if (!r) {
return ENOMEM;
} else {
list = r;
}
if (trim) {
/* trim leading whitespace */
while (isspace(*t)) {
t++;
len--;
if (len == 0) break;
}
/* trim trailing whitespace */
p = t + len - 1;
len--;
p--;
}
if (len == 0) {
} else {
}
} else {
}
if (!list[l]) {
return ENOMEM;
}
l++;
}
return EOK;
}
{
int i;
if (args) {
}
}
/* parse a string into arguments.
* arguments are separated by a space
* '\' is an escape character and can be used only to escape
* itself or the white space.
*/
char **parse_args(const char *str)
{
const char *p;
char **ret, **r;
char *tmp;
int num;
int i, e;
num = 0;
e = 0;
i = 0;
p = str;
while (*p) {
switch (*p) {
case '\\':
if (e) {
tmp[i] = '\\';
i++;
e = 0;
} else {
e = 1;
}
break;
case ' ':
if (e) {
tmp[i] = ' ';
i++;
e = 0;
} else {
tmp[i] = '\0';
i++;
}
break;
default:
if (e) {
tmp[i] = '\\';
i++;
e = 0;
}
tmp[i] = *p;
i++;
break;
}
p++;
/* check if this was the last char */
if (*p == '\0') {
if (e) {
tmp[i] = '\\';
i++;
e = 0;
}
tmp[i] = '\0';
i++;
}
/* check next char and skip multiple spaces */
continue;
}
if (!r) goto fail;
ret = r;
num++;
i = 0;
}
return ret;
fail:
return NULL;
}