1 /*
2  * Local defines for the argv module
3  *
4  * Copyright 2000 by Gray Watson
5  *
6  * This file is part of the argv library.
7  *
8  * Permission to use, copy, modify, and distribute this software for
9  * any purpose and without fee is hereby granted, provided that the
10  * above copyright notice and this permission notice appear in all
11  * copies, and that the name of Gray Watson not be used in advertising
12  * or publicity pertaining to distribution of the document or software
13  * without specific, written prior permission.
14  *
15  * Gray Watson makes no representations about the suitability of the
16  * software described herein for any purpose.  It is provided "as is"
17  * without express or implied warranty.
18  *
19  * The author may be contacted via http://256.com/gray/
20  *
21  * $Id: argv_loc.h,v 1.50 2007/01/08 20:32:57 gray Exp $
22  */
23 
24 #ifndef __ARGV_LOC_H__
25 #define __ARGV_LOC_H__
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #if 0
30 }
31 #endif
32 #endif
33 
34 #include "argv.h"			/* to get the types */
35 
36 /*
37  * some compilation options
38  */
39 
40 /* to include RCS ids in the code */
41 #ifndef INCLUDE_RCS_IDS
42 #define INCLUDE_RCS_IDS 1
43 #endif
44 
45 /*
46  * generic constants
47  */
48 /* special consideration for NULL.  some compilers bitch if I redefine it */
49 #ifndef NULL
50 #define NULL		0L
51 #endif
52 
53 #undef	MIN
54 #define MIN(a,b)	((a) < (b) ? (a) : (b))
55 
56 #undef NOERROR
57 #define NOERROR		0
58 
59 #undef ERROR
60 #define ERROR		(-1)
61 
62 #undef STDIN
63 #define STDIN		0
64 
65 /*
66  * local argv defines
67  */
68 
69 #define ERROR_STREAM_INIT	(FILE *)0x1	/* hack to init stderr FILE* */
70 #define NO_VALUE		(-1)		/* no mandatory args value */
71 #define ARRAY_INCR		10		/* increment by 10 every 10 */
72 #define SCREEN_WIDTH		79		/* width of screen till wrap */
73 #define BITS_IN_BYTE		8		/* bits in a byte */
74 #define SPECIAL_CHARS		"e\033^^\"\"''\\\\n\nr\rt\tb\bf\fa\007"
75 #define DUMP_SPACE_BUF		128		/* space for memory dump */
76 #define ARG_MALLOC_INCR		20		/* alloc in 10 increments */
77 #define FILE_LINE_SIZE		1024		/* max size of file lines */
78 
79 /* internal flags set in the ar_type field */
80 /* NOTE: other external flags defined in argv.h */
81 #define ARGV_FLAG_USED		(1 << 12)	/* if arg has been specified */
82 
83 /* error messages */
84 #define USAGE_ERROR_NAME	"usage problem"
85 #define INTERNAL_ERROR_NAME	"internal argv error"
86 
87 /*
88  * settings to vary the output of the argument routines
89  */
90 
91 #define PROGRAM_NAME		256		/* max size of program name */
92 #define EXIT_CODE		1		/* argv exit code for errors */
93 
94 /* global env settings */
95 #define GLOBAL_NAME		"GLOBAL_ARGV"	/* global argv env name */
96 #define GLOBAL_CLOSE		"close="	/* close arg setting */
97 #define GLOBAL_ENV		"env="		/* env setting */
98 #define GLOBAL_ERROR		"error="	/* error setting */
99 #define GLOBAL_MULTI		"multi="	/* multi setting */
100 #define GLOBAL_USAGE		"usage="	/* usage setting */
101 #define GLOBAL_LASTTOG		"lasttog="	/* last-arg toggle */
102 
103 /* special argument definitions */
104 #define LAST_ARG		"--"		/* arg to mark end of args */
105 #define LONG_PREFIX		"--"		/* prefix for long args */
106 #define SHORT_PREFIX		"-"		/* prefix for short args */
107 #define UNKNOWN_ARG		"??"		/* unknown argument output */
108 #define ARG_EQUALS		'='		/* to assign value to option */
109 #define NUMBER_ARG_CHARS	"0123456789+-."	/* characters in numbers */
110 
111 /* how to produce the env var using sprintf and the argv_program variable */
112 #define ENVIRON_FORMAT		"ARGV_%s"
113 
114 /* special short-argument strings */
115 #define USAGE_CHAR_ARG		'?'		/* default short-opt usage */
116 
117 /* special long-argument strings */
118 #define DISPLAY_ARG		"argv-display"	/* display arg variable vals */
119 #define FILE_ARG		"argv-file"	/* read args from file */
120 #define HELP_ARG		"help"		/* default help option */
121 #define USAGE_ARG		"usage"		/* default usage option */
122 #define USAGE_SHORT_ARG		"usage-short"	/* default usage-short opt */
123 #define USAGE_LONG_ARG		"usage-long"	/* default usage-long opt */
124 #define USAGE_ALL_ARG		"usage-all"	/* default usage-all opt */
125 #define VERSION_ARG		"version"	/* default version option */
126 
127 /* spacing on line for usage messages */
128 #define SHORT_COLUMN		0	/* spaces to indent for short-args */
129 #define LONG_COLUMN		18	/* column for long options */
130 #define COMMENT_COLUMN		34	/* column for comments */
131 
132 /* some in-line "labels" for comments */
133 #define USAGE_LABEL		"Usage: "	/* usage message start */
134 #define LONG_LABEL		"or "		/* put before long-option */
135 #define COMMENT_LABEL		""		/* put before comments */
136 #define ARRAY_LABEL		" array"	/* put after displayed type */
137 #define BOOL_ARG_LABEL		"yes|no"	/* label for bool-arg arg */
138 
139 /* some sizeof defines */
140 #define SHORT_PREFIX_LENGTH	(sizeof(SHORT_PREFIX) - 1)
141 #define LONG_PREFIX_LENGTH	(sizeof(LONG_PREFIX) - 1)
142 
143 #define USAGE_LABEL_LENGTH	(sizeof(USAGE_LABEL) - 1)
144 #define COMMENT_LABEL_LENGTH	(sizeof(COMMENT_LABEL) - 1)
145 #define LONG_LABEL_LENGTH	(sizeof(LONG_LABEL) - 1)
146 #define UNKNOWN_ARG_LENGTH	(sizeof(UNKNOWN_ARG) - 1)
147 #define BOOL_ARG_LENGTH		(sizeof(BOOL_ARG_LABEL) - 1)
148 
149 #define HAS_ARG(type)	(! (ARGV_TYPE(type) == ARGV_BOOL \
150 			    || ARGV_TYPE(type) == ARGV_BOOL_NEG \
151 			    || ARGV_TYPE(type) == ARGV_INCR \
152 			    || ARGV_TYPE(type) == ARGV_BOOL_INT \
153 			    || ARGV_TYPE(type) == ARGV_BOOL_INT_NEG))
154 
155 /******************************** argv types *********************************/
156 
157 /* strcture defining argv types */
158 typedef struct {
159   unsigned int	at_value;		/* value of the type */
160   const char	*at_name;		/* name of the type */
161   unsigned int	at_size;		/* size of type */
162   const char	*at_desc;		/* description of the type */
163 } argv_type_t;
164 
165 static	argv_type_t	argv_types[] = {
166   { ARGV_BOOL,		"flag",			sizeof(char),
167     "if option used, set variable to 1" },
168   { ARGV_BOOL_NEG,	"negative flag",	sizeof(int),
169     "if option used, set variable to 0" },
170   { ARGV_BOOL_ARG,	"flag with arg",	sizeof(char),
171     "like boolean but with an argument, true/yes/1 sets var to 1" },
172   { ARGV_CHAR,		"character",		sizeof(char),
173     "single character" },
174   { ARGV_CHAR_P,	"string",		sizeof(char *),
175     "multiple characters terminated with a '\\0'" },
176   { ARGV_SHORT,		"short integer",	sizeof(short),
177     "decimal short-sized integer value" },
178   { ARGV_U_SHORT,	"unsigned short integer", sizeof(unsigned short),
179     "decimal unsigned short-sized integer value" },
180   { ARGV_INT,		"integer",		sizeof(int),
181     "decimal integer value" },
182   { ARGV_U_INT,		"unsigned integer",	sizeof(unsigned int),
183     "decimal unsigned integer value" },
184   { ARGV_LONG,		"long integer",		sizeof(long),
185     "decimal long-sized integer value" },
186   { ARGV_U_LONG,	"unsigned long",	sizeof(unsigned long),
187     "decimal unsigned long-sized integer value" },
188   { ARGV_FLOAT,		"floating point",	sizeof(float),
189     "real number with decimal point" },
190   { ARGV_DOUBLE,	"double floating point", sizeof(double),
191     "double precision real number with decimal point" },
192   { ARGV_BIN,		"binary",		sizeof(int),
193     "base 2 value with digits of 0 or 1" },
194   { ARGV_OCT,		"octal",		sizeof(int),
195     "base 8 value with digits from 0-7" },
196   { ARGV_HEX,		"hexadecimal",		sizeof(int),
197     "base 16 value with digits from 0-9, A-F" },
198   { ARGV_INCR,		"increment",		sizeof(int),
199     "increment variable each time option used" },
200   { ARGV_SIZE,		"long size",		sizeof(long),
201     "size as long int + [bkmg] b=byte,k=kilo,m=meg,g=gig" },
202   { ARGV_U_SIZE,	"unsigned long size",	sizeof(unsigned long),
203     "size as unsigned long int + [bkmg] b=byte,k=kilo,m=meg,g=gig" },
204   { ARGV_BOOL_INT,	"integer boolean",	sizeof(int),
205     "if option used, set integer variable to 1" },
206   { ARGV_BOOL_INT_NEG,	"integer boolean",	sizeof(int),
207     "if option used, set integer variable to 0" },
208   { ARGV_BOOL_INT_ARG,	"integer boolean",	sizeof(int),
209     "like boolean but with an argument, true/yes/1 sets integer var to 1" },
210   { 0, NULL, 0, NULL }
211 };
212 
213 #ifdef __cplusplus
214 #if 0
215 {
216 #endif
217 }
218 #endif
219 
220 #endif /* ! __ARGV_LOC_H__ */
221