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