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