# HG changeset patch # User Paul Boddie # Date 1646587232 -3600 # Node ID 2f782c1df69aa57e27929526883c329d7871d895 # Parent 1a43c2393e2575b60ea7f235ad0fb822a5fb5709 Moved the script operation and general operation handling to separate files. diff -r 1a43c2393e25 -r 2f782c1df69a libe2access/host/Makefile --- a/libe2access/host/Makefile Sun Mar 06 01:30:32 2022 +0100 +++ b/libe2access/host/Makefile Sun Mar 06 18:20:32 2022 +0100 @@ -42,7 +42,7 @@ # Sources and objects. -E2ACCESS_SRC = e2access.c file.c input.c session.c $(wildcard op_*.c) +E2ACCESS_SRC = e2access.c file.c input.c session.c ops.c $(wildcard op_*.c) E2ACCESS_OBJ = $(E2ACCESS_SRC:.c=.o) TEST_LISTING_SRC = test_listing.c diff -r 1a43c2393e25 -r 2f782c1df69a libe2access/host/e2access.c --- a/libe2access/host/e2access.c Sun Mar 06 01:30:32 2022 +0100 +++ b/libe2access/host/e2access.c Sun Mar 06 18:20:32 2022 +0100 @@ -20,97 +20,15 @@ */ #include -#include #include #include -#include "input.h" #include "ops.h" #include "session.h" -/* Copy buffer size. */ - -extern int BUFSIZE; - -/* Maximum number of arguments in scripts. */ - -const int MAX_ARGS = 32; - - - -/* Read operations from a script file. */ - -enum op_results -{ - OP_SUCCESS = 0, - OP_FAILED = 1, - OP_UNKNOWN = 2, -}; - -int handle_op_result(const char *operation, enum op_results op_result) -{ - if (op_result == OP_UNKNOWN) - { - fprintf(stderr, "Operation not recognised: %s\n", operation); - return 1; - } - else if (op_result == OP_FAILED) - { - fprintf(stderr, "Operation failed: %s\n", operation); - return 1; - } - else - return 0; -} - -enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]); - -int run_script(ext2_filsys fs, int argc, char *argv[]) -{ - FILE *fp; - char buffer[BUFSIZE]; - struct read_line_state state; - enum op_results op_result; - int num_args; - char *args[MAX_ARGS]; - int i; - - for (i = 0; i < argc; i++) - { - fp = fopen(argv[i], "r"); - - state.buffer = buffer; - state.start = buffer; - state.end = buffer; - state.buffer_size = BUFSIZE; - state.remaining = BUFSIZE - 1; - - while (read_line(fp, &state) != NULL) - { - parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); - - if (num_args > 1) - { - op_result = run_operation(fs, args[0], num_args - 1, &args[1]); - - if (handle_op_result(args[0], op_result)) - return 1; - } - - state.start = state.eolp + 1; - } - - fclose(fp); - } - - return 0; -} - - - /* Help message. */ char help_text[] = "\ @@ -145,13 +63,7 @@ /* Operations exposed by the program. */ -struct operation -{ - const char *name; - int (*fn)(ext2_filsys, int, char *[]); -}; - -static struct operation operations[] = { +struct operation operations[] = { {"copy-in", copy_in}, {"copy-out", copy_out}, {"ls", list}, @@ -163,30 +75,6 @@ {NULL, NULL}, }; -/* Invocation of operations. */ - -enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]) -{ - struct operation *op; - int exitcode; - - for (op = &operations[0]; op->name != NULL; op++) - { - if (!strcmp(operation, op->name)) - { - exitcode = op->fn(fs, argc, argv); - if (exitcode) - return OP_FAILED; - break; - } - } - - if (op->name == NULL) - return OP_UNKNOWN; - - return OP_SUCCESS; -} - /* Main program. */ int main(int argc, char *argv[]) diff -r 1a43c2393e25 -r 2f782c1df69a libe2access/host/op_script.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libe2access/host/op_script.c Sun Mar 06 18:20:32 2022 +0100 @@ -0,0 +1,86 @@ +/* + * Run a script accessing a filesystem. + * + * Copyright (C) 2022 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include + +#include "input.h" +#include "ops.h" + + + +/* Copy buffer size. */ + +extern int BUFSIZE; + +/* Maximum number of arguments in scripts. */ + +const int MAX_ARGS = 32; + + + +/* Read operations from a script file. */ + +int run_script(ext2_filsys fs, int argc, char *argv[]) +{ + FILE *fp; + char buffer[BUFSIZE]; + struct read_line_state state; + enum op_results op_result; + int num_args; + char *args[MAX_ARGS]; + int i; + + for (i = 0; i < argc; i++) + { + fp = fopen(argv[i], "r"); + + state.buffer = buffer; + state.start = buffer; + state.end = buffer; + state.buffer_size = BUFSIZE; + state.remaining = BUFSIZE - 1; + + while (read_line(fp, &state) != NULL) + { + parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); + + if (num_args > 1) + { + op_result = run_operation(fs, args[0], num_args - 1, &args[1]); + + if (handle_op_result(args[0], op_result)) + return 1; + } + + state.start = state.eolp + 1; + } + + fclose(fp); + } + + return 0; +} + +/* vim: tabstop=4 expandtab shiftwidth=4 +*/ diff -r 1a43c2393e25 -r 2f782c1df69a libe2access/host/ops.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libe2access/host/ops.c Sun Mar 06 18:20:32 2022 +0100 @@ -0,0 +1,78 @@ +/* + * Operation invocation. + * + * Copyright (C) 2019, 2022 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include "ops.h" + + + +/* Operations exposed by the program. */ + +extern struct operation operations[]; + + + +/* Produce a general result condition from the invocation of an operation. */ + +int handle_op_result(const char *operation, enum op_results op_result) +{ + if (op_result == OP_UNKNOWN) + { + fprintf(stderr, "Operation not recognised: %s\n", operation); + return 1; + } + else if (op_result == OP_FAILED) + { + fprintf(stderr, "Operation failed: %s\n", operation); + return 1; + } + else + return 0; +} + +/* Invocation of operations. */ + +enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]) +{ + struct operation *op; + int exitcode; + + for (op = &operations[0]; op->name != NULL; op++) + { + if (!strcmp(operation, op->name)) + { + exitcode = op->fn(fs, argc, argv); + if (exitcode) + return OP_FAILED; + break; + } + } + + if (op->name == NULL) + return OP_UNKNOWN; + + return OP_SUCCESS; +} + +/* vim: tabstop=4 expandtab shiftwidth=4 +*/ diff -r 1a43c2393e25 -r 2f782c1df69a libe2access/host/ops.h --- a/libe2access/host/ops.h Sun Mar 06 01:30:32 2022 +0100 +++ b/libe2access/host/ops.h Sun Mar 06 18:20:32 2022 +0100 @@ -1,5 +1,5 @@ /* - * Operations. + * Operations and their invocation. * * Copyright (C) 2022 Paul Boddie * @@ -21,12 +21,37 @@ #pragma once +#include + +/* Invocation support. */ + +enum op_results +{ + OP_SUCCESS = 0, + OP_FAILED = 1, + OP_UNKNOWN = 2, +}; + +int handle_op_result(const char *operation, enum op_results op_result); +enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]); + +/* Operations exposed by the program. */ + +typedef int (*op_sig)(ext2_filsys, int, char *[]); + +struct operation +{ + const char *name; + op_sig fn; +}; + int copy_in(ext2_filsys fs, int argc, char *argv[]); int copy_out(ext2_filsys fs, int argc, char *argv[]); int list(ext2_filsys fs, int argc, char *argv[]); int make_dirs(ext2_filsys fs, int argc, char *argv[]); int remove_dirs(ext2_filsys fs, int argc, char *argv[]); int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]); +int run_script(ext2_filsys fs, int argc, char *argv[]); int stat_objects(ext2_filsys fs, int argc, char *argv[]); /* vim: tabstop=4 expandtab shiftwidth=4