diff options
Diffstat (limited to 'lib/extra_opts.c')
-rw-r--r-- | lib/extra_opts.c | 83 |
1 files changed, 34 insertions, 49 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 3a0ce045..2aeb77ac 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c | |||
@@ -44,23 +44,18 @@ is_option (char *str) | |||
44 | } | 44 | } |
45 | 45 | ||
46 | /* this is the externally visible function used by plugins */ | 46 | /* this is the externally visible function used by plugins */ |
47 | /* Shouldn't se modify directly **argv (passed as a char ***) and argc | 47 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name){ |
48 | * (as int *) ? | 48 | np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL; |
49 | */ | ||
50 | char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new){ | ||
51 | np_arg_list *extra_args=NULL, *ea_tmp1=NULL, *ea_tmp2=NULL; | ||
52 | char **argv_new=NULL; | 49 | char **argv_new=NULL; |
53 | char *argptr=NULL; | 50 | char *argptr=NULL; |
54 | int i, j, optfound, ea_num=argc; | 51 | int i, j, optfound, argc_new, ea_num=*argc; |
55 | 52 | ||
56 | if(argc<2) { | 53 | if(*argc<2) { |
57 | /* No arguments provided */ | 54 | /* No arguments provided */ |
58 | *argc_new=argc; | 55 | return argv; |
59 | argv_new=argv; | ||
60 | return argv_new; | ||
61 | } | 56 | } |
62 | 57 | ||
63 | for(i=1; i<argc; i++){ | 58 | for(i=1; i<*argc; i++){ |
64 | argptr=NULL; | 59 | argptr=NULL; |
65 | optfound=0; | 60 | optfound=0; |
66 | 61 | ||
@@ -69,86 +64,76 @@ char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_n | |||
69 | /* It is a single argument with value */ | 64 | /* It is a single argument with value */ |
70 | argptr=argv[i]+13; | 65 | argptr=argv[i]+13; |
71 | /* Delete the extra opts argument */ | 66 | /* Delete the extra opts argument */ |
72 | for(j=i;j<argc;j++) argv[j]=argv[j+1]; | 67 | for(j=i;j<*argc;j++) argv[j]=argv[j+1]; |
73 | i--; | 68 | i--; |
74 | argc--; | 69 | *argc--; |
75 | }else if(strcmp(argv[i], "--extra-opts")==0){ | 70 | }else if(strcmp(argv[i], "--extra-opts")==0){ |
76 | if(!is_option(argv[i+1])){ | 71 | if(!is_option(argv[i+1])){ |
77 | /* It is a argument with separate value */ | 72 | /* It is a argument with separate value */ |
78 | argptr=argv[i+1]; | 73 | argptr=argv[i+1]; |
79 | /* Delete the extra-opts argument/value */ | 74 | /* Delete the extra-opts argument/value */ |
80 | for(j=i;j<argc-1;j++) argv[j]=argv[j+2]; | 75 | for(j=i;j<*argc-1;j++) argv[j]=argv[j+2]; |
81 | i-=2; | 76 | i-=2; |
82 | argc-=2; | 77 | *argc-=2; |
83 | ea_num--; | 78 | ea_num--; |
84 | }else{ | 79 | }else{ |
85 | /* It has no value */ | 80 | /* It has no value */ |
86 | optfound=1; | 81 | optfound=1; |
87 | /* Delete the extra opts argument */ | 82 | /* Delete the extra opts argument */ |
88 | for(j=i;j<argc;j++) argv[j]=argv[j+1]; | 83 | for(j=i;j<*argc;j++) argv[j]=argv[j+1]; |
89 | i--; | 84 | i--; |
90 | argc--; | 85 | *argc--; |
91 | } | 86 | } |
92 | } | 87 | } |
93 | 88 | ||
89 | /* If we found extra-opts, expand them and store them for later*/ | ||
94 | if(argptr||optfound){ | 90 | if(argptr||optfound){ |
95 | /* Process ini section, returning a linked list of arguments */ | 91 | /* Process ini section, returning a linked list of arguments */ |
96 | ea_tmp1=np_get_defaults(argptr, plugin_name); | 92 | ea1=np_get_defaults(argptr, plugin_name); |
97 | if(ea_tmp1==NULL) { | 93 | if(ea1==NULL) { |
98 | /* no extra args? */ | 94 | /* no extra args (empty section)? */ |
99 | ea_num--; | 95 | ea_num--; |
100 | continue; | 96 | continue; |
101 | } | 97 | } |
102 | 98 | ||
103 | /* append the list to extra_args */ | 99 | /* append the list to extra_args */ |
104 | if(extra_args==NULL){ | 100 | if(extra_args==NULL){ |
105 | extra_args=ea_tmp2=ea_tmp1; | 101 | extra_args=ea1; |
106 | while(ea_tmp2->next) { | 102 | while(ea1=ea1->next) ea_num++; |
107 | ea_tmp2=ea_tmp2->next; | ||
108 | ea_num++; | ||
109 | } | ||
110 | }else{ | 103 | }else{ |
111 | ea_tmp2=extra_args; | 104 | ea_tmp=extra_args; |
112 | while(ea_tmp2->next) { | 105 | while(ea_tmp=ea_tmp->next) ea_num++; |
113 | ea_tmp2=ea_tmp2->next; | 106 | ea_tmp->next=ea1; |
114 | ea_num++; | ||
115 | } | ||
116 | ea_tmp2->next=ea_tmp1; | ||
117 | } | 107 | } |
118 | ea_tmp1=ea_tmp2=NULL; | 108 | ea1=ea_tmp=NULL; |
119 | } | 109 | } |
120 | /* lather, rince, repeat */ | 110 | /* lather, rince, repeat */ |
121 | } | 111 | } |
122 | 112 | ||
123 | if(ea_num==argc && extra_args==NULL){ | 113 | if(ea_num==*argc && extra_args==NULL){ |
124 | /* No extra-opts */ | 114 | /* No extra-opts */ |
125 | *argc_new=argc; | 115 | return argv; |
126 | argv_new=argv; | ||
127 | return argv_new; | ||
128 | } | 116 | } |
129 | 117 | ||
130 | /* done processing arguments. now create a new argc/argv set... */ | 118 | /* done processing arguments. now create a new argv array... */ |
131 | argv_new=(char**)malloc((ea_num+1)*sizeof(char**)); | 119 | argv_new=(char**)malloc((ea_num+1)*sizeof(char**)); |
132 | if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); | 120 | if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); |
133 | 121 | ||
134 | /* starting with program name (Should we strdup or just use the poiter?) */ | 122 | /* starting with program name */ |
135 | argv_new[0]=strdup(argv[0]); | 123 | argv_new[0]=strdup(argv[0]); |
136 | *argc_new=1; | 124 | argc_new=1; |
137 | /* then parsed ini opts (frying them up in the same run) */ | 125 | /* then parsed ini opts (frying them up in the same run) */ |
138 | while(extra_args){ | 126 | while(extra_args){ |
139 | argv_new[*argc_new]=strdup(extra_args->arg); | 127 | argv_new[argc_new++]=extra_args->arg; |
140 | *argc_new+=1; | 128 | ea1=extra_args; |
141 | ea_tmp1=extra_args; | ||
142 | extra_args=extra_args->next; | 129 | extra_args=extra_args->next; |
143 | free(ea_tmp1); | 130 | free(ea1); |
144 | } | ||
145 | /* finally the rest of the argv array (Should we strdup or just use the poiter?) */ | ||
146 | for (i=1; i<argc; i++){ | ||
147 | argv_new[*argc_new]=strdup(argv[i]); | ||
148 | *argc_new+=1; | ||
149 | } | 131 | } |
132 | /* finally the rest of the argv array */ | ||
133 | for (i=1; i<*argc; i++) argv_new[argc_new++]=strdup(argv[i]); | ||
134 | *argc=argc_new; | ||
150 | /* and terminate. */ | 135 | /* and terminate. */ |
151 | argv_new[*argc_new]=NULL; | 136 | argv_new[argc_new]=NULL; |
152 | 137 | ||
153 | return argv_new; | 138 | return argv_new; |
154 | } | 139 | } |