Imported Upstream version 1.12
[platform/upstream/ed.git] / carg_parser.h
1 /*  Arg_parser - POSIX/GNU command line argument parser. (C version)
2     Copyright (C) 2006-2015 Antonio Diaz Diaz.
3
4     This library is free software. Redistribution and use in source and
5     binary forms, with or without modification, are permitted provided
6     that the following conditions are met:
7
8     1. Redistributions of source code must retain the above copyright
9     notice, this list of conditions and the following disclaimer.
10
11     2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14
15     This library is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 /*  Arg_parser reads the arguments in 'argv' and creates a number of
21     option codes, option arguments and non-option arguments.
22
23     In case of error, 'ap_error' returns a non-null pointer to an error
24     message.
25
26     'options' is an array of 'struct ap_Option' terminated by an element
27     containing a code which is zero. A null name means a short-only
28     option. A code value outside the unsigned char range means a
29     long-only option.
30
31     Arg_parser normally makes it appear as if all the option arguments
32     were specified before all the non-option arguments for the purposes
33     of parsing, even if the user of your program intermixed option and
34     non-option arguments. If you want the arguments in the exact order
35     the user typed them, call 'ap_init' with 'in_order' = true.
36
37     The argument '--' terminates all options; any following arguments are
38     treated as non-option arguments, even if they begin with a hyphen.
39
40     The syntax for optional option arguments is '-<short_option><argument>'
41     (without whitespace), or '--<long_option>=<argument>'.
42 */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 enum ap_Has_arg { ap_no, ap_yes, ap_maybe };
49
50 struct ap_Option
51   {
52   int code;                     /* Short option letter or code ( code != 0 ) */
53   const char * name;            /* Long option name (maybe null) */
54   enum ap_Has_arg has_arg;
55   };
56
57
58 struct ap_Record
59   {
60   int code;
61   char * argument;
62   };
63
64
65 struct Arg_parser
66   {
67   struct ap_Record * data;
68   char * error;
69   int data_size;
70   int error_size;
71   };
72
73
74 char ap_init( struct Arg_parser * const ap,
75               const int argc, const char * const argv[],
76               const struct ap_Option options[], const char in_order );
77
78 void ap_free( struct Arg_parser * const ap );
79
80 const char * ap_error( const struct Arg_parser * const ap );
81
82     /* The number of arguments parsed (may be different from argc) */
83 int ap_arguments( const struct Arg_parser * const ap );
84
85     /* If ap_code( i ) is 0, ap_argument( i ) is a non-option.
86        Else ap_argument( i ) is the option's argument (or empty). */
87 int ap_code( const struct Arg_parser * const ap, const int i );
88
89 const char * ap_argument( const struct Arg_parser * const ap, const int i );
90
91 #ifdef __cplusplus
92 }
93 #endif