15c10afb9Smrg
25c10afb9Smrg/*
35c10afb9SmrgCopyright 1996, 1998  The Open Group
45c10afb9Smrg
55c10afb9SmrgPermission to use, copy, modify, distribute, and sell this software and its
65c10afb9Smrgdocumentation for any purpose is hereby granted without fee, provided that
75c10afb9Smrgthe above copyright notice appear in all copies and that both that
85c10afb9Smrgcopyright notice and this permission notice appear in supporting
95c10afb9Smrgdocumentation.
105c10afb9Smrg
115c10afb9SmrgThe above copyright notice and this permission notice shall be included
125c10afb9Smrgin all copies or substantial portions of the Software.
135c10afb9Smrg
145c10afb9SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
155c10afb9SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
165c10afb9SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
175c10afb9SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
185c10afb9SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
195c10afb9SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
205c10afb9SmrgOTHER DEALINGS IN THE SOFTWARE.
215c10afb9Smrg
225c10afb9SmrgExcept as contained in this notice, the name of The Open Group shall
235c10afb9Smrgnot be used in advertising or otherwise to promote the sale, use or
245c10afb9Smrgother dealings in this Software without prior written authorization
255c10afb9Smrgfrom The Open Group.
265c10afb9Smrg*/
275c10afb9Smrg
285c10afb9Smrg#include <stdlib.h>
295c10afb9Smrg
305c10afb9Smrg#include "pmint.h"
315c10afb9Smrg#include "config.h"
325c10afb9Smrg
335c10afb9Smrgstatic Bool
345c10afb9Smrggetnextline (
355c10afb9Smrgchar	**pbuf,
365c10afb9Smrgint	*plen,
375c10afb9SmrgFILE	*f)
385c10afb9Smrg
395c10afb9Smrg{
405c10afb9Smrg    int c, i;
415c10afb9Smrg
425c10afb9Smrg    i = 0;
435c10afb9Smrg    while(1)
445c10afb9Smrg    {
455c10afb9Smrg	if (i+2 > *plen)
465c10afb9Smrg	{
475c10afb9Smrg	    if (*plen)
485c10afb9Smrg		*plen *= 2;
495c10afb9Smrg	    else
505c10afb9Smrg		*plen = BUFSIZ;
515c10afb9Smrg	    if (*pbuf)
52cf2f63c2Smrg		*pbuf = realloc (*pbuf, *plen + 1);
535c10afb9Smrg	    else
54cf2f63c2Smrg		*pbuf = malloc (*plen + 1);
555c10afb9Smrg	    if (! *pbuf) {
565c10afb9Smrg		fprintf (stderr, "Memory allocation failure reading config file\n");
575c10afb9Smrg		return 0;
585c10afb9Smrg	    }
595c10afb9Smrg	}
605c10afb9Smrg	c = getc (f);
615c10afb9Smrg	if (c == EOF)
625c10afb9Smrg	    break;
635c10afb9Smrg	(*pbuf)[i++] = c;
645c10afb9Smrg	if (c == '\n') {
655c10afb9Smrg	    i--;
665c10afb9Smrg	    break;
675c10afb9Smrg	}
685c10afb9Smrg    }
695c10afb9Smrg    (*pbuf)[i] = '\0';
705c10afb9Smrg    return i;
715c10afb9Smrg}
725c10afb9Smrg
735c10afb9Smrg
745c10afb9Smrg#ifdef NEED_STRCASECMP
755c10afb9Smrgint
76cf2f63c2Smrgncasecmp (const char *str1, const char *str2, int n)
775c10afb9Smrg{
785c10afb9Smrg    char buf1[512],buf2[512];
795c10afb9Smrg    char c, *s;
805c10afb9Smrg    register int i;
815c10afb9Smrg
825c10afb9Smrg    for (i=0, s = buf1; i < n && (c = *str1++); i++) {
835c10afb9Smrg	if (isupper(c))
845c10afb9Smrg	    c = tolower(c);
855c10afb9Smrg	if (i>510)
865c10afb9Smrg	    break;
875c10afb9Smrg	*s++ = c;
885c10afb9Smrg    }
895c10afb9Smrg    *s = '\0';
905c10afb9Smrg    for (i=0, s = buf2; i < n && (c = *str2++); i++) {
915c10afb9Smrg	if (isupper(c))
925c10afb9Smrg	    c = tolower(c);
935c10afb9Smrg	if (i>510)
945c10afb9Smrg	    break;
955c10afb9Smrg	*s++ = c;
965c10afb9Smrg    }
975c10afb9Smrg    *s = '\0';
985c10afb9Smrg    return (strncmp(buf1, buf2, n));
995c10afb9Smrg}
1005c10afb9Smrg#endif /* NEED_STRCASECMP */
1015c10afb9Smrg
1025c10afb9Smrg
1035c10afb9SmrgStatus
1045c10afb9SmrgGetConfig (
105cf2f63c2Smrg    const char *configFile,
106cf2f63c2Smrg    const char *serviceName,
1075c10afb9Smrg    Bool *managed,
1085c10afb9Smrg    char **startCommand,
1095c10afb9Smrg    char **proxyAddress)
1105c10afb9Smrg
1115c10afb9Smrg{
1125c10afb9Smrg    FILE *fp;
1135c10afb9Smrg    int found = 0;
1145c10afb9Smrg    char *buf, *p;
1155c10afb9Smrg    int buflen, n;
1165c10afb9Smrg
1175c10afb9Smrg    *startCommand = *proxyAddress = NULL;
1185c10afb9Smrg
1195c10afb9Smrg    fp = fopen (configFile, "r");
1205c10afb9Smrg
1215c10afb9Smrg    if (!fp)
1225c10afb9Smrg	return 0;
1235c10afb9Smrg
1245c10afb9Smrg    buf = NULL;
1255c10afb9Smrg    buflen = 0;
1265c10afb9Smrg    n = strlen (serviceName);
1275c10afb9Smrg
1285c10afb9Smrg    while (!found && getnextline (&buf, &buflen, fp))
1295c10afb9Smrg    {
1305c10afb9Smrg	if (buf[0] == '!')
1315c10afb9Smrg	    continue;
1325c10afb9Smrg
1335c10afb9Smrg	if (!(ncasecmp (buf, serviceName, n) == 0 && buf[n] == ' '))
1345c10afb9Smrg	    continue;
1355c10afb9Smrg
1365c10afb9Smrg	/* found the right config line */
1375c10afb9Smrg	p = buf + n + 1;
1385c10afb9Smrg	while (*p == ' ')
1395c10afb9Smrg	    p++;
1405c10afb9Smrg	if (ncasecmp (p, "managed", 7) == 0)
1415c10afb9Smrg	{
1425c10afb9Smrg	    *managed = 1;
1435c10afb9Smrg	    p += 7;
1445c10afb9Smrg	}
1455c10afb9Smrg	else if (ncasecmp (p, "unmanaged", 9) == 0)
1465c10afb9Smrg	{
1475c10afb9Smrg	    *managed = 0;
1485c10afb9Smrg	    p += 9;
1495c10afb9Smrg	}
1505c10afb9Smrg	else
1515c10afb9Smrg	{
1525c10afb9Smrg	    fprintf (stderr, "Error in config file at line \"%s\"\n", buf);
1535c10afb9Smrg	    break;
1545c10afb9Smrg	}
1555c10afb9Smrg
1565c10afb9Smrg	while (*p == ' ')
1575c10afb9Smrg	    p++;
1585c10afb9Smrg
1595c10afb9Smrg	if (*managed)
1605c10afb9Smrg	{
161cf2f63c2Smrg	    n = strlen (p) + 2;
162cf2f63c2Smrg	    *startCommand = malloc (n);
1635c10afb9Smrg	    if (! *startCommand) {
1645c10afb9Smrg		fprintf (stderr,
1655c10afb9Smrg			 "Memory allocation failed for service \"%s\"\n",
1665c10afb9Smrg			 serviceName);
1675c10afb9Smrg		break;
1685c10afb9Smrg	    }
169cf2f63c2Smrg	    snprintf(*startCommand, n, "%s&", p);
1705c10afb9Smrg	}
1715c10afb9Smrg	else
1725c10afb9Smrg	{
173cf2f63c2Smrg	    *proxyAddress = strdup (p);
1745c10afb9Smrg	    if (! *proxyAddress) {
1755c10afb9Smrg		fprintf (stderr,
1765c10afb9Smrg			 "Memory allocation failed for service \"%s\" at %s\n",
1775c10afb9Smrg			 serviceName, p);
1785c10afb9Smrg		break;
1795c10afb9Smrg	    }
1805c10afb9Smrg	}
1815c10afb9Smrg
1825c10afb9Smrg	found = 1;
1835c10afb9Smrg    }
1845c10afb9Smrg
185cf2f63c2Smrg    free (buf);
1865c10afb9Smrg
1875c10afb9Smrg    fclose (fp);
1885c10afb9Smrg    return found;
1895c10afb9Smrg}
1905c10afb9Smrg
1915c10afb9Smrg
1925c10afb9Smrg
1935c10afb9Smrg
194