Example #1
0
/* reads and parses configuration file */
static int
process_configfile(char *configfile)
{
	debug(RPT_DEBUG, "%s()", __FUNCTION__);

	/* Read server settings*/

	if (config_read_file(configfile) != 0) {
		report(RPT_CRIT, "Could not read config file: %s", configfile);
		return -1;
	}

	if (bind_port == UNSET_INT)
		bind_port = config_get_int("Server", "Port", 0, UNSET_INT);

	if (strcmp(bind_addr, UNSET_STR) == 0)
		strncpy(bind_addr, config_get_string("Server", "Bind", 0, UNSET_STR), sizeof(bind_addr));

	if (strcmp(user, UNSET_STR) == 0)
		strncpy(user, config_get_string("Server", "User", 0, UNSET_STR), sizeof(user));

	if (default_duration == UNSET_INT) {
		default_duration = (config_get_float("Server", "WaitTime", 0, 0) * 1e6 / TIME_UNIT);
		if (default_duration == 0)
			default_duration = UNSET_INT;
		else if (default_duration * TIME_UNIT < 2e6) {
			report(RPT_WARNING, "Waittime should be at least 2 (seconds). Set to 2 seconds.");
			default_duration = 2e6 / TIME_UNIT;
		}
	}

	if (foreground_mode == UNSET_INT) {
		int fg = config_get_bool("Server", "Foreground", 0, UNSET_INT);

		if (fg != UNSET_INT)
			foreground_mode = fg;
	}

	if (rotate_server_screen == UNSET_INT) {
		rotate_server_screen = config_get_tristate("Server", "ServerScreen", 0, "blank", UNSET_INT);
	}

	if (backlight == UNSET_INT) {
		backlight = config_get_tristate("Server", "Backlight", 0, "open", UNSET_INT);
	}

	if (heartbeat == UNSET_INT) {
		heartbeat = config_get_tristate("Server", "Heartbeat", 0, "open", UNSET_INT);
	}

	if (autorotate == UNSET_INT) {
		autorotate = config_get_bool("Server", "AutoRotate", 0, DEFAULT_AUTOROTATE);
	}

	if (titlespeed == UNSET_INT) {
		int speed = config_get_int("Server", "TitleSpeed", 0, DEFAULT_TITLESPEED);

		/* set titlespeed */
		titlespeed = (speed <= TITLESPEED_NO)
			     ? TITLESPEED_NO
			     : min(speed, TITLESPEED_MAX);
	}

	if (report_dest == UNSET_INT) {
		int rs = config_get_bool("Server", "ReportToSyslog", 0, UNSET_INT);

		if (rs != UNSET_INT)
			report_dest = (rs) ? RPT_DEST_SYSLOG : RPT_DEST_STDERR;
	}
	if (report_level == UNSET_INT) {
		report_level = config_get_int("Server", "ReportLevel", 0, UNSET_INT);
	}


	/* Read drivers */

	 /* If drivers have been specified on the command line, then do not
	 * use the driver list from the config file.
	 */
	if (num_drivers == 0) {
		/* loop over all the Driver= directives to read the driver names */
		while (1) {
			const char *s = config_get_string("Server", "Driver", num_drivers, NULL);
			if (s == NULL)
				break;
			if (s[0] != '\0') {
				drivernames[num_drivers] = strdup(s);
				if (drivernames[num_drivers] == NULL) {
					report(RPT_ERR, "alloc error storing driver name: %s", s);
					exit(EXIT_FAILURE);
				}
				num_drivers++;
			}
		}
	}

	return 0;
}
/** recursively read the menu hierarchy */
MenuEntry *menu_read(MenuEntry *parent, const char *name)
{
	static int id = 0;

	if ((name != NULL) && (config_has_section(name))) {
		MenuEntry *me = calloc(1, sizeof(MenuEntry)); // auto-NULL elements

		if (me == NULL)
			return NULL;
		// set common entries
		me->id = id++;
		me->name = strdup(name);
		if (me->name == NULL) {
			//menu_free(me);
			return NULL;
		}

		me->displayname = strdup(config_get_string(name, "DisplayName", 0, name));
		if (me->displayname == NULL) {
			//menu_free(me);
			return NULL;
		}

		me->parent = parent;
		me->next = NULL;
		me->children = NULL;
		me->numChildren = 0;

		if (config_get_string(name, "Entry", 0, NULL) != NULL) {
			MenuEntry **addr = &me->children;
			const char *entryname;

			// it is a sub-menu
			me->type = MT_MENU;

			// read menu entries
			while ((entryname = config_get_string(name, "Entry", me->numChildren, NULL)) != NULL) {
				MenuEntry *entry = menu_read(me, entryname);

				if (entry == NULL) {
					//menu_free(me);
					return NULL;
				}

				me->numChildren++;

				*addr = entry;
				addr = &entry->next;
			}
		}
		else if (config_get_string(name, "Exec", 0, NULL) != NULL) {
			MenuEntry **addr = &me->children;
			const char *entryname;

			// it's a command to execute
			me->type = MT_EXEC;

			me->data.exec.command = strdup(config_get_string(name, "Exec", 0, ""));
			if (me->data.exec.command == NULL) {
				//menu_free(me);
				return NULL;
			}
			me->data.exec.feedback = config_get_bool(name, "Feedback", 0, 0);

			// try to read parameters
			while ((entryname = config_get_string(name, "Parameter", me->numChildren, NULL)) != NULL) {
				MenuEntry *entry = menu_read(me, entryname);

				if (entry == NULL) {
					//menu_free(me);
					return NULL;
				}

				me->numChildren++;

				*addr = entry;
				addr = &entry->next;
			}

			// automagically add an "Apply ?" action
			if ((me->numChildren > 0) && (addr != NULL))
				*addr = menu_read(me, NULL);
		}
		else if (config_get_string(name, "Type", 0, NULL) != NULL) {
			// it's a command parameter
			const char *type;

			type = config_get_string(name, "Type", 0, "");

			if (strcasecmp(type, "slider") == 0) {
				char buf[35];

				me->type = MT_ARG_SLIDER;

				me->data.slider.value = config_get_int(name, "Value", 0, 0);
				me->data.slider.minval = config_get_int(name, "MinValue", 0, 0);
				me->data.slider.maxval = config_get_int(name, "MaxValue", 0, 1000);

				sprintf(buf, "%d", me->data.slider.minval);
				me->data.slider.mintext = strdup(config_get_string(name, "MinText", 0, buf));
				sprintf(buf, "%d", me->data.slider.maxval);
				me->data.slider.maxtext = strdup(config_get_string(name, "MaxText", 0, buf));

				me->data.slider.stepsize = config_get_int(name, "StepSize", 0, 1);
			}
			else if (strcasecmp(type, "ring") == 0) {
				const char *tmp;
				int numStrings = 0;
				int i = 0;

				me->type = MT_ARG_RING;

				me->data.ring.value = config_get_int(name, "Value", 0, 0);
				numStrings = config_has_key(name, "String");
				me->data.ring.strings = calloc(sizeof(char *), numStrings+1);
				me->data.ring.strings[numStrings] = NULL;

				while ((tmp = config_get_string(name, "String", i, NULL)) != NULL) {
					me->data.ring.strings[i] = strdup(tmp);
					i++;
				}
				me->data.ring.strings[i] = NULL;
			}
			else if (strcasecmp(type, "numeric") == 0) {
				me->type = MT_ARG_NUMERIC;

				me->data.numeric.value = config_get_int(name, "Value", 0, 0);
				me->data.numeric.minval = config_get_int(name, "MinValue", 0, 0);
				me->data.numeric.maxval = config_get_int(name, "MaxValue", 0, 1000);
			}
			else if (strcasecmp(type, "alpha") == 0) {
				me->type = MT_ARG_ALPHA;

				me->data.alpha.value = strdup(config_get_string(name, "Value", 0, ""));
				me->data.alpha.minlen = config_get_int(name, "MinLength", 0, 0);
				me->data.alpha.maxlen = config_get_int(name, "MaxLength", 0, 100);
				me->data.alpha.allowed = strdup(config_get_string(name, "AllowedChars", 0,
										  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
			}
			else if (strcasecmp(type, "ip") == 0) {
				me->type = MT_ARG_IP;

				me->data.ip.value = strdup(config_get_string(name, "Value", 0, ""));
				me->data.ip.v6 = config_get_bool(name, "Value", 0, 0);
			}
			else if (strcasecmp(type, "checkbox") == 0) {
				const char *tmp;

				me->type = MT_ARG_CHECKBOX;

				me->data.checkbox.allow_gray = config_get_bool(name, "AllowGray", 0, 0);
				me->data.checkbox.value = (me->data.checkbox.allow_gray)
				                          ? config_get_tristate(name, "Value", 0, "gray", 0)
				                          : config_get_bool(name, "Value", 0, 0);
				// get replacement strings for different values
				tmp = config_get_string(name, "OffText", 0, NULL);
				me->data.checkbox.map[0] = (tmp != NULL) ? strdup(tmp) : NULL;
				tmp = config_get_string(name, "OnText", 0, NULL);
				me->data.checkbox.map[1] = (tmp != NULL) ? strdup(tmp) : NULL;
				tmp = config_get_string(name, "GrayText", 0, NULL);
				me->data.checkbox.map[2] = (tmp != NULL) ? strdup(tmp) : NULL;
			}
			else {
				report(RPT_DEBUG, "illegal parameter type");
				//menu_free(me);
				return NULL;
			}
		}
		else {
			report(RPT_DEBUG, "unknown menu entry type");
			//menu_free(me);
			return NULL;
		}

		return me;
	}
	else {
		/* the magic stuff: if name is NULL and parent is an EXEC entry,
		 * then generate an Action entry with the name "Apply" */
		if ((name == NULL) && (parent != NULL) && (parent->type = MT_EXEC)) {
			MenuEntry *me = calloc(1, sizeof(MenuEntry)); // auto-NULL elements

			if (me == NULL)
				return NULL;
			// set common entries
			me->id = id++;
			me->name = malloc(strlen(parent->name) + 10);
			if (me->name == NULL) {
				//menu_free(me);
				return NULL;
			}
			strcpy(me->name, "Apply_");
			strcat(me->name, parent->name);

			me->displayname = strdup("Apply!");
			if (me->displayname == NULL) {
				//menu_free(me);
				return NULL;
			}

			me->parent = parent;
			me->next = NULL;
			me->children = NULL;
			me->numChildren = 0;
			me->type = MT_ARG_ACTION | MT_AUTOMATIC;

			return me;
		}
	}

	return NULL;
}