Merge branch 'master' of ssh://Aleaxander@repo.or.cz/srv/git/fstk
[fstk.git] / shell.c
blob8b7df787c8b2d248fbecda29be6aa629857fdd52
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "version.h"
5 #include "fstk.h"
6 #include "fs.h"
7 #include "file.h"
8 #include "fstklib.h"
9 #include "fstk_malloc.h"
10 #include "cache.h"
11 #include "command.h"
13 #include <readline/readline.h>
14 #include <readline/history.h>
17 static int done = 0;
20 static inline void fstk_shell_help(void)
22 printf("fs --open a new file sytem\n"
23 "help --print this information\n"
24 "print [fs] --print the current fs information\n"
25 "quit, q --quit the program\n"
26 "cat [file]... --show the file content\n"
27 "check_fat --check the vfat FAT table\n"
28 "ls [file|dir]... --list the file or diectory\n"
29 "dir [file|dir}... --the same with 'ls'\n"
30 "block [file|dir]. --show the file block address\n"
31 "sector [file|dir] --show the file sector address\n"
32 "stat [file|dir].. --show the detail information\n");
35 static int parse_shell(char *shell, char *argv[])
37 int argc = 0;
38 char *p = NULL;
40 while(*shell == ' ')
41 shell++;
42 if (!*shell)
43 goto out;
44 while((p = strchr(shell, ' '))) {
45 *p++ = 0;
46 while(*p == ' ')
47 p++;
48 argv[argc++] = shell;
49 shell = p;
51 if (!*shell)
52 goto out;
53 argv[argc++] = shell;
54 out:
55 return argc;
58 static inline int is_cmd(char *cmd, char *input)
60 return !strcmp(cmd, input);
64 #define MAX_ARGC 255
66 int fstk_shell(const char *prompt, struct fstk *fs)
68 int argc = 0;
69 char **argv = malloc(MAX_ARGC * sizeof(char *));
70 char *line;
71 char *cmd;
74 /* the shell part */
75 printf("\tfstk shell, V%s (Copyright) Liu Aleaxander\n"
76 "\ttype 'quit' or 'q' to quit, 'help' for more information\n",
77 VERSION_STR);
79 while (!done) {
80 line = readline(prompt);
82 if (!line)
83 break;
84 add_history(line);
86 argc = parse_shell(line, argv);
87 cmd = argv[0];
89 if (!argc) {
90 continue;
91 } else if (is_cmd(cmd, "quit") || is_cmd(cmd, "q")) {
92 done = 1;
93 break;
94 } else if (is_cmd(cmd, "print")) {
95 if (is_cmd(argv[1], "fs"))
96 fs->ops->print_fs_info(fs);
97 } else if (is_cmd(cmd, "help")) {
98 fstk_shell_help();
99 } else if (is_cmd(cmd, "fs")) {
100 fstk_unmount(fs);
101 return MOUNT_AGAIN;
102 } else if (is_cmd(cmd, "cat")) {
103 if (argc > 1)
104 fstk_cat(fs, argc, argv);
105 else
106 printf("USAGE: cat file\n");
107 } else if (is_cmd(cmd, "ls")) {
108 fstk_ls(fs, argc, argv);
109 } else if (is_cmd(cmd, "cd")) {
110 fstk_cd(fs, argv[1]);
111 } else if (is_cmd(cmd, "check_fat")) {
112 fstk_check_fat(fs);
113 } else if (is_cmd(cmd, "block")) {
114 if (*argv[1])
115 fstk_block(fs, argc, argv);
116 else
117 printf("USAGE: block file | dir\n");
118 } else if (is_cmd(cmd, "sector")) {
119 if (argc > 1)
120 fstk_sector(fs, argc, argv);
121 else
122 printf("USAGE: sector file | dir\n");
123 } else if (is_cmd(cmd, "stat")) {
124 if (argc > 1)
125 fstk_stat(fs, argc, argv);
126 else
127 printf("USAGE: stat file | dir\n");
128 } else
129 printf("Unknow command! type 'help' for more info.\n");
132 return 0;