home *** CD-ROM | disk | FTP | other *** search
- /*
- * FILE: comapp.c
- * AUTHOR: R. Gonzalez
- * CREATED: August 25, 1990
- *
- * Defines command-line application methods.
- */
-
- # ifdef THINK_C
- # include <oops.h>
- # endif
-
- # include <string.h>
- # include <stdio.h>
- # include "comapp.h"
-
- # define MAX_LENGTH 80
-
- /************************************************************************
- * initialize application - programmer should override: first call
- * inherited method, then add new menu items
- ************************************************************************/
- boolean Command_App::init(void)
- {
- Generic_App::init();
- comline = new(Comline);
- comline->init();
-
- return TRUE;
- }
-
- /************************************************************************
- * execute application - no need to override
- ************************************************************************/
- void Command_App::run(void)
- {
- char command[MAX_LENGTH];
- int menu_num,
- item;
- comfunc command_ptr;
-
- respond("Type '?' for help.");
- respond("");
-
- while (!done && comline->read())
- {
- comline->get_command(command);
- if (strcmp(command,"") != 0)
- {
- menu_num = menu->get_menu(command);
- if (menu_num != ILLEGAL)
- {
- item = menu->get_item(command);
- command_ptr = menu->get_command_ptr(menu_num,item);
- if (!(*command_ptr)(this))
- respond("Illegal usage.");
- }
- else
- list_commands();
- }
- }
- }
-
- /************************************************************************
- * get string from user. For command-line application the question
- * is not used. No need to override.
- ************************************************************************/
- void Command_App::query(char *question,char answer[])
- {
- comline->get_next_argument(answer);
- }
-
- /************************************************************************
- * reply to user. No need to override.
- ************************************************************************/
- void Command_App::respond(char *response)
- {
- puts(response);
- }
-
- /************************************************************************
- * list commands logged into menu.
- ************************************************************************/
- void Command_App::list_commands(void)
- {
- int menu_num,
- item,
- added,
- length;
- char row[MAX_LENGTH];
-
- respond("");
- respond("Type first few letters of command;");
- respond("separate arguments using semicolons (;).");
- respond("");
-
- strcpy(row,"");
- length = 0;
-
- for (menu_num=1 ; menu_num <= menu->num_menus ; menu_num++)
- {
- for (item=1 ; item < menu->num_items[menu_num] ; item++)
- {
- added = strlen(menu->command[menu_num][item]) + 2;
- if (length+added >= MAX_LENGTH)
- {
- respond(row);
- strcpy(row,"");
- length = 0;
- }
-
- strcat(row,menu->command[menu_num][item]);
- strcat(row," ");
- length += added;
- }
-
- if (length > 0)
- {
- respond(row);
- strcpy(row,"");
- length = 0;
- }
- }
-
- respond("");
- }
-
- /************************************************************************
- * terminate application. Programmer should call inherited method
- * if the derived application overrides it.
- ************************************************************************/
- boolean Command_App::destroy(void)
- {
- Generic_App::destroy();
- comline->destroy();
- delete(comline);
-
- return TRUE;
- }
-
-