#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>

#include "../bignum.h"

int main(int argc, char *argv[])
{
int i;
int len;
int err;
char *pa, *p;
int  logflag;
char inbuf[4096];
char istr[ISIZE];
BigNum n;

logflag = 0;
if ( (pa = getenv("REMOTE_HOST")) != NULL ) /* decide whether to syslog */
  {
  if( (strncmp("quest",pa,5)) != 0 || ( pa[5] <= '0' || pa[5] >= '9' ))
    {
    logflag = 1;
    }
/*  syslog(LOG_INFO, "%s: REMOTE_HOST=\"%s\"   logflag=%d\n",
	argv[0], pa, logflag);
*/
  }
else if ( (pa = getenv("REMOTE_ADDR")) != NULL ) /* decide whether to syslog */
  {
  logflag = 1;
/*  syslog(LOG_INFO, "%s: REMOTE_ADDR=\"%s\"   logflag=%d\n",
	argv[0], pa, logflag);
*/
  }

if(fgets(inbuf, sizeof(inbuf), stdin) == NULL)
  {
  fprintf(stdout,"Error reading stdin.  Aborting.\n");
  exit(-1);
  }

len = strlen(inbuf);
if(inbuf[len-1] == '\n')
   {
   inbuf[len-1] = '\0';
   len --;
   }
if(inbuf[len-1] == '\r')
   {
   inbuf[len-1] = '\0';
   len --;
   }

fprintf(stdout,"Content-type: text/html\n\n");
fprintf(stdout,"<html>\n<head>\n");
fprintf(stdout,"<title>Prime Factors</title>");
fprintf(stdout,"</head>\n<body>\n");
fflush(stdout);

fflush(stdout);

#ifdef DEBUG
fprintf(stdout,"%s\n",inbuf);
fprintf(stdout,"len=%d\n",len);
#endif

if ( strncmp(inbuf,"theint=",7) != 0 )
  {
  fprintf(stdout,"No integer found to factor\n");
  fflush(stdout);
  return(0);
  }

for ( i = 0, p = strchr(inbuf,'='); p != NULL && *p != '\0'; p++ )
  {
  if ( *p == '=' )
    continue;
  if ( *p == '+' )
    continue;
  if ( *p < '0' || *p > '9' )
    break;
  istr[i] = *p;
  i++;
  if ( i > ISIZE -2 )
    break;
  }
istr[i] = '\0';

err = ascii2bcd(istr,&n);
if ( err != 0 || i < 1 || ( i == 1 && ( istr[0] == '\0' || istr[0] == '0')) )
  {
  fprintf(stdout,"%s: Could not find a number to factor<br>\n","factor");
#ifdef DEBUG
  fprintf(stdout,"input was \"%s\"<br>\n",inbuf);
#endif
  fflush(stdout);
  }
else
  {
  fprintf(stdout,"<center><h2>The prime factors of %s are</h2></center><hr>\n",istr);
  printfactors(&n);
  }


fprintf(stdout,"<p><hr><p>\n");
fprintf(stdout,"Please let us know what you think.<br>\n");
fprintf(stdout,"<a href=\"mailto:jrm@rsok.com\">Send feedback to jrm@rsok.com</a><br>\n");
fprintf(stdout,"<a href=\"mailto:webmaster@rsok.com\">Send feedback to webmaster</a><p>\n");
fprintf(stdout,
"<a href=\"http://www.rsok.com/~jrm/factor.html\">Back to the prime factors page</a><br>\n");
fprintf(stdout,"<a href=\"http://www.rsok.com/~jrm/\">To John Moyer's home page</a><br>\n");
fprintf(stdout,"<a href=\"http://www.rsok.com/\">To the rsok home page</a>\n");
fprintf(stdout,"<p><hr><p>The information presented on this page is provided \n\
for demonstration purposes only.  Research Solutions of Oklahoma \n\
disclaims all warranties, expressed or implied, for use of this \n\
information, including warranties of merchantibility of or fitness \n\
for a particular purpose. Copyright &#169 1996, John Moyer  \n\
All rights reserved.\n");
fprintf(stdout,"</body>\n</html>\n");


fflush(stdout);
return(0);
}
