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