arg.h (2443B)
1 /* 2 * ISC-License 3 * 4 * Copyright 2017 Laslo Hunhold <dev@frign.de> 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #ifndef ARG_H 19 #define ARG_H 20 21 extern char *argv0; 22 23 /* int main(int argc, char *argv[]) */ 24 #define ARGBEGIN for (argv0 = *argv, *argv ? (argc--, argv++) : ((void *)0); \ 25 *argv && (*argv)[0] == '-' && (*argv)[1]; argc--, argv++) { \ 26 int i_, argused_; \ 27 if ((*argv)[1] == '-' && !(*argv)[2]) { \ 28 argc--, argv++; \ 29 break; \ 30 } \ 31 for (i_ = 1, argused_ = 0; (*argv)[i_]; i_++) { \ 32 switch((*argv)[i_]) 33 #define ARGEND if (argused_) { \ 34 if ((*argv)[i_ + 1]) { \ 35 break; \ 36 } else { \ 37 argc--, argv++; \ 38 break; \ 39 } \ 40 } \ 41 } \ 42 } 43 #define ARGC() ((*argv)[i_]) 44 #define ARGF_(x) (((*argv)[i_ + 1]) ? (argused_ = 1, &((*argv)[i_ + 1])) : \ 45 (*(argv + 1)) ? (argused_ = 1, *(argv + 1)) : (x)) 46 #define EARGF(x) ARGF_(((x), exit(1), (char *)0)) 47 #define ARGF() ARGF_((char *)0) 48 49 #endif