/* This file contains utilities to make dealing with CGI programs */
/* easier. */

/* Jonathan Stark */
/* 7/1/94         */

#include "util.c"
#include "util.h"
#include <stdio.h>

/* Do the standard parsing of the URL data into the caller supplied */
/* structure of entries. */

int
ParseURL(entry *entries)
{
int cl;
register int x;

    cl = atoi(getenv("CONTENT_LENGTH"));
    for(x=0;cl && (!feof(stdin));x++)
	{
	entries[x].val = fmakeword(stdin,'&',&cl);
        	plustospace(entries[x].val);
        unescape_url(entries[x].val);
        entries[x].name = makeword(entries[x].val,'=');
/*
	printf("{%d} <i>%s</i> %s<br>\n", x, entries[x].name,
	       entries[x].val);
*/
	}
/*
printf("total: %d", x);
*/
     return(x);
}

/* the following routine searches through the parsed URL data     */
/* and returns a pointer to the value matchine the name passed    */
/* in.  if hard is 0, the search is compared to only the length   */
/* of the string passed in.  If hard is true, an exact match of   */
/* the match and parse strings must occur.  If no match is found, */
/* NULL is returned. */

/* USAGE: URLNamed(entries, x, "admin", 1); */

char *
URLNamed(entry *entries, int num, char *match, int hard)
{
int x;
char *ptr;

	ptr=NULL;

	if (hard)
		{
		for (x=0; (x<=num); x++)
			{
/*
printf("%d: %s compared %s <br>", x, match, entries[x].name);
*/
			if (!strcasecmp(entries[x].name,match) )
		  		ptr=entries[x].val;
			}
		}
	else
		{
		for (x=0; (x<=num); x++)
			{
			if (!strncasecmp(entries[x].name,match,strlen(match)) )
				ptr=entries[x].val;
			}
		}
	return(ptr);
}

/* The next routine returns a newly allocated string with the value */
/* of the namedURL. */

char *
newURLNamed(entry *entries, int num, char *match, int hard)
{
	return(strdup(URLNamed(&entries, num, match, hard)));
}

