home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / swish.tar / search.c < prev    next >
Text File  |  1998-04-09  |  8KB  |  270 lines

  1. /*search.c*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define SWISH_EXEC   "/usr/local/swish/swish"
  8.  
  9. /*---------------------------------------------------------------------------*/
  10. static void seiPrintHeader(void)
  11. {
  12.   printf("<html>\n<head>\n");
  13.   printf("<title>Search Results</title>\n");
  14. printf("<body background=../decr/bldng6.gif color=black link=\"#112288\" vlink=red>\n");
  15.  
  16.   /*printf("<body bgcolor=\"#000000\" text=\"#ff00ff\" link=\"#00ff80\" vlink=\"#8080ff\" alink=\"#00ff80\">\n");*/
  17.   printf("<h2>Search Results</h2>\n");
  18.  
  19. } /* seiPrintHeader */
  20.  
  21. /*---------------------------------------------------------------------------*/
  22. static void seiPrintFooter(char *returnURL)
  23. {
  24.   printf("<p>\n<hr>\n");
  25.   printf("<a href=\"%s\">Search Again</a>\n", returnURL);
  26.   printf("</body>\n");
  27.   printf("</head>\n</html>\n");
  28.  
  29. } /* seiPrintFooter */
  30.  
  31. /*---------------------------------------------------------------------------*/
  32. static int seiHexToDecimal(char first, char second)
  33. {
  34.   int value;
  35.  
  36.   first = tolower(first); 
  37.   second = tolower(second); 
  38.  
  39.   switch (first) {
  40.     case '0':  value = 16 * 0;  break;
  41.     case '1':  value = 16 * 1;  break;
  42.     case '2':  value = 16 * 2;  break;
  43.     case '3':  value = 16 * 3;  break;
  44.     case '4':  value = 16 * 4;  break;
  45.     case '5':  value = 16 * 5;  break;
  46.     case '6':  value = 16 * 6;  break;
  47.     case '7':  value = 16 * 7;  break;
  48.     case '8':  value = 16 * 8;  break;
  49.     case '9':  value = 16 * 9;  break;
  50.     case 'a':  value = 16 * 10;  break;
  51.     case 'b':  value = 16 * 11;  break;
  52.     case 'c':  value = 16 * 12;  break;
  53.     case 'd':  value = 16 * 13;  break;
  54.     case 'e':  value = 16 * 14;  break;
  55.     case 'f':  value = 16 * 15;  break;
  56.   }
  57.   switch (second) {
  58.     case '0':  value += 0;  break;
  59.     case '1':  value += 1;  break;
  60.     case '2':  value += 2;  break;
  61.     case '3':  value += 3;  break;
  62.     case '4':  value += 4;  break;
  63.     case '5':  value += 5;  break;
  64.     case '6':  value += 6;  break;
  65.     case '7':  value += 7;  break;
  66.     case '8':  value += 8;  break;
  67.     case '9':  value += 9;  break;
  68.     case 'a':  value += 10;  break;
  69.     case 'b':  value += 11;  break;
  70.     case 'c':  value += 12;  break;
  71.     case 'd':  value += 13;  break;
  72.     case 'e':  value += 14;  break;
  73.     case 'f':  value += 15;  break;
  74.   }
  75.  
  76.   return(value);
  77.  
  78. } /* seiHexToDecimal */
  79.  
  80. /*---------------------------------------------------------------------------*/
  81. static void seiReadNameValuePair(char *thestring, char *thevalue)
  82. {
  83.   char *cptr = NULL, *endcptr = NULL;
  84.  
  85.   cptr = strstr(thestring, "=");
  86.   cptr++;
  87.   endcptr = strstr(thestring, "&");
  88.   if (endcptr) {
  89.     endcptr++;
  90.     endcptr[-1] = '\0';
  91.   }
  92.  
  93.     /* copy the value of before the original string gets overwritten */
  94.   strcpy(thevalue, cptr);
  95.  
  96.   if (endcptr)
  97.     strcpy(thestring, endcptr);
  98.  
  99. } /* seiReadNameValuePair */
  100.  
  101. /*---------------------------------------------------------------------------*/
  102. static int seiReadString(char *thestring, char *substring)
  103. {
  104.   int len;
  105.   char *cptr = NULL;
  106.  
  107.   if (thestring[0] == '"') {
  108.     len = strlen(thestring);
  109.     cptr = &thestring[len-1];
  110.     while (cptr[0] != ' ')
  111.       cptr--;
  112.   }
  113.   else {
  114.     cptr = strstr(thestring, " ");
  115.   }
  116.   cptr[0] = '\0';
  117.   cptr++;
  118.   strcpy(substring, thestring);
  119.   strcpy(thestring, cptr);
  120.  
  121. } /* seiReadString */
  122.  
  123. /*---------------------------------------------------------------------------*/
  124. void main(void)
  125. {
  126.   char swishindex[256], keywords[512], maxresults[64];
  127.   char returnURL[1000], command[1000], thestring[2000];
  128.   char substring1[2000], substring2[2000], substring3[2000]; 
  129.   char *output = NULL, *cptr = NULL, *ncptr = NULL, *tempcptr;
  130.   int numchar = 10000, character;
  131.   int len, index, nindex, spacecount, done;
  132.   FILE *fp = NULL;
  133.   FILE *spew = NULL;
  134.  
  135.     returnURL[0] = 0;
  136.  
  137.     spew = fopen("/tmp/spew", "w");
  138.     fprintf(spew, "begin search\n"); fflush(spew);
  139.  
  140.     /* print header */
  141.   printf("Content-type: text/html%c%c",10,10);
  142.   fflush(stdout);
  143.   seiPrintHeader();
  144.  
  145.     fprintf(spew, "header done\n"); fflush(spew);
  146.  
  147.   sprintf(thestring, "%s", getenv("QUERY_STRING"));
  148.     fprintf(spew, "thestring %s\n", thestring); fflush(spew);
  149.     if (cptr = getenv("PATH_INFO")) {
  150.           sprintf(returnURL, "%s", cptr);
  151.         fprintf(spew, "returnURL %s\n", returnURL); fflush(spew);
  152.         cptr = NULL;
  153.     }
  154.  
  155.  
  156.   nindex = 0;
  157.   len = strlen(thestring);
  158.   for (index = 0; index < len; index++) {
  159.     if (thestring[index] == '+') {
  160.       thestring[nindex++] = ' ';
  161.     }
  162.     else if (thestring[index] == '%') {
  163.         /* convert hex to decimal */
  164.       thestring[nindex++] = (char)seiHexToDecimal(thestring[index+1],
  165.                                                   thestring[index+2]); 
  166.       index+=2;
  167.     }
  168.     else {
  169.       thestring[nindex++] = thestring[index];
  170.     }
  171.   }
  172.   thestring[nindex] = '\0';
  173.  
  174.        /* the order on the search form must remain constant:
  175.           swishindex, keywords, maxresults */
  176.   seiReadNameValuePair(thestring, swishindex);
  177.   strcpy(keywords, "__NOT_DEFINED__");
  178.   seiReadNameValuePair(thestring, keywords);
  179.   if (strcmp(keywords, "__NOT_DEFINED__")) {
  180.     strcpy(maxresults, "__NOT_DEFINED__");
  181.     seiReadNameValuePair(thestring, maxresults);
  182.  
  183.       /* build the swish command */
  184.     if (!strcmp(maxresults, "__NOT_DEFINED__"))
  185.       sprintf(command, "%s -f %s -w %s", SWISH_EXEC, swishindex, keywords);
  186.     else
  187.       sprintf(command, "%s -f %s -w %s -m %s", SWISH_EXEC, 
  188.               swishindex, keywords, maxresults);
  189.  
  190.     index = 0;
  191.     output = (char *)malloc(numchar * sizeof(char)); 
  192.  
  193.       /* open up a pipe to the swish command */
  194.     fp = popen(command, "r");
  195.     character = fgetc(fp);
  196.     while (character != EOF) {
  197.       output[index++] = (char)character;
  198.       if (index == numchar) {
  199.         numchar *= 2;
  200.         output = (char *)realloc(output, numchar * sizeof(char)); 
  201.       }
  202.       character = fgetc(fp);
  203.     }
  204.     pclose(fp);
  205.  
  206.       /* parse through the output line by line */
  207.     done = 0;
  208.     cptr = output;
  209.     while (cptr && !done) {
  210.       ncptr = strstr(cptr, "\n"); 
  211.       if (ncptr)
  212.         ncptr[0] = '\0';
  213.  
  214.         /* results of swish can be-
  215.             line beginning with "#"
  216.             line beginning with "."
  217.             line beginning with "err"
  218.             line beginning with "search words:"
  219.             line beginning with relevance rank */
  220.       if (cptr[0] == '#') {
  221.           /* do something with comments? */
  222.       }
  223.       else if (cptr[0] == '.') {
  224.         done = 1;
  225.       }
  226.       else if (!strncmp(cptr, "err", 3)) {
  227.         printf("<h4>%s</h4>\n", cptr); 
  228.       }
  229.       else if (!strncmp(cptr, "search words:", 13)) {
  230.         printf("<h3>%s</h3>\n", cptr); 
  231.       }
  232.       else {
  233.         strcpy(thestring, cptr);
  234.         seiReadString(thestring, substring1);
  235.         seiReadString(thestring, substring2);
  236.         seiReadString(thestring, substring3);
  237.         fprintf(spew, "%s <a href = \"%s\">%s</a> (%s bytes)<br>\n", 
  238.                 substring1, &substring2[1], substring3, thestring); fflush(spew);
  239.         printf("%s <a href = \"%s\">%s</a> (%s bytes)<br>\n", 
  240.                 substring1, substring2, substring3, thestring);
  241.       }
  242.       cptr = ncptr;
  243.       if (cptr)
  244.         cptr++;
  245.     }
  246.   }
  247.   else {
  248.       /* no keywords */
  249.     printf("Please specify keywords to perform search.<br>\n");
  250.     printf("__________________________________________<p>\n");
  251.     printf("search example 1: john and doe or jane<br>\n");
  252.     printf("search example 2: john and (doe or jane)<br>\n");
  253.     printf("search example 3: not (john or jane) and doe<br>\n");
  254.     printf("search example 4: j* and doe<br>\n");
  255.   }
  256.  
  257.     if (returnURL[0])
  258.       seiPrintFooter(returnURL);
  259.  
  260.   if (output) {
  261.     free(output);
  262.     output = NULL;
  263.   }
  264.  
  265. } /* main */
  266.  
  267. /*---------------------------------------------------------------------------*/
  268. /* eof */
  269.  
  270.