paul@279 | 1 | /* |
paul@279 | 2 | * Run a script accessing a filesystem. |
paul@279 | 3 | * |
paul@279 | 4 | * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> |
paul@279 | 5 | * |
paul@279 | 6 | * This program is free software; you can redistribute it and/or |
paul@279 | 7 | * modify it under the terms of the GNU General Public License as |
paul@279 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@279 | 9 | * the License, or (at your option) any later version. |
paul@279 | 10 | * |
paul@279 | 11 | * This program is distributed in the hope that it will be useful, |
paul@279 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@279 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@279 | 14 | * GNU General Public License for more details. |
paul@279 | 15 | * |
paul@279 | 16 | * You should have received a copy of the GNU General Public License |
paul@279 | 17 | * along with this program; if not, write to the Free Software |
paul@279 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@279 | 19 | * Boston, MA 02110-1301, USA |
paul@279 | 20 | */ |
paul@279 | 21 | |
paul@279 | 22 | #include <stdio.h> |
paul@279 | 23 | #include <string.h> |
paul@279 | 24 | |
paul@279 | 25 | #include <ext2fs/ext2fs.h> |
paul@279 | 26 | |
paul@279 | 27 | #include "input.h" |
paul@279 | 28 | #include "ops.h" |
paul@279 | 29 | |
paul@279 | 30 | |
paul@279 | 31 | |
paul@281 | 32 | /* Line buffer size. */ |
paul@279 | 33 | |
paul@281 | 34 | static int BUFSIZE = 4096; |
paul@279 | 35 | |
paul@279 | 36 | /* Maximum number of arguments in scripts. */ |
paul@279 | 37 | |
paul@279 | 38 | const int MAX_ARGS = 32; |
paul@279 | 39 | |
paul@279 | 40 | |
paul@279 | 41 | |
paul@279 | 42 | /* Read operations from a script file. */ |
paul@279 | 43 | |
paul@279 | 44 | int run_script(ext2_filsys fs, int argc, char *argv[]) |
paul@279 | 45 | { |
paul@279 | 46 | FILE *fp; |
paul@279 | 47 | char buffer[BUFSIZE]; |
paul@279 | 48 | struct read_line_state state; |
paul@279 | 49 | enum op_results op_result; |
paul@279 | 50 | int num_args; |
paul@279 | 51 | char *args[MAX_ARGS]; |
paul@279 | 52 | int i; |
paul@279 | 53 | |
paul@279 | 54 | for (i = 0; i < argc; i++) |
paul@279 | 55 | { |
paul@279 | 56 | fp = fopen(argv[i], "r"); |
paul@279 | 57 | |
paul@279 | 58 | state.buffer = buffer; |
paul@279 | 59 | state.start = buffer; |
paul@279 | 60 | state.end = buffer; |
paul@279 | 61 | state.buffer_size = BUFSIZE; |
paul@279 | 62 | state.remaining = BUFSIZE - 1; |
paul@279 | 63 | |
paul@279 | 64 | while (read_line(fp, &state) != NULL) |
paul@279 | 65 | { |
paul@279 | 66 | parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); |
paul@279 | 67 | |
paul@279 | 68 | if (num_args > 1) |
paul@279 | 69 | { |
paul@279 | 70 | op_result = run_operation(fs, args[0], num_args - 1, &args[1]); |
paul@279 | 71 | |
paul@279 | 72 | if (handle_op_result(args[0], op_result)) |
paul@279 | 73 | return 1; |
paul@279 | 74 | } |
paul@279 | 75 | |
paul@279 | 76 | state.start = state.eolp + 1; |
paul@279 | 77 | } |
paul@279 | 78 | |
paul@279 | 79 | fclose(fp); |
paul@279 | 80 | } |
paul@279 | 81 | |
paul@279 | 82 | return 0; |
paul@279 | 83 | } |
paul@279 | 84 | |
paul@279 | 85 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@279 | 86 | */ |