2 * libcfg+ - precise command line & config file parsing library
4 * cmdline.c - command line parsing
5 * ____________________________________________________________
7 * Developed by Ondrej Jombik <nepto@platon.sk>
8 * and Lubomir Host <rajo@platon.sk>
9 * Copyright (c) 2001-2004 Platon SDG, http://platon.sk/
10 * All rights reserved.
12 * See README file for more information about this software.
13 * See COPYING file for license information.
15 * Download the latest version from
16 * http://platon.sk/projects/libcfg+/
19 /* $Platon: libcfg+/src/cmdline.c,v 1.36 2004/01/12 06:03:09 nepto Exp $ */
34 # if !STDC_HEADERS && HAVE_MEMORY_H
43 #include <platon/str/strplus.h>
44 #include <platon/str/strdyn.h>
51 cfg_cmdline_get_next_opt(con
)
52 const CFG_CONTEXT con
;
57 con
->error_code
= CFG_OK
;
59 /* Initial position seek */
60 if (! con
->parsing_started
) {
61 con
->parsing_started
= 1;
63 if (con
->begin_pos
< 0) {
64 con
->error_code
= CFG_ERROR_SEEK_ERROR
;
65 return con
->error_code
;
68 if (con
->begin_pos
> 0) {
69 for (; con
->cur_idx
< con
->begin_pos
; con
->cur_idx
++)
70 if (con
->argv
[con
->cur_idx
] == NULL
) {
71 con
->error_code
= CFG_ERROR_SEEK_ERROR
;
72 return con
->error_code
;
76 if (con
->flags
& CFG_SKIP_FIRST
)
89 /* Updating cur_idx step by step and testing for NULL in argv. */
90 for (; con
->cur_idx_tmp
> 0; con
->cur_idx_tmp
--, con
->cur_idx
++)
91 if (con
->argv
[con
->cur_idx
] == NULL
)
94 /* Finished? (size is reached) */
95 if (con
->size
>= 0 && con
->cur_idx
>= con
->begin_pos
+ con
->size
)
98 /* Finished? (NULL is detected) */
99 if (con
->argv
[con
->cur_idx
] == NULL
)
102 if (con
->cur_opt_type
& CFG_SHORT_OPTIONS
) {
104 con
->cur_opt
[0] = con
->cur_arg
[0];
105 PLATON_FUNC(strdel
)(con
->cur_arg
);
107 if (strlen(con
->cur_arg
) == 0) {
108 con
->cur_opt_type
-= CFG_SHORT_OPTIONS
;
111 /* strdup() doesn't accept NULL as parameter */
112 con
->cur_arg
= con
->argv
[con
->cur_idx
+ 1] != NULL
113 ? strdup(con
->argv
[con
->cur_idx
+ 1])
118 register int leftover_init
= 0;
120 /* Test if previous argument was leftover and also there is not
121 advanced leftovers initializations set in context flags. */
122 if (! (con
->flags
& CFG_ADVANCED_LEFTOVERS
)
123 && con
->cur_opt_type
== CFG_NONE_OPTION
124 && con
->cur_opt
== NULL
&& con
->cur_arg
!= NULL
125 && con
->argv
[con
->cur_idx
- 1] != NULL
126 && ! strcmp(con
->cur_arg
, con
->argv
[con
->cur_idx
- 1]))
129 __cfg_free_currents(con
);
131 if (! PLATON_FUNC(strdyn_compare
)(con
->prop
[CFG_LINE_STOP_STRING
],
132 con
->argv
[con
->cur_idx
])) {
133 con
->error_code
= CFG_ERROR_STOP_STR_FOUND
;
134 return con
->error_code
;
137 /* Skip option analyze in __cfg_cmdline_set_currents(),
138 count it as leftover. */
140 con
->cur_opt_type
= CFG_NONE_OPTION
;
142 if ((con
->cur_arg
= strdup(con
->argv
[con
->cur_idx
])) == NULL
) {
143 con
->error_code
= CFG_ERROR_NOMEM
;
144 return con
->error_code
;
148 if (__cfg_cmdline_set_currents(con
) != CFG_OK
) {
149 con
->error_code
= CFG_ERROR_NOMEM
;
150 return con
->error_code
;
155 con
->error_code
= __cfg_process_currents(con
, &ret_val
, &arg_used
);
156 if (con
->error_code
!= CFG_OK
)
157 return con
->error_code
;
160 if (! (con
->cur_opt_type
& CFG_LONG_SEPINIT
)
161 && !(con
->cur_opt_type
& CFG_SHORT_OPTIONS
)
162 && !(con
->cur_opt_type
== CFG_NONE_OPTION
))
165 if (con
->cur_opt_type
& CFG_SHORT_OPTIONS
)
166 con
->cur_opt_type
-= CFG_SHORT_OPTIONS
;
169 if (! (con
->cur_opt_type
& CFG_SHORT_OPTIONS
))
176 return con
->error_code
; /* CFG_OK */
179 /* Modeline for ViM {{{
181 * vim600:fdm=marker fdl=0 fdc=0: