paul@283 | 1 | /* |
paul@283 | 2 | * Operation invocation. |
paul@283 | 3 | * |
paul@283 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@283 | 5 | * |
paul@283 | 6 | * This program is free software; you can redistribute it and/or |
paul@283 | 7 | * modify it under the terms of the GNU General Public License as |
paul@283 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@283 | 9 | * the License, or (at your option) any later version. |
paul@283 | 10 | * |
paul@283 | 11 | * This program is distributed in the hope that it will be useful, |
paul@283 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@283 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@283 | 14 | * GNU General Public License for more details. |
paul@283 | 15 | * |
paul@283 | 16 | * You should have received a copy of the GNU General Public License |
paul@283 | 17 | * along with this program; if not, write to the Free Software |
paul@283 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@283 | 19 | * Boston, MA 02110-1301, USA |
paul@283 | 20 | */ |
paul@283 | 21 | |
paul@283 | 22 | #include <stdio.h> |
paul@283 | 23 | #include <string.h> |
paul@283 | 24 | |
paul@283 | 25 | #include "ops.h" |
paul@283 | 26 | |
paul@283 | 27 | |
paul@283 | 28 | |
paul@283 | 29 | /* Operations exposed by the program. */ |
paul@283 | 30 | |
paul@283 | 31 | extern struct operation operations[]; |
paul@283 | 32 | |
paul@283 | 33 | |
paul@283 | 34 | |
paul@283 | 35 | /* Produce a general result condition from the invocation of an operation. */ |
paul@283 | 36 | |
paul@283 | 37 | int handle_op_result(const char *operation, enum op_results op_result) |
paul@283 | 38 | { |
paul@283 | 39 | if (op_result == OP_UNKNOWN) |
paul@283 | 40 | { |
paul@283 | 41 | fprintf(stderr, "Operation not recognised: %s\n", operation); |
paul@283 | 42 | return 1; |
paul@283 | 43 | } |
paul@283 | 44 | else if (op_result == OP_FAILED) |
paul@283 | 45 | { |
paul@283 | 46 | fprintf(stderr, "Operation failed: %s\n", operation); |
paul@283 | 47 | return 1; |
paul@283 | 48 | } |
paul@283 | 49 | else |
paul@283 | 50 | return 0; |
paul@283 | 51 | } |
paul@283 | 52 | |
paul@283 | 53 | /* Invocation of operations. */ |
paul@283 | 54 | |
paul@602 | 55 | enum op_results run_operation(const char *operation, int argc, char *argv[], int *exitcode) |
paul@283 | 56 | { |
paul@283 | 57 | struct operation *op; |
paul@283 | 58 | |
paul@283 | 59 | for (op = &operations[0]; op->name != NULL; op++) |
paul@283 | 60 | { |
paul@283 | 61 | if (!strcmp(operation, op->name)) |
paul@283 | 62 | { |
paul@602 | 63 | *exitcode = op->fn(argc, argv); |
paul@602 | 64 | if (*exitcode) |
paul@283 | 65 | return OP_FAILED; |
paul@283 | 66 | break; |
paul@283 | 67 | } |
paul@283 | 68 | } |
paul@283 | 69 | |
paul@283 | 70 | if (op->name == NULL) |
paul@283 | 71 | return OP_UNKNOWN; |
paul@283 | 72 | |
paul@283 | 73 | return OP_SUCCESS; |
paul@283 | 74 | } |
paul@283 | 75 | |
paul@283 | 76 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@283 | 77 | */ |