1 /* @(#)getargs.h	1.22 16/10/23 Copyright 1985-2016 J. Schilling */
2 /*
3  *	Definitions for getargs()/getallargs()/getfiles()
4  *
5  *	Copyright (c) 1985-2016 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #ifndef	_SCHILY_GETARGS_H
22 #define	_SCHILY_GETARGS_H
23 
24 #ifndef _SCHILY_MCONFIG_H
25 #include <schily/mconfig.h>
26 #endif
27 #ifndef _SCHILY_UTYPES_H
28 #include <schily/utypes.h>
29 #endif
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 /*
36  * Return values for get*args()/get*files()
37  *
38  * This package calls options "flags", they are returned from get*args().
39  *
40  * Note that NOTAFILE is not returned by the interface functions.
41  * NOTAFILE is however used as return code from the user's callback functions
42  * to signal that the current arg may be an option the callback function does
43  * not know and definitely is no file type argument.
44  *
45  * General rules for the return code of the interface functions
46  * get*args()/get*files():
47  *
48  *	> 0		A file type argument was found
49  *	  0		All arguments have been parsed
50  *	< 0		An error occured
51  *
52  * Flag and file arg processing should be terminated after getting a return
53  * code <= 0.
54  */
55 #define	FLAGDELIM	  2		/* "--" stopped flag processing	*/
56 #define	NOTAFLAG	  1		/* Not a flag type argument	*/
57 #define	NOARGS		  0		/* No more args			*/
58 #define	BADFLAG		(-1)		/* Not a valid flag argument	*/
59 #define	BADFMT		(-2)		/* Error in format string	*/
60 #define	NOTAFILE	(-3)		/* Seems to be a flag type	*/
61 
62 /*
63  * The callback functions are called with the following parameters:
64  *
65  * arg		The option argument
66  * valp		A pointer to the related value argument from a get*arg*() call
67  * pac		A pointer to the current argument counter
68  * pav		A pointer to the current argument vector
69  * opt		The option that caused the call
70  *
71  * The return value of the callback function may be:
72  *
73  * FLAGDELIM	Pretend "--" stopped flag processing
74  * FLAGPARSED	A valid flag was found, getallargs() will continue scanning
75  * ------------ the following codes will interrupt getallargs() processing:
76  * NOARGS	Pretend all arguments have been examined
77  * BADFLAG	Not a valid flag argument
78  * BADFMT	General Error
79  * NOTAFILE	Continue to check the format string for matches with option arg
80  */
81 #define	FLAGPARSED	  1		/* Flag was sucessfully parsed	*/
82 
83 typedef	int	(*getargfun)	__PR((const char *__arg, void *__valp));
84 typedef	int	(*getpargfun)	__PR((const char *__arg, void *__valp,
85 						int *__pac, char *const **__pav,
86 						const char *__opt));
87 
88 #define	NO_ARGFUN	(getargpfun)0
89 
90 struct ga_flags {
91 	const char	*ga_format;	/* Comma separated list for one flag */
92 	void		*ga_arg;	/* Ptr. to variable to fill for flag */
93 	getpargfun	ga_funcp;	/* Ptr. for function to call (&/~)   */
94 };
95 
96 struct ga_props {
97 	UInt32_t	ga_flags;	/* Flags to define behavior	*/
98 	UInt32_t	ga_oflags;	/* State flags			*/
99 	size_t		ga_size;	/* Size of this struct gs_props	*/
100 };
101 
102 /*
103  * This may be used instead of a struct ga_props * parameter:
104  */
105 #define	GA_NO_PROPS	(struct ga_props *)0	/* Default behavior	*/
106 #define	GA_POSIX_PROPS	(struct ga_props *)-1	/* POSIX behavior	*/
107 
108 /*
109  * Definitions for ga_flags
110  */
111 #define	GAF_DEFAULT		0x00	/* The default behavior		    */
112 #define	GAF_NO_PLUS		0x01	/* Options may not start with '+'   */
113 #define	GAF_NO_EQUAL		0x02	/* Disallow '=' between opt and val */
114 #define	GAF_NEED_DASH		0x04	/* Need dash before (-name=val),    */
115 					/*  name=val is not allowed	    */
116 #define	GAF_DELIM_DASHDASH	0x08	/* "--" stops even get?allargs()    */
117 #define	GAF_POSIX		0x1000	/* Will be expanded as shown below  */
118 
119 /*
120  * POSIX does not allow options in the form "+option", "-option=value" or
121  * "option=value". get*files() needs to know what may be a valid option.
122  *
123  * If ga_flags == GAF_POSIX, ga_flags is replaced with the value
124  * of the current definition for GAF_POSIX_DEFAULT.
125  *
126  * GAF_NO_PLUS		do not allow options to start with a '+'
127  * GAF_NO_EQUAL		do not allow options to contain '=' between name & val
128  *
129  * Warning: future versions may need different flags for POSIX, better use the
130  * GA_POSIX_PROPS "struct" or the GAF_POSIX flag.
131  */
132 #define	GAF_POSIX_DEFAULT	(GAF_NO_PLUS | GAF_NO_EQUAL)
133 
134 /*
135  * Keep in sync with schily.h
136  */
137 extern	int	getallargs __PR((int *, char * const**, const char *, ...));
138 extern	int	getargs __PR((int *, char * const**, const char *, ...));
139 extern	int	getfiles __PR((int *, char * const**, const char *));
140 extern	char	*getargerror __PR((int));
141 
142 /*
143  * The new list versions of the functions need struct ga_props and thus need
144  * getargs.h
145  */
146 extern	int	getlallargs __PR((int *, char * const**, struct ga_props *,
147 						const char *, ...));
148 extern	int	getlargs __PR((int *, char * const**, struct ga_props *,
149 						const char *, ...));
150 extern	int	getlfiles __PR((int *, char * const**, struct ga_props *,
151 						const char *));
152 extern	int	_getarginit __PR((struct ga_props *, size_t, UInt32_t));
153 
154 #define	getarginit(p, f)	_getarginit(p, sizeof (struct ga_props), f)
155 
156 /*
157  * The vector versions of the functions need struct ga_flags and thus need
158  * getargs.h
159  */
160 extern	int	getvallargs __PR((int *, char * const**, struct ga_props *,
161 						struct ga_flags *));
162 extern	int	getvargs __PR((int *, char * const**, struct ga_props *,
163 						struct ga_flags *));
164 extern	int	getvfiles __PR((int *, char * const**, struct ga_props *,
165 						struct ga_flags *));
166 
167 #ifdef	__cplusplus
168 }
169 #endif
170 
171 #endif	/* _SCHILY_GETARGS_H */
172