summaryrefslogtreecommitdiff
path: root/config.c
blob: 120e35a5ee1f571d4a36e1510b95ff3d99bdb00b (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
141
142
143
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

/*
Copyright 1996, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/

#include <stdlib.h>

#include "pmint.h"
#include "config.h"

static Bool
getnextline (
char	**pbuf,
int	*plen,
FILE	*f)

{
    int c, i;

    i = 0;
    while(1)
    {
	if (i+2 > *plen)
	{
	    if (*plen)
		*plen *= 2;
	    else
		*plen = BUFSIZ;
	    if (*pbuf)
		*pbuf = realloc (*pbuf, *plen + 1);
	    else
		*pbuf = malloc (*plen + 1);
	    if (! *pbuf) {
		fprintf (stderr, "Memory allocation failure reading config file\n");
		return 0;
	    }
	}
	c = getc (f);
	if (c == EOF)
	    break;
	(*pbuf)[i++] = c;
	if (c == '\n') {
	    i--;
	    break;
	}
    }
    (*pbuf)[i] = '\0';
    return i;
}


#ifdef NEED_STRCASECMP
int
ncasecmp (const char *str1, const char *str2, int n)
{
    char buf1[512],buf2[512];
    char c, *s;
    register int i;

    for (i=0, s = buf1; i < n && (c = *str1++); i++) {
	if (isupper(c))
	    c = tolower(c);
	if (i>510)
	    break;
	*s++ = c;
    }
    *s = '\0';
    for (i=0, s = buf2; i < n && (c = *str2++); i++) {
	if (isupper(c))
	    c = tolower(c);
	if (i>510)
	    break;
	*s++ = c;
    }
    *s = '\0';
    return (strncmp(buf1, buf2, n));
}
#endif /* NEED_STRCASECMP */


Status
GetConfig (
    const char *configFile,
    const char *serviceName,
    Bool *managed,
    char **startCommand,
    char **proxyAddress)

{
    FILE *fp;
    int found = 0;
    char *buf, *p;
    int buflen, n;

    *startCommand = *proxyAddress = NULL;

    fp = fopen (configFile, "r");

    if (!fp)
	return 0;

    buf = NULL;
    buflen = 0;
    n = strlen (serviceName);

    while (!found && getnextline (&buf, &buflen, fp))
    {
	if (buf[0] == '!')
	    continue;

	if (!(ncasecmp (buf, serviceName, n) == 0 && buf[n] == ' '))
	    continue;

	/* found the right config line */
	p = buf + n + 1;
	while (*p == ' ')
	    p++;
	if (ncasecmp (p, "managed", 7) == 0)
	{
	    *managed = 1;
	    p += 7;
	}
	else if (ncasecmp (p, "unmanaged", 9) == 0)
	{
	    *managed = 0;
	    p += 9;
	}
	else
	{
	    fprintf (stderr, "Error in config file at line \"%s\"\n", buf);
	    break;
	}

	while (*p == ' ')
	    p++;

	if (*managed)
	{
	    n = strlen (p) + 2;
	    *startCommand = malloc (n);
	    if (! *startCommand) {
		fprintf (stderr,
			 "Memory allocation failed for service \"%s\"\n",
			 serviceName);
		break;
	    }
	    snprintf(*startCommand, n, "%s&", p);
	}
	else
	{
	    *proxyAddress = strdup (p);
	    if (! *proxyAddress) {
		fprintf (stderr,
			 "Memory allocation failed for service \"%s\" at %s\n",
			 serviceName, p);
		break;
	    }
	}

	found = 1;
    }

    if (buf)
	free (buf);

    fclose (fp);
    return found;
}