source: soft/giet_vm/applications/shell/main.c @ 687

Last change on this file since 687 was 687, checked in by alain, 9 years ago

Introduce the ps command in shell.
Adapt the router application.

File size: 8.4 KB
RevLine 
[589]1///////////////////////////////////////////////////////////////////////////////////////
2// File   : main.c   (for shell application)
[640]3// Date   : july 2015
4// author : Clément Guérin
[589]5///////////////////////////////////////////////////////////////////////////////////////
[640]6// Simple shell for GIET_VM.
[589]7///////////////////////////////////////////////////////////////////////////////////////
8
9#include "stdio.h"
[640]10#include "string.h"
[589]11#include "malloc.h"
12
[640]13#define BUF_SIZE    (256)
14#define MAX_ARGS    (32)
[589]15
[640]16struct command_t
17{
18    char *name;
19    void (*fn)(int, char**);
20};
[589]21
[640]22////////////////////////////////////////////////////////////////////////////////
[643]23//  Shell  Commands
24////////////////////////////////////////////////////////////////////////////////
[640]25
26struct command_t cmd[];
27
[643]28///////////////////////////////////////////
[640]29static void cmd_help(int argc, char** argv)
[589]30{
[640]31    int i;
[589]32
[640]33    giet_tty_printf("available commands:\n");
[610]34
[640]35    for (i = 0; cmd[i].name; i++)
[589]36    {
[640]37        giet_tty_printf("\t%s\n", cmd[i].name);
[589]38    }
[640]39}
40
[643]41///////////////////////////////////////////////
[640]42static void cmd_proctime(int argc, char** argv)
43{
44    giet_tty_printf("%u\n", giet_proctime());
45}
46
[643]47/////////////////////////////////////////
[640]48static void cmd_ls(int argc, char** argv)
49{
[660]50    int fd;
51    fat_dirent_t entry;
52
[640]53    if (argc < 2)
[660]54        fd = giet_fat_opendir("/");
55    else
56        fd = giet_fat_opendir(argv[1]);
57
58    if (fd < 0)
[589]59    {
[660]60        giet_tty_printf("can't list directory (err=%d)\n", fd);
[640]61        return;
[589]62    }
63
[660]64    while (giet_fat_readdir(fd, &entry) == 0)
65    {
66        if (entry.is_dir)
67            giet_tty_printf("dir ");
68        else
69            giet_tty_printf("file");
70
71        giet_tty_printf(" | size = %d \t| cluster = %X \t| %s\n",
72                        entry.size, entry.cluster, entry.name );
73    }
74
75    giet_fat_closedir(fd);
[640]76}
77
[643]78////////////////////////////////////////////
[640]79static void cmd_mkdir(int argc, char** argv)
80{
81    if (argc < 2)
[589]82    {
[640]83        giet_tty_printf("%s <path>\n", argv[0]);
84        return;
[589]85    }
[640]86
87    int ret = giet_fat_mkdir(argv[1]);
88    if (ret < 0)
[589]89    {
[640]90        giet_tty_printf("can't create directory (err=%d)\n", ret);
[589]91    }
[640]92}
[589]93
[643]94/////////////////////////////////////////
[640]95static void cmd_cp(int argc, char** argv)
96{
97    if (argc < 3)
[589]98    {
[640]99        giet_tty_printf("%s <src> <dst>\n", argv[0]);
100        return;
[589]101    }
102
[640]103    char buf[1024];
104    int src_fd = -1;
105    int dst_fd = -1;
[660]106    fat_file_info_t info;
[640]107    int size;
108    int i;
109
110    src_fd = giet_fat_open( argv[1] , O_RDONLY );
111    if (src_fd < 0)
[589]112    {
[640]113        giet_tty_printf("can't open %s (err=%d)\n", argv[1], src_fd);
114        goto exit;
[589]115    }
[640]116
117    giet_fat_file_info(src_fd, &info);
118    if (info.is_dir)
[589]119    {
[655]120        giet_tty_printf("can't copy a directory\n");
[640]121        goto exit;
[589]122    }
[640]123    size = info.size;
[589]124
[654]125    dst_fd = giet_fat_open( argv[2] , O_CREATE | O_TRUNC );
[640]126    if (dst_fd < 0)
[589]127    {
[672]128        giet_tty_printf("can't open %s (err=%d)\n", argv[2], dst_fd);
[640]129        goto exit;
[589]130    }
131
[640]132    giet_fat_file_info(dst_fd, &info);
133    if (info.is_dir)
[589]134    {
[655]135        giet_tty_printf("can't copy to a directory\n"); // TODO
[640]136        goto exit;
[589]137    }
[640]138
139    i = 0;
140    while (i < size)
[589]141    {
[640]142        int len = (size - i < 1024 ? size - i : 1024);
143        int wlen;
144
[655]145        giet_tty_printf("\rwrite %d/%d (%d%%)", i, size, 100*i/size);
146
[640]147        len = giet_fat_read(src_fd, &buf, len);
148        wlen = giet_fat_write(dst_fd, &buf, len);
149        if (wlen != len)
150        {
[655]151            giet_tty_printf("\nwrite error\n");
[640]152            goto exit;
153        }
154        i += len;
[589]155    }
[655]156    giet_tty_printf("\n");
[589]157
[640]158exit:
159    if (src_fd >= 0)
160        giet_fat_close(src_fd);
161    if (dst_fd >= 0)
162        giet_fat_close(dst_fd);
163}
164
[669]165/////////////////////////////////////////
[640]166static void cmd_rm(int argc, char **argv)
167{
168    if (argc < 2)
[589]169    {
[640]170        giet_tty_printf("%s <file>\n", argv[0]);
171        return;
[589]172    }
[640]173
174    int ret = giet_fat_remove(argv[1], 0);
175    if (ret < 0)
[589]176    {
[640]177        giet_tty_printf("can't remove %s (err=%d)\n", argv[1], ret);
[589]178    }
[640]179}
[589]180
[643]181////////////////////////////////////////////
[640]182static void cmd_rmdir(int argc, char **argv)
183{
184    if (argc < 2)
[589]185    {
[640]186        giet_tty_printf("%s <path>\n", argv[0]);
187        return;
[589]188    }
[640]189
190    int ret = giet_fat_remove(argv[1], 1);
191    if (ret < 0)
[589]192    {
[640]193        giet_tty_printf("can't remove %s (err=%d)\n", argv[1], ret);
[589]194    }
[640]195}
[589]196
[643]197/////////////////////////////////////////
[640]198static void cmd_mv(int argc, char **argv)
199{
200    if (argc < 3)
[589]201    {
[640]202        giet_tty_printf("%s <src> <dst>\n", argv[0]);
203        return;
[589]204    }
[640]205
206    int ret = giet_fat_rename(argv[1], argv[2]);
207    if (ret < 0)
[589]208    {
[640]209        giet_tty_printf("can't move %s to %s (err=%d)\n", argv[1], argv[2], ret);
[589]210    }
[640]211}
[589]212
[643]213///////////////////////////////////////////
214static void cmd_exec(int argc, char **argv)
215{
216    if (argc < 2)
217    {
218        giet_tty_printf("%s <pathname>\n", argv[0]);
219        return;
220    }
221
222    int ret = giet_exec_application(argv[1]);
223    if ( ret == -1 )
224    {
225        giet_tty_printf("\n  error : %s not found\n", argv[1] );
226    }
227}
228
229///////////////////////////////////////////
230static void cmd_kill(int argc, char **argv)
231{
232    if (argc < 2)
233    {
234        giet_tty_printf("%s <pathname>\n", argv[0]);
235        return;
236    }
237
238    int ret = giet_kill_application(argv[1]);
239    if ( ret == -1 )
240    {
241        giet_tty_printf("\n  error : %s not found\n", argv[1] );
242    }
243    if ( ret == -2 )
244    {
245        giet_tty_printf("\n  error : %s cannot be killed\n", argv[0] );
246    }
247}
248
[687]249///////////////////////////////////////////////
250static void cmd_ps(int argc, char** argv)
251{
252    giet_tasks_status();
253}
254
[643]255////////////////////////////////////////////////////////////////////
[640]256struct command_t cmd[] =
257{
258    { "help",       cmd_help },
259    { "proctime",   cmd_proctime },
260    { "ls",         cmd_ls },
261    { "mkdir",      cmd_mkdir },
262    { "cp",         cmd_cp },
263    { "rm",         cmd_rm },
264    { "rmdir",      cmd_rmdir },
265    { "mv",         cmd_mv },
[643]266    { "exec",       cmd_exec },
267    { "kill",       cmd_kill },
[687]268    { "ps",         cmd_ps },
[640]269    { NULL,         NULL }
270};
271
[643]272// shell
[640]273
[643]274///////////////////////////////////////
275static void parse(char *buf, int count)
[640]276{
277    int argc = 0;
278    char* argv[MAX_ARGS];
279    int i;
280    int len = strlen(buf);
281
282    // build argc/argv
283    for (i = 0; i < len; i++)
[589]284    {
[640]285        if (buf[i] == ' ')
286        {
287            buf[i] = '\0';
288        }
289        else if (i == 0 || buf[i - 1] == '\0')
290        {
291            if (argc < MAX_ARGS)
292            {
293                argv[argc] = &buf[i];
294                argc++;
295            }
296        }
[589]297    }
[640]298
299    if (argc > 0)
[589]300    {
[640]301        int found = 0;
302
303        // try to match typed command with built-ins
304        for (i = 0; cmd[i].name; i++)
305        {
306            if (strcmp(argv[0], cmd[i].name) == 0)
307            {
308                // invoke
309                cmd[i].fn(argc, argv);
310                found = 1;
311                break;
312            }
313        }
314
315        if (!found)
316        {
[643]317            giet_tty_printf("undefined command %s\n", argv[0]);
[640]318        }
[589]319    }
[640]320}
[589]321
[643]322////////////////////
[640]323static void prompt()
324{
325    giet_tty_printf("# ");
326}
327
328//////////////////////////////////////////
329__attribute__ ((constructor)) void main()
330//////////////////////////////////////////
331{
332    char c;
333    char buf[BUF_SIZE];
334    int count = 0;
335
[669]336    // get a private TTY
337    giet_tty_alloc( 0 );
338
339    // display first prompt
[640]340    prompt();
341
342    while (1)
[589]343    {
[640]344        giet_tty_getc(&c);
345
346        switch (c)
347        {
348        case '\b':      // backspace
349            if (count > 0)
350            {
351                giet_tty_printf("\b \b");
352                count--;
353            }
354            break;
355        case '\n':      // new line
356            giet_tty_printf("\n");
357            if (count > 0)
358            {
359                buf[count] = '\0';
[643]360                parse((char*)&buf, count);
[640]361            }
362            prompt();
363            count = 0;
364            break;
365        case '\t':      // tabulation
366            // do nothing
367            break;
368        case '\03':     // ^C
369            giet_tty_printf("^C\n");
370            prompt();
371            count = 0;
372            break;
373        default:        // regular character
374            if (count < sizeof(buf) - 1)
375            {
376                giet_tty_printf("%c", c);
377                buf[count] = c;
378                count++;
379            }
380        }
[589]381    }
382} // end main()
383
384// Local Variables:
[640]385// tab-width: 4
386// c-basic-offset: 4
[589]387// c-file-offsets:((innamespace . 0)(inline-open . 0))
388// indent-tabs-mode: nil
389// End:
[640]390// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
[589]391
Note: See TracBrowser for help on using the repository browser.