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,gros,"configuration support");
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){
47 gras_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) gras_free(h->name);
57 /*----[ Memory management ]-----------------------------------------------*/
64 * Initialise an config set
68 void gras_cfg_new(gras_cfg_t **whereto) {
69 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;
87 gras_assert0(tocopy,"cannot copy NULL config");
89 gras_dict_foreach((gras_dict_t*)tocopy,cursor,name,cell) {
90 gras_cfg_register(*whereto, name, cell->type, cell->min, cell->max);
94 void gras_cfg_free(gras_cfg_t **cfg) {
95 gras_dict_free((gras_dict_t**)cfg);
100 * @name: The name to give to this config set
101 * @indent: what to write at the begining of each line (right number of spaces)
102 * @cfg: the config set
104 * Dumps a config set for debuging purpose
107 gras_cfg_dump(const char *name,const char *indent,gras_cfg_t *cfg) {
108 gras_dict_t *dict = (gras_dict_t*) cfg;
109 gras_dict_cursor_t *cursor=NULL;
110 gras_cfgelm_t *cell=NULL;
113 gras_error_t errcode;
121 printf("%s>> Dumping of the config set '%s':\n",indent,name);
122 gras_dict_foreach(dict,cursor,key,cell) {
124 printf("%s %s:",indent,key);
126 size = gras_dynar_length(cell->content);
127 printf("%d_to_%d_%s. Actual size=%d. List of values:\n",
128 cell->min,cell->max,gras_cfgelm_type_name[cell->type],
131 switch (cell->type) {
133 case gras_cfgelm_int:
134 for (i=0; i<size; i++) {
135 gras_dynar_get(cell->content,i,&ival);
136 printf ("%s %d\n",indent,ival);
140 case gras_cfgelm_double:
141 for (i=0; i<size; i++) {
142 gras_dynar_get(cell->content,i,&dval);
143 printf ("%s %f\n",indent,dval);
147 case gras_cfgelm_string:
148 for (i=0; i<size; i++) {
149 gras_dynar_get(cell->content,i,&sval);
150 printf ("%s %s\n",indent,sval);
154 case gras_cfgelm_host:
155 for (i=0; i<size; i++) {
156 gras_dynar_get(cell->content,i,&hval);
157 printf ("%s %s:%d\n",indent,hval.name,hval.port);
162 printf("%s Invalid type!!\n",indent);
167 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
170 gras_dict_cursor_free(cursor);
177 * @data: the data to be freed (typed as void* to be usable as free funct in dict)
179 * free an config element
182 void gras_cfgelm_free(void *data) {
183 gras_cfgelm_t *c=(gras_cfgelm_t *)data;
186 gras_dynar_free(c->content);
190 /*----[ Registering stuff ]-----------------------------------------------*/
194 * @cfg: the config set
195 * @type: the type of the config element
199 * register an element within a config set
203 gras_cfg_register(gras_cfg_t *cfg,
204 const char *name, gras_cfgelm_type_t type,
207 gras_error_t errcode;
209 DEBUG4("Register cfg elm %s (%d to %d %s)",name,min,max,gras_cfgelm_type_name[type]);
210 errcode = gras_dict_get((gras_dict_t*)cfg,name,(void**)&res);
212 if (errcode == no_error) {
213 WARN1("Config elem %s registered twice.",name);
214 /* Will be removed by the insertion of the new one */
216 gras_assert_error(mismatch_error);
218 res=gras_new(gras_cfgelm_t,1);
225 case gras_cfgelm_int:
226 gras_dynar_new(&(res->content), sizeof(int), NULL);
229 case gras_cfgelm_double:
230 gras_dynar_new(&(res->content), sizeof(double), NULL);
233 case gras_cfgelm_string:
234 gras_dynar_new(&(res->content),sizeof(char*),&gras_cfg_str_free);
237 case gras_cfgelm_host:
238 gras_dynar_new(&(res->content),sizeof(gras_host_t*),&gras_cfg_host_free);
242 ERROR1("%d is an invalide type code",type);
245 gras_dict_set((gras_dict_t*)cfg,name,res,&gras_cfgelm_free);
249 * gras_cfg_unregister:
251 * @cfg: the config set
252 * @name: the name of the elem to be freed
254 * unregister an element from a config set.
255 * Note that it removes both the DTD and the actual content.
259 gras_cfg_unregister(gras_cfg_t *cfg,const char *name) {
260 return gras_dict_remove((gras_dict_t*)cfg,name);
264 * gras_cfg_register_str:
266 * @cfg: the config set
267 * @entry: a string describing the element to register
269 * Parse a string and register the stuff described.
273 gras_cfg_register_str(gras_cfg_t *cfg,const char *entry) {
274 char *entrycpy=gras_strdup(entry);
278 gras_cfgelm_type_t type;
280 tok=strchr(entrycpy, ':');
283 "Invalid config element descriptor: ",entry,
284 "; Should be <name>:<min nb>_to_<max nb>_<type>");
290 min=strtol(tok, &tok, 10);
292 ERROR1("Invalid minimum in config element descriptor %s",entry);
297 if (!strcmp(tok,"_to_")){
299 "Invalid config element descriptor: ",entry,
300 "; Should be <name>:<min nb>_to_<max nb>_<type>");
304 tok += strlen("_to_");
306 max=strtol(tok, &tok, 10);
308 ERROR1("Invalid maximum in config element descriptor %s",entry);
315 "Invalid config element descriptor: ",entry,
316 "; Should be <name>:<min nb>_to_<max nb>_<type>");
322 type<gras_cfgelm_type_count && strcmp(tok,gras_cfgelm_type_name[type]);
324 if (type == gras_cfgelm_type_count) {
326 "Invalid type in config element descriptor: ",entry,
327 "; Should be one of 'string', 'int', 'host' or 'double'.");
332 gras_cfg_register(cfg,entrycpy,type,min,max);
334 gras_free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
341 * @cfg: the config set
343 * Check the config set
347 gras_cfg_check(gras_cfg_t *cfg) {
348 gras_dict_cursor_t *cursor;
353 gras_assert0(cfg,"NULL config set.");
355 gras_dict_foreach((gras_dict_t*)cfg,cursor,name,cell) {
356 size = gras_dynar_length(cell->content);
357 if (cell->min > size) {
358 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
361 gras_cfgelm_type_name[cell->type],
363 gras_dict_cursor_free(cursor);
364 return mismatch_error;
367 if (cell->max < size) {
368 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
371 gras_cfgelm_type_name[cell->type],
373 gras_dict_cursor_free(cursor);
374 return mismatch_error;
379 gras_dict_cursor_free(cursor);
383 static gras_error_t gras_cfgelm_get(gras_cfg_t *cfg,
385 gras_cfgelm_type_t type,
386 /* OUT */ gras_cfgelm_t **whereto){
388 gras_error_t errcode = gras_dict_get((gras_dict_t*)cfg,name,
391 if (errcode == mismatch_error) {
392 ERROR1("No registered cell %s in this config set",
394 return mismatch_error;
396 if (errcode != no_error)
399 gras_assert3((*whereto)->type == type,
400 "You tried to access to the config element %s as an %s, but its type is %s.",
402 gras_cfgelm_type_name[type],
403 gras_cfgelm_type_name[(*whereto)->type]);
411 * @cfg: the config set
412 * @name: the name of the element
415 * Give the type of the config element
419 gras_cfg_get_type(gras_cfg_t *cfg, const char *name,
420 /* OUT */gras_cfgelm_type_t *type) {
423 gras_error_t errcode;
425 TRYCATCH(mismatch_error,gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell));
427 if (errcode == mismatch_error) {
428 ERROR1("Can't get the type of '%s' since this cell does not exist",
430 return mismatch_error;
438 /*----[ Setting ]---------------------------------------------------------*/
440 * gras_cfg_set_vargs():
441 * @cfg: config set to fill
442 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
444 * Add some values to the config set.
445 * @warning: if the list isn't NULL terminated, it will segfault.
448 gras_cfg_set_vargs(gras_cfg_t *cfg, va_list pa) {
452 gras_cfgelm_type_t type;
454 gras_error_t errcode;
456 while ((name=va_arg(pa,char *))) {
458 if (!gras_cfg_get_type(cfg,name,&type)) {
459 ERROR1("Can't set the property '%s' since it's not registered",name);
460 return mismatch_error;
464 case gras_cfgelm_host:
465 str = va_arg(pa, char *);
467 TRY(gras_cfg_set_host(cfg,name,str,i));
470 case gras_cfgelm_string:
471 str=va_arg(pa, char *);
472 TRY(gras_cfg_set_string(cfg, name, str));
475 case gras_cfgelm_int:
477 TRY(gras_cfg_set_int(cfg,name,i));
480 case gras_cfgelm_double:
482 TRY(gras_cfg_set_double(cfg,name,d));
486 RAISE1(unknown_error,"Config element cell %s not valid.",name);
494 * @cfg: config set to fill
495 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
497 * Add some values to the config set.
498 * @warning: if the list isn't NULL terminated, it will segfault.
500 gras_error_t gras_cfg_set(gras_cfg_t *cfg, ...) {
502 gras_error_t errcode;
505 errcode=gras_cfg_set_vargs(cfg,pa);
511 * gras_cfg_set_parse():
512 * @cfg: config set to fill
513 * @options: a string containing the content to add to the config set. This
514 * is a '\t',' ' or '\n' separated list of cells. Each individual cell is
515 * like "[name]:[value]" where [name] is the name of an already registred
516 * cell, and [value] conforms to the data type under which this cell was
519 * Add the cells described in a string to a config set.
523 gras_cfg_set_parse(gras_cfg_t *cfg, const char *options) {
529 char *optionlist_cpy;
530 char *option, *name,*val;
533 gras_error_t errcode;
536 if (!options || !strlen(options)) { /* nothing to do */
539 optionlist_cpy=gras_strdup(options);
541 DEBUG1("List to parse and set:'%s'",options);
542 option=optionlist_cpy;
543 while (1) { /* breaks in the code */
549 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
550 name,len,(long)(option-name));
553 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
554 /*fprintf(stderr,"Take %c.\n",*option);*/
557 if (option-name == len) {
558 /*fprintf(stderr,"Boundary=EOL\n");*/
559 option=NULL; /* don't do next iteration */
562 /*fprintf(stderr,"Boundary on '%c'. len=%d;option-name=%d\n",*option,len,option-name);*/
564 /* Pass the following blank chars */
566 while (option-name<(len-1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
567 /* fprintf(stderr,"Ignore a blank char.\n");*/
570 if (option-name == len-1)
571 option=NULL; /* don't do next iteration */
573 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
575 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
580 val=strchr(name,':');
582 gras_free(optionlist_cpy);
584 "Malformated option: '%s'; Should be of the form 'name:value'",
589 DEBUG2("name='%s';val='%s'",name,val);
591 errcode=gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell);
596 ERROR1("No registrated cell corresponding to '%s'.",name);
597 gras_free(optionlist_cpy);
598 return mismatch_error;
601 gras_free(optionlist_cpy);
605 switch (cell->type) {
606 case gras_cfgelm_string:
607 TRYCLEAN(gras_cfg_set_string(cfg, name, val),
608 gras_free(optionlist_cpy));
611 case gras_cfgelm_int:
612 i=strtol(val, &val, 0);
614 gras_free(optionlist_cpy);
616 "Value of option %s not valid. Should be an integer",
620 TRYCLEAN(gras_cfg_set_int(cfg,name,i),
621 gras_free(optionlist_cpy));
624 case gras_cfgelm_double:
627 gras_free(optionlist_cpy);
629 "Value of option %s not valid. Should be a double",
633 TRYCLEAN(gras_cfg_set_double(cfg,name,d),
634 gras_free(optionlist_cpy));
637 case gras_cfgelm_host:
641 gras_free(optionlist_cpy);
643 "Value of option %s not valid. Should be an host (machine:port)",
648 i=strtol(val, &val, 0);
650 gras_free(optionlist_cpy);
652 "Value of option %s not valid. Should be an host (machine:port)",
656 TRYCLEAN(gras_cfg_set_host(cfg,name,str,i),
657 gras_free(optionlist_cpy));
661 gras_free(optionlist_cpy);
662 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
666 gras_free(optionlist_cpy);
673 * @cfg: the config set
674 * @name: the name of the cell
675 * @val: the value of the cell
677 * Set the value of the cell @name in @cfg with the provided value.
680 gras_cfg_set_int(gras_cfg_t *cfg,const char*name, int val) {
682 gras_error_t errcode;
684 VERB2("Configuration setting: %s=%d",name,val);
685 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
688 gras_dynar_push(cell->content,&val);
690 gras_dynar_set(cell->content,0,&val);
696 * gras_cfg_set_double:
697 * @cfg: the config set
698 * @name: the name of the cell
699 * @val: the doule to set
701 * Set the value of the cell @name in @cfg with the provided value.
705 gras_cfg_set_double(gras_cfg_t *cfg,const char*name, double val) {
707 gras_error_t errcode;
709 VERB2("Configuration setting: %s=%f",name,val);
710 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
713 gras_dynar_push(cell->content,&val);
715 gras_dynar_set(cell->content,0,&val);
721 * gras_cfg_set_string:
723 * @cfg: the config set
724 * @name: the name of the cell
725 * @val: the value to be added
727 * Set the value of the cell @name in @cfg with the provided value.
731 gras_cfg_set_string(gras_cfg_t *cfg,const char*name, const char*val) {
733 gras_error_t errcode;
734 char *newval = gras_strdup(val);
736 VERB2("Configuration setting: %s=%s",name,val);
737 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
740 gras_dynar_push(cell->content,&newval);
742 gras_dynar_set(cell->content,0,&newval);
750 * @cfg: the config set
751 * @name: the name of the cell
753 * @port: the port number
755 * Set the value of the cell @name in @cfg with the provided value
756 * on the given @host to the given @port
760 gras_cfg_set_host(gras_cfg_t *cfg,const char*name,
761 const char *host,int port) {
763 gras_error_t errcode;
764 gras_host_t *val=gras_new(gras_host_t,1);
766 VERB3("Configuration setting: %s=%s:%d",name,host,port);
768 val->name = gras_strdup(name);
771 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
774 gras_dynar_push(cell->content,&val);
776 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 %lu values",
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 %lu 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 %lu 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 %lu 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 = gras_dict_get((gras_dict_t*)cfg,name,
1085 if (errcode == mismatch_error) {
1086 ERROR1("No registered cell %s in this config set",
1088 return mismatch_error;
1090 if (errcode != no_error)
1093 *dynar = cell->content;