summaryrefslogtreecommitdiffstats
path: root/lib/parse_ini.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r--lib/parse_ini.c76
1 files changed, 39 insertions, 37 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index ef56be95..c915d795 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -42,9 +42,9 @@ typedef struct {
42#define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n)) 42#define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
43 43
44/* internal function that returns the constructed defaults options */ 44/* internal function that returns the constructed defaults options */
45static char* read_defaults(FILE *f, const char *stanza); 45static np_arg_list* read_defaults(FILE *f, const char *stanza);
46/* internal function that converts a single line into options format */ 46/* internal function that converts a single line into options format */
47static int add_option(FILE *f, char **optbuf, size_t *bufsize); 47static int add_option(FILE *f, np_arg_list **optlst);
48 48
49/* parse_locator decomposes a string of the form 49/* parse_locator decomposes a string of the form
50 * [stanza][@filename] 50 * [stanza][@filename]
@@ -76,9 +76,9 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in
76} 76}
77 77
78/* this is the externally visible function used by plugins */ 78/* this is the externally visible function used by plugins */
79char* np_get_defaults(const char *locator, const char *default_section){ 79np_arg_list* np_get_defaults(const char *locator, const char *default_section){
80 FILE *inifile=NULL; 80 FILE *inifile=NULL;
81 char *defaults=NULL; 81 np_arg_list *defaults=NULL;
82 np_ini_info i; 82 np_ini_info i;
83 83
84 parse_locator(locator, default_section, &i); 84 parse_locator(locator, default_section, &i);
@@ -104,10 +104,10 @@ char* np_get_defaults(const char *locator, const char *default_section){
104 * be extra careful about user-supplied input (i.e. avoiding possible 104 * be extra careful about user-supplied input (i.e. avoiding possible
105 * format string vulnerabilities, etc) 105 * format string vulnerabilities, etc)
106 */ 106 */
107static char* read_defaults(FILE *f, const char *stanza){ 107static np_arg_list* read_defaults(FILE *f, const char *stanza){
108 int c; 108 int c;
109 char *opts=NULL; 109 np_arg_list *opts=NULL;
110 size_t i, stanza_len, opts_buf_size=0; 110 size_t i, stanza_len;
111 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA; 111 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
112 112
113 stanza_len=strlen(stanza); 113 stanza_len=strlen(stanza);
@@ -154,7 +154,7 @@ static char* read_defaults(FILE *f, const char *stanza){
154 /* okay, this is where we start taking the config */ 154 /* okay, this is where we start taking the config */
155 case RIGHTSTANZA: 155 case RIGHTSTANZA:
156 ungetc(c, f); 156 ungetc(c, f);
157 if(add_option(f, &opts, &opts_buf_size)){ 157 if(add_option(f, &opts)){
158 die(STATE_UNKNOWN, _("Config file error")); 158 die(STATE_UNKNOWN, _("Config file error"));
159 } 159 }
160 break; 160 break;
@@ -170,15 +170,14 @@ static char* read_defaults(FILE *f, const char *stanza){
170 * ^option[[:space:]]*(=[[:space:]]*value)? 170 * ^option[[:space:]]*(=[[:space:]]*value)?
171 * and creates it as a cmdline argument 171 * and creates it as a cmdline argument
172 * --option[=value] 172 * --option[=value]
173 * appending it to the string pointed to by optbuf (which will 173 * appending it to the linked list optbuf.
174 * be dynamically grown if needed)
175 */ 174 */
176static int add_option(FILE *f, char **optbuf, size_t *bufsize){ 175static int add_option(FILE *f, np_arg_list **optlst){
177 char *newbuf=*optbuf; 176 np_arg_list *opttmp=*optlst, *optnew;
178 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL; 177 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
179 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL; 178 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
180 short done_reading=0, equals=0, value=0; 179 short done_reading=0, equals=0, value=0;
181 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0, bs=*bufsize; 180 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
182 size_t opt_len=0, val_len=0; 181 size_t opt_len=0, val_len=0;
183 182
184 /* read one line from the file */ 183 /* read one line from the file */
@@ -214,14 +213,13 @@ static int add_option(FILE *f, char **optbuf, size_t *bufsize){
214 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n")); 213 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
215 /* continue from '=' to start of value or EOL */ 214 /* continue from '=' to start of value or EOL */
216 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++); 215 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
217 /* continue to the end of value, watching for trailing space/comments */ 216 /* continue to the end of value (FIXME: watching for trailing comments) */
218 for(valend=valptr; valend<lineend; valend++){ 217 for(valend=valptr; valend<lineend; valend++)
219 if(isspace(*valend) && spaceptr==NULL) spaceptr=valend; 218 /* FIXME: N::P doesn't allow comments. Remove next line and parse_ini won't either */
220 else if(*valend=='#') break; 219 if(*valend=='#') break;
221 else spaceptr=NULL;
222 }
223 if(spaceptr!=NULL) valend=spaceptr;
224 --valend; 220 --valend;
221 /* Finally trim off trailing spaces */
222 for(valend; isspace(*valend); valend--);
225 /* calculate the length of "--foo" */ 223 /* calculate the length of "--foo" */
226 opt_len=1+optend-optptr; 224 opt_len=1+optend-optptr;
227 cfg_len=2+(opt_len); 225 cfg_len=2+(opt_len);
@@ -237,27 +235,31 @@ static int add_option(FILE *f, char **optbuf, size_t *bufsize){
237 cfg_len+=1; 235 cfg_len+=1;
238 } 236 }
239 237
240 /* okay, now we have all the info we need, so we grow the default opts 238 /* okay, now we have all the info we need, so we create a new np_arg_list
241 * buffer if it's necessary, and put everything together. 239 * element and set the argument...
242 * (+2 is for a potential space and a null byte)
243 */ 240 */
244 read_pos=(newbuf==NULL)?0:strlen(newbuf); 241 optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
245 if(newbuf==NULL || read_pos+cfg_len+2 >= bs){ 242 optnew->next=NULL;
246 bs=(bs>0)?(bs+cfg_len+2)<<1:cfg_len+1; 243
247 newbuf=realloc(newbuf, bs); 244 read_pos=0;
248 if(newbuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); 245 optnew->arg=(char *)malloc(cfg_len+1);
249 } 246 strncpy(&optnew->arg[read_pos], "--", 2); read_pos+=2;
250 if(read_pos>0) newbuf[read_pos++]=' '; 247 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
251 strncpy(&newbuf[read_pos], "--", 2); read_pos+=2; 248 if(equals) optnew->arg[read_pos++]='=';
252 strncpy(&newbuf[read_pos], optptr, opt_len); read_pos+=opt_len;
253 if(equals) newbuf[read_pos++]='=';
254 if(value) { 249 if(value) {
255 strncpy(&newbuf[read_pos], valptr, val_len); read_pos+=val_len; 250 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
256 } 251 }
257 newbuf[read_pos]='\0'; 252 optnew->arg[read_pos]='\0';
258 253
259 *optbuf=newbuf; 254 /* ...and put that to the end of the list */
260 *bufsize=bs; 255 if (*optlst==NULL) {
256 *optlst=optnew;
257 } else {
258 while (opttmp->next!=NULL) {
259 opttmp=opttmp->next;
260 }
261 opttmp->next = optnew;
262 }
261 263
262 free(linebuf); 264 free(linebuf);
263 return 0; 265 return 0;