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 "input.h" 26 #include "ops.h" 27 28 29 30 /* Line buffer size. */ 31 32 static int BUFSIZE = 4096; 33 34 /* Maximum number of arguments in scripts. */ 35 36 const int MAX_ARGS = 32; 37 38 39 40 /* Read operations from a script file. */ 41 42 int run_script(int argc, char *argv[]) 43 { 44 FILE *fp; 45 char buffer[BUFSIZE]; 46 struct read_line_state state; 47 enum op_results op_result; 48 int num_args; 49 char *args[MAX_ARGS]; 50 int i; 51 52 for (i = 0; i < argc; i++) 53 { 54 fp = fopen(argv[i], "r"); 55 56 state.buffer = buffer; 57 state.start = buffer; 58 state.end = buffer; 59 state.buffer_size = BUFSIZE; 60 state.remaining = BUFSIZE - 1; 61 62 while (read_line(fp, &state) != NULL) 63 { 64 parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); 65 66 if (num_args > 1) 67 { 68 op_result = run_operation(args[0], num_args - 1, &args[1]); 69 70 if (handle_op_result(args[0], op_result)) 71 return 1; 72 } 73 74 state.start = state.eolp + 1; 75 } 76 77 fclose(fp); 78 } 79 80 return 0; 81 } 82 83 /* vim: tabstop=4 expandtab shiftwidth=4 84 */