Changeset 747 for soft


Ignore:
Timestamp:
Dec 15, 2015, 4:29:02 PM (8 years ago)
Author:
alain
Message:

Introduce the cat command in the shell.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/applications/shell/shell.c

    r712 r747  
    1111#include "malloc.h"
    1212
    13 #define BUF_SIZE    (256)
    14 #define MAX_ARGS    (32)
     13#define BUF_SIZE    (256)        // buffer for one command
     14#define MAX_ARGS    (32)         // max number of arguments in a command
    1515
    1616
     
    6969    while (giet_fat_readdir(fd, &entry) == 0)
    7070    {
    71         if (entry.is_dir)
    72             giet_tty_printf("dir ");
    73         else
    74             giet_tty_printf("file");
    75 
    76         giet_tty_printf(" | size = %d \t| cluster = %X \t| %s\n",
     71        if (entry.is_dir) giet_tty_printf("dir ");
     72        else              giet_tty_printf("file");
     73
     74        giet_tty_printf(" | size = %d \t| cluster = %x \t| %s\n",
    7775                        entry.size, entry.cluster, entry.name );
    7876    }
     
    307305}
    308306
     307/////////////////////////////////////////////
     308static void cmd_cat(int argc, char** argv)
     309{
     310    if (argc < 2)
     311    {
     312        giet_tty_printf("  usage : %s <path_name> \n", argv[0] );
     313        return;
     314    }
     315
     316    unsigned int     x,y,p;   // processor coordinates
     317    unsigned int     fd;             // file descriptor
     318    fat_file_info_t  info;           // file info
     319    unsigned int     size;           // buffer size (file_size + 1)
     320    unsigned int     len;            // number of read bytes from file
     321    char*            buf;            // temporary buffer
     322
     323    // get processor coordinates
     324    giet_proc_xyp( &x , &y , &p );
     325   
     326    // open the file to display   
     327    fd = giet_fat_open( argv[1] , O_RDONLY );
     328    if (fd < 0)
     329    {
     330        giet_tty_printf("  error : cannot open %s\n", argv[1]);
     331        goto exit;
     332    }
     333
     334    // get file size
     335    giet_fat_file_info( fd, &info );
     336    if ( info.is_dir )
     337    {
     338        giet_tty_printf("  error : %s is a directory\n", argv[1] );
     339        goto exit;
     340    }
     341    size = info.size; 
     342
     343    // allocate a temporary buffer for the file
     344    heap_init( x , y );
     345    buf = (char*)malloc( size ); 
     346    if( buf == NULL )
     347    {
     348        giet_tty_printf("  error : cannot allocate buffer with size = %d\n", size );
     349        goto exit;
     350    }
     351    // load the file and set terminating '0'
     352    len = giet_fat_read( fd , buf , size );
     353    if ( len != size )
     354    {
     355        giet_tty_printf("  error : cannot load file %s / size = %d / len = %d\n",
     356                        argv[1] , size , len );
     357        goto exit;
     358    }
     359    buf[size] = 0;
     360
     361    // display the file content
     362    giet_tty_printf("\n%s", buf );
     363
     364exit:
     365    if ( fd >= 0 )     giet_fat_close( fd );
     366    if ( buf != NULL ) free( buf );
     367}
    309368
    310369////////////////////////////////////////////////////////////////////
    311370struct command_t cmd[] =
    312371{
     372    { "cat",        "display file content",                 cmd_cat },
     373    { "context",    "display a thread context",             cmd_context },
     374    { "cp",         "replicate a file in file system",      cmd_cp },
     375    { "exec",       "start an application",                 cmd_exec },
    313376    { "help",       "list available commands",              cmd_help },
    314     { "time",       "return current date",                  cmd_time },
     377    { "kill",       "kill an application (all threads)",    cmd_kill },
    315378    { "ls",         "list content of a directory",          cmd_ls },
    316379    { "mkdir",      "create a new directory",               cmd_mkdir },
    317     { "cp",         "replicate a file in file system",      cmd_cp },
     380    { "mv",         "move a file in file system",           cmd_mv },
     381    { "pause",      "pause a thread",                       cmd_pause },
     382    { "ps",         "list all mapped applications status",  cmd_ps },
     383    { "resume",     "resume a thread",                      cmd_resume },
    318384    { "rm",         "remove a file from file system",       cmd_rm },
    319385    { "rmdir",      "remove a directory from file system",  cmd_rmdir },
    320     { "mv",         "move a file in file system",           cmd_mv },
    321     { "exec",       "start an application",                 cmd_exec },
    322     { "kill",       "kill an application (all threads)",    cmd_kill },
    323     { "ps",         "list all mapped applications status",  cmd_ps },
    324     { "pause",      "pause a thread",                       cmd_pause },
    325     { "resume",     "resume a thread",                      cmd_resume },
    326     { "context",    "display a thread context",             cmd_context },
     386    { "time",       "return current date",                  cmd_time },
    327387    { NULL,         NULL,                                   NULL }
    328388};
     
    330390// shell
    331391
    332 ////////////////////////////
     392///////////////////////////////////////////////////////////////////
     393// This function analyses one command (with arguments)
     394///////////////////////////////////////////////////////////////////
    333395static void parse(char *buf)
    334396{
     
    405467        switch (c)
    406468        {
    407         case '\b':      // backspace
     469        case '\b':                       // backspace
    408470            if (count > 0)
    409471            {
     
    412474            }
    413475            break;
    414         case '\n':      // new line
     476        case '\n':                       // new line
    415477            giet_tty_printf("\n");
    416478            if (count > 0)
     
    422484            count = 0;
    423485            break;
    424         case '\t':      // tabulation
     486        case '\t':                       // tabulation
    425487            // do nothing
    426488            break;
    427         case '\03':     // ^C
     489        case '\03':                      // ^C
    428490            giet_tty_printf("^C\n");
    429491            prompt();
    430492            count = 0;
    431493            break;
    432         default:        // regular character
     494        default:                         // regular character
    433495            if (count < sizeof(buf) - 1)
    434496            {
Note: See TracChangeset for help on using the changeset viewer.