• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/maloc/tools/tests/vsh/main.c

00001 /*
00002  * ***************************************************************************
00003  * MALOC = < Minimal Abstraction Layer for Object-oriented C >
00004  * Copyright (C) 1994--2008 Michael Holst
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  *
00020  * rcsid="$Id: main.c,v 1.12 2008/03/12 05:14:00 fetk Exp $"
00021  * ***************************************************************************
00022  */
00023 
00024 /*
00025  * ***************************************************************************
00026  * File:     main.c
00027  *
00028  * Purpose:  Main driver for testing the Vsh_shell.
00029  *
00030  * Author:   Michael Holst
00031  * ***************************************************************************
00032  */
00033 
00034 #include <maloc/maloc.h>
00035 
00036 #define VEMBED(rctag) VPRIVATE const char* rctag; \
00037     static void* use_rcsid=(0 ? &use_rcsid : (void*)&rcsid);
00038 VEMBED(rcsid="$Id: main.c,v 1.12 2008/03/12 05:14:00 fetk Exp $")
00039 
00040 /* Some help to organize the application-specific shell commands */
00041 typedef enum APPcommand {
00042     app_none,
00043     app_help,
00044     app_stat,
00045     app_hello,
00046     app_bye
00047 } APPcommand;
00048 
00049 /*
00050  * ***************************************************************************
00051  * Routine:  APPgetCmd
00052  *
00053  * Purpose: The application-specific shell parser
00054  *
00055  * Author:   Michael Holst
00056  * ***************************************************************************
00057  */
00058 VPRIVATE APPcommand APPgetCmd(void *thee, int argc, char *argv[])
00059 {
00060     APPcommand theCmd = app_none;
00061     if (!strcmp(argv[0],"")) {
00062         theCmd = app_none;
00063     } else if (!strcmp(argv[0],"help")) {
00064         theCmd = app_help;
00065     } else if (!strcmp(argv[0],"stat")) {
00066         theCmd = app_stat;
00067     } else if (!strcmp(argv[0],"hello")) {
00068         theCmd = app_hello;
00069     } else if (!strcmp(argv[0],"bye")) {
00070         theCmd = app_bye;
00071     } else {
00072         theCmd = app_none;
00073     }
00074     return theCmd;
00075 }
00076 
00077 /*
00078  * ***************************************************************************
00079  * Routine:  APPsh
00080  *
00081  * Purpose: The application-specific shell for enriching the vsh_shell
00082  *          or for overriding the builtin vsh_shell commands.
00083  *
00084  * Return codes (required):
00085  *
00086  *     rc=0   --> APPsh does not know about this command
00087  *     rc=1   --> APPsh handled this command sucessfully
00088  *     rc=2   --> APPsh handled this command sucessfully and wants to exit!
00089  *
00090  * Author:   Michael Holst
00091  * ***************************************************************************
00092  */
00093 VPRIVATE int APPsh(void *pthee, int argc, char *argv[])
00094 {
00095     Vsh *thee = (Vsh*)pthee;
00096 
00097     int rc;
00098     APPcommand theCmd;
00099     static int init=0;
00100 
00101     /* one-time intialization */
00102     if (!init) {
00103        init=1;
00104     }
00105 
00106     /* default return code (success) */
00107     rc = 1;
00108 
00109     /* get the command */
00110     theCmd = APPgetCmd(thee, argc, argv);
00111 
00112     /* decode and execute the command */
00113     switch (theCmd) {
00114 
00115       case app_help:
00116         if (argc==1) {
00117             Vnm_print(1,"%s: Application-layer Help Menu:\n",
00118                 Vsh_getenv(thee,"SHELL"));
00119             Vnm_print(1,"    help      --> Print this menu.\n");
00120             Vnm_print(1,"    stat      --> Print some environ variables.\n");
00121             Vnm_print(1,"    hello     --> Print 'Hello, World!'.\n");
00122             Vnm_print(1,"    bye       --> Print 'Bye, World!' and exit.\n");
00123             rc = 0;  /* pretend we didn't see it so subshell can help too */
00124         } else {
00125             rc = 0;  /* pretend we didn't see it so subshell can help too */
00126         }
00127         break;
00128 
00129       case app_stat:
00130         Vnm_print(1,"%s(APPsh): IFNAM=<%s>  LMAX=<%d>  LTOL=<%e>\n",
00131             Vsh_getenv(thee,"SHELL"),
00132             Vsh_getenv(thee,"IFNAM"),
00133             Vsh_getenvInt(thee,"LMAX"),
00134             Vsh_getenvReal(thee,"LTOL"));
00135         break;
00136 
00137       case app_hello:
00138         Vnm_print(1,"%s(APPsh): Hello, World!\n",
00139             Vsh_getenv(thee,"SHELL"));
00140         break;
00141 
00142       case app_bye:
00143         Vnm_print(1,"%s(APPsh): Bye, World!\n",
00144             Vsh_getenv(thee,"SHELL"));
00145         rc = 2;
00146         break;
00147 
00148       default:
00149         rc = 0;
00150         break;
00151     }
00152     return rc;
00153 }
00154 
00155 /*
00156  * ***************************************************************************
00157  * Routine:  main
00158  *
00159  * Purpose: The main driver for initiating the Vsh_shell.
00160  *
00161  * Author:   Michael Holst
00162  * ***************************************************************************
00163  */
00164 int main(int argc, char **argv)
00165 {
00166     int rc;
00167     Vsh *vsh;
00168 
00169     /* ********************************************************************* */
00170     /* NOTE: avoiding Vnm_print before Vsh_ctor() allows output file tagging */
00171     /* ********************************************************************* */
00172 
00173     vsh = Vsh_ctor(VNULL, argc, argv);
00174     rc = Vsh_shell(vsh, VNULL, vsh, &APPsh);
00175     Vsh_dtor(&vsh);
00176 
00177     /* some final i/o */
00178     Vnm_print(1,"\n");
00179     Vnm_print(1,"maloc_leaks = [\n");
00180     Vnm_print(1,"%% --------------------------------------"
00181                 "--------------------------------------\n");
00182     Vnm_print(1,"%%  Footprint        Areas       Malloc         Free"
00183                 "    Highwater   Class\n"),
00184     Vnm_print(1,"%% --------------------------------------"
00185                 "--------------------------------------\n");
00186     Vmem_print(VNULL);
00187     Vmem_printTotal();
00188     Vnm_print(1,"%% --------------------------------------"
00189                 "--------------------------------------\n");
00190     Vnm_print(1,"];\n");
00191 
00192     /* normal return */
00193     return rc;
00194 }
00195 

Generated on Wed Oct 20 2010 11:12:14 for APBS by  doxygen 1.7.2