Tizen 2.0 Release
[external/libgnutls26.git] / src / cfg / props.c
1 /*
2  * libcfg+ - precise command line & config file parsing library
3  *
4  * props.c - context properties manipulation
5  * ____________________________________________________________
6  *
7  * Developed by Ondrej Jombik <nepto@platon.sk>
8  *          and Lubomir Host <rajo@platon.sk>
9  * Copyright (c) 2001-2004 Platon SDG, http://platon.sk/
10  * All rights reserved.
11  *
12  * See README file for more information about this software.
13  * See COPYING file for license information.
14  *
15  * Download the latest version from
16  * http://platon.sk/projects/libcfg+/
17  */
18
19 /* $Platon: libcfg+/src/props.c,v 1.34 2004/01/12 06:03:09 nepto Exp $ */
20
21 /* Includes {{{ */
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include <platon/str/strdyn.h>
30 #include "cfg+.h"
31 /* }}} */
32
33 /* Default properties {{{ */
34 char *cfg_default_properties[CFG_N_PROPS][4] = {
35         /* CFG_LINE_STOP_STRING */                                              { NULL, NULL, NULL, NULL },
36         /* CFG_LINE_SHORT_OPTION_PREFIX */                              { "-",  NULL, NULL, NULL },
37         /* CFG_LINE_LONG_OPTION_PREFIX */                               { "--", NULL, NULL, NULL },
38         /* CFG_LINE_OPTION_ARG_SEPARATOR */                             { "=",  NULL, NULL, NULL },
39         /* CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR */              { ",",  ";",  NULL, NULL },
40         /* CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR */    { NULL, NULL, NULL, NULL },
41         /* CFG_LINE_QUOTE_PREFIX */                                             { NULL, NULL, NULL, NULL },
42         /* CFG_LINE_QUOTE_POSTFIX */                                    { NULL, NULL, NULL, NULL },
43         /* CFG_FILE_STOP_PREFIX */                                              { NULL, NULL, NULL, NULL },
44         /* CFG_FILE_COMMENT_PREFIX */                                   { "#",  ";",  NULL, NULL },
45         /* CFG_FILE_MULTI_LINE_POSTFIX */                               { "\\", NULL, NULL, NULL },
46         /* CFG_FILE_OPTION_ARG_SEPARATOR */                             { "=",  NULL, NULL, NULL },
47         /* CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR */              { ",",  ";",  " ",  NULL },
48         /* CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR */    { " ",  NULL, NULL, NULL },
49         /* CFG_FILE_QUOTE_PREFIX */                                             { "\"", "'",  NULL, NULL },
50         /* CFG_FILE_QUOTE_POSTFIX */                                    { "\"", "'",  NULL, NULL },
51         }; /* }}} */
52
53 /*
54  * Static functions for strdyn manipulation
55  */
56
57         static int
58 strdyn_array_clear(where)
59         char ***where;
60 { /* {{{ */
61
62 #if 0
63         if (*where != NULL)
64 #endif
65                 *where = PLATON_FUNC(strdyn_remove_all)(*where);
66
67         return *where != NULL;
68 } /* }}} */
69
70         static int
71 strdyn_array_add(where, str)
72         char ***where;
73         char *str;
74 { /* {{{ */
75
76 #if 0
77         if (*where == NULL)
78                 *where = strdyn_create();
79 #endif
80
81         *where = PLATON_FUNC(strdyn_add)(*where, str);
82
83         return *where != NULL;
84 } /* }}} */
85
86         static int
87 strdyn_array_remove(where, str)
88         char ***where;
89         char *str;
90 { /* {{{ */
91
92 #if 0
93         if (*where != NULL)
94 #endif
95                 *where = PLATON_FUNC(strdyn_remove_str_all)(*where, str);
96
97         return *where != NULL;
98 } /* }}} */
99
100
101 /*
102  * Functions for context flags manipulation.
103  */
104
105         void
106 cfg_set_context_flag(con, flag)
107         const CFG_CONTEXT con;
108         int               flag;
109 { /* {{{ */
110         con->flags |= flag;
111 } /* }}} */
112
113         void
114 cfg_clear_context_flag(con, flag)
115         const CFG_CONTEXT con;
116         int               flag;
117 { /* {{{ */
118         con->flags &= ~flag;
119 } /* }}} */
120
121         int
122 cfg_get_context_flag(con, flag)
123         const CFG_CONTEXT con;
124         int               flag;
125 { /* {{{ */
126         return con->flags & flag;
127 } /* }}} */
128
129
130         void
131 cfg_set_context_flags(con, flags)
132         const CFG_CONTEXT con;
133         int               flags;
134 { /* {{{ */
135         con->flags       = flags;
136         con->cur_idx_tmp = con->flags & CFG_PROCESS_FIRST ? 0 : 1;
137 } /* }}} */
138
139         int
140 cfg_get_context_flags(con)
141         const CFG_CONTEXT con;
142 { /* {{{ */
143         return con->flags;
144 } /* }}} */
145
146 /*
147  * Macros
148  */
149
150 #define cfg_normal_property_type(type) ((type) >= 0 && (type) < CFG_N_PROPS)
151 #define cfg_virtual_property_type(type) ((type) > CFG_N_PROPS)
152
153 /*
154  * Clear functions
155  */
156
157         int
158 cfg_clear_property(con, type)
159         const CFG_CONTEXT con;
160         enum cfg_property_type type;
161 { /* {{{ */
162         if (cfg_normal_property_type(type)) {
163                 return strdyn_array_clear(&(con->prop[type]));
164         }
165         else if (cfg_virtual_property_type(type)) {
166                 register int ret = 1;
167                 switch (type) {
168                         default: /* unknown special property; do nothing */
169                                 ret = 0;
170                                 break;
171                         case CFG_QUOTE:
172                                 ret &= cfg_clear_property(con, CFG_LINE_QUOTE);
173                                 ret &= cfg_clear_property(con, CFG_FILE_QUOTE);
174                                 break;
175                         case CFG_LINE_QUOTE:
176                                 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_PREFIX);
177                                 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_POSTFIX);
178                                 break;
179                         case CFG_FILE_QUOTE:
180                                 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_PREFIX);
181                                 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_POSTFIX);
182                                 break;
183                         case CFG_QUOTE_PREFIX:
184                                 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_PREFIX);
185                                 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_PREFIX);
186                                 break;
187                         case CFG_QUOTE_POSTFIX:
188                                 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_POSTFIX);
189                                 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_POSTFIX);
190                                 break;
191                         case CFG_MULTI_VALS_SEPARATOR:
192                                 ret &= cfg_clear_property(con, CFG_LINE_MULTI_VALS_SEPARATOR);
193                                 ret &= cfg_clear_property(con, CFG_FILE_MULTI_VALS_SEPARATOR);
194                                 break;
195                         case CFG_LINE_MULTI_VALS_SEPARATOR:
196                                 ret &= cfg_clear_property(con,
197                                                 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR);
198                                 ret &= cfg_clear_property(con,
199                                                 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR);
200                                 break;
201                         case CFG_FILE_MULTI_VALS_SEPARATOR:
202                                 ret &= cfg_clear_property(con,
203                                                 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR);
204                                 ret &= cfg_clear_property(con,
205                                                 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR);
206                                 break;
207                         case CFG_NORMAL_MULTI_VALS_SEPARATOR:
208                                 ret &= cfg_clear_property(con,
209                                                 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR);
210                                 ret &= cfg_clear_property(con,
211                                                 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR);
212                                 break;
213                         case CFG_LEFTOVER_MULTI_VALS_SEPARATOR:
214                                 ret &= cfg_clear_property(con,
215                                                 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR);
216                                 ret &= cfg_clear_property(con,
217                                                 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR);
218                                 break;
219                         case CFG_OPTION_ARG_SEPARATOR:
220                                 ret &= cfg_clear_property(con, CFG_LINE_OPTION_ARG_SEPARATOR);
221                                 ret &= cfg_clear_property(con, CFG_FILE_OPTION_ARG_SEPARATOR);
222                                 break;
223                 }
224
225                 return ret;
226         }
227
228         return 0; /* failure */
229 } /* }}} */
230
231 int
232 cfg_clear_properties(
233                 const CFG_CONTEXT con,
234                 enum cfg_property_type type, ...)
235 { /* {{{ */
236         va_list ap;
237         enum cfg_property_type tmp_type;
238         int ret = 1;
239
240         /* initialization */
241         tmp_type = type;
242         va_start(ap, type);
243
244         /* argument list must be termited with typeN = CFG_EOT */
245         while (tmp_type != CFG_EOT) {
246                 ret &= cfg_clear_property(con, tmp_type);
247                 if (!ret)
248                         break;
249                 tmp_type = va_arg(ap, enum cfg_property_type);
250         }
251
252         /* cleanup */
253         va_end(ap);
254
255         return ret;
256 } /* }}} */
257
258 /*
259  * Add functions
260  */
261
262         int
263 cfg_add_property(con, type, str)
264         const CFG_CONTEXT con;
265         enum cfg_property_type type;
266         char *str;
267 { /* {{{ */
268
269         /* TODO:
270            remove cfg_add_property_old() calls from here,
271            substitute it by new function cfg_add_properties() */
272
273         if (cfg_normal_property_type(type)) {
274                 return strdyn_array_add(&(con->prop[type]), str);
275         }
276         else if (cfg_virtual_property_type(type)) {
277                 register int ret = 1;
278                 switch (type) {
279                         default: /* unknown special property; do nothing */
280                                 ret = 0;
281                                 break;
282                         case CFG_QUOTE:
283                                 ret &= cfg_add_property(con, CFG_LINE_QUOTE, str);
284                                 ret &= cfg_add_property(con, CFG_FILE_QUOTE, str);
285                                 break;
286                         case CFG_LINE_QUOTE:
287                                 ret &= cfg_add_property(con, CFG_LINE_QUOTE_PREFIX, str);
288                                 ret &= cfg_add_property(con, CFG_LINE_QUOTE_POSTFIX, str);
289                                 break;
290                         case CFG_FILE_QUOTE:
291                                 ret &= cfg_add_property(con, CFG_FILE_QUOTE_PREFIX, str);
292                                 ret &= cfg_add_property(con, CFG_FILE_QUOTE_POSTFIX, str);
293                                 break;
294                         case CFG_QUOTE_PREFIX:
295                                 ret &= cfg_add_property(con, CFG_LINE_QUOTE_PREFIX, str);
296                                 ret &= cfg_add_property(con, CFG_FILE_QUOTE_PREFIX, str);
297                                 break;
298                         case CFG_QUOTE_POSTFIX:
299                                 ret &= cfg_add_property(con, CFG_LINE_QUOTE_POSTFIX, str);
300                                 ret &= cfg_add_property(con, CFG_FILE_QUOTE_POSTFIX, str);
301                                 break;
302                         case CFG_MULTI_VALS_SEPARATOR:
303                                 ret &= cfg_add_property(con,
304                                                 CFG_LINE_MULTI_VALS_SEPARATOR, str);
305                                 ret &= cfg_add_property(con,
306                                                 CFG_FILE_MULTI_VALS_SEPARATOR, str);
307                                 break;
308                         case CFG_LINE_MULTI_VALS_SEPARATOR:
309                                 ret &= cfg_add_property(con,
310                                                 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
311                                 ret &= cfg_add_property(con,
312                                                 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
313                                 break;
314                         case CFG_FILE_MULTI_VALS_SEPARATOR:
315                                 ret &= cfg_add_property(con,
316                                                 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
317                                 ret &= cfg_add_property(con,
318                                                 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
319                                 break;
320                         case CFG_NORMAL_MULTI_VALS_SEPARATOR:
321                                 ret &= cfg_add_property(con,
322                                                 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
323                                 ret &= cfg_add_property(con,
324                                                 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
325                                 break;
326                         case CFG_LEFTOVER_MULTI_VALS_SEPARATOR:
327                                 ret &= cfg_add_property(con,
328                                                 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
329                                 ret &= cfg_add_property(con,
330                                                 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
331                                 break;
332                         case CFG_OPTION_ARG_SEPARATOR:
333                                 ret &= cfg_add_property(con,
334                                                 CFG_LINE_OPTION_ARG_SEPARATOR, str);
335                                 ret &= cfg_add_property(con,
336                                                 CFG_FILE_OPTION_ARG_SEPARATOR, str);
337                                 break;
338                 }
339
340                 return ret;
341         }
342
343         return 0; /* failure */
344 } /* }}} */
345
346 int
347 cfg_add_properties(
348                 const CFG_CONTEXT con,
349                 enum cfg_property_type type,
350                 char *str, ...)
351 { /* {{{ */
352         va_list ap;
353         enum cfg_property_type tmp_type;
354         char * tmp_str;
355         int ret = 1;
356
357         /* initialization */
358         tmp_type = type;
359         tmp_str  = str;
360         va_start(ap, str);
361
362         /* argument list must be termited with typeN = CFG_EOT or
363          * str = NULL
364          */
365         while (tmp_type != CFG_EOT && tmp_str != NULL) {
366                 ret &= cfg_add_property(con, tmp_type, tmp_str);
367                 if (!ret)
368                         break;
369                 tmp_type = va_arg(ap, enum cfg_property_type);
370                 if (tmp_type == CFG_EOT) /* if typeN == CFG_EOT, strN may be not specified! */
371                         break;
372                 tmp_str  = va_arg(ap, char *);
373         }
374
375         /* cleanup */
376         va_end(ap);
377
378         return ret;
379 } /* }}} */
380
381 int
382 cfg_add_properties_str(
383                 const CFG_CONTEXT con,
384                 char *str,
385                 enum cfg_property_type type, ...)
386 { /* {{{ */
387         va_list ap;
388         enum cfg_property_type tmp_type;
389         int ret = 1;
390
391         /* initialization */
392         tmp_type = type;
393         va_start(ap, type);
394
395         /* argument list must be termited with typeN = CFG_EOT or */
396         if (str != NULL)
397                 while (tmp_type != CFG_EOT) {
398                         ret &= cfg_add_property(con, tmp_type, str);
399                         if (!ret)
400                                 break;
401                         tmp_type = va_arg(ap, enum cfg_property_type);
402                 }
403
404         /* cleanup */
405         va_end(ap);
406
407         return ret;
408 } /* }}} */
409
410 int
411 cfg_add_properties_type(
412                 const CFG_CONTEXT con,
413                 enum cfg_property_type type,
414                 char *str, ...)
415 { /* {{{ */
416         va_list ap;
417         char * tmp_str;
418         int ret = 1;
419
420         /* initialization */
421         tmp_str  = str;
422         va_start(ap, str);
423
424         /* argument list must be termited with str = NULL */
425         if (type != CFG_EOT)
426                 while (tmp_str != NULL) {
427                         ret &= cfg_add_property(con, type, tmp_str);
428                         if (!ret)
429                                 break;
430                         tmp_str  = va_arg(ap, char *);
431                 }
432
433         /* cleanup */
434         va_end(ap);
435
436         return ret;
437 } /* }}} */
438
439 /*
440  * Remove functions
441  */
442
443         int
444 cfg_remove_property(con, type, str)
445         const CFG_CONTEXT con;
446         enum cfg_property_type type;
447         char *str;
448 { /* {{{ */
449
450         /* TODO:
451            remove cfg_remove_property_old() calls from here,
452            substitute it by new function cfg_remove_properties() */
453
454         if (cfg_normal_property_type(type)) {
455                 return strdyn_array_remove(&(con->prop[type]), str);
456         }
457         else if (cfg_virtual_property_type(type)) {
458                 register int ret = 1;
459                 switch (type) {
460                         default: /* unknown special property; do nothing */
461                                 ret = 0;
462                                 break;
463                         case CFG_QUOTE:
464                                 ret &= cfg_remove_property(con, CFG_LINE_QUOTE, str);
465                                 ret &= cfg_remove_property(con, CFG_FILE_QUOTE, str);
466                                 break;
467                         case CFG_LINE_QUOTE:
468                                 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_PREFIX, str);
469                                 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_POSTFIX, str);
470                                 break;
471                         case CFG_FILE_QUOTE:
472                                 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_PREFIX, str);
473                                 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_POSTFIX, str);
474                                 break;
475                         case CFG_QUOTE_PREFIX:
476                                 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_PREFIX, str);
477                                 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_PREFIX, str);
478                                 break;
479                         case CFG_QUOTE_POSTFIX:
480                                 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_POSTFIX, str);
481                                 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_POSTFIX, str);
482                                 break;
483                         case CFG_MULTI_VALS_SEPARATOR:
484                                 ret &= cfg_remove_property(con,
485                                                 CFG_LINE_MULTI_VALS_SEPARATOR, str);
486                                 ret &= cfg_remove_property(con,
487                                                 CFG_FILE_MULTI_VALS_SEPARATOR, str);
488                                 break;
489                         case CFG_LINE_MULTI_VALS_SEPARATOR:
490                                 ret &= cfg_remove_property(con,
491                                                 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
492                                 ret &= cfg_remove_property(con,
493                                                 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
494                                 break;
495                         case CFG_FILE_MULTI_VALS_SEPARATOR:
496                                 ret &= cfg_remove_property(con,
497                                                 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
498                                 ret &= cfg_remove_property(con,
499                                                 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
500                                 break;
501                         case CFG_NORMAL_MULTI_VALS_SEPARATOR:
502                                 ret &= cfg_remove_property(con,
503                                                 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
504                                 ret &= cfg_remove_property(con,
505                                                 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
506                                 break;
507                         case CFG_LEFTOVER_MULTI_VALS_SEPARATOR:
508                                 ret &= cfg_remove_property(con,
509                                                 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
510                                 ret &= cfg_remove_property(con,
511                                                 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
512                                 break;
513                         case CFG_OPTION_ARG_SEPARATOR:
514                                 ret &= cfg_remove_property(con,
515                                                 CFG_LINE_OPTION_ARG_SEPARATOR, str);
516                                 ret &= cfg_remove_property(con,
517                                                 CFG_FILE_OPTION_ARG_SEPARATOR, str);
518                                 break;
519                 }
520
521                 return ret;
522         }
523
524         return 0; /* failure */
525 } /* }}} */
526
527 int
528 cfg_remove_properties( /* code same like cfg_add_properties() */
529                 const CFG_CONTEXT con,
530                 enum cfg_property_type type,
531                 char *str, ...)
532 { /* {{{ */
533         va_list ap;
534         enum cfg_property_type tmp_type;
535         char * tmp_str;
536         int ret = 1;
537
538         /* initialization */
539         tmp_type = type;
540         tmp_str  = str;
541         va_start(ap, str);
542
543         /* argument list must be termited with typeN = CFG_EOT or
544          * str = NULL
545          */
546         while (tmp_type != CFG_EOT && tmp_str != NULL) {
547                 ret &= cfg_remove_property(con, tmp_type, tmp_str);
548                 if (!ret)
549                         break;
550                 tmp_type = va_arg(ap, enum cfg_property_type);
551                 if (tmp_type == CFG_EOT) /* if typeN == CFG_EOT, strN may be not specified! */
552                         break;
553                 tmp_str  = va_arg(ap, char *);
554         }
555
556         /* cleanup */
557         va_end(ap);
558
559         return ret;
560 } /* }}} */
561
562 int
563 cfg_remove_properties_str(
564                 const CFG_CONTEXT con,
565                 char *str,
566                 enum cfg_property_type type, ...)
567 { /* {{{ */
568         va_list ap;
569         enum cfg_property_type tmp_type;
570         int ret = 1;
571
572         /* initialization */
573         tmp_type = type;
574         va_start(ap, type);
575
576         /* argument list must be termited with typeN = CFG_EOT or */
577         if (str != NULL)
578                 while (tmp_type != CFG_EOT) {
579                         ret &= cfg_remove_property(con, tmp_type, str);
580                         if (!ret)
581                                 break;
582                         tmp_type = va_arg(ap, enum cfg_property_type);
583                 }
584
585         /* cleanup */
586         va_end(ap);
587
588         return ret;
589 } /* }}} */
590
591 int
592 cfg_remove_properties_type(
593                 const CFG_CONTEXT con,
594                 enum cfg_property_type type,
595                 char *str, ...)
596 { /* {{{ */
597         va_list ap;
598         char * tmp_str;
599         int ret = 1;
600
601         /* initialization */
602         tmp_str  = str;
603         va_start(ap, str);
604
605         /* argument list must be termited with str = NULL */
606         if (type != CFG_EOT)
607                 while (tmp_str != NULL) {
608                         ret &= cfg_remove_property(con, type, tmp_str);
609                         if (!ret)
610                                 break;
611                         tmp_str  = va_arg(ap, char *);
612                 }
613
614         /* cleanup */
615         va_end(ap);
616
617         return ret;
618 } /* }}} */
619
620
621 /* Modeline for ViM {{{
622  * vim:set ts=4:
623  * vim600:fdm=marker fdl=0 fdc=0:
624  * }}} */
625