#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");
fprintf(stdout,
"<img src=\"http://www.questconsult.com/qimages/tiny_q_2.gif\" alt=\"Quest Logo\" align=\"left\"><center><h3>Quest Consultants Inc.</h3></center>\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@questconsult.com\">Send feedback to jrm@questconsult.com</a><br>\n");
fprintf(stdout,"<a href=\"mailto:webmaster@questconsult.com\">Send feedback to webmaster</a><p>\n");
fprintf(stdout,
"<a href=\"http://www.questconsult.com/~jrm/factor.html\">Back to the prime factors page</a><br>\n");
fprintf(stdout,"<a href=\"http://www.questconsult.com/~jrm/\">To John Moyer's home page</a><br>\n");
fprintf(stdout,"<a href=\"http://www.questconsult.com/\">To the Quest Consultants Inc. home page</a>\n");
fprintf(stdout,"<p><hr><p>The information presented on this page is provided \n\
for demonstration purposes only.  Quest Consultants Inc. \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, Quest Consultants Inc.  \n\
All rights reserved.\n");
fprintf(stdout,"</body>\n</html>\n");


fflush(stdout);
return(0);
}
