1
0
mirror of https://github.com/elua/elua.git synced 2025-01-25 01:02:54 +08:00

Code added for an "ls" (or "dir") shell command

This commit is contained in:
Dado Sutter 2008-11-24 13:48:46 +00:00
parent fd7cbd0109
commit 13d6993b43
3 changed files with 61 additions and 9 deletions

View File

@ -9,11 +9,13 @@
#define MAX_FNAME_LENGTH 14
/*******************************************************************************
We assume that the "filesystem" resides in a contiguous zone of memory, with the
following structure:
The Read-Only "filesystem" resides in a contiguous zone of memory, with the
following structure, repeated for each file:
Filename: ASCIIZ, max length is MAX_FNAME_LENGTH defined here, empty if last file
File size: (2 bytes)
File data: (file size bytes)
filename (ASCIIZ, max length is MAX_FNAME_LENGTH defined in "netdefs.h", empty if last file)
file size (2 bytes)
*******************************************************************************/
enum
@ -34,8 +36,9 @@ typedef struct
u16 size;
p_read_fs_byte p_read_func;
} FS;
// FS functions
DM_DEVICE* romfs_init();
u16 romfs_get_dir_entry(u16 offset, char *fname, int *fsize);
#endif

View File

@ -37,6 +37,11 @@ static void romfs_close_fd( int fd )
memset( romfs_fd_table + fd, 0, sizeof( FS ) );
}
// Open the given file, returning one of FS_FILE_NOT_FOUND, FS_FILE_ALREADY_OPENED
// or FS_FILE_OK
u8 romfs_open_file( const char* fname, p_read_fs_byte p_read_func, FS* pfs )
@ -160,6 +165,7 @@ static int romfs_ioctl_r( struct _reent *r, int fd, unsigned long request, void
return -1;
}
// Our UART device descriptor structure
static DM_DEVICE romfs_device =
{
@ -176,6 +182,28 @@ DM_DEVICE* romfs_init()
return &romfs_device;
}
// Retrieves file name and size from ROMFS entry at romfiles[offset]
// Returns the next file entry offset or null on last entry
u16 romfs_get_dir_entry( u16 offset, char *fname, int *fsize )
{
u16 i;
int j;
i = offset;
j = 0;
if ( romfs_read( i ) != 0 )
{
while ( ( fname[j++] = romfs_read( i++ )));
*fsize = (int) ( romfs_read( i ) + ( romfs_read( i+1 ) << 8 ) );
return (u16) ( i + 2 + *fsize );
}
else
return 0;
}
#else // #ifdef BUILD_ROMFS
DM_DEVICE* romfs_init()

View File

@ -11,6 +11,7 @@
#include <stdlib.h>
#include "platform.h"
#include "elua_net.h"
#include "romfs.h"
#include "platform_conf.h"
#ifdef BUILD_SHELL
@ -46,11 +47,12 @@ static void shell_help( char* args )
{
args = args;
printf( "Shell commands:\n" );
printf( " help - print this help\n" );
printf( " exit - exit from this shell\n" );
printf( " help - print this help\n" );
printf( " ls or dir - lists ROMFS files and sizes\n" );
printf( " lua [args] - run Lua with the given arguments\n" );
printf( " recv - receive a file (XMODEM) and execute it\n" );
printf( " ver - print eLua version\n" );
printf( " exit - exit from this shell\n" );
printf( " recv - receive a file (XMODEM) and execute it\n" );
printf( " ver - print eLua version\n" );
}
// 'lua' handler
@ -164,6 +166,22 @@ static void shell_ver( char* args )
printf( "For more information go to http://elua.berlios.de\n" );
}
// 'ls' and 'dir' handler
static void shell_ls( char* args )
{
u16 offset;
char fname[MAX_FNAME_LENGTH + 1];
int size;
args = args;
offset = 0;
while ( offset = romfs_get_dir_entry( offset, fname, &size ) )
printf(" %s %d bytes \n", fname, size);
}
// Insert shell commands here
static const SHELL_COMMAND shell_commands[] =
{
@ -172,9 +190,12 @@ static const SHELL_COMMAND shell_commands[] =
{ "recv", shell_recv },
{ "ver", shell_ver },
{ "exit", NULL },
{ "ls", shell_ls },
{ "dir", shell_ls },
{ NULL, NULL }
};
// Execute the eLua "shell" in an infinite loop
void shell_start()
{