cfgfile_options.c 9.82 KB
Newer Older
1 2 3 4 5 6 7 8
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
 * PLEASE READ THESE TERMS BEFORE DISTRIBUTING.                     *
 *                                                                  *
 * THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2001                *
9
 * by Stan Seibert <volsung@xiph.org> AND OTHER CONTRIBUTORS        *
10 11 12 13
 * http://www.xiph.org/                                             *
 *                                                                  *
 ********************************************************************

14
 last mod: $Id: cfgfile_options.c,v 1.3 2002/01/26 11:06:37 segher Exp $
15 16 17 18 19 20 21 22

 ********************************************************************/


/* if strcasecmp is giving you problems, switch to strcmp or the appropriate
 * function for your platform / compiler.
 */

23 24 25 26
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

27 28 29 30 31 32 33 34
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h> /* for LONG_MAX / LONG_MIN */
#include <errno.h>

#include "cfgfile_options.h"
#include "status.h"
35
#include "i18n.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/* ------------------- Private Functions ---------------------- */

int print_space (FILE *f, int s, int c)
{
  int tmp = 0;
  do {
    fputc (c, f);
    tmp++;
  } while (--s > 0);
  return tmp;
}


int parse_error (parse_code_t pcode, int lineno, const char *filename,
		 char *line)
{
  if (pcode == parse_syserr) {
    if (errno != EEXIST && errno != ENOENT)
55
      perror (_("System error"));
56 57
    return -1;
  } else {
58
    status_error (_("=== Parse error: %s on line %d of %s (%s)\n"), 
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		  parse_error_string(pcode), 
		  lineno, filename, line);
    return 0;
  }
}


/* ------------------- Public Interface ----------------------- */

void file_options_init (file_option_t opts[])
{

  while (opts && opts->name) {

    opts->found = 0;

    if (opts->dfl) {
      switch (opts->type) {
      case opt_type_none:
	/* do nothing */
	break;
	
      case opt_type_char:
	*(char *) opts->ptr = *(char*) opts->dfl;
	break;
	
      case opt_type_string:
	*(char **) opts->ptr = *(char **) opts->dfl;
	break;

      case opt_type_bool:
      case opt_type_int:
	*(long int *) opts->ptr = *(int *) opts->dfl;
	break;
	
      case opt_type_float:
	*(float *) opts->ptr = *(float *) opts->dfl;
	break;
	
      case opt_type_double:
	*(double *) opts->ptr = *(double *) opts->dfl;
	break;
      }
    }

    opts++;
  }
}


/* DescribeOptions - describe available options to outfile */
void file_options_describe (file_option_t opts[], FILE *f)
{
  /* name | description | type | default */
  int colWidths[] = {0, 0, 7, 7};
  int totalWidth = 0;
  file_option_t *opt = opts;

  while (opt->name) {
    int len = strlen (opt->name) + 1;
    if (len  > colWidths[0])
      colWidths[0] = len;
    opt++;
  }

  opt = opts;
  while (opt->name) {
    int len = strlen (opt->desc) + 1;
    if (len > colWidths[1])
      colWidths[1] = len;
    opt++;
  }

  /* Column headers */
  /* Name */
134
  totalWidth += fprintf (f, "%-*s", colWidths[0], _("Name"));
135 136
  
  /* Description */
137
  totalWidth += fprintf (f, "%-*s", colWidths[1], _("Description"));
138 139
  
  /* Type */
140
  totalWidth += fprintf (f, "%-*s", colWidths[2], _("Type"));
141 142
  
  /* Default */
143
  totalWidth += fprintf (f, "%-*s", colWidths[3], _("Default"));
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

  fputc ('\n', f);
  
  /* Divider */
  print_space (f, totalWidth, '-');

  fputc ('\n', f);

  opt = opts;
  while (opt->name)
    {
      /* name */
      int w = colWidths[0];
      w -= fprintf (f, "%s", opt->name);
      print_space (f, w, ' ');

      /* description */
      w = colWidths[1];
      w -= fprintf (f, "%s", opt->desc);
      print_space (f, w, ' ');

      /* type */
      w = colWidths[2];
      switch (opt->type) {
      case opt_type_none:
169
	w -= fprintf (f, _("none"));
170 171
	break;
      case opt_type_bool:
172
	w -= fprintf (f, _("bool"));
173 174
	break;
      case opt_type_char:
175
	w -= fprintf (f, _("char"));
176 177
	break;
      case opt_type_string:
178
	w -= fprintf (f, _("string"));
179 180
	break;
      case opt_type_int:
181
	w -= fprintf (f, _("int"));
182 183
	break;
      case opt_type_float:
184
	w -= fprintf (f, _("float"));
185 186
	break;
      case opt_type_double:
187
	w -= fprintf (f, _("double"));
188 189
	break;
      default:
190
	w -= fprintf (f, _("other"));
191 192 193 194 195
      }
      print_space (f, w, ' ');

      /* default */
      if (opt->dfl == NULL)
196
	fputs (_("(NULL)"), f);
197 198 199
      else {
	switch (opt->type) {
	case opt_type_none:
200
	  fputs (_("(none)"), f);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	  break;
	case opt_type_char:
	  fputc (*(char *) opt->dfl, f);
	  break;
	case opt_type_string:
	  fputs (*(char **) opt->dfl, f);
	  break;
	case opt_type_bool:
	case opt_type_int:
	  fprintf (f, "%ld", *(long int *) opt->dfl);
	  break;
	case opt_type_float:
	  fprintf (f, "%f", (double) (*(float *) opt->dfl));
	  break;
	case opt_type_double:
	  fprintf (f, "%f", *(double *) opt->dfl);
	  break;
	}
      }
      fputc ('\n', f);
      opt++;
    }
}


