| 1 | /* gopt-errors.c PUBILC DOMAIN 2015 [email protected] */ |
| 2 | |
| 3 | /* <http:///www.purposeful.co.uk/software/gopt> */ |
| 4 | |
| 5 | /* |
| 6 | I, Tom Vajzovic, am the author of this software and its documentation. |
| 7 | I permanently abandon all intellectual property rights in them, including |
| 8 | copyright, trademarks, design rights, database right, patents, and the right |
| 9 | to be identified as the author. |
| 10 | |
| 11 | I am fairly certain that the software does what the documentation says it |
| 12 | does, but I do not guarantee that it does, or that it does what you think it |
| 13 | should. I do not guarantee that it will not have undesirable side effects. |
| 14 | |
| 15 | You are free to use, modify and distribute this software as you please, but |
| 16 | you do so at your own risk. If you do not pass on this warning then you may |
| 17 | be responsible for any problems encountered by those who obtain the software |
| 18 | through you. |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #ifdef _BSD_SOURCE |
| 25 | #include <sysexits.h> |
| 26 | #else |
| 27 | #define EX_USAGE EXIT_FAILURE |
| 28 | #endif |
| 29 | |
| 30 | #include "gopt.h" |
| 31 | |
| 32 | void gopt_errors (const char *argv0, const struct option *options) |
| 33 | { |
| 34 | unsigned int bad_option_index = 0; |
| 35 | unsigned int i; |
| 36 | |
| 37 | while (!(options[bad_option_index].flags & GOPT_LAST)) |
| 38 | { |
| 39 | bad_option_index++; |
| 40 | } |
| 41 | |
| 42 | if (options[bad_option_index].short_name) |
| 43 | { |
| 44 | fprintf (stderr, "%s: unrecognised option -%c\n", argv0, options[bad_option_index].short_name); |
| 45 | exit (EX_USAGE); |
| 46 | } |
| 47 | |
| 48 | if (options[bad_option_index].long_name) |
| 49 | { |
| 50 | fprintf (stderr, "%s: unrecognised option --%s\n", argv0, options[bad_option_index].long_name); |
| 51 | exit (EX_USAGE); |
| 52 | } |
| 53 | |
| 54 | for (i = 0; i < bad_option_index; i++) |
| 55 | { |
| 56 | if ((options[i].flags & GOPT_SEEN_SHORT_WITHOUT) && (options[i].flags & GOPT_ARGUMENT_REQUIRED)) |
| 57 | { |
| 58 | fprintf (stderr, "%s: option -%c requires an option-argument\n", argv0, options[i].short_name); |
| 59 | exit (EX_USAGE); |
| 60 | } |
| 61 | |
| 62 | if ((options[i].flags & GOPT_SEEN_SHORT_WITH) && (options[i].flags & GOPT_ARGUMENT_FORBIDDEN)) |
| 63 | { |
| 64 | fprintf (stderr, "%s: option -%c must not have an option-argument\n", argv0, options[i].short_name); |
| 65 | exit (EX_USAGE); |
| 66 | } |
| 67 | |
| 68 | if ((options[i].flags & GOPT_SEEN_LONG_WITHOUT) && (options[i].flags & GOPT_ARGUMENT_REQUIRED)) |
| 69 | { |
| 70 | fprintf (stderr, "%s: option --%s requires an option-argument\n", argv0, options[i].long_name); |
| 71 | exit (EX_USAGE); |
| 72 | } |
| 73 | |
| 74 | if ((options[i].flags & GOPT_SEEN_LONG_WITH) && (options[i].flags & GOPT_ARGUMENT_FORBIDDEN)) |
| 75 | { |
| 76 | fprintf (stderr, "%s: option --%s must not have an option-argument\n", argv0, options[i].long_name); |
| 77 | exit (EX_USAGE); |
| 78 | } |
| 79 | |
| 80 | if ((options[i].count > 1) && !(options[i].flags & GOPT_REPEATABLE)) |
| 81 | { |
| 82 | fprintf (stderr, "%s: option -%c/--%s may not be repeated\n", argv0, options[i].short_name, options[i].long_name); |
| 83 | exit (EX_USAGE); |
| 84 | } |
| 85 | } |
| 86 | } |