#include <stdio.h>
#include <time.h>

#define BUFFERLENGTH 200  /* Buffer to read from and write to  User interface */
#define OUTPUTLENGTH 100    /* total length of output string */
#define LONGLENGTH 10   /* at least number of bytes per long + 1 */
#define TIMELENGTH 40  /* length for time string */
#define TIMERINTERVAL 5  /* how long to wait with the calculation */

void
Xui(string)
char *string;
     {
     char tempBuffer[BUFFERLENGTH];

     sprintf(tempBuffer, "%%%s\n", string);
     write(fileno(stdout), tempBuffer, strlen(tempBuffer));
     return;
     } 

void
setLabel(widget, string)
char *widget;
char *string;
     {
     char  tempBuffer[BUFFERLENGTH];

     sprintf(tempBuffer, "%%sV %s label {%s}\n", widget, string);
     write(fileno(stdout), tempBuffer, strlen(tempBuffer));      
     return;
     }

void
inputError()
     {
     Xui("sV result label {}");
     Xui("sV info label {invalid input}");
     return;
     }


main()        
     {
     char  buffer[BUFFERLENGTH];
     char output[OUTPUTLENGTH];
     char divisorString[LONGLENGTH];
     char timeString[TIMELENGTH];
     long  value;
     long divisor;
#ifndef ISC
     struct timeval  endTime, startTime;
     struct timezone  dummy;
#endif

     Xui("form top topLevel");
     Xui("asciiText input top editType edit width 200");
     Xui("action input override {<Key>Return: exec(echo [gV input string])}\n"); 
     Xui("label result top label {} width 200 fromVert input"); 
     Xui("command quit top fromVert result callback quit");
     Xui("label info top fromVert result fromHoriz quit label {} borderWidth 0 width 150");
     Xui("realize");

     while(1)
         {
	 if (!fgets(buffer, BUFFERLENGTH, stdin))
              exit(1);  /* Means: EOF or error condition, probably termination of user interface */
         else
	      {              

              if (!sscanf(buffer, "%ld", &value))
                   {
                   inputError();
                   break;
	           }

               setLabel("info", "thinking...");
               setLabel("result", "");

               output[0] = '\0';
#ifndef ISC
               gettimeofday(&startTime, &dummy);
#endif
               for (divisor = 2; divisor <= value; divisor++)
                    while (!(value % divisor))
                         {
                         value /= divisor;
                         sprintf(divisorString, "%ld", divisor);

                         if (!output[0])
                              strcpy(output, divisorString);
                         else
	       	              {
                              strcat(output, "*");
                              strcat(output, divisorString);
                              }
	                 }

#ifndef ISC
               gettimeofday(&endTime, &dummy);

	      sprintf(timeString, "%ld milliseconds", 
		       (endTime.tv_sec-startTime.tv_sec)*1000 + 
		       (endTime.tv_usec-startTime.tv_usec)/1000);
#endif

               setLabel("info", timeString);
               setLabel("result", output); 
               }
         }
   }


