argh

A C23 implementation of Plan 9 arg(2)
git clone anongit@cat-n.org:argh.git
Log | Files | Refs

arg.h (2034B)


      1 /*
      2  * MIT/Expat license
      3  *
      4  * Copyright © 2021 Plan 9 Foundation
      5  * Copyright © 2026 9front authors
      6  * Copyright © 2026 afiw
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a copy
      9  * of this software and associated documentation files (the "Software"), to deal
     10  * in the Software without restriction, including without limitation the rights
     11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     12  * copies of the Software, and to permit persons to whom the Software is
     13  * furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in all
     16  * copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     24  * SOFTWARE.
     25  */
     26 
     27 extern char	*argv0;
     28 
     29 #define ARGBEGIN	for(argv0 = argv[0], argv++, argc--;\
     30 					    argv[0] != nullptr && argv[0][0] == '-' && argv[0][1] != '\0';\
     31 					    argc--, argv++){\
     32 						char *argh__s = &argv[0][1];\
     33 						[[maybe_unused]] char *argh__t;\
     34 						char argh__c = '\0';\
     35 						if(argh__s[0] == '-' && argh__s[1] == '\0'){\
     36 							argc--;\
     37 							argv++;\
     38 							break;\
     39 						}\
     40 						while((argh__c = argh__s[0]) != '\0'){\
     41 							argh__s++;\
     42 							switch(argh__c)
     43 
     44 #define ARGEND			}\
     45 					}
     46 
     47 #define ARGF()		(argh__t = argh__s, argh__s = "",\
     48 					    (argh__t[0] != '\0' ? argh__t : argv[1] != nullptr ? (argc--, *++argv) : nullptr))
     49 
     50 #define EARGF()		(argh__t = argh__s, argh__s = "",\
     51 					    (argh__t[0] != '\0' ? argh__t : argv[1] != nullptr ? (argc--, *++argv) : ((x), abort())))
     52 
     53 #define ARGC()		(argh__c)