1.1 --- a/libe2access/host/Makefile Sun Mar 06 01:30:32 2022 +0100
1.2 +++ b/libe2access/host/Makefile Sun Mar 06 18:20:32 2022 +0100
1.3 @@ -42,7 +42,7 @@
1.4
1.5 # Sources and objects.
1.6
1.7 -E2ACCESS_SRC = e2access.c file.c input.c session.c $(wildcard op_*.c)
1.8 +E2ACCESS_SRC = e2access.c file.c input.c session.c ops.c $(wildcard op_*.c)
1.9 E2ACCESS_OBJ = $(E2ACCESS_SRC:.c=.o)
1.10
1.11 TEST_LISTING_SRC = test_listing.c
2.1 --- a/libe2access/host/e2access.c Sun Mar 06 01:30:32 2022 +0100
2.2 +++ b/libe2access/host/e2access.c Sun Mar 06 18:20:32 2022 +0100
2.3 @@ -20,97 +20,15 @@
2.4 */
2.5
2.6 #include <stdio.h>
2.7 -#include <string.h>
2.8 #include <unistd.h>
2.9
2.10 #include <ext2fs/ext2fs.h>
2.11
2.12 -#include "input.h"
2.13 #include "ops.h"
2.14 #include "session.h"
2.15
2.16
2.17
2.18 -/* Copy buffer size. */
2.19 -
2.20 -extern int BUFSIZE;
2.21 -
2.22 -/* Maximum number of arguments in scripts. */
2.23 -
2.24 -const int MAX_ARGS = 32;
2.25 -
2.26 -
2.27 -
2.28 -/* Read operations from a script file. */
2.29 -
2.30 -enum op_results
2.31 -{
2.32 - OP_SUCCESS = 0,
2.33 - OP_FAILED = 1,
2.34 - OP_UNKNOWN = 2,
2.35 -};
2.36 -
2.37 -int handle_op_result(const char *operation, enum op_results op_result)
2.38 -{
2.39 - if (op_result == OP_UNKNOWN)
2.40 - {
2.41 - fprintf(stderr, "Operation not recognised: %s\n", operation);
2.42 - return 1;
2.43 - }
2.44 - else if (op_result == OP_FAILED)
2.45 - {
2.46 - fprintf(stderr, "Operation failed: %s\n", operation);
2.47 - return 1;
2.48 - }
2.49 - else
2.50 - return 0;
2.51 -}
2.52 -
2.53 -enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]);
2.54 -
2.55 -int run_script(ext2_filsys fs, int argc, char *argv[])
2.56 -{
2.57 - FILE *fp;
2.58 - char buffer[BUFSIZE];
2.59 - struct read_line_state state;
2.60 - enum op_results op_result;
2.61 - int num_args;
2.62 - char *args[MAX_ARGS];
2.63 - int i;
2.64 -
2.65 - for (i = 0; i < argc; i++)
2.66 - {
2.67 - fp = fopen(argv[i], "r");
2.68 -
2.69 - state.buffer = buffer;
2.70 - state.start = buffer;
2.71 - state.end = buffer;
2.72 - state.buffer_size = BUFSIZE;
2.73 - state.remaining = BUFSIZE - 1;
2.74 -
2.75 - while (read_line(fp, &state) != NULL)
2.76 - {
2.77 - parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS);
2.78 -
2.79 - if (num_args > 1)
2.80 - {
2.81 - op_result = run_operation(fs, args[0], num_args - 1, &args[1]);
2.82 -
2.83 - if (handle_op_result(args[0], op_result))
2.84 - return 1;
2.85 - }
2.86 -
2.87 - state.start = state.eolp + 1;
2.88 - }
2.89 -
2.90 - fclose(fp);
2.91 - }
2.92 -
2.93 - return 0;
2.94 -}
2.95 -
2.96 -
2.97 -
2.98 /* Help message. */
2.99
2.100 char help_text[] = "\
2.101 @@ -145,13 +63,7 @@
2.102
2.103 /* Operations exposed by the program. */
2.104
2.105 -struct operation
2.106 -{
2.107 - const char *name;
2.108 - int (*fn)(ext2_filsys, int, char *[]);
2.109 -};
2.110 -
2.111 -static struct operation operations[] = {
2.112 +struct operation operations[] = {
2.113 {"copy-in", copy_in},
2.114 {"copy-out", copy_out},
2.115 {"ls", list},
2.116 @@ -163,30 +75,6 @@
2.117 {NULL, NULL},
2.118 };
2.119
2.120 -/* Invocation of operations. */
2.121 -
2.122 -enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[])
2.123 -{
2.124 - struct operation *op;
2.125 - int exitcode;
2.126 -
2.127 - for (op = &operations[0]; op->name != NULL; op++)
2.128 - {
2.129 - if (!strcmp(operation, op->name))
2.130 - {
2.131 - exitcode = op->fn(fs, argc, argv);
2.132 - if (exitcode)
2.133 - return OP_FAILED;
2.134 - break;
2.135 - }
2.136 - }
2.137 -
2.138 - if (op->name == NULL)
2.139 - return OP_UNKNOWN;
2.140 -
2.141 - return OP_SUCCESS;
2.142 -}
2.143 -
2.144 /* Main program. */
2.145
2.146 int main(int argc, char *argv[])
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/libe2access/host/op_script.c Sun Mar 06 18:20:32 2022 +0100
3.3 @@ -0,0 +1,86 @@
3.4 +/*
3.5 + * Run a script accessing a filesystem.
3.6 + *
3.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#include <stdio.h>
3.26 +#include <string.h>
3.27 +
3.28 +#include <ext2fs/ext2fs.h>
3.29 +
3.30 +#include "input.h"
3.31 +#include "ops.h"
3.32 +
3.33 +
3.34 +
3.35 +/* Copy buffer size. */
3.36 +
3.37 +extern int BUFSIZE;
3.38 +
3.39 +/* Maximum number of arguments in scripts. */
3.40 +
3.41 +const int MAX_ARGS = 32;
3.42 +
3.43 +
3.44 +
3.45 +/* Read operations from a script file. */
3.46 +
3.47 +int run_script(ext2_filsys fs, int argc, char *argv[])
3.48 +{
3.49 + FILE *fp;
3.50 + char buffer[BUFSIZE];
3.51 + struct read_line_state state;
3.52 + enum op_results op_result;
3.53 + int num_args;
3.54 + char *args[MAX_ARGS];
3.55 + int i;
3.56 +
3.57 + for (i = 0; i < argc; i++)
3.58 + {
3.59 + fp = fopen(argv[i], "r");
3.60 +
3.61 + state.buffer = buffer;
3.62 + state.start = buffer;
3.63 + state.end = buffer;
3.64 + state.buffer_size = BUFSIZE;
3.65 + state.remaining = BUFSIZE - 1;
3.66 +
3.67 + while (read_line(fp, &state) != NULL)
3.68 + {
3.69 + parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS);
3.70 +
3.71 + if (num_args > 1)
3.72 + {
3.73 + op_result = run_operation(fs, args[0], num_args - 1, &args[1]);
3.74 +
3.75 + if (handle_op_result(args[0], op_result))
3.76 + return 1;
3.77 + }
3.78 +
3.79 + state.start = state.eolp + 1;
3.80 + }
3.81 +
3.82 + fclose(fp);
3.83 + }
3.84 +
3.85 + return 0;
3.86 +}
3.87 +
3.88 +/* vim: tabstop=4 expandtab shiftwidth=4
3.89 +*/
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/libe2access/host/ops.c Sun Mar 06 18:20:32 2022 +0100
4.3 @@ -0,0 +1,78 @@
4.4 +/*
4.5 + * Operation invocation.
4.6 + *
4.7 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
4.8 + *
4.9 + * This program is free software; you can redistribute it and/or
4.10 + * modify it under the terms of the GNU General Public License as
4.11 + * published by the Free Software Foundation; either version 2 of
4.12 + * the License, or (at your option) any later version.
4.13 + *
4.14 + * This program is distributed in the hope that it will be useful,
4.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + * GNU General Public License for more details.
4.18 + *
4.19 + * You should have received a copy of the GNU General Public License
4.20 + * along with this program; if not, write to the Free Software
4.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
4.22 + * Boston, MA 02110-1301, USA
4.23 + */
4.24 +
4.25 +#include <stdio.h>
4.26 +#include <string.h>
4.27 +
4.28 +#include "ops.h"
4.29 +
4.30 +
4.31 +
4.32 +/* Operations exposed by the program. */
4.33 +
4.34 +extern struct operation operations[];
4.35 +
4.36 +
4.37 +
4.38 +/* Produce a general result condition from the invocation of an operation. */
4.39 +
4.40 +int handle_op_result(const char *operation, enum op_results op_result)
4.41 +{
4.42 + if (op_result == OP_UNKNOWN)
4.43 + {
4.44 + fprintf(stderr, "Operation not recognised: %s\n", operation);
4.45 + return 1;
4.46 + }
4.47 + else if (op_result == OP_FAILED)
4.48 + {
4.49 + fprintf(stderr, "Operation failed: %s\n", operation);
4.50 + return 1;
4.51 + }
4.52 + else
4.53 + return 0;
4.54 +}
4.55 +
4.56 +/* Invocation of operations. */
4.57 +
4.58 +enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[])
4.59 +{
4.60 + struct operation *op;
4.61 + int exitcode;
4.62 +
4.63 + for (op = &operations[0]; op->name != NULL; op++)
4.64 + {
4.65 + if (!strcmp(operation, op->name))
4.66 + {
4.67 + exitcode = op->fn(fs, argc, argv);
4.68 + if (exitcode)
4.69 + return OP_FAILED;
4.70 + break;
4.71 + }
4.72 + }
4.73 +
4.74 + if (op->name == NULL)
4.75 + return OP_UNKNOWN;
4.76 +
4.77 + return OP_SUCCESS;
4.78 +}
4.79 +
4.80 +/* vim: tabstop=4 expandtab shiftwidth=4
4.81 +*/
5.1 --- a/libe2access/host/ops.h Sun Mar 06 01:30:32 2022 +0100
5.2 +++ b/libe2access/host/ops.h Sun Mar 06 18:20:32 2022 +0100
5.3 @@ -1,5 +1,5 @@
5.4 /*
5.5 - * Operations.
5.6 + * Operations and their invocation.
5.7 *
5.8 * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
5.9 *
5.10 @@ -21,12 +21,37 @@
5.11
5.12 #pragma once
5.13
5.14 +#include <ext2fs/ext2fs.h>
5.15 +
5.16 +/* Invocation support. */
5.17 +
5.18 +enum op_results
5.19 +{
5.20 + OP_SUCCESS = 0,
5.21 + OP_FAILED = 1,
5.22 + OP_UNKNOWN = 2,
5.23 +};
5.24 +
5.25 +int handle_op_result(const char *operation, enum op_results op_result);
5.26 +enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]);
5.27 +
5.28 +/* Operations exposed by the program. */
5.29 +
5.30 +typedef int (*op_sig)(ext2_filsys, int, char *[]);
5.31 +
5.32 +struct operation
5.33 +{
5.34 + const char *name;
5.35 + op_sig fn;
5.36 +};
5.37 +
5.38 int copy_in(ext2_filsys fs, int argc, char *argv[]);
5.39 int copy_out(ext2_filsys fs, int argc, char *argv[]);
5.40 int list(ext2_filsys fs, int argc, char *argv[]);
5.41 int make_dirs(ext2_filsys fs, int argc, char *argv[]);
5.42 int remove_dirs(ext2_filsys fs, int argc, char *argv[]);
5.43 int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]);
5.44 +int run_script(ext2_filsys fs, int argc, char *argv[]);
5.45 int stat_objects(ext2_filsys fs, int argc, char *argv[]);
5.46
5.47 /* vim: tabstop=4 expandtab shiftwidth=4