source: mainline/uspace/lib/c/generic/arg_parse.c@ c39a103

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c39a103 was 2721a75, checked in by Martin Decky <martin@…>, 15 years ago

ping overhaul:

  • coding style
  • shorter and better readable main()
  • tool output is much similar to GNU's ping, demonstrating what functionality is still missing

related changes:

  • rename strerror.h → str_error.h for consistency
  • replace network apps' specific command-line parsing functions with a generalized libc versions
  • Property mode set to 100644
File size: 4.8 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
37/** Parse the next argument as an integer.
38 *
39 * The actual argument is pointed by the index.
40 * Parse the offseted argument value if the offset is set
41 * or the next one if not.
42 *
43 * @param[in] argc The total number of arguments.
44 * @param[in] argv The arguments.
45 * @param[in,out] index The actual argument index. The index is incremented
46 * by the number of processed arguments.
47 * @param[out] value The parsed argument value.
48 * @param[in] offset The value offset in the actual argument. If not set,
49 * the next argument is parsed instead.
50 *
51 * @return EOK on success.
52 * @return ENOENT if the argument is missing.
53 * @return EINVAL if the argument is in wrong format.
54 *
55 */
56int arg_parse_int(int argc, char *argv[], int *index, int *value,
57 int offset)
58{
59 char *rest;
60
61 if (offset)
62 *value = strtol(argv[*index] + offset, &rest, 10);
63 else if ((*index) + 1 < argc) {
64 (*index)++;
65 *value = strtol(argv[*index], &rest, 10);
66 } else
67 return ENOENT;
68
69 if ((rest) && (*rest))
70 return EINVAL;
71
72 return EOK;
73}
74
75/** Parse the next named argument as an integral number.
76 *
77 * The actual argument is pointed by the index.
78 * Parse the offseted actual argument if the offset is set or the next
79 * one if not. Translate the argument using the parse_value function.
80 * Increment the actual index by the number of processed arguments.
81 *
82 * @param[in] argc The total number of arguments.
83 * @param[in] argv The arguments.
84 * @param[in,out] index The actual argument index. The index is
85 * incremented by the number of processed arguments.
86 * @param[out] value The parsed argument value.
87 * @param[in] offset The value offset in the actual argument. If not
88 * set, the next argument is parsed instead.
89 * @param[in] parse_value The translation function to parse the named value.
90 *
91 * @return EOK on success.
92 * @return ENOENT if the argument is missing.
93 * @return EINVAL if the argument name has not been found.
94 *
95 */
96int arg_parse_name_int(int argc, char *argv[], int *index, int *value,
97 int offset, arg_parser parser)
98{
99 char *arg;
100
101 int ret = arg_parse_string(argc, argv, index, &arg, offset);
102 if (ret != EOK)
103 return ret;
104
105 return parser(arg, value);
106}
107
108/** Parse the next argument as a character string.
109 *
110 * The actual argument is pointed by the index.
111 * Parse the offseted actual argument value if the offset is set or the next
112 * one if not. Increment the actual index by the number of processed arguments.
113 *
114 * @param[in] argc The total number of arguments.
115 * @param[in] argv The arguments.
116 * @param[in,out] index The actual argument index. The index is
117 * incremented by the number of processed arguments.
118 * @param[out] value The parsed argument value.
119 * @param[in] offset The value offset in the actual argument. If not set,
120 * the next argument is parsed instead.
121 *
122 * @return EOK on success.
123 * @return ENOENT if the parameter is missing.
124 *
125 */
126int arg_parse_string(int argc, char **argv, int *index, char **value,
127 int offset)
128{
129 if (offset)
130 *value = argv[*index] + offset;
131 else if ((*index) + 1 < argc) {
132 (*index)++;
133 *value = argv[*index];
134 } else
135 return ENOENT;
136
137 return EOK;
138}
139
140/** @}
141 */
Note: See TracBrowser for help on using the repository browser.