#include "apr.h"
#include "apr_strings.h"
#include "apr_private.h"
#include "apr_lib.h"
#if APR_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if APR_HAVE_STRING_H
#include <string.h>
#endif
#if APR_HAVE_CTYPE_H
#include <ctype.h>
#endif
APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src, apr_size_t dst_size)
{
char *d, *end;
if (dst_size == 0) {
return (dst);
}
d = dst;
end = dst + dst_size - 1;
for (; d < end; ++d, ++src) {
if (!(*d = *src)) {
return (d);
}
}
*d = '\0';
return (d);
}
APR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str,
char ***argv_out,
apr_pool_t *token_context)
{
const char *cp;
const char *ct;
char *cleaned, *dirty;
int escaped;
int isquoted, numargs = 0, argnum;
#define SKIP_WHITESPACE(cp) \
for ( ; *cp == ' ' || *cp == '\t'; ) { \
cp++; \
};
#define CHECK_QUOTATION(cp,isquoted) \
isquoted = 0; \
if (*cp == '"') { \
isquoted = 1; \
cp++; \
} \
else if (*cp == '\'') { \
isquoted = 2; \
cp++; \
}
#define DETERMINE_NEXTSTRING(cp,isquoted) \
for ( ; *cp != '\0'; cp++) { \
if ( (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t' || \
*(cp+1) == '"' || *(cp+1) == '\''))) { \
cp++; \
continue; \
} \
if ( (!isquoted && (*cp == ' ' || *cp == '\t')) \
|| (isquoted == 1 && *cp == '"') \
|| (isquoted == 2 && *cp == '\'') ) { \
break; \
} \
}
#define REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped) \
escaped = 0; \
while(*dirty) { \
if (!escaped && *dirty == '\\') { \
escaped = 1; \
} \
else { \
escaped = 0; \
*cleaned++ = *dirty; \
} \
++dirty; \
} \
*cleaned = 0;
cp = arg_str;
SKIP_WHITESPACE(cp);
ct = cp;
numargs = 1;
while (*ct != '\0') {
CHECK_QUOTATION(ct, isquoted);
DETERMINE_NEXTSTRING(ct, isquoted);
if (*ct != '\0') {
ct++;
}
numargs++;
SKIP_WHITESPACE(ct);
}
*argv_out = apr_palloc(token_context, numargs * sizeof(char*));
for (argnum = 0; argnum < (numargs-1); argnum++) {
SKIP_WHITESPACE(cp);
CHECK_QUOTATION(cp, isquoted);
ct = cp;
DETERMINE_NEXTSTRING(cp, isquoted);
cp++;
(*argv_out)[argnum] = apr_palloc(token_context, cp - ct);
apr_cpystrn((*argv_out)[argnum], ct, cp - ct);
cleaned = dirty = (*argv_out)[argnum];
REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped);
}
(*argv_out)[argnum] = NULL;
return APR_SUCCESS;
}
APR_DECLARE(const char *) apr_filepath_name_get(const char *pathname)
{
const char path_separator = '/';
const char *s = strrchr(pathname, path_separator);
#ifdef WIN32
const char path_separator_win = '\\';
const char drive_separator_win = ':';
const char *s2 = strrchr(pathname, path_separator_win);
if (s2 > s) s = s2;
if (!s) s = strrchr(pathname, drive_separator_win);
#endif
return s ? ++s : pathname;
}
APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src)
{
while (*src) {
if (!apr_isspace(*src))
*dest++ = *src;
++src;
}
*dest = 0;
return (dest);
}
#if !APR_HAVE_STRDUP
char *strdup(const char *str)
{
char *sdup;
size_t len = strlen(str) + 1;
sdup = (char *) malloc(len);
memcpy(sdup, str, len);
return sdup;
}
#endif
#if (!APR_HAVE_STRCASECMP && !APR_HAVE_STRICMP)
int strcasecmp(const char *a, const char *b)
{
const char *p = a;
const char *q = b;
for (p = a, q = b; *p && *q; p++, q++) {
int diff = apr_tolower(*p) - apr_tolower(*q);
if (diff)
return diff;
}
if (*p)
return 1;
if (*q)
return -1;
return 0;
}
#endif
#if (!APR_HAVE_STRNCASECMP && !APR_HAVE_STRNICMP)
int strncasecmp(const char *a, const char *b, size_t n)
{
const char *p = a;
const char *q = b;
for (p = a, q = b; ; p++, q++) {
int diff;
if (p == a + n)
return 0;
if (!(*p && *q))
return *p - *q;
diff = apr_tolower(*p) - apr_tolower(*q);
if (diff)
return diff;
}
}
#endif
#if (!APR_HAVE_STRSTR)
char *strstr(char *s1, char *s2)
{
char *p1, *p2;
if (*s2 == '\0') {
return(s1);
}
while((s1 = strchr(s1, *s2)) != NULL) {
p1 = s1;
p2 = s2;
while (*++p1 == *++p2) {
if (*p1 == '\0') {
return(s1);
}
}
if (*p2 == '\0') {
break;
}
s1++;
}
return(s1);
}
#endif