10 #include <disktool_common.h>
12 #define ACESS_VERSION "0.15-pr"
13 #define VERSION_STR "Acess2 DiskTool v"ACESS_VERSION
15 int main(int argc, const char *argv[]);
16 void PrintUsage(void);
17 int RunCommand(int argc, const char *argv[]);
18 void RunScript(const char *Filename);
21 int main(int argc, const char *argv[])
24 for( int i = 1; i < argc; i ++ )
26 const char *arg = argv[i];
29 int rv = RunCommand(argc-i, argv+i);
41 else if( arg[1] != '-' )
47 fprintf(stderr, "Option '-s' requires an argument\n");
50 RunScript( argv[++i] );
53 fprintf(stderr, "Unkown option '-%c', try --help\n", *arg);
59 if( strcmp(arg, "--help") == 0 ) {
63 else if( strcmp(arg, "--version") == 0 ) {
64 fprintf(stderr, VERSION_STR);
68 fprintf(stderr, "Unknown option '%s', try --help\n", arg);
83 " disktool <commands...>\n"
86 " lvm <image> <ident>\n"
87 " - Register an image with LVM\n"
89 " `lvm ../AcessHDD.img HDD`\n"
90 " mount <image> <mountname>\n"
91 " - Bind an image to a name.\n"
93 " `mount ../AcessFDD.img FDD`\n"
94 " `mount :HDD/0 hda1`\n"
96 " - List a directory\n"
104 int RunCommand(int argc, const char *argv[])
106 if( argc < 1 ) return 0;
108 const char *name = argv[0];
109 if( strcmp("mount", name) == 0 )
113 fprintf(stderr, "mount takes 2 arguments (image and mountpoint)\n");
117 if( DiskTool_MountImage(argv[2], argv[1]) ) {
118 fprintf(stderr, "Unable to mount '%s' as '%s'\n", argv[1], argv[2]);
124 else if( strcmp("lvm", name) == 0 )
126 // Bind a "file" to LVM
128 fprintf(stderr, "lvm takes 2 arguments (iamge and ident)\n");
132 if( DiskTool_RegisterLVM(argv[2], argv[1]) ) {
133 fprintf(stderr, "Unable to register '%s' as LVM '%s'\n", argv[1], argv[2]);
139 else if( strcmp("ls", name) == 0 )
142 fprintf(stderr, "ls takes 1 argument (path)\n");
146 DiskTool_ListDirectory(argv[1]);
149 else if( strcmp("cp", name) == 0 )
153 fprintf(stderr, "cp takes 2 arguments (source and destination)\n");
157 DiskTool_Copy(argv[1], argv[2]);
161 else if( strcmp("cat", name) == 0 ) {
164 fprintf(stderr, "cat takes 1 argument (path)\n");
168 DiskTool_Cat(argv[1]);
173 fprintf(stderr, "Unknown command '%s'\n", name);
178 int tokenise(const char **ptrs, int max_ptrs, char *buffer)
183 // Eat leading whitespace
184 while( *buffer && isspace(*buffer) )
186 if( *buffer == '"' ) {
187 // Double-quoted string
189 ptrs[idx++] = buffer;
190 while( *buffer && *buffer != '"' )
192 if( *buffer == '\\' && buffer[1] == '"' ) {
205 // whitespace delimited string
206 ptrs[idx++] = buffer;
207 while( *buffer && !isspace(*buffer) )
216 void RunScript(const char *Filename)
218 FILE *fp = fopen(Filename, "r");
220 fprintf(stderr, "Unable to open script '%s': %s\n", Filename, strerror(errno));
226 while( NULL != fgets(buf, sizeof(buf), fp) )
229 const int max_tokens = 4;
230 const char *tokens[max_tokens];
231 int ntok = tokenise(tokens, max_tokens, buf);
233 if( ntok > max_tokens ) {
238 int rv = RunCommand(ntok, tokens);
239 assert(rv + 1 <= ntok);
244 else if( rv == -1 ) {
248 else if( rv + 1 != ntok ) {
249 // Too many arguments
257 // NOTE: This is in a native compiled file because it needs access to the real errno macro
258 int *Threads_GetErrno(void)
263 size_t _fwrite_stdout(size_t bytes, const void *data)
265 return fwrite(data, bytes, 1, stdout);