00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef lint
00042 static const char RCScmd[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/cmd.c,v 14.11 2006/09/16 02:04:24 lbutler Exp $ (BRL)";
00043 #endif
00044
00045 #include "common.h"
00046
00047
00048
00049 #include <stdio.h>
00050 #include <ctype.h>
00051 #ifdef HAVE_STRING_H
00052 #include <string.h>
00053 #else
00054 #include <strings.h>
00055 #endif
00056 #include "machine.h"
00057 #include "vmath.h"
00058 #include "raytrace.h"
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 char *
00072 rt_read_cmd(register FILE *fp)
00073 {
00074 register int c;
00075 register char *buf;
00076 register int curpos;
00077 register int curlen;
00078
00079 curpos = 0;
00080 curlen = 400;
00081 buf = bu_malloc( curlen, "rt_read_cmd command buffer" );
00082
00083 do {
00084 c = fgetc(fp);
00085 if( c == EOF ) {
00086 c = '\0';
00087 } else if( c == '#' ) {
00088
00089 while( (c = fgetc(fp)) != EOF && c != '\n' ) ;
00090 continue;
00091 } else if( c == '\n' ) {
00092 c = ' ';
00093 } else if( c == ';' ) {
00094 c = '\0';
00095 } else if( c == '\\' ) {
00096
00097
00098
00099
00100 c = fgetc(fp);
00101 } else if( !isascii(c) ) {
00102 c = '?';
00103 }
00104 if( c != '\0' && curpos == 0 && isspace(c) ) {
00105
00106
00107
00108 continue;
00109 }
00110 if( curpos >= curlen ) {
00111 curlen *= 2;
00112 buf = bu_realloc( buf, curlen, "rt_read_cmd command buffer" );
00113 }
00114 buf[curpos++] = c;
00115 } while( c != '\0' );
00116 if( curpos <= 1 ) {
00117 bu_free( buf, "rt_read_cmd command buffer (EOF)" );
00118 return( (char *)0 );
00119 }
00120 return( buf );
00121 }
00122
00123 #define MAXWORDS 4096
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 int
00137 rt_split_cmd(char **argv, int lim, register char *lp)
00138 {
00139 register int nwords;
00140 register char *lp1;
00141
00142 argv[0] = "_NIL_";
00143
00144 while( *lp != '\0' && isspace( *lp ) )
00145 lp++;
00146
00147 if( *lp == '\0' )
00148 return(0);
00149
00150 #ifdef HAVE_SHELL_ESCAPE
00151
00152 if( *lp == '!' ) {
00153 int ret;
00154 ret = system( lp+1 );
00155 if( ret != 0 ) {
00156 perror("system(3)");
00157 bu_log("rt_split_cmd() FAILED: !%s\n", lp);
00158 }
00159 return(0);
00160 }
00161 #endif
00162
00163
00164 nwords = 1;
00165 argv[0] = lp;
00166
00167 for( ; *lp != '\0'; lp++ ) {
00168 if( !isspace( *lp ) )
00169 continue;
00170
00171 *lp = '\0';
00172 lp1 = lp + 1;
00173 if( *lp1 != '\0' && !isspace( *lp1 ) ) {
00174
00175 if( nwords >= lim-1 )
00176 break;
00177
00178 argv[nwords++] = lp1;
00179 }
00180 }
00181 argv[nwords] = (char *)0;
00182 return( nwords );
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 int
00197 rt_do_cmd(struct rt_i *rtip, const char *ilp, register const struct command_tab *tp)
00198
00199
00200
00201 {
00202 register int nwords;
00203 char *cmd_args[MAXWORDS+1];
00204 char *lp;
00205 int retval;
00206
00207 lp = bu_malloc(strlen(ilp)+1, "rt_do_cmd lp");
00208 strcpy(lp, ilp);
00209
00210 nwords = rt_split_cmd( cmd_args, MAXWORDS, lp );
00211 if( nwords <= 0 )
00212 return(0);
00213
00214
00215 for( ; tp->ct_cmd != (char *)0; tp++ ) {
00216 if( cmd_args[0][0] != tp->ct_cmd[0] ||
00217
00218 strncmp( cmd_args[0], tp->ct_cmd, MAXWORDS ) != 0 )
00219 continue;
00220 if( (nwords >= tp->ct_min) && (nwords <= tp->ct_max) ) {
00221 retval = tp->ct_func( nwords, cmd_args );
00222 bu_free(lp, "rt_do_cmd lp");
00223 return retval;
00224 }
00225 bu_log("rt_do_cmd Usage: %s %s\n\t%s\n",
00226 tp->ct_cmd, tp->ct_parms, tp->ct_comment );
00227 bu_free(lp, "rt_do_cmd lp");
00228 return(-1);
00229 }
00230 bu_log("rt_do_cmd(%s): command not found\n", cmd_args[0]);
00231 bu_free(lp, "rt_do_cmd lp");
00232 return(-1);
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243