1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/fsaccess/op_script.c Fri Mar 18 00:14:07 2022 +0100
1.3 @@ -0,0 +1,84 @@
1.4 +/*
1.5 + * Run a script accessing a filesystem.
1.6 + *
1.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <stdio.h>
1.26 +#include <string.h>
1.27 +
1.28 +#include "input.h"
1.29 +#include "ops.h"
1.30 +
1.31 +
1.32 +
1.33 +/* Line buffer size. */
1.34 +
1.35 +static int BUFSIZE = 4096;
1.36 +
1.37 +/* Maximum number of arguments in scripts. */
1.38 +
1.39 +const int MAX_ARGS = 32;
1.40 +
1.41 +
1.42 +
1.43 +/* Read operations from a script file. */
1.44 +
1.45 +int run_script(int argc, char *argv[])
1.46 +{
1.47 + FILE *fp;
1.48 + char buffer[BUFSIZE];
1.49 + struct read_line_state state;
1.50 + enum op_results op_result;
1.51 + int num_args;
1.52 + char *args[MAX_ARGS];
1.53 + int i;
1.54 +
1.55 + for (i = 0; i < argc; i++)
1.56 + {
1.57 + fp = fopen(argv[i], "r");
1.58 +
1.59 + state.buffer = buffer;
1.60 + state.start = buffer;
1.61 + state.end = buffer;
1.62 + state.buffer_size = BUFSIZE;
1.63 + state.remaining = BUFSIZE - 1;
1.64 +
1.65 + while (read_line(fp, &state) != NULL)
1.66 + {
1.67 + parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS);
1.68 +
1.69 + if (num_args > 1)
1.70 + {
1.71 + op_result = run_operation(args[0], num_args - 1, &args[1]);
1.72 +
1.73 + if (handle_op_result(args[0], op_result))
1.74 + return 1;
1.75 + }
1.76 +
1.77 + state.start = state.eolp + 1;
1.78 + }
1.79 +
1.80 + fclose(fp);
1.81 + }
1.82 +
1.83 + return 0;
1.84 +}
1.85 +
1.86 +/* vim: tabstop=4 expandtab shiftwidth=4
1.87 +*/