1.1 --- a/fsaccess/op_script.c Thu Jan 11 19:24:44 2024 +0100
1.2 +++ b/fsaccess/op_script.c Thu Jan 11 19:25:29 2024 +0100
1.3 @@ -1,7 +1,7 @@
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 + * Copyright (C) 2022, 2024 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -43,38 +43,51 @@
1.13 {
1.14 FILE *fp;
1.15 char buffer[BUFSIZE];
1.16 - struct read_line_state state;
1.17 enum op_results op_result;
1.18 int num_args;
1.19 char *args[MAX_ARGS];
1.20 - int i;
1.21 + int i, use_stdin;
1.22
1.23 for (i = 0; i < argc; i++)
1.24 {
1.25 - fp = fopen(argv[i], "r");
1.26 + /* Test for standard input as a special filename. */
1.27 +
1.28 + use_stdin = !strcmp(argv[i], "-");
1.29 +
1.30 + if (use_stdin)
1.31 + fp = stdin;
1.32 + else
1.33 + fp = fopen(argv[i], "r");
1.34 +
1.35 + while (1)
1.36 + {
1.37 + /* Show a prompt if employing standard input. */
1.38
1.39 - state.buffer = buffer;
1.40 - state.start = buffer;
1.41 - state.end = buffer;
1.42 - state.buffer_size = BUFSIZE;
1.43 - state.remaining = BUFSIZE - 1;
1.44 + if (use_stdin)
1.45 + printf("> ");
1.46 +
1.47 + /* Read and parse the input line, running any specified command. */
1.48
1.49 - while (read_line(fp, &state) != NULL)
1.50 - {
1.51 - parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS);
1.52 + if (read_line(fp, buffer, BUFSIZE) == NULL)
1.53 + break;
1.54
1.55 - if (num_args > 1)
1.56 + parse_line(buffer, buffer + strlen(buffer), &num_args, args, MAX_ARGS);
1.57 +
1.58 + /* Permit commands with and without arguments. */
1.59 +
1.60 + if (num_args >= 1)
1.61 {
1.62 op_result = run_operation(args[0], num_args - 1, &args[1]);
1.63
1.64 - if (handle_op_result(args[0], op_result))
1.65 + if (handle_op_result(args[0], op_result) && !use_stdin)
1.66 return 1;
1.67 }
1.68 -
1.69 - state.start = state.eolp + 1;
1.70 }
1.71
1.72 - fclose(fp);
1.73 + /* Close normal files. */
1.74 +
1.75 + if (!use_stdin)
1.76 + fclose(fp);
1.77 }
1.78
1.79 return 0;