home *** CD-ROM | disk | FTP | other *** search
- /*
- * FILE: comline.c
- * AUTHOR: R. Gonzalez
- * CREATED: August 25, 1990
- *
- * Defines command line methods, for command-line applications.
- */
-
- # include <stdio.h>
- # include <string.h>
- # include <stdlib.h>
- # include "comline.h"
- # include "morestr.h"
-
- # define MAX_LENGTH 80
-
- /************************************************************************
- * initialize command line
- ************************************************************************/
- boolean Comline::init(void)
- {
- line = NULL;
- start_next_argument = 0;
-
- return TRUE;
- }
-
- /************************************************************************
- * read command line
- ************************************************************************/
- boolean Comline::read(void)
- {
- int i;
- boolean success;
- char line[MAX_LENGTH],
- *temp; /* "shadowing" for TC safety */
-
- printf("> "); /* prompt character */
-
- if (get_line(stdin,line,MAX_LENGTH) == EOF)
- success = FALSE;
- else
- {
- if (this->line != NULL) /* Think C doesn't need this */
- free(this->line);
-
- /* is it right that Turbo C++ requires me to cast malloc's
- return value here? */
- temp = (char*) malloc(sizeof(char) * (strlen(line)+1));
- strcpy(temp,line);
- this->line = temp;
-
- for (i=0 ; line[i] != ' ' && line[i] != '\0' ; i++)
- ;
-
- start_next_argument = i;
- success = TRUE;
- }
-
- return success;
- }
-
- /************************************************************************
- * get command name
- ************************************************************************/
- void Comline::get_command(char command[])
- {
- int i;
-
- if (line == NULL)
- command[0] = '\0';
- else
- {
- for (i=0 ; line[i] != ' ' && line[i] != '\0' ; i++)
- command[i] = line[i];
- command[i] = '\0';
- }
- }
-
- /************************************************************************
- * get next argument string
- ************************************************************************/
- void Comline::get_next_argument(char argument[])
- {
- int i,
- end_arg;
-
- if (line == NULL || line[start_next_argument] == '\0')
- argument[0] = '\0';
- else
- {
- while (line[start_next_argument] == ' ')
- start_next_argument++; /* eliminate leading spaces */
-
- for (i=start_next_argument ;
- line[i] != ';' && line[i] != '\0' ; i++)
- argument[i-start_next_argument] = line[i];
-
- end_arg = i - start_next_argument;
-
- while (argument[end_arg - 1] == ' ')
- end_arg--; /* eliminate trailing spaces */
-
- argument[end_arg] = '\0';
-
- if (line[i] == ';')
- start_next_argument = i+1;
- else
- start_next_argument = i;
- }
- }
-
- /************************************************************************
- * destroy command line
- ************************************************************************/
- boolean Comline::destroy(void)
- {
- if (line != NULL)
- free(line);
-
- return TRUE;
- }
-
-