// 'recv' handler static void shell_recv( char* args ) { args = args; #ifndef BUILD_XMODEM printf( "XMODEM support not compiled, unable to recv\n" ); return; #else // #ifndef BUILD_XMODEM char *p; long actsize; lua_State* L; if( ( shell_prog = malloc( XMODEM_INITIAL_BUFFER_SIZE ) ) == NULL ) { printf( "Unable to allocate memory\n" ); return; } printf( "Waiting for file ... " ); if( ( actsize = xmodem_receive( &shell_prog ) ) < 0 ) { free( shell_prog ); shell_prog = NULL; if( actsize == XMODEM_ERROR_OUTOFMEM ) printf( "file too big\n" ); else printf( "XMODEM error\n" ); return; } // Eliminate the XMODEM padding bytes p = shell_prog + actsize - 1; while( *p == '\x1A' ) p --; p ++; printf( "done, got %u bytes\n", ( unsigned )( p - shell_prog ) ); // Execute if( ( L = lua_open() ) == NULL ) { printf( "Unable to create Lua state\n" ); free( shell_prog ); shell_prog = NULL; return; } luaL_openlibs( L ); if( luaL_loadbuffer( L, shell_prog, p - shell_prog, "xmodem" ) != 0 ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); else if( lua_pcall( L, 0, LUA_MULTRET, 0 ) != 0 ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); lua_close( L ); free( shell_prog ); shell_prog = NULL; #endif // #ifndef BUILD_XMODEM }
void shell_recv( int argc, char **argv ) { char *p; long actsize; if( ( argc > 2 ) || ( argc == 0 ) ) { SHELL_SHOW_HELP( recv ); return; } if( ( shell_prog = malloc( XMODEM_INITIAL_BUFFER_SIZE ) ) == NULL ) { printf( "Unable to allocate memory\n" ); return; } printf( "Waiting for file ... " ); if( ( actsize = xmodem_receive( &shell_prog ) ) < 0 ) { if( actsize == XMODEM_ERROR_OUTOFMEM ) printf( "file too big\n" ); else printf( "XMODEM error\n" ); goto exit; } // Eliminate the XMODEM padding bytes p = shell_prog + actsize - 1; while( *p == '\x1A' ) p --; p ++; printf( "done, got %u bytes\n", ( unsigned )( p - shell_prog ) ); // we've received an argument, try saving it to a file if( argc == 2 ) { FILE *foutput = fopen( argv[ 1 ], "w" ); size_t file_sz = p - shell_prog; if( foutput == NULL ) { printf( "unable to open file %s\n", argv[ 1 ] ); goto exit; } if( fwrite( shell_prog, sizeof( char ), file_sz, foutput ) == file_sz ) printf( "received and saved as %s\n", argv[ 1 ] ); else printf( "unable to save file %s (no space left on target?)\n", argv[ 1 ] ); fclose( foutput ); } exit: free( shell_prog ); shell_prog = NULL; }
// 'recv' handler static void shell_recv( char* args ) { args = args; #ifndef BUILD_XMODEM printf( "XMODEM support not compiled, unable to recv\n" ); return; #else // #ifndef BUILD_XMODEM char *p; long actsize; lua_State* L; if( ( shell_prog = malloc( shell_max_prog + 1 ) ) == NULL ) { printf( "Unable to allocate memory\n" ); return; } printf( "Waiting for file ... " ); while( 1 ) { actsize = xmodem_receive( shell_prog, shell_max_prog ); if( actsize > 0 ) break; if( actsize == XMODEM_ERROR_OUTOFMEM ) { printf( "file too big\n ... " ); printf( "Waiting for file ... " ); } } shell_prog[ actsize ] = 0; if( ( p = strchr( shell_prog, '\x1A' ) ) != NULL ) *p = 0; else p = shell_prog + strlen( shell_prog ); printf( "done, got %ld bytes\n", p - shell_prog ); if( ( L = lua_open() ) == NULL ) { printf( "Unable to create Lua state\n" ); free( shell_prog ); shell_prog = NULL; return; } luaL_openlibs( L ); if( luaL_dostring( L, shell_prog ) ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); lua_close( L ); free( shell_prog ); shell_prog = NULL; #endif // #ifndef BUILD_XMODEM }
// 'recv' handler static void shell_recv( char* args ) { args = args; #ifndef BUILD_XMODEM printf( "XMODEM support not compiled, unable to recv\n" ); return; #else // #ifndef BUILD_XMODEM char *p; long actsize; lua_State* L; if( ( shell_prog = malloc( XMODEM_INITIAL_BUFFER_SIZE ) ) == NULL ) { printf( "Unable to allocate memory\n" ); return; } printf( "Waiting for file ... " ); if( ( actsize = xmodem_receive( &shell_prog ) ) < 0 ) { free( shell_prog ); shell_prog = NULL; if( actsize == XMODEM_ERROR_OUTOFMEM ) printf( "file too big\n" ); else printf( "XMODEM error\n" ); return; } // Eliminate the XMODEM padding bytes p = shell_prog + actsize - 1; while( *p == '\x1A' ) p --; p ++; printf( "done, got %u bytes\n", ( unsigned )( p - shell_prog ) ); // we've received an argument, try saving it to a file if( strcmp( args, "") != 0 ) { FILE *foutput = fopen( args, "w" ); size_t file_sz = p - shell_prog; if( foutput == NULL ) { printf( "unable to open file %s\n", args); free( shell_prog ); shell_prog = NULL; return; } if( fwrite( shell_prog, sizeof( char ), file_sz, foutput ) == file_sz ) printf( "received and saved as %s\n", args ); else printf( "disk full, unable to save file %s\n", args ); fclose( foutput ); } else // no arg, running the file with lua. { if( ( L = lua_open() ) == NULL ) { printf( "Unable to create Lua state\n" ); free( shell_prog ); shell_prog = NULL; return; } luaL_openlibs( L ); if( luaL_loadbuffer( L, shell_prog, p - shell_prog, "xmodem" ) != 0 ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); else if( lua_pcall( L, 0, LUA_MULTRET, 0 ) != 0 ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); lua_close( L ); } free( shell_prog ); shell_prog = NULL; #endif // #ifndef BUILD_XMODEM }
void shell_recv( int argc, char **argv ) { char *p; long actsize; lua_State* L; if( argc > 2 ) { SHELL_SHOW_HELP( recv ); return; } if( ( shell_prog = malloc( XMODEM_INITIAL_BUFFER_SIZE ) ) == NULL ) { printf( "Unable to allocate memory\n" ); return; } printf( "Waiting for file ... " ); if( ( actsize = xmodem_receive( &shell_prog ) ) < 0 ) { if( actsize == XMODEM_ERROR_OUTOFMEM ) printf( "file too big\n" ); else printf( "XMODEM error\n" ); goto exit; } // Eliminate the XMODEM padding bytes p = shell_prog + actsize - 1; while( *p == '\x1A' ) p --; p ++; printf( "done, got %u bytes\n", ( unsigned )( p - shell_prog ) ); // we've received an argument, try saving it to a file if( argc == 2 ) { FILE *foutput = fopen( argv[ 1 ], "w" ); size_t file_sz = p - shell_prog; if( foutput == NULL ) { printf( "unable to open file %s\n", argv[ 1 ] ); goto exit; } if( fwrite( shell_prog, sizeof( char ), file_sz, foutput ) == file_sz ) printf( "received and saved as %s\n", argv[ 1 ] ); else printf( "unable to save file %s (no space left on target?)\n", argv[ 1 ] ); fclose( foutput ); } else // no arg, running the file with lua. { if( ( L = lua_open() ) == NULL ) { printf( "Unable to create Lua state\n" ); goto exit; } luaL_openlibs( L ); if( luaL_loadbuffer( L, shell_prog, p - shell_prog, "xmodem" ) != 0 ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); else if( lua_pcall( L, 0, LUA_MULTRET, 0 ) != 0 ) printf( "Error: %s\n", lua_tostring( L, -1 ) ); lua_close( L ); } exit: free( shell_prog ); shell_prog = NULL; }