home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / mactool / thinkcgu.sit / app ƒ / comline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-27  |  2.7 KB  |  124 lines

  1. /*
  2. *    FILE:        comline.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    August 25, 1990
  5. *
  6. *    Defines command line methods, for command-line applications.
  7. */
  8.  
  9. # include    <stdio.h>
  10. # include    <string.h>
  11. # include    <stdlib.h>
  12. # include    "comline.h"
  13. # include    "morestr.h"
  14.  
  15. # define    MAX_LENGTH    80
  16.  
  17. /************************************************************************
  18. *    initialize command line
  19. ************************************************************************/
  20. boolean    Comline::init(void)
  21. {
  22.     line = NULL;
  23.     start_next_argument = 0;
  24.     
  25.     return TRUE;
  26. }
  27.  
  28. /************************************************************************
  29. *    read command line
  30. ************************************************************************/
  31. boolean    Comline::read(void)
  32. {
  33.     int        i;
  34.     boolean    success;
  35.     char    line[MAX_LENGTH],
  36.             *temp;                /* "shadowing" for TC safety */
  37.     
  38.     printf("> ");                /* prompt character */
  39.     
  40.     if (get_line(stdin,line,MAX_LENGTH) == EOF)
  41.         success = FALSE;
  42.     else
  43.     {
  44.         if (this->line != NULL)        /* Think C doesn't need this */
  45.             free(this->line);
  46.             
  47.         /* is it right that Turbo C++ requires me to cast malloc's
  48.             return value here? */
  49.         temp = (char*) malloc(sizeof(char) * (strlen(line)+1));
  50.         strcpy(temp,line);
  51.         this->line = temp;
  52.         
  53.         for (i=0 ; line[i] != ' ' && line[i] != '\0' ; i++)
  54.             ;        
  55.         
  56.         start_next_argument = i;
  57.         success = TRUE;
  58.     }
  59.     
  60.     return success;
  61. }
  62.  
  63. /************************************************************************
  64. *    get command name
  65. ************************************************************************/
  66. void    Comline::get_command(char command[])
  67. {
  68.     int        i;
  69.     
  70.     if (line == NULL)
  71.         command[0] = '\0';
  72.     else
  73.     {
  74.         for (i=0 ; line[i] != ' ' && line[i] != '\0' ; i++)
  75.             command[i] = line[i];
  76.         command[i] = '\0';
  77.     }
  78. }
  79.     
  80. /************************************************************************
  81. *    get next argument string
  82. ************************************************************************/
  83. void    Comline::get_next_argument(char argument[])
  84. {
  85.     int        i,
  86.             end_arg;
  87.     
  88.     if (line == NULL || line[start_next_argument] == '\0')
  89.         argument[0] = '\0';
  90.     else
  91.     {
  92.         while (line[start_next_argument] == ' ')
  93.             start_next_argument++;        /* eliminate leading spaces */
  94.             
  95.         for (i=start_next_argument ;
  96.             line[i] != ';' && line[i] != '\0' ; i++)
  97.             argument[i-start_next_argument] = line[i];
  98.  
  99.         end_arg = i - start_next_argument;
  100.         
  101.         while (argument[end_arg - 1] == ' ')
  102.             end_arg--;                /* eliminate trailing spaces */
  103.             
  104.         argument[end_arg] = '\0';
  105.     
  106.         if (line[i] == ';')
  107.             start_next_argument = i+1;
  108.         else
  109.             start_next_argument = i;
  110.     }
  111. }
  112.  
  113. /************************************************************************
  114. *    destroy command line
  115. ************************************************************************/
  116. boolean    Comline::destroy(void)
  117. {
  118.     if (line != NULL)
  119.         free(line);
  120.         
  121.     return TRUE;
  122. }
  123.  
  124.