2 * Configuration file loader
4 * $Id: load.c,v 1.1.1.1 2006/04/12 06:26:44 michaelc Exp $
27 /* Type definitions */
29 VT_INT, VT_STR, VT_BOOL, VT_ADDR, VT_PVC
40 const AtmAddr_t *addrval;
41 const InitPvc_t *init;
45 typedef struct _VarList_t {
47 struct _VarList_t *next;
50 /* Local function prototypes */
51 static void load_init0(void);
52 static void load_init1(void);
53 static void load_dump(void);
54 static void load_release(void);
56 static Var_t *find_var(const Unit_t *unit, const char *varname);
57 static void add_var(Var_t *var);
58 static void load_vars(const char *filename);
63 static const char *rcsid = "$Id: load.c,v 1.1.1.1 2006/04/12 06:26:44 michaelc Exp $";
65 static VarList_t *varlist;
66 extern char *var_file;
68 const Unit_t load_unit = {
78 /* Initialize local data */
85 /* Initialization for data that needs other units */
89 set_var_str(&load_unit, "version", rcsid);
91 * Load variables from file
97 * #S4, Join Timeout, s
102 Debug_unit(&load_unit, "Initialized.");
105 /* Dump status, local data etc. */
112 /* Release allocated memory, close files etc. */
117 LaneDestList_t *ltmp, *ltmp2;
119 Debug_unit(&load_unit, "Releasing unit");
120 for (tmp = varlist; tmp != NULL;) {
121 Debug_unit(&load_unit, "Freeing var %s/%s", tmp->var->unit->name, tmp->var->name);
122 assert(tmp->var != NULL);
123 assert(tmp->var->name != NULL);
124 varlist = varlist->next;
125 if (tmp->var->type == VT_STR){
126 assert(tmp->var->val_u.strval != NULL);
127 mem_free(&load_unit, tmp->var->val_u.strval);
129 if (tmp->var->type == VT_ADDR){
130 assert(tmp->var->val_u.addrval != NULL);
131 mem_free(&load_unit, tmp->var->val_u.addrval);
133 if (tmp->var->type == VT_PVC){
134 assert(tmp->var->val_u.init != NULL);
135 assert(tmp->var->val_u.init->pvc != NULL);
136 mem_free(&load_unit, tmp->var->val_u.init->pvc);
137 assert(tmp->var->val_u.init->address != NULL);
138 mem_free(&load_unit, tmp->var->val_u.init->address);
139 ltmp = tmp->var->val_u.init->destinations;
140 while (ltmp != NULL) {
142 assert(ltmp->addr != NULL);
143 mem_free(&load_unit, ltmp->addr);
144 mem_free(&load_unit, ltmp);
147 mem_free(&load_unit, tmp->var->val_u.init);
149 mem_free(&load_unit, tmp->var->name);
150 mem_free(&load_unit, tmp->var);
151 mem_free(&load_unit, tmp);
157 find_var(const Unit_t *unit, const char *varname)
161 assert(unit != NULL);
162 assert(unit->name != NULL);
163 assert(varname != NULL);
165 for (tmp = varlist; tmp != NULL; tmp = tmp->next) {
166 assert(tmp->var != NULL);
167 assert(tmp->var->unit != NULL);
168 assert(tmp->var->unit->name != NULL);
169 assert(tmp->var->name != NULL);
170 if (strcmp(unit->name, tmp->var->unit->name) == 0 && strcmp(tmp->var->name, varname) == 0) {
182 /* Get or initialize variable */
184 get_var_int(const Unit_t *unit, const char *varname)
188 tmp = find_var(unit, varname);
190 assert(tmp->type == VT_INT);
191 return tmp->val_u.intval;
198 const InitPvc_t *get_var_vcc(const Unit_t *unit, const char *varname)
202 tmp=find_var(unit, varname);
204 assert(tmp->type == VT_PVC);
205 return tmp->val_u.init;
212 get_var_str(const Unit_t *unit, const char *varname)
216 tmp=find_var(unit, varname);
218 assert(tmp->type == VT_STR);
219 return tmp->val_u.strval;
227 get_var_bool(const Unit_t *unit, const char *varname)
231 tmp = find_var(unit, varname);
233 assert(tmp->type == VT_BOOL);
234 return tmp->val_u.boolval;
242 get_var_addr(const Unit_t *unit, const char *varname)
246 tmp = find_var(unit, varname);
248 assert(tmp->type == VT_ADDR);
249 return tmp->val_u.addrval;
261 tmpl = (VarList_t *)mem_alloc(&load_unit, sizeof(VarList_t));
263 tmpl->next = varlist;
267 /* Set or initialize variable */
270 set_var_vcc(const Unit_t *unit, const char *varname,
271 const InitPvc_t *vcc)
274 LaneDestList_t *ltmp, *ltmp2;
276 assert(unit != NULL && unit->name != NULL && varname != NULL);
277 tmp = find_var(unit, varname);
279 tmp = (Var_t *)mem_alloc(&load_unit, sizeof(Var_t));
282 tmp->val_u.init = vcc;
286 assert(tmp->type == VT_PVC);
287 mem_free(&load_unit, tmp->name);
289 assert(tmp->val_u.init->pvc != NULL);
290 mem_free(&load_unit, tmp->val_u.init->pvc);
291 assert(tmp->val_u.init->address != NULL);
292 mem_free(&load_unit, tmp->val_u.init->address);
293 ltmp = tmp->val_u.init->destinations;
294 while(ltmp != NULL) {
296 assert(ltmp->addr != NULL);
297 mem_free(&load_unit, ltmp->addr);
298 mem_free(&load_unit, ltmp);
301 mem_free(&load_unit, tmp->val_u.init);
302 tmp->val_u.init = vcc;
307 set_var_int(const Unit_t *unit, const char *varname, int intval)
311 assert(unit != NULL && unit->name != NULL && varname != NULL);
312 tmp = find_var(unit, varname);
314 tmp = (Var_t *)mem_alloc(&load_unit, sizeof(Var_t));
317 tmp->val_u.intval = intval;
322 assert(tmp->type == VT_INT);
323 mem_free(&load_unit, tmp->name);
325 tmp->val_u.intval = intval;
330 set_var_str(const Unit_t *unit, const char *varname, const char *strval)
334 assert(unit != NULL && unit->name != NULL && varname != NULL);
335 tmp = find_var(unit, varname);
337 tmp = (Var_t *)mem_alloc(&load_unit, sizeof(Var_t));
340 tmp->val_u.strval = strval;
344 assert(tmp->type == VT_STR);
345 mem_free(&load_unit, tmp->name);
347 mem_free(&load_unit, tmp->val_u.strval);
348 tmp->val_u.strval = strval;
354 set_var_addr(const Unit_t *unit, const char *varname, const AtmAddr_t *addr)
358 assert(unit != NULL && unit->name != NULL && varname != NULL);
359 tmp = find_var(unit, varname);
361 tmp = (Var_t *)mem_alloc(&load_unit, sizeof(Var_t));
364 tmp->val_u.addrval = addr;
369 assert(tmp->type == VT_ADDR);
370 mem_free(&load_unit, tmp->name);
372 mem_free(&load_unit, tmp->val_u.addrval);
373 tmp->val_u.addrval = addr;
378 set_var_bool(const Unit_t *unit, const char *varname, Bool_t boolval)
382 assert(unit != NULL && unit->name != NULL && varname != NULL);
383 tmp = find_var(unit, varname);
385 tmp = (Var_t *)mem_alloc(&load_unit, sizeof(Var_t));
388 tmp->val_u.boolval = boolval;
393 mem_free(&load_unit, tmp->name);
395 assert(tmp->type == VT_BOOL);
396 tmp->val_u.boolval = boolval;
401 dump_vars(const Unit_t *unit)
403 const VarList_t *tmp;
404 LaneDestList_t *ltmp;
406 Debug_unit(&load_unit, "Dumping variables");
407 for (tmp = varlist; tmp != NULL; tmp = tmp->next) {
408 assert(tmp->var != NULL);
409 assert(tmp->var->unit != NULL);
410 assert(tmp->var->unit->name != NULL);
411 assert(tmp->var->name != NULL);
412 if (unit == NULL || strcmp(unit->name, tmp->var->unit->name) == 0) {
413 switch (tmp->var->type) {
415 Debug_unit(&load_unit, "%s/%s = %d", tmp->var->unit->name, tmp->var->name, tmp->var->val_u.intval);
418 Debug_unit(&load_unit, "%s/%s = \"%s\"", tmp->var->unit->name, tmp->var->name, tmp->var->val_u.strval);
421 Debug_unit(&load_unit, "%s/%s = %s", tmp->var->unit->name, tmp->var->name, tmp->var->val_u.boolval == BL_TRUE? "True" : "False" );
424 Debug_unit(&load_unit, "%s/%s =", tmp->var->unit->name, tmp->var->name);
425 dump_atmaddr(tmp->var->val_u.addrval);
428 Debug_unit(&load_unit,"%s/%s = %d,%d,%d with lecid:%d ",
429 tmp->var->unit->name,
431 tmp->var->val_u.init->pvc->port,
432 tmp->var->val_u.init->pvc->vpi,
433 tmp->var->val_u.init->pvc->vci,
434 tmp->var->val_u.init->lecid);
435 dump_atmaddr(tmp->var->val_u.init->address);
436 ltmp = tmp->var->val_u.init->destinations;
438 dump_printf(EL_CONT,"\t");
439 dump_addr(ltmp->addr);
440 dump_printf(EL_CONT,"\n");
450 load_vars(const char *file)
452 const Unit_t *curr_unit = NULL;
454 int ret = 0; /* to silence gcc 2.7.2.1 */
457 LaneDestList_t *ltmp;
460 assert(file != NULL);
461 Debug_unit(&load_unit, "Loading variables from file %s", file);
462 yyin = fopen(file, "r");
464 Debug_unit(&load_unit, "Cannot open file %s: %s", file, strerror(errno));
475 Debug_unit(&load_unit, "EOF");
478 Debug_unit(&load_unit, "Got unit %s", g_return.stringgi);
479 curr_unit = find_unit(g_return.stringgi);
480 if (curr_unit == NULL) {
481 Debug_unit(&load_unit, "Unknown unit %s", g_return.stringgi);
483 Debug_unit(&load_unit, "Got unit %s", g_return.stringgi);
484 mem_free(&load_unit,g_return.stringgi);
487 varname = g_return.stringgi;
488 Debug_unit(&load_unit, "Got variable name %s", varname);
492 Debug_unit(&load_unit, "Variable is string: %s", g_return.stringgi);
493 set_var_str(curr_unit, varname, g_return.stringgi);
496 Debug_unit(&load_unit, "Variable is boolean: %s",
497 g_return.bool==BL_TRUE?"True":"False");
498 set_var_bool(curr_unit, varname, g_return.bool);
501 Debug_unit(&load_unit, "Variable is integer: %d", g_return.intti);
502 set_var_int(curr_unit, varname, g_return.intti);
505 Debug_unit(&load_unit, "Variable is atmaddress ");
506 dump_atmaddr(g_return.atmaddress);
507 set_var_addr(curr_unit, varname, g_return.atmaddress);
510 Debug_unit(&load_unit, "Invalid variable value for %s", varname);
511 mem_free(&load_unit, g_return.destaddr);
514 Debug_unit(&load_unit, "Invalid variable value for %s", varname);
515 mem_free(&load_unit, g_return.stringgi);
518 Debug_unit(&load_unit, "Variable is vcc");
519 pvc = (InitPvc_t *)mem_alloc(curr_unit, sizeof(InitPvc_t));
520 pvc->pvc = (LaneVcc_t *)mem_alloc(curr_unit, sizeof(LaneVcc_t));
521 pvc->pvc->port = g_return.vcc.port;
522 pvc->pvc->vpi = g_return.vcc.vpi;
523 pvc->pvc->vci = g_return.vcc.vci;
526 pvc->destinations = NULL;
528 if (ret != ATMADDRESS) {
529 Debug_unit(&load_unit, "Invalid atm_address for pvc %d,%d,%d",
530 pvc->pvc->port, pvc->pvc->vpi, pvc->pvc->vci);
535 mem_free(&load_unit, g_return.stringgi);
538 mem_free(&load_unit, g_return.destaddr);
542 pvc->address = g_return.atmaddress;
545 if (ret != INTEGER) {
546 Debug_unit(&load_unit, "Invalid lecid for pvc %d,%d,%d\n",
547 pvc->pvc->port,pvc->pvc->vpi,pvc->pvc->vci);
552 mem_free(&load_unit, g_return.stringgi);
555 mem_free(&load_unit, g_return.destaddr);
558 mem_free(&load_unit, g_return.atmaddress);
562 pvc->lecid = g_return.intti;
564 while((ret=yylex())==LANEDEST) {
565 ltmp=(LaneDestList_t *)mem_alloc(&load_unit, sizeof(LaneDestList_t));
566 ltmp->addr = g_return.destaddr;
567 ltmp->next = pvc->destinations;
568 pvc->destinations = ltmp;
571 set_var_vcc(curr_unit, varname, pvc);
574 Debug_unit(&load_unit, "Invalid variable value for %s", varname);
579 Debug_unit(&load_unit,"Invalid string placement %s",g_return.stringgi);
580 mem_free(&load_unit, g_return.stringgi);
583 Debug_unit(&load_unit, "Invalid atm address placement");
584 mem_free(&load_unit, g_return.atmaddress);
587 Debug_unit(&load_unit, "Invalid lane destination placement");
588 mem_free(&load_unit, g_return.destaddr);
591 Debug_unit(&load_unit, "Invalid integer placement");
594 Debug_unit(&load_unit, "Invalid input");
597 } while (ret != END);
598 if (fclose(yyin) != 0) {
599 Debug_unit(&load_unit, "Cannot close file %s: %s", file, strerror(errno));