summaryrefslogtreecommitdiffstats
path: root/lib/extra_opts.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/extra_opts.c')
-rw-r--r--lib/extra_opts.c55
1 files changed, 35 insertions, 20 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 88787336..3fe69014 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -27,27 +27,32 @@
27 27
28/* FIXME: copied from utils.h; we should move a bunch of libs! */ 28/* FIXME: copied from utils.h; we should move a bunch of libs! */
29bool is_option2(char *str) { 29bool is_option2(char *str) {
30 if (!str) 30 if (!str) {
31 return false; 31 return false;
32 else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) 32 }
33
34 if (strspn(str, "-") == 1 || strspn(str, "-") == 2) {
33 return true; 35 return true;
34 else 36 }
35 return false; 37
38 return false;
36} 39}
37 40
38/* this is the externally visible function used by plugins */ 41/* this is the externally visible function used by plugins */
39char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { 42char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
40 np_arg_list *extra_args = NULL, *ea1 = NULL, *ea_tmp = NULL;
41 char **argv_new = NULL;
42 char *argptr = NULL;
43 int i, j, optfound, argc_new, ea_num = *argc;
44
45 if (*argc < 2) { 43 if (*argc < 2) {
46 /* No arguments provided */ 44 /* No arguments provided */
47 return argv; 45 return argv;
48 } 46 }
49 47
50 for (i = 1; i < *argc; i++) { 48 np_arg_list *extra_args = NULL;
49 np_arg_list *ea1 = NULL;
50 np_arg_list *ea_tmp = NULL;
51 char *argptr = NULL;
52 int optfound;
53 size_t ea_num = (size_t)*argc;
54
55 for (int i = 1; i < *argc; i++) {
51 argptr = NULL; 56 argptr = NULL;
52 optfound = 0; 57 optfound = 0;
53 58
@@ -56,8 +61,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
56 /* It is a single argument with value */ 61 /* It is a single argument with value */
57 argptr = argv[i] + 13; 62 argptr = argv[i] + 13;
58 /* Delete the extra opts argument */ 63 /* Delete the extra opts argument */
59 for (j = i; j < *argc; j++) 64 for (int j = i; j < *argc; j++) {
60 argv[j] = argv[j + 1]; 65 argv[j] = argv[j + 1];
66 }
67
61 i--; 68 i--;
62 *argc -= 1; 69 *argc -= 1;
63 } else if (strcmp(argv[i], "--extra-opts") == 0) { 70 } else if (strcmp(argv[i], "--extra-opts") == 0) {
@@ -65,8 +72,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
65 /* It is a argument with separate value */ 72 /* It is a argument with separate value */
66 argptr = argv[i + 1]; 73 argptr = argv[i + 1];
67 /* Delete the extra-opts argument/value */ 74 /* Delete the extra-opts argument/value */
68 for (j = i; j < *argc - 1; j++) 75 for (int j = i; j < *argc - 1; j++) {
69 argv[j] = argv[j + 2]; 76 argv[j] = argv[j + 2];
77 }
78
70 i -= 2; 79 i -= 2;
71 *argc -= 2; 80 *argc -= 2;
72 ea_num--; 81 ea_num--;
@@ -74,8 +83,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
74 /* It has no value */ 83 /* It has no value */
75 optfound = 1; 84 optfound = 1;
76 /* Delete the extra opts argument */ 85 /* Delete the extra opts argument */
77 for (j = i; j < *argc; j++) 86 for (int j = i; j < *argc; j++) {
78 argv[j] = argv[j + 1]; 87 argv[j] = argv[j + 1];
88 }
89
79 i--; 90 i--;
80 *argc -= 1; 91 *argc -= 1;
81 } 92 }
@@ -94,34 +105,37 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
94 /* append the list to extra_args */ 105 /* append the list to extra_args */
95 if (extra_args == NULL) { 106 if (extra_args == NULL) {
96 extra_args = ea1; 107 extra_args = ea1;
97 while ((ea1 = ea1->next)) 108 while ((ea1 = ea1->next)) {
98 ea_num++; 109 ea_num++;
110 }
99 } else { 111 } else {
100 ea_tmp = extra_args; 112 ea_tmp = extra_args;
101 while (ea_tmp->next) { 113 while (ea_tmp->next) {
102 ea_tmp = ea_tmp->next; 114 ea_tmp = ea_tmp->next;
103 } 115 }
104 ea_tmp->next = ea1; 116 ea_tmp->next = ea1;
105 while ((ea1 = ea1->next)) 117 while ((ea1 = ea1->next)) {
106 ea_num++; 118 ea_num++;
119 }
107 } 120 }
108 ea1 = ea_tmp = NULL; 121 ea1 = ea_tmp = NULL;
109 } 122 }
110 } /* lather, rince, repeat */ 123 } /* lather, rince, repeat */
111 124
112 if (ea_num == *argc && extra_args == NULL) { 125 if (ea_num == (size_t)*argc && extra_args == NULL) {
113 /* No extra-opts */ 126 /* No extra-opts */
114 return argv; 127 return argv;
115 } 128 }
116 129
117 /* done processing arguments. now create a new argv array... */ 130 /* done processing arguments. now create a new argv array... */
118 argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); 131 char **argv_new = (char **)malloc((ea_num + 1) * sizeof(char **));
119 if (argv_new == NULL) 132 if (argv_new == NULL) {
120 die(STATE_UNKNOWN, _("malloc() failed!\n")); 133 die(STATE_UNKNOWN, _("malloc() failed!\n"));
134 }
121 135
122 /* starting with program name */ 136 /* starting with program name */
123 argv_new[0] = argv[0]; 137 argv_new[0] = argv[0];
124 argc_new = 1; 138 int argc_new = 1;
125 /* then parsed ini opts (frying them up in the same run) */ 139 /* then parsed ini opts (frying them up in the same run) */
126 while (extra_args) { 140 while (extra_args) {
127 argv_new[argc_new++] = extra_args->arg; 141 argv_new[argc_new++] = extra_args->arg;
@@ -130,8 +144,9 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
130 free(ea1); 144 free(ea1);
131 } 145 }
132 /* finally the rest of the argv array */ 146 /* finally the rest of the argv array */
133 for (i = 1; i < *argc; i++) 147 for (int i = 1; i < *argc; i++) {
134 argv_new[argc_new++] = argv[i]; 148 argv_new[argc_new++] = argv[i];
149 }
135 *argc = argc_new; 150 *argc = argc_new;
136 /* and terminate. */ 151 /* and terminate. */
137 argv_new[argc_new] = NULL; 152 argv_new[argc_new] = NULL;