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.
For most systems most of the time, this change will make no
difference, because a preceding memset() writes a bunch of binary
zeros to the pointer array. However not all systems represent
NULL as all-bits-zero.
There is a further subtlety: the memset() doesn't fill the entire
array. It fills a number of bytes equal to the number of pointers
in the array. However a pointer occupies more than one byte (with
rare exceptions). The result is that, on a system with four-byte
pointers, only one fourth of the pointers get nullified.
This incomplete nullification probably explains the following
mysterious fragment that occurs after building the array of token
pointers:
// not sure why (strtok?), but this is necessary
memset( words + i, 0, COMMAND_BUFSIZE - i );
For a sufficiently large number of tokens, where we exhaust the
pointers nullified by the first memset(), this second memset()
provides at least one NULL to terminate the list.
At least that's my theory. I have left the memsets alone for now.
I don't really know what sort of misbehavior the second memset() is
intended to prevent, so I can't test the effects of its removal.
Scott McKellar
http://home.swbell.net/mck9/ct/*** trunk/src/srfsh/srfsh.c 2007-07-12 17:47:09.000000000 -0500
--- trunk-mod/src/srfsh/srfsh.c 2007-07-12 19:09:12.000000000 -0500
***************
*** 50,56 ****
/* 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,
--- 50,55 ----
***************
*** 72,79 ****
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[] ) {
--- 71,76 ----
***************
*** 121,130 ****
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))) {
--- 118,123 ----
***************
*** 142,148 ****
free(request);
free(req_copy);
- fflush(shell_writer);
fflush(stderr);
fflush(stdout);
}
--- 135,140 ----
***************
*** 233,239 ****
cur_tok = strtok( NULL, " " );
}
!
// not sure why (strtok?), but this is necessary
memset( words + i, 0, COMMAND_BUFSIZE - i );
--- 225,232 ----
cur_tok = strtok( NULL, " " );
}
! words[i] = NULL;
!
// not sure why (strtok?), but this is necessary
memset( words + i, 0, COMMAND_BUFSIZE - i );
***************
*** 271,292 ****
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;
}
--- 264,279 ----
ret_val = handle_login(words);
else if (words[0][0] == '!') {
system( original_request + 1 );
ret_val = 1;
}
free( original_request );
! if(!ret_val)
! return parse_error( words );
! else
! return 1;
}
***************
*** 484,549 ****
}
- /* 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 ) {
--- 471,476 ----