/*
    11 Feb 09.  WJS
	Type "main" and include stdlib.h to avoid diagnostics
	Exit w/explicit status so said status can be checked.
	Note that poststring is probably not used.  At present, it is
    in plotmap, and I believe that plotmap is no longer used
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>

char *getenv();
static char querystuff[2049];

char x2c(what) 
char *what;
{
    register char digit;

    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
    digit *= 16;
    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
    return(digit);
}

void fixstr(url)
char *url;
{
    register int x,y;
    
    for(x=0,y=0;url[y];++x,++y) {
      if((url[x]=url[y])=='&'){
	url[x]=';';}
      else if(url[x] == '%') {
            url[x] = x2c(&url[y+1]);
            y+=2;
        }
    }
    url[x] = '\0';
}


int main(argc,argv)
int argc;
char *argv[];
{
int i,j;
char *sp,*sp2;
char key[80];

/* want to bracket the incoming argument with ';' */
querystuff[0]=';';
querystuff[1]='\0';
if(strcmp(getenv("REQUEST_METHOD"),"POST")) exit(1);
i=atoi(getenv("CONTENT_LENGTH"));

for(j=0;j<i && !feof(stdin);j++)
  querystuff[j+1]=fgetc(stdin);

/* place the semi-colon at end of string, too */
querystuff[j+1]=';';
querystuff[j+2]='\0';

/* so that now, we have a string that is of the form             */
/*  ;var=paramXXXval;  , where XXX is a hex value begun with %   */
/*  user can append a bunch of these things together             */
/*  so each piece would be separated with %2C (comma) or %7C (pipe) */
/*  or %26 (ampersand) or maybe %26%26 (2 ampersands)            */

/* fprintf(stderr,"querystuff string is %s\n",querystuff); */
fixstr(querystuff); 

for (i=1;i<argc;i++){
  key[0]=';';
  strcpy(key+1,argv[i]);
  strcat(key,"=");
  
  sp=strstr(querystuff,key);
  if(sp==NULL){
    printf(" 0");
  } else {  
    sp=strchr(sp,'=')+1;
    sp2=strchr(sp,';');
    *sp2='\0';
    if(i==1)printf("%s",sp);else printf(" %s",sp);
    *sp2=';';
  };
};
printf("\n");
exit(0);
}
