summaryrefslogtreecommitdiff
path: root/esd_config.c
blob: 086d2ebd56af9c9cd17f5bf4ed59f03ff9db57e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
134
135
136
137
138
139
140
#include "esd-config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>

int esd_no_spawn=0; /* If we can't even find the system config file,
		       things are screwed up - don't try to make things
		       worse. */
int esd_spawn_wait_ms=100; /* Time to wait trying to connect to an
			      autospawned ESD, in milliseconds. */
char esd_spawn_options[LINEBUF_SIZE] = "-terminate -nobeeps -as 2";

char esd_default_options [LINEBUF_SIZE] = ""; /* Default options, always applied */

static int read_esd_config = 0;

static void esd_config_read_file(FILE *fh);

void
esd_config_read(void)
{
  FILE *fh;
  char *fn;
  const char *tmpenv;

  if(read_esd_config) return;

  fn = malloc(sizeof(SYSCONFDIR) + sizeof("/esd.conf"));
  strcpy(fn, SYSCONFDIR "/esd.conf");
  fh = fopen(fn, "r");
  if(fh)
    {
      esd_config_read_file(fh);
      fclose(fh);
    }
  free(fn);

  tmpenv = getenv("HOME");
  if(tmpenv) {
    fn = malloc(strlen(tmpenv) + sizeof("/.esd.conf"));
    sprintf(fn, "%s/.esd.conf", tmpenv);

    fh = fopen(fn, "r");

    if(fh)
      {
	esd_config_read_file(fh);
	fclose(fh);
      }

    free(fn);
  }

  tmpenv=getenv("ESD_NO_SPAWN");
  if(tmpenv)
    esd_no_spawn=1;

  tmpenv = getenv("ESD_SPAWN_OPTIONS");
  if(tmpenv && strlen(tmpenv) < (sizeof(esd_spawn_options) - 1))
    strcpy(esd_spawn_options, tmpenv);

  tmpenv = getenv("ESD_DEFAULT_OPTIONS");
  if(tmpenv && strlen(tmpenv) < (sizeof(esd_default_options) - 1))
    strcpy(esd_default_options, tmpenv);

  read_esd_config = 1;
}

static void
esd_config_read_file(FILE *fh)
{
  char aline[LINEBUF_SIZE];
  char *key, *value, *start;
  int i;

  while(fgets(aline, sizeof(aline), fh))
    {
      /* first, chomp & chug */
      for(start = aline; *start && isspace(*start); start++) /**/;
      if(*start && start != aline) memmove(aline, start, strlen(start) + 1);

      i = strlen(aline) - 1;
      while(i >= 0 && isspace(aline[i])) aline[i--] = '\0';

      switch(aline[0])
	{
	case '#': /* it's a comment, skip it */
	  continue;
	case '[': /* It's a section delimiter to placate gnome_config,
		     skip it */
	  continue;
	case '\0': /* Junk line, skip it */
	  continue;
	default:
	  break;
	}

      key = DO_STRTOK(aline, "=");
      if(!key) continue;
      value = DO_STRTOK(NULL, "=");
      if(!value) value = "";

      if(!strcasecmp(key, "auto_spawn"))
	{
	  if(!strcasecmp(value, "true")
	     || !strcasecmp(value, "yes")
	     || !strcasecmp(value, "1"))
	    esd_no_spawn=0;
	  else if(!strcasecmp(value, "false")
		  || !strcasecmp(value, "no")
		  || !strcasecmp(value, "0"))
	    esd_no_spawn=1;
	  else
	    fprintf(stderr, "Invalid value %s for option %s\n", value, key);
	}
      else if(!strcasecmp(key, "spawn_options"))
	{
	  strcpy(esd_spawn_options, value);
	}
      else if(!strcasecmp(key, "default_options"))
	{
	  strcpy(esd_default_options, value);
	}
      else if(!strcasecmp(key, "spawn_wait_ms"))
	{
	  char *endptr;
	  long val = strtol(value, &endptr, 0);
	  if(value != '\0' && *endptr == '\0')
	    esd_spawn_wait_ms = (int) val;
	  else
	    fprintf(stderr, "Invalid value %s for option %s\n", value, key);
	}
      else
	fprintf(stderr, "Unknown option %s.\n", key);
    } /* while(fgets(...)) */
}