13#define QUOTATION_MARK_STR "\""
17 unsigned int length = 0;
18 os_char *res = malloc(strlen(str) + 1);
19 bool quote_opened =
false;
20 for(
int i = 0; str[i] !=
'\0'; ++i) {
22 quote_opened = !quote_opened;
23 }
else if(quote_opened) {
42 os_char *tmp = realloc(res, length + 1);
53 os_char **argv = (os_char **)malloc(
MAX_ARGC *
sizeof(os_char *));
54 unsigned int argc = 0;
57 bool was_space =
true;
60 for(
int i = 0;
true; ++i) {
62 if(is_space && !was_space) {
67 const unsigned int len = (i - 1) - start + 1;
68 argv[argc] = malloc(len + 1);
69 memcpy(argv[argc], str + start, len);
70 argv[argc][len] =
'\0';
73 if(!is_space && was_space) {
89 for(
int i = 0; i <
args->
argc; ++i) {
90 if(strcmp(
args->
argv[i],
"&") == 0) {
100 for(
int i = first_oc + 1; i <
args->
argc; ++i) {
109 const unsigned int len = strlen(arg);
110 for(
int i = 0; i < len; ++i) {
118 assert(obj &&
"NULL input");
123 if(!quote_transformed) {
127 free(quote_transformed);
128 for(
int i = 0; i < obj->
argc; ++i) {
139 for(
int i = 0; i < obj->
argc; ++i) {
142 free((
void *)obj->
argv);
149 struct args *res = malloc(
sizeof(
struct args));
152 res->
argv = (os_char **)malloc((obj->
argc + 1) *
sizeof(os_char *));
153 for(
int i = 0; i < obj->
argc; ++i) {
154 const unsigned int len = strlen(obj->
argv[i]);
155 res->
argv[i] = malloc((len + 1) *
sizeof(os_char));
156 memcpy(res->
argv[i], obj->
argv[i], len *
sizeof(os_char));
157 res->
argv[i][len] =
'\0';
164 assert(source != NULL &&
"Cannot deep copy null");
167 obj->
argv = (os_char **)malloc((source->
argc + 1) *
sizeof(os_char *));
168 for(
int i = 0; i < source->
argc; ++i) {
169 const unsigned int len = strlen(source->
argv[i]);
170 obj->
argv[i] = malloc((len + 1) *
sizeof(os_char));
171 memcpy(obj->
argv[i], source->
argv[i], len *
sizeof(os_char));
172 obj->
argv[i][len] =
'\0';
178 return c ==
' ' || c ==
'\t' || c ==
'\n';
void verify_background(struct args *args)
void args_deep_copy_init(struct args *obj, const struct args *source)
Initialize the object by deep copying source
void split_by_whitespaces(const os_char *str, struct args *buffer)
bool is_whitespace(os_char c)
!
struct args * args_deep_copy(const struct args *obj)
Deep copy the object, including arguments string.
void args_destroy(struct args *obj)
bool args_init_from_str(struct args *obj, const os_char *input)
Build args from an input string.
os_char * transform_quotes(const os_char *str)
Transform whitespaces inside quotes into MAGIC_TOKEN
void re_transform_arg(os_char *arg)
Transform MAGIC_TOKEN inside arg into space.
#define QUOTATION_MARK_STR
void format_error(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Result after parsing an arbitrary string.