config.c revision 5c10afb9
1/* $Xorg: config.c,v 1.4 2001/02/09 02:05:34 xorgcvs Exp $ */
2
3/*
4Copyright 1996, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included
13in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall
24not be used in advertising or otherwise to promote the sale, use or
25other dealings in this Software without prior written authorization
26from The Open Group.
27*/
28/* $XFree86: xc/programs/proxymngr/config.c,v 1.6 2001/01/17 23:45:02 dawes Exp $ */
29
30#include <stdlib.h>
31
32#include "pmint.h"
33#include "config.h"
34
35static Bool
36getnextline (
37char	**pbuf,
38int	*plen,
39FILE	*f)
40
41{
42    int c, i;
43
44    i = 0;
45    while(1)
46    {
47	if (i+2 > *plen)
48	{
49	    if (*plen)
50		*plen *= 2;
51	    else
52		*plen = BUFSIZ;
53	    if (*pbuf)
54		*pbuf = (char *) realloc (*pbuf, *plen + 1);
55	    else
56		*pbuf = (char *) malloc (*plen + 1);
57	    if (! *pbuf) {
58		fprintf (stderr, "Memory allocation failure reading config file\n");
59		return 0;
60	    }
61	}
62	c = getc (f);
63	if (c == EOF)
64	    break;
65	(*pbuf)[i++] = c;
66	if (c == '\n') {
67	    i--;
68	    break;
69	}
70    }
71    (*pbuf)[i] = '\0';
72    return i;
73}
74
75
76#ifdef NEED_STRCASECMP
77int
78ncasecmp (str1, str2, n)
79    char *str1, *str2;
80    int n;
81{
82    char buf1[512],buf2[512];
83    char c, *s;
84    register int i;
85
86    for (i=0, s = buf1; i < n && (c = *str1++); i++) {
87	if (isupper(c))
88	    c = tolower(c);
89	if (i>510)
90	    break;
91	*s++ = c;
92    }
93    *s = '\0';
94    for (i=0, s = buf2; i < n && (c = *str2++); i++) {
95	if (isupper(c))
96	    c = tolower(c);
97	if (i>510)
98	    break;
99	*s++ = c;
100    }
101    *s = '\0';
102    return (strncmp(buf1, buf2, n));
103}
104#endif /* NEED_STRCASECMP */
105
106
107Status
108GetConfig (
109    char *configFile,
110    char *serviceName,
111    Bool *managed,
112    char **startCommand,
113    char **proxyAddress)
114
115{
116    FILE *fp;
117    int found = 0;
118    char *buf, *p;
119    int buflen, n;
120
121    *startCommand = *proxyAddress = NULL;
122
123    fp = fopen (configFile, "r");
124
125    if (!fp)
126	return 0;
127
128    buf = NULL;
129    buflen = 0;
130    n = strlen (serviceName);
131
132    while (!found && getnextline (&buf, &buflen, fp))
133    {
134	if (buf[0] == '!')
135	    continue;
136
137	if (!(ncasecmp (buf, serviceName, n) == 0 && buf[n] == ' '))
138	    continue;
139
140	/* found the right config line */
141	p = buf + n + 1;
142	while (*p == ' ')
143	    p++;
144	if (ncasecmp (p, "managed", 7) == 0)
145	{
146	    *managed = 1;
147	    p += 7;
148	}
149	else if (ncasecmp (p, "unmanaged", 9) == 0)
150	{
151	    *managed = 0;
152	    p += 9;
153	}
154	else
155	{
156	    fprintf (stderr, "Error in config file at line \"%s\"\n", buf);
157	    break;
158	}
159
160	while (*p == ' ')
161	    p++;
162
163	if (*managed)
164	{
165	    n = strlen (p);
166	    *startCommand = (char *) malloc (n + 2);
167	    if (! *startCommand) {
168		fprintf (stderr,
169			 "Memory allocation failed for service \"%s\"\n",
170			 serviceName);
171		break;
172	    }
173	    strcpy (*startCommand, p);
174	    (*startCommand)[n] = '&';
175	    (*startCommand)[n + 1] = '\0';
176	}
177	else
178	{
179	    *proxyAddress = (char *) malloc (strlen (p) + 1);
180	    if (! *proxyAddress) {
181		fprintf (stderr,
182			 "Memory allocation failed for service \"%s\" at %s\n",
183			 serviceName, p);
184		break;
185	    }
186	    strcpy (*proxyAddress, p);
187	}
188
189	found = 1;
190    }
191
192    if (buf)
193	free (buf);
194
195    fclose (fp);
196    return found;
197}
198
199
200
201
202