cmd.c

Go to the documentation of this file.
00001 /*                           C M D . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1998-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 /** @addtogroup butcl */
00022 /*@{*/
00023 /** @file libbu/cmd.c
00024  * @brief
00025  *      Utility routines for handling commands.
00026  *
00027  * @author      Robert G. Parker
00028  *
00029  * @par Source -
00030  *      SLAD CAD Team                                   @n
00031  *      The U. S. Army Research Laboratory              @n
00032  *      Aberdeen Proving Ground, Maryland  21005
00033  *
00034  */
00035 #include "common.h"
00036 
00037 #ifdef HAVE_STRING_H
00038 #  include <string.h>
00039 #else
00040 #  include <strings.h>
00041 #endif
00042 
00043 #include "tcl.h"
00044 #include "machine.h"
00045 #include "cmd.h"                        /* includes bu.h */
00046 
00047 /**
00048  *
00049  *      bu_cmd
00050  *
00051  *      This function is intended to be used for parsing subcommands.
00052  *      If the command is found in the array of commands, the associated
00053  *      function is called. Otherwise, an error message is created and
00054  *      added to interp.
00055  *
00056  *      @param clientData       - data/state associated with the command
00057  *      @param interp           - tcl interpreter wherein this command is registered
00058  *                        (Note - the result of the command is also stored here)
00059  *      @param argc             - number of arguments in argv
00060  *      @param argv             - command to execute and its arguments
00061  *      @param cmds             - commands and related function pointers
00062  *      @param cmd_index        - indicates which argv element holds the subcommand
00063  *
00064  *      @return TCL_OK if successful, otherwise, TCL_ERROR.
00065  */
00066 int
00067 bu_cmd(ClientData       clientData,
00068        Tcl_Interp       *interp,
00069        int              argc,
00070        char             **argv,
00071        struct bu_cmdtab *cmds,
00072        int              cmd_index)
00073 {
00074         register struct bu_cmdtab *ctp;
00075 
00076         /* sanity */
00077         if (cmd_index >= argc) {
00078                 Tcl_AppendResult(interp,
00079                                  "missing command; must be one of:",
00080                                  (char *)NULL);
00081                 goto missing_cmd;
00082         }
00083 
00084         for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++) {
00085                 if (ctp->ct_name[0] == argv[cmd_index][0] &&
00086                     strcmp(ctp->ct_name, argv[cmd_index]) == 0) {
00087                         return (*ctp->ct_func)(clientData, interp, argc, argv);
00088                 }
00089         }
00090 
00091         Tcl_AppendResult(interp,
00092                          "unknown command: ",
00093                          argv[cmd_index], ";",
00094                          " must be one of: ",
00095                          (char *)NULL);
00096 
00097 missing_cmd:
00098         for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++) {
00099                 Tcl_AppendResult(interp, " ", ctp->ct_name, (char *)NULL);
00100         }
00101         Tcl_AppendResult(interp, "\n", (char *)NULL);
00102 
00103         return TCL_ERROR;
00104 }
00105 
00106 /**
00107  *
00108  *      bu_register_cmds
00109  *
00110  *      This is a convenience routine for registering an array of commands
00111  *      with a Tcl interpreter. Note - this is not intended for use by
00112  *      commands with associated state (i.e. ClientData).
00113  *
00114  *      @param interp           - Tcl interpreter wherein to register the commands
00115  *      @param cmds             - commands and related function pointers
00116  *
00117  * @return
00118  *      void
00119  */
00120 void
00121 bu_register_cmds(Tcl_Interp             *interp,
00122                  struct bu_cmdtab       *cmds)
00123 {
00124         register struct bu_cmdtab *ctp;
00125 
00126         for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++)
00127                 (void)Tcl_CreateCommand(interp, ctp->ct_name, ctp->ct_func,
00128                                         (ClientData)ctp, (Tcl_CmdDeleteProc *)NULL);
00129 }
00130 /*@}*/
00131 /*
00132  * Local Variables:
00133  * mode: C
00134  * tab-width: 8
00135  * c-basic-offset: 4
00136  * indent-tabs-mode: t
00137  * End:
00138  * ex: shiftwidth=4 tabstop=8
00139  */

Generated on Mon Sep 18 01:24:47 2006 for BRL-CAD by  doxygen 1.4.6