[Opensrf-commits] r1032 - in branches/new-json2: . src/srfsh
svn at svn.open-ils.org
svn at svn.open-ils.org
Fri Jul 13 14:59:01 EDT 2007
Author: erickson
Date: 2007-07-13 14:54:00 -0400 (Fri, 13 Jul 2007)
New Revision: 1032
Modified:
branches/new-json2/
branches/new-json2/src/srfsh/srfsh.c
Log:
Merged revisions 1026-1031 via svnmerge from
svn://svn.open-ils.org/OpenSRF/trunk
........
r1029 | erickson | 2007-07-12 14:21:22 -0400 (Thu, 12 Jul 2007) | 3 lines
Initialized merge tracking via "svnmerge" with revisions "1003" from
svn://svn.open-ils.org/OpenSRF/branches/new-json2
........
r1030 | miker | 2007-07-12 23:15:51 -0400 (Thu, 12 Jul 2007) | 46 lines
Patch from Scott McKellar, with modifications, to remove unused code and provide some memory protection:
This patch contains one minor change and one less minor change.
--------
The less minor change is the elimination of some old code that once
opened a pipe to send commands to bash. According to Bill Erickson
in a private email, that code is a leftover remnant from an
experiment and may be removed.
Before a patch was applied several days ago, things worked like this:
IF you compiled srfsh with a certain macro #defined, AND you entered
a command that was invalid to srfsh, THEN srfsh would pipe the
command to bash for execution.
It is doubtful that anyone but Bill ever used this variation, but if
anyone wants to keep it around, then this is his or her chance to
protest. Before protesting, however, please note that the shell
escape mechanism now provides shell-like command processing. I.e.
if you start the srfsh command line with an exclamation point, srfsh
passes the rest of the command to the default shell (normally
/bin/sh) for execution. The command so passed can use environmental
variables, pipes, IO redirection, wild card expansion, and most of
the sorts of things you're used to from the shell command line.
Note that not all shells behave the same. If you're used to using
tcsh as your shell, for example, you may find that sh won't work
quite the same way. For now, at least, that's just too bad, but
that's the way shell escapes typically work.
It is probably possible to invoke the user's default shell as
identified by the environmental variable $SHELL. However that
nicety would not be trivial to code.
----------
The minor change is that, after building an array of pointers
pointing to tokens from the input command, I set the next
pointer to NULL so that it mark the end of the token list.
(miker: I changed to calloc/free instead of an array, but left the final
NULL in place.)
........
r1031 | miker | 2007-07-13 10:52:20 -0400 (Fri, 13 Jul 2007) | 1 line
changing back to an array, from calloc. more straight-forward, and avoids memset overhead
........
Property changes on: branches/new-json2
___________________________________________________________________
Name: svnmerge-integrated
- /trunk:1-1025
Modified: branches/new-json2/src/srfsh/srfsh.c
===================================================================
--- branches/new-json2/src/srfsh/srfsh.c 2007-07-13 14:52:20 UTC (rev 1031)
+++ branches/new-json2/src/srfsh/srfsh.c 2007-07-13 18:54:00 UTC (rev 1032)
@@ -50,7 +50,6 @@
/* handles app level requests */
static int handle_request( char* words[], int relay );
-//static int handle_exec(char* words[], int new_shell);
static int handle_set( char* words[]);
static int handle_print( char* words[]);
static int send_request( char* server,
@@ -72,10 +71,7 @@
static int recv_timeout = 120;
static int is_from_script = 0;
-static FILE* shell_writer = NULL;
-// static FILE* shell_reader = NULL;
-
int main( int argc, char* argv[] ) {
/* --------------------------------------------- */
@@ -121,10 +117,6 @@
client = osrf_system_get_transport_client();
- /* open the shell handle */
- shell_writer = popen( "bash", "w");
- //shell_reader = popen( "bash", "r");
-
/* main process loop */
char* request;
while((request=readline(prompt))) {
@@ -142,7 +134,6 @@
free(request);
free(req_copy);
- fflush(shell_writer);
fflush(stderr);
fflush(stdout);
}
@@ -212,14 +203,14 @@
if( request == NULL )
return 0;
- char * original_request = strdup( request );
+ char* original_request = strdup( request );
+ char* words[COMMAND_BUFSIZE];
int ret_val = 0;
int i = 0;
- char* words[COMMAND_BUFSIZE];
- memset(words,0,COMMAND_BUFSIZE);
- char* req = request;
+
+ char* req = request;
char* cur_tok = strtok( req, " " );
if( cur_tok == NULL )
@@ -228,15 +219,22 @@
return 0;
}
+ /* Load an array with pointers to */
+ /* the tokens as defined by strtok() */
+
while(cur_tok != NULL) {
- words[i++] = cur_tok;
- cur_tok = strtok( NULL, " " );
+ if( i < COMMAND_BUFSIZE - 1 ) {
+ words[i++] = cur_tok;
+ cur_tok = strtok( NULL, " " );
+ } else {
+ fprintf( stderr, "Too many tokens in command\n" );
+ free( original_request );
+ return 1;
+ }
}
-
- // not sure why (strtok?), but this is necessary
- memset( words + i, 0, COMMAND_BUFSIZE - i );
-
+ words[i] = NULL;
+
/* pass off to the top level command */
if( !strcmp(words[0],"router") )
ret_val = handle_router( words );
@@ -271,23 +269,16 @@
ret_val = handle_login(words);
else if (words[0][0] == '!') {
- //ret_val = handle_exec( words, 1 );
system( original_request + 1 );
ret_val = 1;
}
free( original_request );
- if(!ret_val) {
- #ifdef EXEC_DEFAULT
- return handle_exec( words, 0 );
- #else
- return parse_error( words );
- #endif
- }
-
- return 1;
-
+ if(!ret_val)
+ return parse_error( words );
+ else
+ return 1;
}
@@ -484,67 +475,7 @@
}
-/* if new shell, spawn a new child and subshell to do the work,
- otherwise pipe the request to the currently open (piped) shell */
-/*
-static int handle_exec(char* words[], int new_shell) {
- if(!words[0]) return 0;
-
- if( words[0] && words[0][0] == '!') {
- int len = strlen(words[0]);
- char command[len];
- memset(command,0,len);
-
- int i; // chop out the !
- for( i=1; i!= len; i++) {
- command[i-1] = words[0][i];
- }
-
- free(words[0]);
- words[0] = strdup(command);
- }
-
- if(new_shell) {
- signal(SIGCHLD, sig_child_handler);
-
- if(fork()) {
-
- waitpid(-1, 0, 0);
- if(child_dead) {
- signal(SIGCHLD,sig_child_handler);
- child_dead = 0;
- }
-
- } else {
- execvp( words[0], words );
- exit(0);
- }
-
- } else {
-
-
- growing_buffer* b = buffer_init(64);
- int i = 0;
- while(words[i])
- buffer_fadd( b, "%s ", words[i++] );
-
- buffer_add( b, "\n");
-
- fprintf( shell_writer, b->buf );
- buffer_free(b);
-
- fflush(shell_writer);
- usleep(1000);
-
- }
-
-
- return 1;
-}
-*/
-
-
static int handle_request( char* words[], int relay ) {
if(!client)
More information about the opensrf-commits
mailing list