#include "string.h"
#include "errno.h"
#include "stdlib.h"
#include "lp.h"
#if defined(__STDC__)
static char *unq_strdup ( char * , char * );
#else
static char *unq_strdup();
#endif
char **
#if defined(__STDC__)
getlist (
char * str,
char * ws,
char * hardsep
)
#else
getlist (str, ws, hardsep)
register char *str,
*ws;
char *hardsep;
#endif
{
register char **list,
*p,
*sep,
c;
int n,
len;
char buf[10];
char *copy,
*begin;
if (!str || !*str)
return (0);
len = strlen(ws) + strlen(hardsep) + 1;
if (len > sizeof(buf)) {
if (!(sep = Malloc(len))) {
errno = ENOMEM;
return (0);
}
} else
sep = buf;
strcpy (sep, hardsep);
strcat (sep, ws);
if (!(begin = Strdup(str))) {
errno = ENOMEM;
return (0);
}
copy = begin;
copy += strspn(copy, ws);
if (!*copy) {
Free (begin);
return (0);
}
p = strchr(copy, '\0');
while (--p != copy && strchr(ws, *p))
;
*++p = 0;
for (n = 0, p = copy; *p; ) {
if ((c = *p++) == '\\')
p++;
else
if (strchr(sep, c)) {
n++;
p += strspn(p, ws);
if (
!strchr(hardsep, c)
&& strchr(hardsep, *p)
) {
p++;
p += strspn(p, ws);
}
}
}
if (!(list = (char **)Malloc((n+2) * sizeof(char *)))) {
errno = ENOMEM;
goto Done;
}
for (n = 0, p = copy; *p; )
if ((c = *p++) == '\\')
p++;
else
if (strchr(sep, c)) {
p[-1] = 0;
list[n++] = unq_strdup(copy, sep);
p[-1] = c;
p += strspn(p, ws);
if (
!strchr(hardsep, c)
&& strchr(hardsep, *p)
) {
p++;
p += strspn(p, ws);
}
copy = p;
}
list[n++] = unq_strdup(copy, sep);
list[n] = 0;
Done: if (sep != buf)
Free (sep);
Free (begin);
return (list);
}
static char *
#if defined(__STDC__)
unq_strdup (
char * str,
char * sep
)
#else
unq_strdup (str, sep)
char *str,
*sep;
#endif
{
register int len = 0;
register char *p,
*q,
*ret;
for (p = str; *p; p++)
if (*p != '\\' || !p[1] || !strchr(sep, p[1]))
len++;
if (!(q = ret = Malloc(len + 1)))
return (0);
for (p = str; *p; p++)
if (*p != '\\' || !p[1] || !strchr(sep, p[1]))
*q++ = *p;
*q = 0;
return (ret);
}