source: mainline/uspace/lib/c/generic/arg_parse.c@ 99d3123

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 99d3123 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file
30 * Command-line arguments parsing functions
31 */
32
33#include <arg_parse.h>
34#include <errno.h>
35#include <str.h>
36
37int arg_parse_short_long(const char *arg, const char *ashort, const char *along)
38{
39 if (str_cmp(arg, ashort) == 0)
40 return 0;
41
42 if (str_lcmp(arg, along, str_length(along)) == 0)
43 return str_length(along);
44
45 return -1;
46}
47
48/** Parse the next argument as an integer.
49 *
50 * The actual argument is pointed by the index.
51 * Parse the offseted argument value if the offset is set
52 * or the next one if not.
53 *
54 * @param[in] argc The total number of arguments.
55 * @param[in] argv The arguments.
56 * @param[in,out] index The actual argument index. The index is incremented
57 * by the number of processed arguments.
58 * @param[out] value The parsed argument value.
59 * @param[in] offset The value offset in the actual argument. If not set,
60 * the next argument is parsed instead.
61 *
62 * @return EOK on success.
63 * @return ENOENT if the argument is missing.
64 * @return EINVAL if the argument is in wrong format.
65 *
66 */
67errno_t arg_parse_int(int argc, char *argv[], int *index, int *value,
68 int offset)
69{
70 char *rest;
71
72 if (offset)
73 *value = strtol(argv[*index] + offset, &rest, 10);
74 else if ((*index) + 1 < argc) {
75 (*index)++;
76 *value = strtol(argv[*index], &rest, 10);
77 } else
78 return ENOENT;
79
80 if ((rest) && (*rest))
81 return EINVAL;
82
83 return EOK;
84}
85
86/** Parse the next named argument as an integral number.
87 *
88 * The actual argument is pointed by the index.
89 * Parse the offseted actual argument if the offset is set or the next
90 * one if not. Translate the argument using the parse_value function.
91 * Increment the actual index by the number of processed arguments.
92 *
93 * @param[in] argc The total number of arguments.
94 * @param[in] argv The arguments.
95 * @param[in,out] index The actual argument index. The index is
96 * incremented by the number of processed arguments.
97 * @param[out] value The parsed argument value.
98 * @param[in] offset The value offset in the actual argument. If not
99 * set, the next argument is parsed instead.
100 * @param[in] parse_value The translation function to parse the named value.
101 *
102 * @return EOK on success.
103 * @return ENOENT if the argument is missing.
104 * @return EINVAL if the argument name has not been found.
105 *
106 */
107errno_t arg_parse_name_int(int argc, char *argv[], int *index, int *value,
108 int offset, arg_parser parser)
109{
110 char *arg;
111
112 errno_t ret = arg_parse_string(argc, argv, index, &arg, offset);
113 if (ret != EOK)
114 return ret;
115
116 return parser(arg, value);
117}
118
119/** Parse the next argument as a character string.
120 *
121 * The actual argument is pointed by the index.
122 * Parse the offseted actual argument value if the offset is set or the next
123 * one if not. Increment the actual index by the number of processed arguments.
124 *
125 * @param[in] argc The total number of arguments.
126 * @param[in] argv The arguments.
127 * @param[in,out] index The actual argument index. The index is
128 * incremented by the number of processed arguments.
129 * @param[out] value The parsed argument value.
130 * @param[in] offset The value offset in the actual argument. If not set,
131 * the next argument is parsed instead.
132 *
133 * @return EOK on success.
134 * @return ENOENT if the parameter is missing.
135 *
136 */
137errno_t arg_parse_string(int argc, char **argv, int *index, char **value,
138 int offset)
139{
140 if (offset)
141 *value = argv[*index] + offset;
142 else if ((*index) + 1 < argc) {
143 (*index)++;
144 *value = argv[*index];
145 } else
146 return ENOENT;
147
148 return EOK;
149}
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.