You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
363 lines
7.5 KiB
363 lines
7.5 KiB
10 years ago
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
9 years ago
|
#include <string.h>
|
||
9 years ago
|
#include <strings.h>
|
||
|
#include <ctype.h>
|
||
|
#include "stringop.h"
|
||
|
#include "log.h"
|
||
10 years ago
|
#include "string.h"
|
||
|
#include "list.h"
|
||
9 years ago
|
|
||
9 years ago
|
const char whitespace[] = " \f\n\r\t\v";
|
||
10 years ago
|
|
||
9 years ago
|
char *strip_whitespace(char *_str) {
|
||
9 years ago
|
if (*_str == '\0')
|
||
|
return _str;
|
||
|
char *strold = _str;
|
||
|
while (*_str == ' ' || *_str == '\t') {
|
||
|
_str++;
|
||
|
}
|
||
9 years ago
|
char *str = strdup(_str);
|
||
9 years ago
|
free(strold);
|
||
|
int i;
|
||
|
for (i = 0; str[i] != '\0'; ++i);
|
||
9 years ago
|
do {
|
||
9 years ago
|
i--;
|
||
9 years ago
|
} while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
|
||
9 years ago
|
str[i + 1] = '\0';
|
||
9 years ago
|
return str;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void strip_quotes(char *str) {
|
||
|
bool in_str = false;
|
||
|
bool in_chr = false;
|
||
|
bool escaped = false;
|
||
|
char *end = strchr(str,0);
|
||
|
while (*str) {
|
||
|
if (*str == '\'' && !in_str && !escaped) {
|
||
|
in_chr = !in_chr;
|
||
|
goto shift_over;
|
||
|
} else if (*str == '\"' && !in_chr && !escaped) {
|
||
|
in_str = !in_str;
|
||
|
goto shift_over;
|
||
|
} else if (*str == '\\') {
|
||
|
escaped = !escaped;
|
||
|
++str;
|
||
|
continue;
|
||
|
}
|
||
|
escaped = false;
|
||
|
++str;
|
||
|
continue;
|
||
|
shift_over:
|
||
|
memmove(str, str+1, end-- - str);
|
||
|
}
|
||
|
*end = '\0';
|
||
|
}
|
||
|
|
||
9 years ago
|
// strcmp that also handles null pointers.
|
||
|
int lenient_strcmp(char *a, char *b) {
|
||
|
if (a == b) {
|
||
|
return 0;
|
||
|
} else if (!a) {
|
||
|
return -1;
|
||
|
} else if (!b) {
|
||
|
return 1;
|
||
|
} else {
|
||
|
return strcmp(a, b);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
list_t *split_string(const char *str, const char *delims) {
|
||
|
list_t *res = create_list();
|
||
9 years ago
|
char *copy = strdup(str);
|
||
9 years ago
|
char *token;
|
||
|
|
||
|
token = strtok(copy, delims);
|
||
|
while(token) {
|
||
|
token = strdup(token);
|
||
|
list_add(res, token);
|
||
|
token = strtok(NULL, delims);
|
||
10 years ago
|
}
|
||
9 years ago
|
free(copy);
|
||
10 years ago
|
return res;
|
||
|
}
|
||
|
|
||
|
void free_flat_list(list_t *list) {
|
||
|
int i;
|
||
|
for (i = 0; i < list->length; ++i) {
|
||
|
free(list->items[i]);
|
||
|
}
|
||
|
list_free(list);
|
||
|
}
|
||
|
|
||
9 years ago
|
char **split_args(const char *start, int *argc) {
|
||
|
*argc = 0;
|
||
|
int alloc = 2;
|
||
9 years ago
|
char **argv = malloc(sizeof(char *) * alloc);
|
||
9 years ago
|
bool in_token = false;
|
||
|
bool in_string = false;
|
||
|
bool in_char = false;
|
||
9 years ago
|
bool in_brackets = false; // brackets are used for critera
|
||
9 years ago
|
bool escaped = false;
|
||
|
const char *end = start;
|
||
9 years ago
|
if (start) {
|
||
|
while (*start) {
|
||
|
if (!in_token) {
|
||
|
start = (end += strspn(end, whitespace));
|
||
|
in_token = true;
|
||
|
}
|
||
|
if (*end == '"' && !in_char && !escaped) {
|
||
|
in_string = !in_string;
|
||
|
} else if (*end == '\'' && !in_string && !escaped) {
|
||
|
in_char = !in_char;
|
||
9 years ago
|
} else if (*end == '[' && !in_string && !in_char && !in_brackets && !escaped) {
|
||
|
in_brackets = true;
|
||
|
} else if (*end == ']' && !in_string && !in_char && in_brackets && !escaped) {
|
||
|
in_brackets = false;
|
||
9 years ago
|
} else if (*end == '\\') {
|
||
|
escaped = !escaped;
|
||
9 years ago
|
} else if (*end == '\0' || (!in_string && !in_char && !in_brackets
|
||
|
&& !escaped && strchr(whitespace, *end))) {
|
||
9 years ago
|
goto add_token;
|
||
|
}
|
||
|
if (*end != '\\') {
|
||
|
escaped = false;
|
||
|
}
|
||
|
++end;
|
||
|
continue;
|
||
|
add_token:
|
||
|
if (end - start > 0) {
|
||
|
char *token = malloc(end - start + 1);
|
||
|
strncpy(token, start, end - start + 1);
|
||
|
token[end - start] = '\0';
|
||
|
argv[*argc] = token;
|
||
|
if (++*argc + 1 == alloc) {
|
||
|
argv = realloc(argv, (alloc *= 2) * sizeof(char *));
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
in_token = false;
|
||
|
escaped = false;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
argv[*argc] = NULL;
|
||
|
return argv;
|
||
9 years ago
|
}
|
||
|
|
||
|
void free_argv(int argc, char **argv) {
|
||
9 years ago
|
while (--argc > 0) {
|
||
9 years ago
|
free(argv[argc]);
|
||
|
}
|
||
|
free(argv);
|
||
|
}
|
||
|
|
||
10 years ago
|
char *code_strstr(const char *haystack, const char *needle) {
|
||
|
/* TODO */
|
||
|
return strstr(haystack, needle);
|
||
|
}
|
||
|
|
||
|
char *code_strchr(const char *str, char delimiter) {
|
||
|
int in_string = 0, in_character = 0;
|
||
|
int i = 0;
|
||
|
while (str[i] != '\0') {
|
||
|
if (str[i] == '"' && !in_character) {
|
||
|
in_string = !in_string;
|
||
|
} else if (str[i] == '\'' && !in_string) {
|
||
|
in_character = !in_character;
|
||
|
} else if (!in_character && !in_string) {
|
||
|
if (str[i] == delimiter) {
|
||
|
return (char *)str + i;
|
||
|
}
|
||
|
}
|
||
|
++i;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
int unescape_string(char *string) {
|
||
|
/* TODO: More C string escapes */
|
||
|
int len = strlen(string);
|
||
|
int i;
|
||
|
for (i = 0; string[i]; ++i) {
|
||
|
if (string[i] == '\\') {
|
||
|
switch (string[++i]) {
|
||
|
case '0':
|
||
|
string[i - 1] = '\0';
|
||
9 years ago
|
return i - 1;
|
||
10 years ago
|
case 'a':
|
||
|
string[i - 1] = '\a';
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
|
case 'b':
|
||
|
string[i - 1] = '\b';
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
9 years ago
|
case 'f':
|
||
|
string[i - 1] = '\f';
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
|
case 'n':
|
||
|
string[i - 1] = '\n';
|
||
9 years ago
|
string[i] = '\0';
|
||
9 years ago
|
break;
|
||
|
case 'r':
|
||
|
string[i - 1] = '\r';
|
||
9 years ago
|
string[i] = '\0';
|
||
9 years ago
|
break;
|
||
|
case 't':
|
||
|
string[i - 1] = '\t';
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
|
case 'v':
|
||
|
string[i - 1] = '\v';
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
9 years ago
|
case '\\':
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
9 years ago
|
case '\'':
|
||
|
string[i - 1] = '\'';
|
||
9 years ago
|
string[i] = '\0';
|
||
9 years ago
|
break;
|
||
|
case '\"':
|
||
|
string[i - 1] = '\"';
|
||
9 years ago
|
string[i] = '\0';
|
||
9 years ago
|
break;
|
||
|
case '?':
|
||
|
string[i - 1] = '?';
|
||
9 years ago
|
string[i] = '\0';
|
||
10 years ago
|
break;
|
||
9 years ago
|
case 'x':
|
||
|
{
|
||
|
unsigned char c = 0;
|
||
|
if (string[i+1] >= '0' && string[i+1] <= '9') {
|
||
|
c = string[i+1] - '0';
|
||
|
if (string[i+2] >= '0' && string[i+2] <= '9') {
|
||
|
c *= 0x10;
|
||
|
c += string[i+2] - '0';
|
||
9 years ago
|
string[i+2] = '\0';
|
||
9 years ago
|
}
|
||
9 years ago
|
string[i+1] = '\0';
|
||
9 years ago
|
}
|
||
9 years ago
|
string[i] = '\0';
|
||
9 years ago
|
string[i - 1] = c;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
// Shift characters over nullspaces
|
||
|
int shift = 0;
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
if (string[i] == 0) {
|
||
|
shift++;
|
||
|
continue;
|
||
|
}
|
||
|
string[i-shift] = string[i];
|
||
|
}
|
||
|
string[len - shift] = 0;
|
||
|
return len - shift;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
char *join_args(char **argv, int argc) {
|
||
10 years ago
|
int len = 0, i;
|
||
|
for (i = 0; i < argc; ++i) {
|
||
|
len += strlen(argv[i]) + 1;
|
||
|
}
|
||
|
char *res = malloc(len);
|
||
|
len = 0;
|
||
|
for (i = 0; i < argc; ++i) {
|
||
|
strcpy(res + len, argv[i]);
|
||
|
len += strlen(argv[i]);
|
||
|
res[len++] = ' ';
|
||
|
}
|
||
|
res[len - 1] = '\0';
|
||
|
return res;
|
||
|
}
|
||
9 years ago
|
|
||
|
/*
|
||
|
* Join a list of strings, adding separator in between. Separator can be NULL.
|
||
|
*/
|
||
|
char *join_list(list_t *list, char *separator) {
|
||
|
if (!sway_assert(list != NULL, "list != NULL") || list->length == 0) {
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
size_t len = 1; // NULL terminator
|
||
|
size_t sep_len = 0;
|
||
|
if (separator != NULL) {
|
||
|
sep_len = strlen(separator);
|
||
|
len += (list->length - 1) * sep_len;
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i < list->length; i++) {
|
||
|
len += strlen(list->items[i]);
|
||
|
}
|
||
|
|
||
|
char *res = malloc(len);
|
||
|
|
||
|
char *p = res + strlen(list->items[0]);
|
||
|
strcpy(res, list->items[0]);
|
||
|
|
||
|
for (int i = 1; i < list->length; i++) {
|
||
|
if (sep_len) {
|
||
|
memcpy(p, separator, sep_len);
|
||
|
p += sep_len;
|
||
|
}
|
||
|
strcpy(p, list->items[i]);
|
||
|
p += strlen(list->items[i]);
|
||
|
}
|
||
|
|
||
|
*p = '\0';
|
||
|
|
||
|
return res;
|
||
|
}
|
||
9 years ago
|
|
||
|
char *cmdsep(char **stringp, const char *delim) {
|
||
9 years ago
|
// skip over leading delims
|
||
|
char *head = *stringp + strspn(*stringp, delim);
|
||
|
// Find end token
|
||
|
char *tail = *stringp += strcspn(*stringp, delim);
|
||
|
// Set stringp to begining of next token
|
||
|
*stringp += strspn(*stringp, delim);
|
||
|
// Set stringp to null if last token
|
||
|
if (!**stringp) *stringp = NULL;
|
||
|
// Nullify end of first token
|
||
|
*tail = 0;
|
||
9 years ago
|
return head;
|
||
|
}
|
||
|
|
||
|
char *argsep(char **stringp, const char *delim) {
|
||
|
char *start = *stringp;
|
||
|
char *end = start;
|
||
|
bool in_string = false;
|
||
|
bool in_char = false;
|
||
|
bool escaped = false;
|
||
|
while (1) {
|
||
|
if (*end == '"' && !in_char && !escaped) {
|
||
|
in_string = !in_string;
|
||
|
} else if (*end == '\'' && !in_string && !escaped) {
|
||
|
in_char = !in_char;
|
||
|
} else if (*end == '\\') {
|
||
|
escaped = !escaped;
|
||
|
} else if (*end == '\0') {
|
||
|
*stringp = NULL;
|
||
|
goto found;
|
||
|
} else if (!in_string && !in_char && !escaped && strchr(delim, *end)) {
|
||
|
if (end - start) {
|
||
|
*(end++) = 0;
|
||
|
*stringp = end + strspn(end, delim);;
|
||
|
if (!**stringp) *stringp = NULL;
|
||
|
goto found;
|
||
|
} else {
|
||
|
++start;
|
||
|
end = start;
|
||
|
}
|
||
|
}
|
||
|
if (*end != '\\') {
|
||
|
escaped = false;
|
||
|
}
|
||
|
++end;
|
||
|
}
|
||
|
found:
|
||
|
return start;
|
||
|
}
|