source: trunk/third/esound/esd_config.c @ 21330

Revision 21330, 3.2 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21329, which included commits to RCS files with non-trunk default branches.
Line 
1#include "esd-config.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <ctype.h>
7#include <string.h>
8
9int esd_no_spawn=1; /* If we can't even find the system config file,
10                       things are screwed up - don't try to make things
11                       worse. */
12int esd_spawn_wait_ms=100; /* Time to wait trying to connect to an
13                              autospawned ESD, in milliseconds. */
14char esd_spawn_options[LINEBUF_SIZE] = "-terminate -nobeeps -as 2";
15
16char esd_default_options [LINEBUF_SIZE] = ""; /* Default options, always applied */
17
18static int read_esd_config = 0;
19
20static void esd_config_read_file(FILE *fh);
21
22void
23esd_config_read(void)
24{
25  FILE *fh;
26  char *fn, *tmpenv;
27
28  if(read_esd_config) return;
29
30  fn = malloc(sizeof(SYSCONFDIR) + sizeof("/esd.conf"));
31  strcpy(fn, SYSCONFDIR "/esd.conf");
32  fh = fopen(fn, "r");
33  if(fh)
34    {
35      esd_config_read_file(fh);
36      fclose(fh);
37    }
38  free(fn);
39
40  tmpenv = getenv("HOME");
41  if(tmpenv) {
42    fn = malloc(strlen(tmpenv) + sizeof("/.esd.conf"));
43    sprintf(fn, "%s/.esd.conf", tmpenv);
44
45    fh = fopen(fn, "r");
46
47    if(fh)
48      {
49        esd_config_read_file(fh);
50        fclose(fh);
51      }
52
53    free(fn);
54  }
55
56  tmpenv=getenv("ESD_NO_SPAWN");
57  if(tmpenv)
58    esd_no_spawn=1;
59
60  tmpenv = getenv("ESD_SPAWN_OPTIONS");
61  if(tmpenv && strlen(tmpenv) < (sizeof(esd_spawn_options) - 1))
62    strcpy(esd_spawn_options, tmpenv);
63
64  tmpenv = getenv("ESD_DEFAULT_OPTIONS");
65  if(tmpenv && strlen(tmpenv) < (sizeof(esd_default_options) - 1))
66    strcpy(esd_default_options, tmpenv);
67
68  read_esd_config = 1;
69}
70
71static void
72esd_config_read_file(FILE *fh)
73{
74  char aline[LINEBUF_SIZE];
75  char *key, *value, *start;
76  int i;
77
78  while(fgets(aline, sizeof(aline), fh))
79    {
80      /* first, chomp & chug */
81      for(start = aline; *start && isspace(*start); start++) /**/;
82      if(*start && start != aline) memmove(aline, start, strlen(start) + 1);
83
84      i = strlen(aline) - 1;
85      while(i >= 0 && isspace(aline[i])) aline[i--] = '\0';
86
87      switch(aline[0])
88        {
89        case '#': /* it's a comment, skip it */
90          continue;
91        case '[': /* It's a section delimiter to placate gnome_config,
92                     skip it */
93          continue;
94        case '\0': /* Junk line, skip it */
95          continue;
96        default:
97          break;
98        }
99
100      key = DO_STRTOK(aline, "=");
101      if(!key) continue;
102      value = DO_STRTOK(NULL, "=");
103      if(!value) value = "";
104
105      if(!strcasecmp(key, "auto_spawn"))
106        {
107          if(!strcasecmp(value, "true")
108             || !strcasecmp(value, "yes")
109             || !strcasecmp(value, "1"))
110            esd_no_spawn=0;
111          else if(!strcasecmp(value, "false")
112                  || !strcasecmp(value, "no")
113                  || !strcasecmp(value, "0"))
114            esd_no_spawn=1;
115          else
116            fprintf(stderr, "Invalid value %s for option %s\n", value, key);
117        }
118      else if(!strcasecmp(key, "spawn_options"))
119        {
120          strcpy(esd_spawn_options, value);
121        }
122      else if(!strcasecmp(key, "default_options"))
123        {
124          strcpy(esd_default_options, value);
125        }
126      else if(!strcasecmp(key, "spawn_wait_ms"))
127        {
128          char *endptr;
129          long val = strtol(value, &endptr, 0);
130          if(value != '\0' && *endptr == '\0')
131            esd_spawn_wait_ms = (int) val;
132          else
133            fprintf(stderr, "Invalid value %s for option %s\n", value, key);
134        }
135      else
136        fprintf(stderr, "Unknown option %s.\n", key);
137    } /* while(fgets(...)) */
138}
139
Note: See TracBrowser for help on using the repository browser.