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