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