From: NeilBrown Date: Fri, 25 Mar 2011 22:04:02 +0000 (+1100) Subject: Add 'source' command X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=3858ec561b2de7611abb1340c20181d4d10ad905;p=lafs-utils.git Add 'source' command this allows a command file to be sourced. Signed-off-by: NeilBrown --- diff --git a/tools/lafs.c b/tools/lafs.c index 1a21b23..b1a82b7 100644 --- a/tools/lafs.c +++ b/tools/lafs.c @@ -377,12 +377,9 @@ static char **complete_in_context(const char *word, int start, int end); * '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 = "\"'"; @@ -394,8 +391,8 @@ static void interact(void) 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) { @@ -405,7 +402,7 @@ static void interact(void) if (*line) add_history(line); - execute_line(&st, line); + execute_line(st, line); free(line); } @@ -416,13 +413,10 @@ static void interact(void) * 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; @@ -430,9 +424,9 @@ static void runfile(FILE *f) 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); } @@ -441,18 +435,23 @@ static void runfile(FILE *f) 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); } @@ -1022,12 +1021,14 @@ static void c_add_device(struct state *st, void **args) 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); } @@ -1232,7 +1233,8 @@ static void c_mount(struct state *st, void **args) if (err) { printf("mount: cannot mount filesystem: %s\n", err); free(err); - } + } else if (st->verbose) + printf("filesystem mounted\n"); } /****** SHOW *****/ @@ -1643,7 +1645,8 @@ static void c_store(struct state *st, void **args) 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 ******/ @@ -1781,6 +1784,31 @@ static void c_trace(struct state *st, void **args) 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} @@ -1797,6 +1825,7 @@ static struct cmd lafs_cmds[] = { CMD(quit), CMD(reset), CMD(show), + CMD(source), CMD(store), CMD(trace), CMD(write),