2013-02-16 12:25:37 +01:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
int parameters_prepare(char *param, char* argv[]) {
|
|
|
|
int i, param_len;
|
|
|
|
int argc = 1;
|
|
|
|
int quote = 0;
|
|
|
|
int argv_len;
|
|
|
|
|
|
|
|
param_len = strlen(param);
|
|
|
|
if ( param_len == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
argv[0] = (char*) malloc(4096);
|
|
|
|
|
|
|
|
argv_len = 0;
|
|
|
|
for (i = 0; i < param_len; i++) {
|
|
|
|
switch (param[i]) {
|
|
|
|
case '\\':
|
|
|
|
if (param[i+1]!='\0') {
|
|
|
|
switch(param[i+1]) {
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
argv[argc-1][argv_len] = '\n';
|
|
|
|
argv_len++;
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
argv[argc-1][argv_len] = '\t';
|
|
|
|
argv_len++;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '?':
|
|
|
|
argv[argc-1][argv_len] = param[i+1];
|
|
|
|
argv_len++;
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
if (quote == 0)
|
2013-02-16 12:25:37 +01:00
|
|
|
quote = 1;
|
2021-06-12 23:34:41 +02:00
|
|
|
else {
|
2013-02-16 12:25:37 +01:00
|
|
|
quote = 0;
|
|
|
|
argv[argc-1][argv_len] = '\0';
|
|
|
|
argc++;
|
|
|
|
argv[argc-1] = (char*) malloc(4096);
|
|
|
|
argv[argc-1][argv_len] = '\0';
|
|
|
|
argv_len = 0;
|
|
|
|
}
|
2021-06-12 23:34:41 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
case 32:
|
|
|
|
if (quote == 0) {
|
|
|
|
if ( ( param[i+1] != 32) || ( param[i+1] != 9) ) {
|
|
|
|
if (argv_len != 0) {
|
|
|
|
argv[argc-1][argv_len] = '\0';
|
|
|
|
argc++;
|
|
|
|
argv[argc-1] = (char*) malloc(4096);
|
|
|
|
argv[argc-1][argv_len] = '\0';
|
|
|
|
argv_len = 0;
|
2013-02-16 12:25:37 +01:00
|
|
|
}
|
2021-06-12 23:34:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-02-16 12:25:37 +01:00
|
|
|
argv[argc-1][argv_len] = param[i];
|
|
|
|
argv_len++;
|
2021-06-12 23:34:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
argv[argc-1][argv_len] = param[i];
|
|
|
|
argv_len++;
|
|
|
|
break;
|
2013-02-24 11:33:37 +01:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
}; // switch end
|
|
|
|
} // for end
|
2013-02-16 12:25:37 +01:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
argv[argc-1][argv_len] = '\0';
|
2013-02-16 12:25:37 +01:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
if ( strlen(argv[argc-1]) == 0 ) {
|
|
|
|
free(argv[argc-1]);
|
|
|
|
argc--;
|
|
|
|
}
|
2013-02-16 12:25:37 +01:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
return argc;
|
2013-02-16 12:25:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
void parameters_free(int argc, char* argv[]) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < argc; i++) free(argv[i]);
|
2013-02-16 12:25:37 +01:00
|
|
|
}
|
|
|
|
|