/* serv.c
Date: Fri, 2 Feb 96 08:10:11 EST
From: Glenn Flierl <glenn@plume.mit.edu>

Hi - I've made a fix to serv.c so that the code which allows
changing the optionserver in the .objects file for individual
objects works properly (I think). I'd appreciate your testing it
and making sure it works all right in the usual mode as well. */

#include <stdio.h>
#include <string.h>
#ifdef ULTRIX
char *strdup();
#endif
#define MAX_BUF 1025

char obj[1025],par[1025],file[1025];
char *getenv();
char *parptr[250];
static char *env[200],option[256];
static int envmax=0;

int dctsearch();

extern char **environ;

int putenv(str)
char *str;
{
int i;
char *sp,sv;
sp=strchr(str,'=');
if(sp == NULL)return -1;
sp++;
sv = *sp;
*sp = 0;
i=0;
while(env[i] != NULL && i<200){
  if(strstr(env[i],str)==env[i]) break;
  i++;
};
if (i >= 199)return -1;
if (i >= envmax && env[i] != NULL)free(env[i]);
*sp = sv;
if(env[i] == NULL)env[i+1]=NULL;
env[i]=strdup(str);
return 0;
}

main(argc,argv,envp)
int argc;
char *argv[];
char **envp;
{
char *sp;
int j,np;

envmax=0;
while(envp[envmax]){env[envmax]=envp[envmax];envmax++;};
env[envmax]=NULL;
environ=env;

sp=getenv("PATH_INFO");
if(sp==NULL){
  printf("Content-type: text/html\n\n");
  printf("&x <b>No object specified</b>\n");
  exit(1);
};
strcpy(obj,sp);
sp=strchr(obj,'.');
if(sp) *sp = 0;
sp=getenv("QUERY_STRING");
if(sp) strcpy(par,sp) ; else *par = 0;

strcpy(option,"OPTIONSERVER=");
strcat(option,OPTIONHOST);
putenv(option);

j=dctsearch(obj,file,par,0);

if(!j){
  printf("Content-type: text/html\n\n");
  printf("&x <b>Error:</b> %s %s.\n",file,obj);
  exit(1);
};

j=0;
parptr[j]=file;
parptr[++j]=par;
np=0;
sp=par;
while(*sp){
  switch(*sp){
  case '(':np++;break;
  case ')':np--;break;
  case ',':if(np)break;
    *sp=0;
    parptr[++j]=sp+1;
    break;
  };
  sp++;
};
parptr[++j]=NULL;

execvp(file,parptr);
printf("Content-type: text/html\n\n");
printf("&x <b>Method not found:</b> %s\n",file);
exit(1);
}

#ifdef ULTRIX
char *strdup(s)
char *s;
{
char *t;
if(s==NULL)return NULL;
t=malloc(strlen(s)+1);
strcpy(t,s);
return t;
}
#endif

