home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-09  |  5.4 KB  |  267 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.3
  4.  * 1993/04/09 16:50:24
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/command.c,v
  6.  * Exp
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992, 1993 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: command.c
  14.  * Routines to parse commands from the client.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * command.c,v
  18.  * Revision 3.3  1993/04/09  16:50:24  lindner
  19.  * nothing
  20.  *
  21.  * Revision 3.2  1993/03/24  20:19:40  lindner
  22.  * Fixed memory leak
  23.  *
  24.  * Revision 3.1  1993/03/19  19:56:53  lindner
  25.  * New CMD object
  26.  *
  27.  *
  28.  *********************************************************************/
  29.  
  30.  
  31.  
  32. #include "Malloc.h"
  33. #include "String.h"
  34. #include <stdio.h>
  35.  
  36. #include "command.h"
  37. #include "openers.h"
  38.  
  39. extern int DEBUG;
  40.  
  41. CMDobj *
  42. CMDnew()
  43. {
  44.      CMDobj *temp;
  45.  
  46.      temp = (CMDobj *) malloc(sizeof(CMDobj));
  47.  
  48.      temp->selstr  = temp->command = temp->search = NULL;
  49.  
  50.      temp->datafromnet   = STRnew();
  51.      temp->askfile       = STRnew();
  52.  
  53.      temp->secureuser    = STRnew();
  54.      temp->ticket        = STRnew();
  55.  
  56.      CMDsetGplus(temp, FALSE);
  57.      
  58.      return(temp);
  59. }
  60.  
  61.  
  62. void
  63. CMDdestroy(cmd)
  64.   CMDobj *cmd;
  65. {
  66.      STRdestroy(cmd->datafromnet);
  67.      STRdestroy(cmd->askfile);
  68.      STRdestroy(cmd->ticket);
  69.      STRdestroy(cmd->secureuser);
  70.  
  71.      free(cmd);
  72. }
  73.  
  74.  
  75. void
  76. CMDfromNet(cmd, sockfd)
  77.   CMDobj *cmd;
  78.   int    sockfd;
  79. {
  80.      char inputline[512];
  81.      char *cp, *incoming;
  82.      char *field1=NULL, *field2=NULL, *field3 = NULL;
  83.      char *extradata = NULL;
  84.      int length;
  85.      
  86.      length = readline(sockfd, inputline, sizeof(inputline));
  87.  
  88.      /** Set the alarm signal for about an hour, just in case.. **/
  89.      (void) alarm(60 * 60);
  90.  
  91.  
  92.      if (length <= 0) {
  93.       close(sockfd);
  94.       err_quit("getcommand: readline error");
  95.      }
  96.  
  97.      ZapCRLF(inputline);
  98.  
  99.      if (DEBUG)
  100.       printf("\nReceived: %s\n", inputline);
  101.  
  102.      CMDsetData(cmd, inputline);
  103.  
  104.      cp = CMDgetData(cmd);
  105.  
  106.  
  107.      cp = CMDticketfromLine(cmd, cp);
  108.  
  109.      CMDsetSelstr(cmd, cp);
  110.      
  111.  
  112.      /** Find the first field, if it exists... **/
  113.      cp = strchr(cp, '\t');
  114.      if (cp != NULL) {
  115.       *cp = '\0';
  116.       cp++;
  117.       
  118.       field1 = cp;
  119.       
  120.       /** find the second field, if it exists **/
  121.       cp = strchr(cp, '\t');
  122.       if (cp != NULL) {
  123.            *cp = '\0';
  124.            cp++;
  125.            field2 = cp;
  126.       } else {
  127.            /** find the third field, if it exists **/
  128.            if (cp != NULL) 
  129.             cp = strchr(cp, '\t');
  130.            if (cp != NULL) {
  131.             *cp = '\0';
  132.             cp++;
  133.             field3 = cp;
  134.            }
  135.       }
  136.  
  137.      }
  138.      /** Okay, now decide which field is the search and 
  139.        which is the command */
  140.      
  141.      if (*inputline == '7' || strncmp(inputline, "waissrc:",8)==0 ||
  142.      strncmp(inputline, "mindex:",7) ==0) {
  143.       
  144.       CMDsetSearch(cmd, field1);
  145.       CMDsetCommand(cmd, field2);
  146.      } else {
  147.       CMDsetCommand(cmd, field1);
  148.       CMDsetSearch(cmd, NULL);
  149.      }
  150.  
  151.      /** Get the extra data if it exists... **/
  152.      if (field3 != NULL)
  153.       extradata = field3;
  154.      else if     (CMDgetSearch(cmd) == NULL && field2 != NULL)
  155.       extradata = field2;
  156.  
  157.      if (extradata != NULL) {
  158.       char *tmp = tempnam(NULL, "gdata");
  159.       if (DEBUG)
  160.            printf("Ask data is in %s\n", tmp);
  161.       CMDsetAskfile(cmd, tmp);
  162.       CMDgetXtra(cmd, sockfd, atoi(extradata));
  163.       free(tmp);
  164.      }
  165.  
  166.      if (CMDgetCommand(cmd) != NULL && *CMDgetCommand(cmd) != '\0')
  167.       CMDsetGplus(cmd, TRUE);
  168.  
  169.      if (DEBUG) 
  170.       printf("Command:: selstr %s, command %s, search %s, extradata %s, user %s, ticket %s\n",
  171.          CMDgetSelstr(cmd),
  172.          CMDgetCommand(cmd),
  173.          CMDgetSearch(cmd),
  174.          CMDgetAskfile(cmd),
  175.          CMDgetUser(cmd),
  176.          CMDgetTicket(cmd));
  177. }
  178.  
  179.  
  180. /*
  181.  * Retrieve extra data from the client request..  This stuff is optional
  182.  *
  183.  */
  184.  
  185. void
  186. CMDgetXtra(cmd, fd, extradata)
  187.   CMDobj *cmd;
  188.   int fd;
  189.   int extradata;
  190. {
  191.      FILE *retrfile;
  192.      char inputline[512];
  193.  
  194.      /** Siphon off data if it's there.. **/
  195.  
  196.      /** A ticket? **/
  197.      if ((extradata & 0x2) == 0x2) {
  198.       ;
  199.      }
  200.  
  201.      /** An ask block **/
  202.      if ((extradata & 0x1) == 0x1) {
  203.       retrfile = ufopen(CMDgetAskfile(cmd), "w",0777);
  204.  
  205.       /** Okay, the next line is either +-1, or +bytes .. **/
  206.       readline(fd, inputline, sizeof(inputline));
  207.       ZapCRLF(inputline);
  208.       if (strcmp(inputline, "+-1")==0) {
  209.            while (readline(fd, inputline, sizeof(inputline))>0)  {
  210.             ZapCRLF(inputline);
  211.             ZapCRLF(inputline);
  212.             if (*inputline == '.' && *(inputline+1) == '\0')
  213.              break;
  214.             fputs(inputline, retrfile);
  215.             putc('\n', retrfile);
  216.            }
  217.       }
  218.      }
  219.      fclose(retrfile);
  220. }
  221.  
  222.  
  223.  
  224. char *
  225. CMDticketfromLine(cmd, input)
  226.   CMDobj *cmd;
  227.   char   *input;
  228. {
  229.      char *cp;
  230.      char *originput = input;
  231.  
  232.      if (strncmp(input, "*UMNDES ",8)!=0)
  233.       return(originput);
  234.      
  235.      input+=8;
  236.      
  237.      cp = strchr(input, ' ');
  238.      if (cp == NULL)
  239.       return(originput);
  240.  
  241.      *cp = '\0';
  242.      
  243.      CMDsetUser(cmd,input);
  244.  
  245.      if (strlen(CMDgetUser(cmd)) == 0)
  246.       return(originput);
  247.  
  248.      /** Special cases for icky turbogopher **/
  249.  
  250.      if (strlen(cp+1) < 16)  {
  251.       input = strchr(cp+1, '\t');
  252.       *(input-1) = '\0';
  253.       CMDsetTicket(cmd, cp+1);
  254.       return(input);
  255.      } else if (*(cp+17) == ' ') {
  256.       *(cp+17) = '\0';
  257.       CMDsetTicket(cmd, cp+1);
  258.       return(cp+18);
  259.      } else {
  260.       CMDsetTicket(cmd, cp+1);
  261.       *(CMDgetTicket(cmd)+17) = '\0';
  262.       return(cp+17);
  263.      }
  264.  
  265. }
  266.  
  267.