paul@279 | 1 | /* |
paul@279 | 2 | * Operation invocation. |
paul@279 | 3 | * |
paul@279 | 4 | * Copyright (C) 2019, 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 "ops.h" |
paul@279 | 26 | |
paul@279 | 27 | |
paul@279 | 28 | |
paul@279 | 29 | /* Operations exposed by the program. */ |
paul@279 | 30 | |
paul@279 | 31 | extern struct operation operations[]; |
paul@279 | 32 | |
paul@279 | 33 | |
paul@279 | 34 | |
paul@279 | 35 | /* Produce a general result condition from the invocation of an operation. */ |
paul@279 | 36 | |
paul@279 | 37 | int handle_op_result(const char *operation, enum op_results op_result) |
paul@279 | 38 | { |
paul@279 | 39 | if (op_result == OP_UNKNOWN) |
paul@279 | 40 | { |
paul@279 | 41 | fprintf(stderr, "Operation not recognised: %s\n", operation); |
paul@279 | 42 | return 1; |
paul@279 | 43 | } |
paul@279 | 44 | else if (op_result == OP_FAILED) |
paul@279 | 45 | { |
paul@279 | 46 | fprintf(stderr, "Operation failed: %s\n", operation); |
paul@279 | 47 | return 1; |
paul@279 | 48 | } |
paul@279 | 49 | else |
paul@279 | 50 | return 0; |
paul@279 | 51 | } |
paul@279 | 52 | |
paul@279 | 53 | /* Invocation of operations. */ |
paul@279 | 54 | |
paul@279 | 55 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]) |
paul@279 | 56 | { |
paul@279 | 57 | struct operation *op; |
paul@279 | 58 | int exitcode; |
paul@279 | 59 | |
paul@279 | 60 | for (op = &operations[0]; op->name != NULL; op++) |
paul@279 | 61 | { |
paul@279 | 62 | if (!strcmp(operation, op->name)) |
paul@279 | 63 | { |
paul@279 | 64 | exitcode = op->fn(fs, argc, argv); |
paul@279 | 65 | if (exitcode) |
paul@279 | 66 | return OP_FAILED; |
paul@279 | 67 | break; |
paul@279 | 68 | } |
paul@279 | 69 | } |
paul@279 | 70 | |
paul@279 | 71 | if (op->name == NULL) |
paul@279 | 72 | return OP_UNKNOWN; |
paul@279 | 73 | |
paul@279 | 74 | return OP_SUCCESS; |
paul@279 | 75 | } |
paul@279 | 76 | |
paul@279 | 77 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@279 | 78 | */ |