3 /* config - Dictionnary where the type of each cell is provided. */
5 /* This is useful to build named structs, like option or property sets. */
7 /* Authors: Martin Quinson */
8 /* Copyright (C) 2001,2002,2003,2004 the OURAGAN project. */
10 /* This program is free software; you can redistribute it and/or modify it
11 under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "gras_private.h" /* prototypes of this module */
15 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(config,tbx);
17 /* gras_cfgelm_t: the typedef corresponding to a config cell.
19 Both data and DTD are mixed, but fixing it now would prevent me to ever
23 /* Allowed type of the cell */
24 gras_cfgelm_type_t type;
28 (cannot be an union because type host uses both str and i) */
29 gras_dynar_t *content;
32 static const char *gras_cfgelm_type_name[gras_cfgelm_type_count]=
33 {"int","double","string","host"};
35 /* Internal stuff used in cache to free a cell */
36 static void gras_cfgelm_free(void *data);
38 /* Retrieve the cell we'll modify */
39 static gras_error_t gras_cfgelm_get(gras_cfg_t *cfg, const char *name,
40 gras_cfgelm_type_t type,
41 /* OUT */ gras_cfgelm_t **whereto);
43 void gras_cfg_str_free(void *d);
44 void gras_cfg_host_free(void *d);
46 void gras_cfg_str_free(void *d){
49 void gras_cfg_host_free(void *d){
50 gras_host_t *h=(gras_host_t*) *(void**)d;
52 if (h->name) free(h->name);
57 /*----[ Memory management ]-----------------------------------------------*/
64 * Initialise an config set
68 gras_error_t gras_cfg_new(gras_cfg_t **whereto) {
69 return gras_dict_new((gras_dict_t**)whereto);
75 * @whereto: the config set to be created
76 * @tocopy: the source data
81 gras_cfg_cpy(gras_cfg_t **whereto, gras_cfg_t *tocopy) {
82 gras_dict_cursor_t *cursor=NULL;
83 gras_cfgelm_t *cell=NULL;
88 gras_assert0(tocopy,"cannot copy NULL config");
90 gras_dict_foreach((gras_dict_t*)tocopy,cursor,name,cell) {
91 if ((errcode=gras_cfg_register(*whereto, name, cell->type,
92 cell->min, cell->max)) != no_error) {
93 if (cursor) gras_dict_cursor_free(cursor);
94 if (*whereto) gras_cfg_free(whereto);
102 void gras_cfg_free(gras_cfg_t **cfg) {
103 gras_dict_free((gras_dict_t**)cfg);
108 * @name: The name to give to this config set
109 * @indent: what to write at the begining of each line (right number of spaces)
110 * @cfg: the config set
112 * Dumps a config set for debuging purpose
115 gras_cfg_dump(const char *name,const char *indent,gras_cfg_t *cfg) {
116 gras_dict_t *dict = (gras_dict_t*) cfg;
117 gras_dict_cursor_t *cursor=NULL;
118 gras_cfgelm_t *cell=NULL;
121 gras_error_t errcode;
129 printf("%s>> Dumping of the config set '%s':\n",indent,name);
130 gras_dict_foreach(dict,cursor,key,cell) {
131 if ((errcode=gras_dict_cursor_get_key(cursor,(char**)&name)) != no_error ||
132 (errcode=gras_dict_cursor_get_data(cursor,(void**)&cell)) != no_error) {
133 gras_dict_cursor_free(cursor);
137 printf("%s %s:",indent,key);
140 size = gras_dynar_length(cell->content);
141 printf("%d_to_%d_%s. Actual size=%d. List of values:\n",
142 cell->min,cell->max,gras_cfgelm_type_name[cell->type],
145 switch (cell->type) {
146 case gras_cfgelm_int:
147 for (i=0; i<size; i++) {
148 gras_dynar_get(cell->content,i,&ival);
149 printf ("%s %d\n",indent,ival);
153 case gras_cfgelm_double:
154 for (i=0; i<size; i++) {
155 gras_dynar_get(cell->content,i,&dval);
156 printf ("%s %f\n",indent,dval);
160 case gras_cfgelm_string:
161 for (i=0; i<size; i++) {
162 gras_dynar_get(cell->content,i,&sval);
163 printf ("%s %s\n",indent,sval);
167 case gras_cfgelm_host:
168 for (i=0; i<size; i++) {
169 gras_dynar_get(cell->content,i,&hval);
170 printf ("%s %s:%d\n",indent,hval.name,hval.port);
175 printf("%s Invalid type!!\n",indent);
180 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
182 gras_dict_cursor_free(cursor);
189 * @data: the data to be freed (typed as void* to be usable as free funct in dict)
191 * free an config element
194 void gras_cfgelm_free(void *data) {
195 gras_cfgelm_t *c=(gras_cfgelm_t *)data;
198 gras_dynar_free(c->content);
202 /*----[ Registering stuff ]-----------------------------------------------*/
206 * @cfg: the config set
207 * @type: the type of the config element
211 * register an element within a config set
215 gras_cfg_register(gras_cfg_t *cfg,
216 const char *name, gras_cfgelm_type_t type,
219 gras_error_t errcode;
221 TRYCATCH(mismatch_error,gras_dict_get((gras_dict_t*)cfg,name,(void**)&res));
223 if (errcode != mismatch_error) {
224 WARN1("Config elem %s registered twice.",name);
225 /* Will be removed by the insertion of the new one */
228 res=malloc(sizeof(gras_cfgelm_t));
237 case gras_cfgelm_int:
238 TRY(gras_dynar_new(&(res->content), sizeof(int), NULL));
241 case gras_cfgelm_double:
242 TRY(gras_dynar_new(&(res->content), sizeof(double), NULL));
245 case gras_cfgelm_string:
246 TRY(gras_dynar_new(&(res->content),sizeof(char*),&gras_cfg_str_free));
249 case gras_cfgelm_host:
250 TRY(gras_dynar_new(&(res->content),sizeof(gras_host_t*),&gras_cfg_host_free));
254 ERROR1("%d is an invalide type code",type);
257 return gras_dict_set((gras_dict_t*)cfg,name,res,&gras_cfgelm_free);
261 * gras_cfg_unregister:
263 * @cfg: the config set
264 * @name: the name of the elem to be freed
266 * unregister an element from a config set.
267 * Note that it removes both the DTD and the actual content.
271 gras_cfg_unregister(gras_cfg_t *cfg,const char *name) {
272 return gras_dict_remove((gras_dict_t*)cfg,name);
276 * gras_cfg_register_str:
278 * @cfg: the config set
279 * @entry: a string describing the element to register
281 * Parse a string and register the stuff described.
285 gras_cfg_register_str(gras_cfg_t *cfg,const char *entry) {
286 char *entrycpy=strdup(entry);
290 gras_cfgelm_type_t type;
292 gras_error_t errcode;
294 tok=strchr(entrycpy, ':');
297 "Invalid config element descriptor: ",entry,
298 "; Should be <name>:<min nb>_to_<max nb>_<type>");
304 min=strtol(tok, &tok, 10);
306 ERROR1("Invalid minimum in config element descriptor %s",entry);
311 if (!strcmp(tok,"_to_")){
313 "Invalid config element descriptor: ",entry,
314 "; Should be <name>:<min nb>_to_<max nb>_<type>");
318 tok += strlen("_to_");
320 max=strtol(tok, &tok, 10);
322 ERROR1("Invalid maximum in config element descriptor %s",entry);
329 "Invalid config element descriptor: ",entry,
330 "; Should be <name>:<min nb>_to_<max nb>_<type>");
336 type<gras_cfgelm_type_count && strcmp(tok,gras_cfgelm_type_name[type]);
338 if (type == gras_cfgelm_type_count) {
340 "Invalid type in config element descriptor: ",entry,
341 "; Should be one of 'string', 'int', 'host' or 'double'.");
346 TRYCLEAN(gras_cfg_register(cfg,entrycpy,type,min,max),
349 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
356 * @cfg: the config set
358 * Check the config set
362 gras_cfg_check(gras_cfg_t *cfg) {
363 gras_dict_cursor_t *cursor;
368 gras_assert0(cfg,"NULL config set.");
370 gras_dict_foreach((gras_dict_t*)cfg,cursor,name,cell) {
371 size = gras_dynar_length(cell->content);
372 if (cell->min > size) {
373 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
376 gras_cfgelm_type_name[cell->type],
378 return mismatch_error;
381 if (cell->max < size) {
382 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
385 gras_cfgelm_type_name[cell->type],
387 return mismatch_error;
395 static gras_error_t gras_cfgelm_get(gras_cfg_t *cfg,
397 gras_cfgelm_type_t type,
398 /* OUT */ gras_cfgelm_t **whereto){
399 gras_error_t errcode;
401 TRYCATCH(mismatch_error,gras_dict_get((gras_dict_t*)cfg,name,(void**)whereto));
403 if (errcode == mismatch_error) {
404 ERROR1("No registered cell %s in this config set",
406 return mismatch_error;
409 gras_assert3((*whereto)->type == type,
410 "You tried to access to the config element %s as an %s, but its type is %s.",
412 gras_cfgelm_type_name[type],
413 gras_cfgelm_type_name[(*whereto)->type]);
421 * @cfg: the config set
422 * @name: the name of the element
425 * Give the type of the config element
429 gras_cfg_get_type(gras_cfg_t *cfg, const char *name,
430 /* OUT */gras_cfgelm_type_t *type) {
433 gras_error_t errcode;
435 TRYCATCH(mismatch_error,gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell));
437 if (errcode == mismatch_error) {
438 ERROR1("Can't get the type of '%s' since this cell does not exist",
440 return mismatch_error;
448 /*----[ Setting ]---------------------------------------------------------*/
450 * gras_cfg_set_vargs():
451 * @cfg: config set to fill
452 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
454 * Add some values to the config set.
455 * @warning: if the list isn't NULL terminated, it will segfault.
458 gras_cfg_set_vargs(gras_cfg_t *cfg, va_list pa) {
462 gras_cfgelm_type_t type;
464 gras_error_t errcode;
466 while ((name=va_arg(pa,char *))) {
468 if (!gras_cfg_get_type(cfg,name,&type)) {
469 ERROR1("Can't set the property '%s' since it's not registered",name);
470 return mismatch_error;
474 case gras_cfgelm_host:
475 str = va_arg(pa, char *);
477 TRY(gras_cfg_set_host(cfg,name,str,i));
480 case gras_cfgelm_string:
481 str=va_arg(pa, char *);
482 TRY(gras_cfg_set_string(cfg, name, str));
485 case gras_cfgelm_int:
487 TRY(gras_cfg_set_int(cfg,name,i));
490 case gras_cfgelm_double:
492 TRY(gras_cfg_set_double(cfg,name,d));
496 RAISE1(unknown_error,"Config element cell %s not valid.",name);
504 * @cfg: config set to fill
505 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
507 * Add some values to the config set.
508 * @warning: if the list isn't NULL terminated, it will segfault.
510 gras_error_t gras_cfg_set(gras_cfg_t *cfg, ...) {
512 gras_error_t errcode;
515 errcode=gras_cfg_set_vargs(cfg,pa);
521 * gras_cfg_set_parse():
522 * @cfg: config set to fill
523 * @options: a string containing the content to add to the config set. This
524 * is a '\t',' ' or '\n' separated list of cells. Each individual cell is
525 * like "[name]:[value]" where [name] is the name of an already registred
526 * cell, and [value] conforms to the data type under which this cell was
529 * Add the cells described in a string to a config set.
533 gras_cfg_set_parse(gras_cfg_t *cfg, const char *options) {
539 char *optionlist_cpy;
540 char *option, *name,*val;
543 gras_error_t errcode;
545 if (!options || !strlen(options)) { /* nothing to do */
548 optionlist_cpy=strdup(options);
550 DEBUG1("Options list='%s'",options);
551 option=optionlist_cpy;
552 while (1) { /* breaks in the code */
558 DEBUG3("Parse list '%s'. len=%d; option-name=%d",name,len,option-name);
561 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
562 //fprintf(stderr,"Take %c.\n",*option);
565 if (option-name == len) {
566 //fprintf(stderr,"Boundary=EOL\n");
567 option=NULL; /* don't do next iteration */
570 //fprintf(stderr,"Boundary on '%c'. len=%d;option-name=%d\n",*option,len,option-name);
572 /* Pass the following blank chars */
574 while (option-name<(len-1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
575 // fprintf(stderr,"Ignore a blank char.\n");
578 if (option-name == len-1)
579 option=NULL; /* don't do next iteration */
581 DEBUG2("this='%s';rest='%s'",name,option);
583 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
588 val=strchr(name,':');
590 free(optionlist_cpy);
592 "Malformated option: '%s'; Should be of the form 'name:value'",
597 DEBUG2("name='%s';val='%s'",name,val);
599 errcode=gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell);
604 ERROR1("No registrated cell corresponding to '%s'.",name);
605 free(optionlist_cpy);
606 return mismatch_error;
609 free(optionlist_cpy);
613 switch (cell->type) {
614 case gras_cfgelm_string:
615 TRYCLEAN(gras_cfg_set_string(cfg, name, val),
616 free(optionlist_cpy));
619 case gras_cfgelm_int:
620 i=strtol(val, &val, 0);
622 free(optionlist_cpy);
624 "Value of option %s not valid. Should be an integer",
628 TRYCLEAN(gras_cfg_set_int(cfg,name,i),
629 free(optionlist_cpy));
632 case gras_cfgelm_double:
635 free(optionlist_cpy);
637 "Value of option %s not valid. Should be a double",
641 TRYCLEAN(gras_cfg_set_double(cfg,name,d),
642 free(optionlist_cpy));
645 case gras_cfgelm_host:
649 free(optionlist_cpy);
651 "Value of option %s not valid. Should be an host (machine:port)",
656 i=strtol(val, &val, 0);
658 free(optionlist_cpy);
660 "Value of option %s not valid. Should be an host (machine:port)",
664 TRYCLEAN(gras_cfg_set_host(cfg,name,str,i),
665 free(optionlist_cpy));
669 free(optionlist_cpy);
670 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
674 free(optionlist_cpy);
681 * @cfg: the config set
682 * @name: the name of the cell
683 * @val: the value of the cell
685 * Set the value of the cell @name in @cfg with the provided value.
688 gras_cfg_set_int(gras_cfg_t *cfg,const char*name, int val) {
690 gras_error_t errcode;
692 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
695 return gras_dynar_push(cell->content,&val);
697 return gras_dynar_set(cell->content,0,&val);
702 * gras_cfg_set_double:
703 * @cfg: the config set
704 * @name: the name of the cell
705 * @val: the doule to set
707 * Set the value of the cell @name in @cfg with the provided value.
711 gras_cfg_set_double(gras_cfg_t *cfg,const char*name, double val) {
713 gras_error_t errcode;
715 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
718 return gras_dynar_push(cell->content,&val);
720 return gras_dynar_set(cell->content,0,&val);
725 * gras_cfg_set_string:
727 * @cfg: the config set
728 * @name: the name of the cell
729 * @val: the value to be added
731 * Set the value of the cell @name in @cfg with the provided value.
735 gras_cfg_set_string(gras_cfg_t *cfg,const char*name, const char*val) {
737 gras_error_t errcode;
739 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
742 return gras_dynar_push(cell->content,&val);
744 return gras_dynar_set(cell->content,0,&val);
751 * @cfg: the config set
752 * @name: the name of the cell
754 * @port: the port number
756 * Set the value of the cell @name in @cfg with the provided value
757 * on the given @host to the given @port
761 gras_cfg_set_host(gras_cfg_t *cfg,const char*name,
762 const char *host,int port) {
764 gras_error_t errcode;
765 gras_host_t *val=malloc(sizeof(gras_host_t));
769 val->name = strdup(name);
772 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
775 return gras_dynar_push(cell->content,&val);
777 return gras_dynar_set(cell->content,0,&val);
781 /* ---- [ Removing ] ---- */
786 * @cfg: the config set
787 * @name: the name of the cell
788 * @val: the value to be removed
790 * Remove the provided @val from the cell @name in @cfg.
792 gras_error_t gras_cfg_rm_int (gras_cfg_t *cfg,const char*name, int val) {
796 gras_error_t errcode;
798 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
800 gras_dynar_foreach(cell->content,cpt,seen) {
802 gras_dynar_cursor_rm(cell->content,&cpt);
807 ERROR2("Can't remove the value %d of config element %s: value not found.",
809 return mismatch_error;
813 * gras_cfg_rm_double:
815 * @cfg: the config set
816 * @name: the name of the cell
817 * @val: the value to be removed
819 * Remove the provided @val from the cell @name in @cfg.
822 gras_error_t gras_cfg_rm_double(gras_cfg_t *cfg,const char*name, double val) {
826 gras_error_t errcode;
828 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
830 gras_dynar_foreach(cell->content,cpt,seen) {
832 gras_dynar_cursor_rm(cell->content,&cpt);
837 ERROR2("Can't remove the value %f of config element %s: value not found.",
839 return mismatch_error;
843 * gras_cfg_rm_string:
845 * @cfg: the config set
846 * @name: the name of the cell
847 * @val: the value of the string which will be removed
849 * Remove the provided @val from the cell @name in @cfg.
852 gras_cfg_rm_string(gras_cfg_t *cfg,const char*name, const char *val) {
856 gras_error_t errcode;
858 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
860 gras_dynar_foreach(cell->content,cpt,seen) {
861 if (!strcpy(seen,val)) {
862 gras_dynar_cursor_rm(cell->content,&cpt);
867 ERROR2("Can't remove the value %s of config element %s: value not found.",
869 return mismatch_error;
875 * @cfg: the config set
876 * @name: the name of the cell
877 * @host: the hostname
878 * @port: the port number
880 * Remove the provided @host:@port from the cell @name in @cfg.
884 gras_cfg_rm_host (gras_cfg_t *cfg,const char*name, const char *host,int port) {
888 gras_error_t errcode;
890 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
892 gras_dynar_foreach(cell->content,cpt,seen) {
893 if (!strcpy(seen->name,host) && seen->port == port) {
894 gras_dynar_cursor_rm(cell->content,&cpt);
899 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
901 return mismatch_error;
909 * @cfg: the config set
910 * @name: the name of the cell
916 gras_cfg_empty(gras_cfg_t *cfg,const char*name) {
919 gras_error_t errcode;
921 TRYCATCH(mismatch_error,
922 gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell));
923 if (errcode == mismatch_error) {
924 ERROR1("Can't empty '%s' since this config element does not exist",
926 return mismatch_error;
930 gras_dynar_reset(cell->content);
935 /*----[ Getting ]---------------------------------------------------------*/
939 * @cfg: the config set
940 * @name: the name of the cell
941 * @val: the wanted value
943 * Returns the first value from the config set under the given name.
944 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
947 * @warning the returned value is the actual content of the config set
950 gras_cfg_get_int (gras_cfg_t *cfg,
954 gras_error_t errcode;
956 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
958 if (gras_dynar_length(cell->content) > 1) {
959 WARN2("You asked for the first value of the config element '%s', but there is %d values\n",
960 name, gras_dynar_length(cell->content));
963 gras_dynar_get(cell->content, 0, (void*)val);
968 * gras_cfg_get_double:
969 * @cfg: the config set
970 * @name: the name of the cell
971 * @val: the wanted value
973 * Returns the first value from the config set under the given name.
974 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
977 * @warning the returned value is the actual content of the config set
981 gras_cfg_get_double(gras_cfg_t *cfg,
985 gras_error_t errcode;
987 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
989 if (gras_dynar_length(cell->content) > 1) {
990 WARN2("You asked for the first value of the config element '%s', but there is %d values\n",
991 name, gras_dynar_length(cell->content));
994 gras_dynar_get(cell->content, 0, (void*)val);
999 * gras_cfg_get_string:
1001 * @th: the config set
1002 * @name: the name of the cell
1003 * @val: the wanted value
1005 * Returns the first value from the config set under the given name.
1006 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
1009 * @warning the returned value is the actual content of the config set
1012 gras_error_t gras_cfg_get_string(gras_cfg_t *cfg,
1015 gras_cfgelm_t *cell;
1016 gras_error_t errcode;
1020 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
1022 if (gras_dynar_length(cell->content) > 1) {
1023 WARN2("You asked for the first value of the config element '%s', but there is %d values\n",
1024 name, gras_dynar_length(cell->content));
1027 gras_dynar_get(cell->content, 0, (void*)val);
1032 * gras_cfg_get_host:
1034 * @cfg: the config set
1035 * @name: the name of the cell
1037 * @port: the port number
1039 * Returns the first value from the config set under the given name.
1040 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
1043 * @warning the returned value is the actual content of the config set
1046 gras_error_t gras_cfg_get_host (gras_cfg_t *cfg,
1050 gras_cfgelm_t *cell;
1051 gras_error_t errcode;
1054 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
1056 if (gras_dynar_length(cell->content) > 1) {
1057 WARN2("You asked for the first value of the config element '%s', but there is %d values\n",
1058 name, gras_dynar_length(cell->content));
1061 gras_dynar_get(cell->content, 0, (void*)val);
1069 * gras_cfg_get_dynar:
1070 * @cfg: where to search in
1071 * @name: what to search for
1074 * Get the data stored in the config bag.
1076 * @warning the returned value is the actual content of the config set
1078 gras_error_t gras_cfg_get_dynar (gras_cfg_t *cfg,
1080 gras_dynar_t **dynar) {
1081 gras_cfgelm_t *cell;
1082 gras_error_t errcode;
1084 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
1085 *dynar = cell->content;