Esempio n. 1
0
/*
 * Function: xy_rc_init
 *
 * Initializes the configuration.
 */
static CONFIG * xy_rc_init() {
    CONFIG *ret = NULL;

    char *rcpath = rc_path();

    struct stat *st = malloc(sizeof(struct stat));;

    if (stat(rcpath, st) != 0) {
        write_default_config();
    }

    log_info(globals->log, READING_CONFIGURATION_MSG);
    ret = config_init();
    free(st);
    free(rcpath);
    return ret;
}
/** read the command-line options
 *
 * \return length of modified argv
 *
 * \todo possibly maintain a list of dirs to search??
 */
int
setup_options(int argc, char *argv[])
{
	char *str = NULL;
	int pos, i;

	/* first set up defaults */
	for (i = 0; i < (int) ARRAY_LENGTH(env_vars); ++i)
	{
		if ((str = getenv(env_vars[i].name)))
			*env_vars[i].dest = xstrdup(str);
		else if (strcmp(env_vars[i].name, ENVIRON_SAVEDIR) == 0
			&& (str = get_homedir()))
		{
			size_t len = strlen(str) + strlen(PACKAGE_TARNAME) + 3;

			*env_vars[i].dest = xmalloc(len);
			sprintf(*env_vars[i].dest, "%s/.%s", str, PACKAGE_TARNAME);
			free(str);
		}
		else
			*env_vars[i].dest = xstrdup(env_vars[i].def_val);
	}

	/* set up default values */
	options.want_audio = 1;
	options.want_intro = 1;
	options.want_cheats = 0;
	options.want_fullscreen = 0;
	options.want_debug = 0;
	options.feat_shorter_advanced_training = 0;
	options.feat_random_nauts =0;    //Naut Randomize, Nikakd, 10/8/10
	options.feat_compat_nauts =10;   //Naut Compatibility, Nikakd, 10/8/10
	options.feat_no_cTraining =1;    //No Capsule Training, Nikakd, 10/8/10
	options.feat_no_backup =1;       //No Backup crew required -Leon
	options.cheat_no_damage=0;       //Damaged Equipment Cheat, Nikakd, 10/8/10
	options.feat_random_eq=0;
	options.feat_eq_new_name =0;
	options.cheat_altasOnMoon=0;
	options.cheat_addMaxS=1;
	options.boosterSafety=0;

	fixpath_options();

	/* now try to read config file, if it exists */
	if (read_config_file() < 0)
		/* if not, then write default config template */
		write_default_config();

	/* first pass: command line options */
	for (pos = 1; pos < argc; ++pos)
	{
		str = argv[pos];

		if (str[0] != '-')
			continue;

		if (!str[1])
			continue;

		if (strcmp(str, "--") == 0)
		{
			shift_argv(argv + pos, argc - pos, 1);
			argc--;
			break;
		}

		/* check what option matches */
		if (strcmp(str, "-h") == 0)
			usage(0);
		else if (strcmp(str, "-i") == 0)
			options.want_intro = 0;
		else if (strcmp(str, "-n") == 0)
			options.want_cheats = 1;
		else if (strcmp(str, "-a") == 0)
			options.want_audio = 0;
		else if (strcmp(str, "-f") == 0)
			options.want_fullscreen = 1;
		else if (strcmp(str, "-v") == 0)
			options.want_debug++;
		else
		{
			ERROR2("unknown option %s", str);
			usage(1);
		}

		shift_argv(argv + pos, argc - pos, 1);
		argc--;
		pos--; /* for loop will advance it again */
	}

	/* second pass: variable assignments */
	for (pos = 1; pos < argc; ++pos)
	{
		/** \todo should use PATH_MAX or something similar here */
		char name[32 + 1], *value;
		int offset = 0;
		int fields = 0;

		fields = sscanf(argv[pos], "%32[A-Z_]=%n", name, &offset);

		/* it is unclear whether %n increments return value */
		if (fields < 1)
			continue;

		value = argv[pos] + offset;

		for (i = 0; i < (int) ARRAY_LENGTH(env_vars); ++i)
		{
			if (strcmp(name, env_vars[i].name) == 0)
			{
				free(*env_vars[i].dest);
				*env_vars[i].dest = xstrdup(value);
				break;
			}
		}

		if (i == (int) ARRAY_LENGTH(env_vars))
			WARNING2("unsupported command line variable `%s'", name);

		/* remove matched string from argv */
		shift_argv(argv + pos, argc - pos, 1);

		/*
		 * now we have one fewer arg, pos points to the next arg,
		 * keep it this way after incrementing it on top of the loop
		 */
		pos--;
		argc--;
	}

	fixpath_options();

	return argc;
}
Esempio n. 3
0
static void read_config ()
{
  // Set default options.
  options.vim_mode = FALSE;
  options.box_width = 200;
  options.box_height = 40;
  options.colorize = TRUE;
  options.color_offset = 0;
  options.show_icons = TRUE;
  options.show_desktop = TRUE;
  options.show_titles = TRUE;
  options.icon_size = 16;
  options.font = g_strdup ("Sans 10");
  options.read_stdin = FALSE;
  options.screenshot = FALSE;
  options.screenshot_offset_x = 0;
  options.screenshot_offset_y = 0;

  gchar *filename = g_strjoin ("/", g_get_user_config_dir (), "xwinmosaic/config", NULL);

  GError *error = NULL;
  GKeyFile *config = g_key_file_new ();

  if (!g_key_file_load_from_file (config, filename, 0, &error)) {
    write_default_config ();
    return;
  }

  const gchar *group = "default";
  if (g_key_file_has_group (config, group)) {
    if (g_key_file_has_key (config, group, "vim_mode", &error))
      options.vim_mode = g_key_file_get_boolean (config, group, "vim_mode", &error);
    if (g_key_file_has_key (config, group, "box_width", &error))
      options.box_width = g_key_file_get_integer (config, group, "box_width", &error);
    if (g_key_file_has_key (config, group, "box_height", &error))
      options.box_height = g_key_file_get_integer (config, group, "box_height", &error);
    if (g_key_file_has_key (config, group, "colorize", &error))
      options.colorize = g_key_file_get_boolean (config, group, "colorize", &error);
    if (g_key_file_has_key (config, group, "color_offset", &error))
      options.color_offset = g_key_file_get_integer (config, group, "color_offset", &error);
    if (g_key_file_has_key (config, group, "show_icons", &error))
      options.show_icons = g_key_file_get_boolean (config, group, "show_icons", &error);
    if (g_key_file_has_key (config, group, "show_desktop", &error))
      options.show_desktop = g_key_file_get_boolean (config, group, "show_desktop", &error);
    if (g_key_file_has_key (config, group, "show_titles", &error))
      options.show_titles = g_key_file_get_boolean (config, group, "show_titles", &error);
    if (g_key_file_has_key (config, group, "icon_size", &error))
      options.icon_size = g_key_file_get_integer (config, group, "icon_size", &error);
    if (g_key_file_has_key (config, group, "font", &error))
      options.font = g_key_file_get_string (config, group, "font", &error);
    if (g_key_file_has_key (config, group, "screenshot", &error))
      options.screenshot = g_key_file_get_boolean (config, group, "screenshot", &error);
    if (g_key_file_has_key (config, group, "screenshot_offset_x", &error))
      options.screenshot_offset_x = g_key_file_get_integer (config, group, "screenshot_offset_x", &error);
    if (g_key_file_has_key (config, group, "screenshot_offset_y", &error))
      options.screenshot_offset_y = g_key_file_get_integer (config, group, "screenshot_offset_y", &error);
    if (g_key_file_has_key (config, group, "at_pointer", &error))
      options.at_pointer = g_key_file_get_boolean (config, group, "at_pointer", &error);
    if (g_key_file_has_key (config, group, "color_file", &error))
      options.color_file = g_key_file_get_string (config, group, "color_file", &error);
  }

  g_key_file_free (config);
}