xref: /netbsd-src/external/gpl2/grep/dist/lib/xstrtol.h (revision a8fa202a6440953be7b92a8960a811bff58203f4)
1 /*	$NetBSD: xstrtol.h,v 1.1.1.1 2016/01/10 21:36:19 christos Exp $	*/
2 
3 #ifndef XSTRTOL_H_
4 # define XSTRTOL_H_ 1
5 
6 # if HAVE_INTTYPES_H
7 #  include <inttypes.h> /* for uintmax_t */
8 # endif
9 
10 # ifndef PARAMS
11 #  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
12 #   define PARAMS(Args) Args
13 #  else
14 #   define PARAMS(Args) ()
15 #  endif
16 # endif
17 
18 # ifndef _STRTOL_ERROR
19 enum strtol_error
20   {
21     LONGINT_OK, LONGINT_INVALID, LONGINT_INVALID_SUFFIX_CHAR, LONGINT_OVERFLOW
22   };
23 typedef enum strtol_error strtol_error;
24 # endif
25 
26 # define _DECLARE_XSTRTOL(name, type) \
27   strtol_error \
28     name PARAMS ((const char *s, char **ptr, int base, \
29 		  type *val, const char *valid_suffixes));
30 _DECLARE_XSTRTOL (xstrtol, long int)
31 _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
32 _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
33 
34 # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err)	\
35   do									\
36     {									\
37       switch ((Err))							\
38 	{								\
39 	case LONGINT_OK:						\
40 	  abort ();							\
41 									\
42 	case LONGINT_INVALID:						\
43 	  error ((Exit_code), 0, "invalid %s `%s'",			\
44 		 (Argument_type_string), (Str));			\
45 	  break;							\
46 									\
47 	case LONGINT_INVALID_SUFFIX_CHAR:				\
48 	  error ((Exit_code), 0, "invalid character following %s `%s'",	\
49 		 (Argument_type_string), (Str));			\
50 	  break;							\
51 									\
52 	case LONGINT_OVERFLOW:						\
53 	  error ((Exit_code), 0, "%s `%s' too large",			\
54 		 (Argument_type_string), (Str));			\
55 	  break;							\
56 	}								\
57     }									\
58   while (0)
59 
60 # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err)		\
61   _STRTOL_ERROR (2, Str, Argument_type_string, Err)
62 
63 # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err)		\
64   _STRTOL_ERROR (0, Str, Argument_type_string, Err)
65 
66 #endif /* not XSTRTOL_H_ */
67