home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part01 / parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  2.0 KB  |  99 lines

  1. /* 
  2.  * Command completion and parsing routines.
  3.  *
  4.  * Robert W. Baldwin, December 1984.
  5.  */
  6.  
  7.  
  8. #include    <stdio.h>
  9. #include    "window.h"
  10. #include    "specs.h"
  11. #include    "parser.h"
  12.  
  13.  
  14. /* Return TRUE if the cmdstring equals the model string up to and including
  15.  * the first space.
  16.  */
  17. int    cmpword(model, cmdstring)
  18. char    *model, *cmdstring;
  19. {
  20.     while (*model != 0  &&  *cmdstring != 0) {
  21.         if (*model == *cmdstring  &&  *model == ' ')  return (TRUE);
  22.         if (*model != *cmdstring)  break;
  23.         model++;
  24.         cmdstring++;
  25.         }
  26.     if (*model == 0)  return(TRUE);
  27.     return(FALSE);
  28. }
  29.  
  30.  
  31. /* Return TRUE if the first word of the cmdstring is a substring
  32.  * of the first word of model string.
  33.  */
  34. int    submatch(model, cmdstring)
  35. char    *model;
  36. char    *cmdstring;
  37. {
  38.     while (*model != 0  &&  *cmdstring != 0) {
  39.         if (*cmdstring == ' ')  break;
  40.         if (*model != *cmdstring)  return(FALSE);
  41.         if (*model == ' ')  break;
  42.         model++;
  43.         cmdstring++;
  44.         }
  45.  
  46.     return(TRUE);
  47. }
  48.  
  49.  
  50.  
  51. /* Lookup and perform a command from a command table.
  52.  * If not found, return an error message.
  53.  */
  54. char    *cmddo(cmdtab, cmdstring)
  55. cmdent    *cmdtab;
  56. char    *cmdstring;
  57. {
  58.     for ( ; cmdtab->cmdname != 0 ; cmdtab++)  {
  59.         if (cmpword(cmdtab->cmdname, cmdstring))  {
  60.             return((*(cmdtab->cmdproc))(cmdstring));
  61.             }
  62.         }
  63.  
  64.     return(CMDBAD);
  65. }
  66.  
  67.  
  68.  
  69. /* Do automatic completion of the string based on the
  70.  * command choices in the command table.
  71.  * Be careful to avoid doubly expanding a string, by requiring that
  72.  * the first few characters to match, but the whole word must not match.
  73.  * If sucessful, return a pointer to the template string
  74.  * in the command table.  Otherwise return NULL.
  75.  */
  76. char    *cmdcomplete(cmdtab, cmdstring)
  77. cmdent    *cmdtab;
  78. char    *cmdstring;
  79. {
  80.     cmdent    *centp;
  81.     char    *close;
  82.  
  83.     for (centp = cmdtab ; centp->cmdname != 0 ; centp++)  {
  84.         if (cmpword(centp->cmdname, cmdstring))  {
  85.             return(NULL);    /* Has been expanded. */
  86.             }
  87.         }
  88.  
  89.     close = NULL;
  90.     for (centp = cmdtab ; centp->cmdname != 0 ; centp++)  {
  91.         if (submatch(centp->cmdname, cmdstring))  {
  92.             if (close != NULL)  return(NULL);    /* Not yet unique. */
  93.             close = centp->cmdname;
  94.             }
  95.         }
  96.  
  97.     return(close);
  98. }
  99.