parse_code_t parse_line (file_option_t opts[], char *line)
{
  char *equals, *value = "";
  file_option_t *opt;
  int len;

  /* skip leading whitespace */
  while (line[0] == ' ')
    line++;

  /* remove comments */
  equals = strchr (line, '#');
  if (equals)
    *equals = '\0';

  /* return if only whitespace on line */
  if (!line[0] || line[0] == '#')
    return parse_ok;

  /* check for an '=' and set to \0 */
  equals = strchr (line, '=');
  if (equals) {
    value = equals + 1;
    *equals = '\0';
  }

  /* cut trailing whitespace from key (line = key now) */
  while ((equals = strrchr(line, ' ')))
    *equals = '\0';

  /* remove this if you want a zero-length key */
  if (strlen(line) == 0)
    return parse_nokey;
  
  if (value) {
    /* cut leading whitespace from value */
    while (*value == ' ')
      value++;
    
    /* cut trailing whitespace from value */
    len = strlen (value);
    while (len > 0 && value[len-1] == ' ') {
      len--;
      value[len] = '\0';
    }
  }

  /* now key is in line and value is in value. Search for a matching option. */
  opt = opts;
  while (opt->name) {
    if (!strcasecmp (opt->name, line)) {
      long tmpl;
      char *endptr;

      /* found the key. now set the value. */
      switch (opt->type) {
      case opt_type_none:
	if (value != NULL || strlen(value) > 0)
	  return parse_badvalue;
	opt->found++;
	break;

      case opt_type_bool:
	if (!value || *value == '\0')
	  return parse_badvalue;
	
	/* Maybe this is a numeric bool */
	tmpl = strtol (value, &endptr, 0);

	if ( strncasecmp(value, "y", 1) == 0
	     || strcasecmp(value, "true")
	     || (*endptr == '\0' && tmpl) )
	  *(long int *) opt->ptr = 1;
	else if ( strncasecmp(value, "n", 1) == 0
		  || strcasecmp(value, "false")
		  || (*endptr == '\0' && !tmpl) )
	  *(long int *) opt->ptr = 0;
	else
	  return parse_badvalue;
	break;

      case opt_type_char:
	if (strlen(value) != 1)
	  return parse_badvalue;
	opt->found++;
	*(char *) opt->ptr = value[0];
	break;

      case opt_type_string:
	opt->found++;
	if (*(char **)opt->ptr) free(*(char **)opt->ptr);
	*(char **) opt->ptr = strdup (value);
	break;

      case opt_type_int:
	if (!value || *value == '\0')
	  return parse_badvalue;
	tmpl = strtol (value, &endptr, 0);
	if (((tmpl == LONG_MIN || tmpl == LONG_MAX) && errno == ERANGE)
	    || (*endptr != '\0'))
	  return parse_badvalue;
	opt->found++;
	*(long int *) opt->ptr = tmpl;
	break;
	
      case opt_type_float:
	if (!value || *value == '\0')
	  return parse_badvalue;
	opt->found++;
	*(float *) opt->ptr = atof (value);
	break;

      case opt_type_double:
	if (!value || *value == '\0')
	  return parse_badvalue;
	opt->found++;
	*(double *) opt->ptr = atof (value);
	break;

      default:
	return parse_badtype;
      }
      return parse_ok;
    }
    opt++;
  }
  return parse_keynotfound;
}


parse_code_t parse_config_file (file_option_t opts[], const char *filename)
{
  unsigned int len=80;
  char *line = malloc(len);
  int readoffset, thischar, lineno;
  FILE *file;
  parse_code_t pcode;
  char empty[] = "";

  if (!line) {
      parse_error(parse_syserr, 0, empty, empty);
      return parse_syserr;
  }

  file = fopen (filename, "r");
  if (!file) {
      parse_error (parse_syserr, 0, empty, empty);
      free (line);
      return parse_syserr;
  }

  lineno = 0;
  while (!feof (file)) {

    lineno++;
    readoffset = 0;
    memset (line, 0, len);

    while ((thischar = fgetc(file)) != EOF) {

      if (readoffset + 1 > len) {
	len *= 2;
	line = realloc (line, len);
	if (!line)
	  {
	    parse_error(parse_syserr, 0, empty, empty);
	    fclose (file);
	    return parse_syserr;
	  }
      }

      if (thischar == '\n') {
	line[readoffset] = '\0';
	break;
      }
      else
	line[readoffset] = (unsigned char) thischar;
      readoffset++;

    }

    pcode = parse_line (opts, line);

    if (pcode != parse_ok)
      if (!parse_error(pcode, lineno, filename, line)) {
	free (line);
	return pcode;
      }

  }

  free (line);
  return parse_ok;
}

/* ParseErr - returns a string corresponding to parse code pcode */
const char *parse_error_string (parse_code_t pcode)
{
  switch (pcode) {
  case parse_ok:
426
    return _("Success");
427 428 429
  case parse_syserr:
    return strerror(errno);
  case parse_keynotfound:
430
    return _("Key not found");
431
  case parse_nokey:
432
    return _("No key");
433
  case parse_badvalue:
434
    return _("Bad value");
435
  case parse_badtype:
436
    return _("Bad type in options list");
437
  default:
438
    return _("Unknown error");
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
  }
}


void parse_std_configs (file_option_t opts[])
{
  char filename[FILENAME_MAX];
  char *homedir = getenv("HOME");

  parse_config_file(opts, "/etc/ogg123rc");
  if (homedir && strlen(homedir) < FILENAME_MAX - 10) {
    /* Try ~/.ogg123 */
    strncpy(filename, homedir, FILENAME_MAX);
    strcat(filename, "/.ogg123rc");
    parse_config_file(opts, filename);
  }
}