1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
* makeargv.c: parse string to argv[]
*
* $Id: makeargv.c,v 1.8 2016/07/27 09:10:44 tom Exp $
*/
#include <estruct.h>
#include <makeargv.h>
//--------------------------------------------------------------
int
option_has_param(const char *option)
{
static const char *table[] =
{
"-c",
#if OPT_ENCRYPT
"-k",
#endif
#if OPT_TAGS
#if DISP_X11 /* because -title is predefined */
"-T",
#else
"-t",
#endif
#endif
#if DISP_NTWIN
"-font",
"-fn",
"-geometry",
#endif
};
unsigned n;
int result = 0;
for (n = 0; n < TABLESIZE(table); ++n) {
if (!strcmp(option, table[n])) {
result = 1;
break;
}
}
return result;
}
int
after_options(int first, int argc, char **argv)
{
int result = first;
while (result < argc && argv[result] != 0 && is_option(argv[result]))
result += 1 + option_has_param(argv[result]);
return result;
}
int
is_option(const char *param)
{
return (*param == '-'
|| *param == '+'
|| *param == '@');
}
int
make_argv(const char *program,
const char *cmdline,
char ***argvp,
int *argcp,
char **argend)
{
int maxargs = 2 + ((int) strlen(cmdline) + 2) / 2;
char *blob;
char *ptr;
char **argv;
int argc = 0;
if ((blob = typeallocn(char, strlen(cmdline) + 1)) == 0)
return -1;
if ((argv = typeallocn(char *, maxargs)) == 0) {
free(blob);
return -1;
}
if (argend != 0)
*argend = 0;
strcpy(blob, cmdline);
if (program != 0)
argv[argc++] = (char *) program;
for (ptr = blob; *ptr != '\0';) {
char *dst;
char delim = ' ';
while (*ptr == ' ')
ptr++;
if (*ptr == SQUOTE
|| *ptr == DQUOTE
|| *ptr == ' ') {
delim = *ptr++;
}
/*
* Save the beginning of non-options in *argend
*/
if (argend != 0
&& *argend == 0
&& !is_option(ptr)) {
*argend = strdup(ptr);
}
argv[argc++] = dst = ptr;
if (argc + 1 >= maxargs) {
break;
}
while (*ptr != delim && *ptr != '\0') {
if (*ptr == '"') {
ptr++;
delim = (char) ((delim == ' ') ? '"' : ' ');
} else {
*dst++ = *ptr++;
}
}
if (*ptr == '"') {
++ptr;
}
if (dst != ptr) {
*dst = '\0';
} else if (*ptr == ' ') {
*ptr++ = '\0';
}
}
argv[argc] = 0;
*argvp = argv;
*argcp = argc;
return 0;
}
|