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