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. */
14 #include "xbt/sysdep.h"
16 #include "xbt/error.h"
17 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(config,xbt,"configuration support");
24 /* gras_cfgelm_t: the typedef corresponding to a config cell.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
30 /* Allowed type of the cell */
31 gras_cfgelm_type_t type;
35 (cannot be an union because type host uses both str and i) */
36 gras_dynar_t *content;
39 static const char *gras_cfgelm_type_name[gras_cfgelm_type_count]=
40 {"int","double","string","host"};
42 /* Internal stuff used in cache to free a cell */
43 static void gras_cfgelm_free(void *data);
45 /* Retrieve the cell we'll modify */
46 static gras_error_t gras_cfgelm_get(gras_cfg_t *cfg, const char *name,
47 gras_cfgelm_type_t type,
48 /* OUT */ gras_cfgelm_t **whereto);
50 void gras_cfg_str_free(void *d);
51 void gras_cfg_host_free(void *d);
53 void gras_cfg_str_free(void *d){
54 gras_free(*(void**)d);
56 void gras_cfg_host_free(void *d){
57 gras_host_t *h=(gras_host_t*) *(void**)d;
59 if (h->name) gras_free(h->name);
64 /*----[ Memory management ]-----------------------------------------------*/
71 * Initialise an config set
75 gras_cfg_t *gras_cfg_new(void) {
76 return (gras_cfg_t *)gras_dict_new();
82 * @whereto: the config set to be created
83 * @tocopy: the source data
88 gras_cfg_cpy(gras_cfg_t **whereto, gras_cfg_t *tocopy) {
89 gras_dict_cursor_t *cursor=NULL;
90 gras_cfgelm_t *cell=NULL;
94 gras_assert0(tocopy,"cannot copy NULL config");
96 gras_dict_foreach((gras_dict_t*)tocopy,cursor,name,cell) {
97 gras_cfg_register(*whereto, name, cell->type, cell->min, cell->max);
101 void gras_cfg_free(gras_cfg_t **cfg) {
102 gras_dict_free((gras_dict_t**)cfg);
107 * @name: The name to give to this config set
108 * @indent: what to write at the begining of each line (right number of spaces)
109 * @cfg: the config set
111 * Dumps a config set for debuging purpose
114 gras_cfg_dump(const char *name,const char *indent,gras_cfg_t *cfg) {
115 gras_dict_t *dict = (gras_dict_t*) cfg;
116 gras_dict_cursor_t *cursor=NULL;
117 gras_cfgelm_t *cell=NULL;
120 gras_error_t errcode;
128 printf("%s>> Dumping of the config set '%s':\n",indent,name);
129 gras_dict_foreach(dict,cursor,key,cell) {
131 printf("%s %s:",indent,key);
133 size = gras_dynar_length(cell->content);
134 printf("%d_to_%d_%s. Actual size=%d. List of values:\n",
135 cell->min,cell->max,gras_cfgelm_type_name[cell->type],
138 switch (cell->type) {
140 case gras_cfgelm_int:
141 for (i=0; i<size; i++) {
142 ival = gras_dynar_get_as(cell->content,i,int);
143 printf ("%s %d\n",indent,ival);
147 case gras_cfgelm_double:
148 for (i=0; i<size; i++) {
149 dval = gras_dynar_get_as(cell->content,i,double);
150 printf ("%s %f\n",indent,dval);
154 case gras_cfgelm_string:
155 for (i=0; i<size; i++) {
156 sval = gras_dynar_get_as(cell->content,i,char*);
157 printf ("%s %s\n",indent,sval);
161 case gras_cfgelm_host:
162 for (i=0; i<size; i++) {
163 hval = gras_dynar_get_as(cell->content,i,gras_host_t*);
164 printf ("%s %s:%d\n",indent,hval->name,hval->port);
169 printf("%s Invalid type!!\n",indent);
174 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
177 gras_dict_cursor_free(cursor);
184 * @data: the data to be freed (typed as void* to be usable as free funct in dict)
186 * free an config element
189 void gras_cfgelm_free(void *data) {
190 gras_cfgelm_t *c=(gras_cfgelm_t *)data;
193 gras_dynar_free(c->content);
197 /*----[ Registering stuff ]-----------------------------------------------*/
201 * @cfg: the config set
202 * @type: the type of the config element
206 * register an element within a config set
210 gras_cfg_register(gras_cfg_t *cfg,
211 const char *name, gras_cfgelm_type_t type,
214 gras_error_t errcode;
216 DEBUG4("Register cfg elm %s (%d to %d %s)",name,min,max,gras_cfgelm_type_name[type]);
217 errcode = gras_dict_get((gras_dict_t*)cfg,name,(void**)&res);
219 if (errcode == no_error) {
220 WARN1("Config elem %s registered twice.",name);
221 /* Will be removed by the insertion of the new one */
223 gras_assert_error(mismatch_error);
225 res=gras_new(gras_cfgelm_t,1);
232 case gras_cfgelm_int:
233 res->content = gras_dynar_new(sizeof(int), NULL);
236 case gras_cfgelm_double:
237 res->content = gras_dynar_new(sizeof(double), NULL);
240 case gras_cfgelm_string:
241 res->content = gras_dynar_new(sizeof(char*),&gras_cfg_str_free);
244 case gras_cfgelm_host:
245 res->content = gras_dynar_new(sizeof(gras_host_t*),&gras_cfg_host_free);
249 ERROR1("%d is an invalide type code",type);
252 gras_dict_set((gras_dict_t*)cfg,name,res,&gras_cfgelm_free);
256 * gras_cfg_unregister:
258 * @cfg: the config set
259 * @name: the name of the elem to be freed
261 * unregister an element from a config set.
262 * Note that it removes both the DTD and the actual content.
266 gras_cfg_unregister(gras_cfg_t *cfg,const char *name) {
267 return gras_dict_remove((gras_dict_t*)cfg,name);
271 * gras_cfg_register_str:
273 * @cfg: the config set
274 * @entry: a string describing the element to register
276 * Parse a string and register the stuff described.
280 gras_cfg_register_str(gras_cfg_t *cfg,const char *entry) {
281 char *entrycpy=gras_strdup(entry);
285 gras_cfgelm_type_t type;
287 tok=strchr(entrycpy, ':');
290 "Invalid config element descriptor: ",entry,
291 "; Should be <name>:<min nb>_to_<max nb>_<type>");
297 min=strtol(tok, &tok, 10);
299 ERROR1("Invalid minimum in config element descriptor %s",entry);
304 if (!strcmp(tok,"_to_")){
306 "Invalid config element descriptor: ",entry,
307 "; Should be <name>:<min nb>_to_<max nb>_<type>");
311 tok += strlen("_to_");
313 max=strtol(tok, &tok, 10);
315 ERROR1("Invalid maximum in config element descriptor %s",entry);
322 "Invalid config element descriptor: ",entry,
323 "; Should be <name>:<min nb>_to_<max nb>_<type>");
329 type<gras_cfgelm_type_count && strcmp(tok,gras_cfgelm_type_name[type]);
331 if (type == gras_cfgelm_type_count) {
333 "Invalid type in config element descriptor: ",entry,
334 "; Should be one of 'string', 'int', 'host' or 'double'.");
339 gras_cfg_register(cfg,entrycpy,type,min,max);
341 gras_free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
348 * @cfg: the config set
350 * Check the config set
354 gras_cfg_check(gras_cfg_t *cfg) {
355 gras_dict_cursor_t *cursor;
360 gras_assert0(cfg,"NULL config set.");
362 gras_dict_foreach((gras_dict_t*)cfg,cursor,name,cell) {
363 size = gras_dynar_length(cell->content);
364 if (cell->min > size) {
365 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
368 gras_cfgelm_type_name[cell->type],
370 gras_dict_cursor_free(cursor);
371 return mismatch_error;
374 if (cell->max < size) {
375 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
378 gras_cfgelm_type_name[cell->type],
380 gras_dict_cursor_free(cursor);
381 return mismatch_error;
386 gras_dict_cursor_free(cursor);
390 static gras_error_t gras_cfgelm_get(gras_cfg_t *cfg,
392 gras_cfgelm_type_t type,
393 /* OUT */ gras_cfgelm_t **whereto){
395 gras_error_t errcode = gras_dict_get((gras_dict_t*)cfg,name,
398 if (errcode == mismatch_error) {
399 ERROR1("No registered cell %s in this config set",
401 return mismatch_error;
403 if (errcode != no_error)
406 gras_assert3((*whereto)->type == type,
407 "You tried to access to the config element %s as an %s, but its type is %s.",
409 gras_cfgelm_type_name[type],
410 gras_cfgelm_type_name[(*whereto)->type]);
418 * @cfg: the config set
419 * @name: the name of the element
422 * Give the type of the config element
426 gras_cfg_get_type(gras_cfg_t *cfg, const char *name,
427 /* OUT */gras_cfgelm_type_t *type) {
430 gras_error_t errcode;
432 TRYCATCH(mismatch_error,gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell));
434 if (errcode == mismatch_error) {
435 ERROR1("Can't get the type of '%s' since this cell does not exist",
437 return mismatch_error;
445 /*----[ Setting ]---------------------------------------------------------*/
447 * gras_cfg_set_vargs():
448 * @cfg: config set to fill
449 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
451 * Add some values to the config set.
452 * @warning: if the list isn't NULL terminated, it will segfault.
455 gras_cfg_set_vargs(gras_cfg_t *cfg, va_list pa) {
459 gras_cfgelm_type_t type;
461 gras_error_t errcode;
463 while ((name=va_arg(pa,char *))) {
465 if (!gras_cfg_get_type(cfg,name,&type)) {
466 ERROR1("Can't set the property '%s' since it's not registered",name);
467 return mismatch_error;
471 case gras_cfgelm_host:
472 str = va_arg(pa, char *);
474 TRY(gras_cfg_set_host(cfg,name,str,i));
477 case gras_cfgelm_string:
478 str=va_arg(pa, char *);
479 TRY(gras_cfg_set_string(cfg, name, str));
482 case gras_cfgelm_int:
484 TRY(gras_cfg_set_int(cfg,name,i));
487 case gras_cfgelm_double:
489 TRY(gras_cfg_set_double(cfg,name,d));
493 RAISE1(unknown_error,"Config element cell %s not valid.",name);
501 * @cfg: config set to fill
502 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
504 * Add some values to the config set.
505 * @warning: if the list isn't NULL terminated, it will segfault.
507 gras_error_t gras_cfg_set(gras_cfg_t *cfg, ...) {
509 gras_error_t errcode;
512 errcode=gras_cfg_set_vargs(cfg,pa);
518 * gras_cfg_set_parse():
519 * @cfg: config set to fill
520 * @options: a string containing the content to add to the config set. This
521 * is a '\t',' ' or '\n' separated list of cells. Each individual cell is
522 * like "[name]:[value]" where [name] is the name of an already registred
523 * cell, and [value] conforms to the data type under which this cell was
526 * Add the cells described in a string to a config set.
530 gras_cfg_set_parse(gras_cfg_t *cfg, const char *options) {
536 char *optionlist_cpy;
537 char *option, *name,*val;
540 gras_error_t errcode;
543 if (!options || !strlen(options)) { /* nothing to do */
546 optionlist_cpy=gras_strdup(options);
548 DEBUG1("List to parse and set:'%s'",options);
549 option=optionlist_cpy;
550 while (1) { /* breaks in the code */
556 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
557 name,len,(long)(option-name));
560 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
561 /*fprintf(stderr,"Take %c.\n",*option);*/
564 if (option-name == len) {
565 /*fprintf(stderr,"Boundary=EOL\n");*/
566 option=NULL; /* don't do next iteration */
569 /*fprintf(stderr,"Boundary on '%c'. len=%d;option-name=%d\n",*option,len,option-name);*/
571 /* Pass the following blank chars */
573 while (option-name<(len-1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
574 /* fprintf(stderr,"Ignore a blank char.\n");*/
577 if (option-name == len-1)
578 option=NULL; /* don't do next iteration */
580 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
582 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
587 val=strchr(name,':');
589 gras_free(optionlist_cpy);
591 "Malformated option: '%s'; Should be of the form 'name:value'",
596 DEBUG2("name='%s';val='%s'",name,val);
598 errcode=gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell);
603 ERROR1("No registrated cell corresponding to '%s'.",name);
604 gras_free(optionlist_cpy);
605 return mismatch_error;
608 gras_free(optionlist_cpy);
612 switch (cell->type) {
613 case gras_cfgelm_string:
614 TRYCLEAN(gras_cfg_set_string(cfg, name, val),
615 gras_free(optionlist_cpy));
618 case gras_cfgelm_int:
619 i=strtol(val, &val, 0);
621 gras_free(optionlist_cpy);
623 "Value of option %s not valid. Should be an integer",
627 TRYCLEAN(gras_cfg_set_int(cfg,name,i),
628 gras_free(optionlist_cpy));
631 case gras_cfgelm_double:
634 gras_free(optionlist_cpy);
636 "Value of option %s not valid. Should be a double",
640 TRYCLEAN(gras_cfg_set_double(cfg,name,d),
641 gras_free(optionlist_cpy));
644 case gras_cfgelm_host:
648 gras_free(optionlist_cpy);
650 "Value of option %s not valid. Should be an host (machine:port)",
655 i=strtol(val, &val, 0);
657 gras_free(optionlist_cpy);
659 "Value of option %s not valid. Should be an host (machine:port)",
663 TRYCLEAN(gras_cfg_set_host(cfg,name,str,i),
664 gras_free(optionlist_cpy));
668 gras_free(optionlist_cpy);
669 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
673 gras_free(optionlist_cpy);
680 * @cfg: the config set
681 * @name: the name of the cell
682 * @val: the value of the cell
684 * Set the value of the cell @name in @cfg with the provided value.
687 gras_cfg_set_int(gras_cfg_t *cfg,const char*name, int val) {
689 gras_error_t errcode;
691 VERB2("Configuration setting: %s=%d",name,val);
692 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
695 gras_dynar_push(cell->content,&val);
697 gras_dynar_set(cell->content,0,&val);
703 * gras_cfg_set_double:
704 * @cfg: the config set
705 * @name: the name of the cell
706 * @val: the doule to set
708 * Set the value of the cell @name in @cfg with the provided value.
712 gras_cfg_set_double(gras_cfg_t *cfg,const char*name, double val) {
714 gras_error_t errcode;
716 VERB2("Configuration setting: %s=%f",name,val);
717 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
720 gras_dynar_push(cell->content,&val);
722 gras_dynar_set(cell->content,0,&val);
728 * gras_cfg_set_string:
730 * @cfg: the config set
731 * @name: the name of the cell
732 * @val: the value to be added
734 * Set the value of the cell @name in @cfg with the provided value.
738 gras_cfg_set_string(gras_cfg_t *cfg,const char*name, const char*val) {
740 gras_error_t errcode;
741 char *newval = gras_strdup(val);
743 VERB2("Configuration setting: %s=%s",name,val);
744 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
747 gras_dynar_push(cell->content,&newval);
749 gras_dynar_set(cell->content,0,&newval);
757 * @cfg: the config set
758 * @name: the name of the cell
760 * @port: the port number
762 * Set the value of the cell @name in @cfg with the provided value
763 * on the given @host to the given @port
767 gras_cfg_set_host(gras_cfg_t *cfg,const char*name,
768 const char *host,int port) {
770 gras_error_t errcode;
771 gras_host_t *val=gras_new(gras_host_t,1);
773 VERB3("Configuration setting: %s=%s:%d",name,host,port);
775 val->name = gras_strdup(name);
778 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
781 gras_dynar_push(cell->content,&val);
783 gras_dynar_set(cell->content,0,&val);
788 /* ---- [ Removing ] ---- */
793 * @cfg: the config set
794 * @name: the name of the cell
795 * @val: the value to be removed
797 * Remove the provided @val from the cell @name in @cfg.
799 gras_error_t gras_cfg_rm_int (gras_cfg_t *cfg,const char*name, int val) {
803 gras_error_t errcode;
805 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
807 gras_dynar_foreach(cell->content,cpt,seen) {
809 gras_dynar_cursor_rm(cell->content,&cpt);
814 ERROR2("Can't remove the value %d of config element %s: value not found.",
816 return mismatch_error;
820 * gras_cfg_rm_double:
822 * @cfg: the config set
823 * @name: the name of the cell
824 * @val: the value to be removed
826 * Remove the provided @val from the cell @name in @cfg.
829 gras_error_t gras_cfg_rm_double(gras_cfg_t *cfg,const char*name, double val) {
833 gras_error_t errcode;
835 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
837 gras_dynar_foreach(cell->content,cpt,seen) {
839 gras_dynar_cursor_rm(cell->content,&cpt);
844 ERROR2("Can't remove the value %f of config element %s: value not found.",
846 return mismatch_error;
850 * gras_cfg_rm_string:
852 * @cfg: the config set
853 * @name: the name of the cell
854 * @val: the value of the string which will be removed
856 * Remove the provided @val from the cell @name in @cfg.
859 gras_cfg_rm_string(gras_cfg_t *cfg,const char*name, const char *val) {
863 gras_error_t errcode;
865 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
867 gras_dynar_foreach(cell->content,cpt,seen) {
868 if (!strcpy(seen,val)) {
869 gras_dynar_cursor_rm(cell->content,&cpt);
874 ERROR2("Can't remove the value %s of config element %s: value not found.",
876 return mismatch_error;
882 * @cfg: the config set
883 * @name: the name of the cell
884 * @host: the hostname
885 * @port: the port number
887 * Remove the provided @host:@port from the cell @name in @cfg.
891 gras_cfg_rm_host (gras_cfg_t *cfg,const char*name, const char *host,int port) {
895 gras_error_t errcode;
897 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
899 gras_dynar_foreach(cell->content,cpt,seen) {
900 if (!strcpy(seen->name,host) && seen->port == port) {
901 gras_dynar_cursor_rm(cell->content,&cpt);
906 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
908 return mismatch_error;
916 * @cfg: the config set
917 * @name: the name of the cell
923 gras_cfg_empty(gras_cfg_t *cfg,const char*name) {
926 gras_error_t errcode;
928 TRYCATCH(mismatch_error,
929 gras_dict_get((gras_dict_t*)cfg,name,(void**)&cell));
930 if (errcode == mismatch_error) {
931 ERROR1("Can't empty '%s' since this config element does not exist",
933 return mismatch_error;
937 gras_dynar_reset(cell->content);
942 /*----[ Getting ]---------------------------------------------------------*/
946 * @cfg: the config set
947 * @name: the name of the cell
948 * @val: the wanted value
950 * Returns the first value from the config set under the given name.
951 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
954 * @warning the returned value is the actual content of the config set
957 gras_cfg_get_int (gras_cfg_t *cfg,
961 gras_error_t errcode;
963 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_int,&cell));
965 if (gras_dynar_length(cell->content) > 1) {
966 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
967 name, gras_dynar_length(cell->content));
970 *val = gras_dynar_get_as(cell->content, 0, int);
975 * gras_cfg_get_double:
976 * @cfg: the config set
977 * @name: the name of the cell
978 * @val: the wanted value
980 * Returns the first value from the config set under the given name.
981 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
984 * @warning the returned value is the actual content of the config set
988 gras_cfg_get_double(gras_cfg_t *cfg,
992 gras_error_t errcode;
994 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_double,&cell));
996 if (gras_dynar_length(cell->content) > 1) {
997 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
998 name, gras_dynar_length(cell->content));
1001 *val = gras_dynar_get_as(cell->content, 0, double);
1006 * gras_cfg_get_string:
1008 * @th: the config set
1009 * @name: the name of the cell
1010 * @val: the wanted value
1012 * Returns the first value from the config set under the given name.
1013 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
1016 * @warning the returned value is the actual content of the config set
1019 gras_error_t gras_cfg_get_string(gras_cfg_t *cfg,
1022 gras_cfgelm_t *cell;
1023 gras_error_t errcode;
1027 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_string,&cell));
1029 if (gras_dynar_length(cell->content) > 1) {
1030 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1031 name, gras_dynar_length(cell->content));
1034 *val = gras_dynar_get_as(cell->content, 0, char *);
1039 * gras_cfg_get_host:
1041 * @cfg: the config set
1042 * @name: the name of the cell
1044 * @port: the port number
1046 * Returns the first value from the config set under the given name.
1047 * If there is more than one value, it will issue a warning. Consider using gras_cfg_get_dynar()
1050 * @warning the returned value is the actual content of the config set
1053 gras_error_t gras_cfg_get_host (gras_cfg_t *cfg,
1057 gras_cfgelm_t *cell;
1058 gras_error_t errcode;
1061 TRY (gras_cfgelm_get(cfg,name,gras_cfgelm_host,&cell));
1063 if (gras_dynar_length(cell->content) > 1) {
1064 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1065 name, gras_dynar_length(cell->content));
1068 val = gras_dynar_get_as(cell->content, 0, gras_host_t*);
1076 * gras_cfg_get_dynar:
1077 * @cfg: where to search in
1078 * @name: what to search for
1081 * Get the data stored in the config bag.
1083 * @warning the returned value is the actual content of the config set
1085 gras_error_t gras_cfg_get_dynar (gras_cfg_t *cfg,
1087 gras_dynar_t **dynar) {
1088 gras_cfgelm_t *cell;
1089 gras_error_t errcode = gras_dict_get((gras_dict_t*)cfg,name,
1092 if (errcode == mismatch_error) {
1093 ERROR1("No registered cell %s in this config set",
1095 return mismatch_error;
1097 if (errcode != no_error)
1100 *dynar = cell->content;