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

Last change on this file since 660 was 660, checked in by guerin, 9 years ago

shell: use readdir for ls

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