1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* ***************************************************************************
* MALOC = < Minimal Abstraction Layer for Object-oriented C >
* Copyright (C) 1994--2000 Michael Holst
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*
* rcsid="$Id: main.c,v 1.10 2006/05/08 04:39:44 mholst Exp $"
* ***************************************************************************
*/
/*
* ***************************************************************************
* File: main.c
*
* Purpose: Main driver for testing the Vsh_shell.
*
* Author: Michael Holst
* ***************************************************************************
*/
#include <maloc/maloc.h>
#define VEMBED(rctag) VPRIVATE const char* rctag; \
static void* use_rcsid=(0 ? &use_rcsid : (void*)&rcsid);
VEMBED(rcsid="$Id: main.c,v 1.10 2006/05/08 04:39:44 mholst Exp $")
/* Some help to organize the application-specific shell commands */
typedef enum APPcommand {
app_none,
app_help,
app_stat,
app_hello,
app_bye
} APPcommand;
/*
* ***************************************************************************
* Routine: APPgetCmd
*
* Purpose: The application-specific shell parser
*
* Author: Michael Holst
* ***************************************************************************
*/
VPRIVATE APPcommand APPgetCmd(void *thee, int argc, char *argv[])
{
APPcommand theCmd = app_none;
if (!strcmp(argv[0],"")) {
theCmd = app_none;
} else if (!strcmp(argv[0],"help")) {
theCmd = app_help;
} else if (!strcmp(argv[0],"stat")) {
theCmd = app_stat;
} else if (!strcmp(argv[0],"hello")) {
theCmd = app_hello;
} else if (!strcmp(argv[0],"bye")) {
theCmd = app_bye;
} else {
theCmd = app_none;
}
return theCmd;
}
/*
* ***************************************************************************
* Routine: APPsh
*
* Purpose: The application-specific shell for enriching the vsh_shell
* or for overriding the builtin vsh_shell commands.
*
* Return codes (required):
*
* rc=0 --> APPsh does not know about this command
* rc=1 --> APPsh handled this command sucessfully
* rc=2 --> APPsh handled this command sucessfully and wants to exit!
*
* Author: Michael Holst
* ***************************************************************************
*/
VPRIVATE int APPsh(void *pthee, int argc, char *argv[])
{
Vsh *thee = (Vsh*)pthee;
int rc;
APPcommand theCmd;
static int init=0;
/* one-time intialization */
if (!init) {
init=1;
}
/* default return code (success) */
rc = 1;
/* get the command */
theCmd = APPgetCmd(thee, argc, argv);
/* decode and execute the command */
switch (theCmd) {
case app_help:
if (argc==1) {
Vnm_print(1,"%s: Application-layer Help Menu:\n",
Vsh_getenv(thee,"SHELL"));
Vnm_print(1," help --> Print this menu.\n");
Vnm_print(1," stat --> Print some environ variables.\n");
Vnm_print(1," hello --> Print 'Hello, World!'.\n");
Vnm_print(1," bye --> Print 'Bye, World!' and exit.\n");
rc = 0; /* pretend we didn't see it so subshell can help too */
} else {
rc = 0; /* pretend we didn't see it so subshell can help too */
}
break;
case app_stat:
Vnm_print(1,"%s(APPsh): IFNAM=<%s> LMAX=<%d> LTOL=<%e>\n",
Vsh_getenv(thee,"SHELL"),
Vsh_getenv(thee,"IFNAM"),
Vsh_getenvInt(thee,"LMAX"),
Vsh_getenvReal(thee,"LTOL"));
break;
case app_hello:
Vnm_print(1,"%s(APPsh): Hello, World!\n",
Vsh_getenv(thee,"SHELL"));
break;
case app_bye:
Vnm_print(1,"%s(APPsh): Bye, World!\n",
Vsh_getenv(thee,"SHELL"));
rc = 2;
break;
default:
rc = 0;
break;
}
return rc;
}
/*
* ***************************************************************************
* Routine: main
*
* Purpose: The main driver for initiating the Vsh_shell.
*
* Author: Michael Holst
* ***************************************************************************
*/
int main(int argc, char **argv)
{
int rc;
Vsh *vsh;
/* ********************************************************************* */
/* NOTE: avoiding Vnm_print before Vsh_ctor() allows output file tagging */
/* ********************************************************************* */
vsh = Vsh_ctor(VNULL, argc, argv);
rc = Vsh_shell(vsh, VNULL, vsh, &APPsh);
Vsh_dtor(&vsh);
/* some final i/o */
Vnm_print(1,"\n");
Vnm_print(1,"maloc_leaks = [\n");
Vnm_print(1,"%% --------------------------------------"
"--------------------------------------\n");
Vnm_print(1,"%% Footprint Areas Malloc Free"
" Highwater Class\n"),
Vnm_print(1,"%% --------------------------------------"
"--------------------------------------\n");
Vmem_print(VNULL);
Vmem_printTotal();
Vnm_print(1,"%% --------------------------------------"
"--------------------------------------\n");
Vnm_print(1,"];\n");
/* normal return */
return rc;
}
|