* 'readline' is configured to provide context sensitive completion
* and help.
*/
-static void interact(void)
+static void interact(struct state *st)
{
- struct state st = {0};
char *home, *hist;
- st.lafs = lafs_alloc();
- st.verbose = 1;
rl_attempted_completion_function = complete_in_context;
rl_basic_word_break_characters = " \t\n=";
rl_completer_quote_characters = "\"'";
asprintf(&hist, "%s/.lafs_history", home);
read_history(hist);
- gen_state = &st;
- while (!st.done) {
+ gen_state = st;
+ while (!st->done) {
char *line = readline("LaFS: ");
if (!line) {
if (*line)
add_history(line);
- execute_line(&st, line);
+ execute_line(st, line);
free(line);
}
* given with commands. It reads and executes commands until
* it finds an error.
*/
-static void runfile(FILE *f)
+static void runfile(struct state *st, FILE *f)
{
- struct state st = {0};
- st.lafs = lafs_alloc();
- st.verbose = 0;
- while (!st.done) {
+ while (!st->done) {
char *line = NULL;
ssize_t len;
size_t size;
len = getline(&line, &size, f);
if (len <= 0)
- st.done = 1;
- else if (execute_line(&st, line) < 0)
- st.done = 1;
+ st->done = 1;
+ else if (execute_line(st, line) < 0)
+ st->done = 1;
free(line);
}
int main(int argc, char *argv[])
{
+ struct state st = {0};
+ st.lafs = lafs_alloc();
if (argc > 1) {
if (strcmp(argv[1], "-") == 0)
- runfile(stdin);
+ runfile(&st, stdin);
else {
FILE *f = fopen(argv[1], "r");
- if (f)
- runfile(f);
- else
+ if (f) {
+ runfile(&st, f);
+ fclose(f);
+ } else
fprintf(stderr, "lafs: cannot open %s\n", argv[1]);
}
- } else
- interact();
+ } else {
+ st.verbose = 1;
+ interact(&st);
+ }
exit(0);
}
lafs_dirty_inode(segmap);
lafs_imap_set(imfile, usage_inum);
- printf("Added device %s at %llu with %llu segments of %llu %dk blocks\n"
- " Usage inode %d\n",
- devname, (unsigned long long)dev->start,
+ if (st->verbose)
+ printf("Added device %s at %llu with %llu segments "
+ "of %llu %dk blocks\n"
+ " Usage inode %d\n",
+ devname, (unsigned long long)dev->start,
(unsigned long long)dev->segment_count,
- (unsigned long long)dev->segment_size,
- st->lafs->blocksize/1024, usage_inum);
+ (unsigned long long)dev->segment_size,
+ st->lafs->blocksize/1024, usage_inum);
}
if (err) {
printf("mount: cannot mount filesystem: %s\n", err);
free(err);
- }
+ } else if (st->verbose)
+ printf("filesystem mounted\n");
}
/****** SHOW *****/
lafs_dirty_blk(db);
bnum++;
}
- printf("Created %s as inode %d in %d\n", tail, inum, dir->inum);
+ if (st->verbose)
+ printf("Created %s as inode %d in %d\n", tail, inum, dir->inum);
}
/****** LS ******/
lafs_trace_level = level;
}
+/****** SOURCE ******/
+static char help_source[] = "Read commands from a file";
+static struct args args_source[] = {
+ { "FILE", external, -1, {NULL}, "File to read commands from"},
+ TERMINAL_ARG
+};
+static void c_source(struct state *st, void *args[])
+{
+ char *fname = args[1];
+ FILE *f;
+ if (!fname) {
+ printf("source: no file name given\n");
+ return;
+ }
+ f = fopen(fname, "r");
+ if (f) {
+ st->verbose = 0;
+ runfile(st, f);
+ st->verbose = 1;
+ st->done = 0;
+ fclose(f);
+ } else
+ printf("source: cannot open %s\n", fname);
+}
+
/***********************************************************/
/* list of all commands - preferably in alphabetical order */
#define CMD(x) {#x, c_##x, args_##x, help_##x}
CMD(quit),
CMD(reset),
CMD(show),
+ CMD(source),
CMD(store),
CMD(trace),
CMD(write),