1 | /**
|
---|
2 | * @file ntopt.c
|
---|
3 | * @author CuBeatSystems
|
---|
4 | * @author Shinichiro Nakamura
|
---|
5 | * @copyright
|
---|
6 | * ===============================================================
|
---|
7 | * Natural Tiny Shell (NT-Shell) Version 0.3.1
|
---|
8 | * ===============================================================
|
---|
9 | * Copyright (c) 2010-2016 Shinichiro Nakamura
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person
|
---|
12 | * obtaining a copy of this software and associated documentation
|
---|
13 | * files (the "Software"), to deal in the Software without
|
---|
14 | * restriction, including without limitation the rights to use,
|
---|
15 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
16 | * sell copies of the Software, and to permit persons to whom the
|
---|
17 | * Software is furnished to do so, subject to the following
|
---|
18 | * conditions:
|
---|
19 | *
|
---|
20 | * The above copyright notice and this permission notice shall be
|
---|
21 | * included in all copies or substantial portions of the Software.
|
---|
22 | *
|
---|
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
30 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include <string.h>
|
---|
34 | #include "ntopt.h"
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @brief Is the character delimiter?
|
---|
38 | * @param c The character.
|
---|
39 | * @retval true It's a delimiter.
|
---|
40 | * @retval false It's not a delimiter.
|
---|
41 | */
|
---|
42 | #define IS_DELIM(c) \
|
---|
43 | (((c) == '\r') || ((c) == '\n') || ((c) == '\t') || ((c) == '\0') || ((c) == ' '))
|
---|
44 |
|
---|
45 | static int ntopt_get_count(const char *str);
|
---|
46 | static char *ntopt_get_text(
|
---|
47 | const char *str, const int n, char *buf, int siz, int *len);
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * @brief Get the sentence count of the given text string.
|
---|
51 | * @param str A text string.
|
---|
52 | * @return Count of the given sentence.
|
---|
53 | */
|
---|
54 | static int ntopt_get_count(const char *str)
|
---|
55 | {
|
---|
56 | int cnt = 0;
|
---|
57 | int wc = 0;
|
---|
58 | char *p = (char *)str;
|
---|
59 | while (*p) {
|
---|
60 | if (!IS_DELIM(*p)) {
|
---|
61 | wc++;
|
---|
62 | if (wc == 1) {
|
---|
63 | cnt++;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | wc = 0;
|
---|
68 | }
|
---|
69 | p++;
|
---|
70 | }
|
---|
71 | return cnt;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * @brief Get the sentence of the given text string.
|
---|
76 | * @param str A text string.
|
---|
77 | * @param n Index number. (0 to ntopt-get_count(str) - 1)
|
---|
78 | * @param buf The pointer to a stored buffer.
|
---|
79 | * @param siz The size of the stored buffer.
|
---|
80 | * @param len The stored string length.
|
---|
81 | * @retval !NULL Success. The pointer to the buffer.
|
---|
82 | * @retval NULL Failure.
|
---|
83 | */
|
---|
84 | static char *ntopt_get_text(
|
---|
85 | const char *str, const int n, char *buf, int siz, int *len)
|
---|
86 | {
|
---|
87 | int cnt = 0;
|
---|
88 | int wc = 0;
|
---|
89 | char *p = (char *)str;
|
---|
90 | *len = 0;
|
---|
91 | while (*p) {
|
---|
92 | if (!IS_DELIM(*p)) {
|
---|
93 | wc++;
|
---|
94 | if (wc == 1) {
|
---|
95 | if (cnt == n) {
|
---|
96 | char *des = buf;
|
---|
97 | int cc = 0;
|
---|
98 | while (!IS_DELIM(*p)) {
|
---|
99 | cc++;
|
---|
100 | if (siz <= cc) {
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | *des = *p;
|
---|
104 | des++;
|
---|
105 | p++;
|
---|
106 | }
|
---|
107 | *des = '\0';
|
---|
108 | *len = cc;
|
---|
109 | return buf;
|
---|
110 | }
|
---|
111 | cnt++;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | else {
|
---|
115 | wc = 0;
|
---|
116 | }
|
---|
117 | p++;
|
---|
118 | }
|
---|
119 | return (char *)0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * @brief Parse the given text string.
|
---|
124 | * @param str A text string.
|
---|
125 | * @param func The callback function.
|
---|
126 | * @param extobj An external object for the callback function.
|
---|
127 | * @return The return value from the callback.
|
---|
128 | */
|
---|
129 | int ntopt_parse(const char *str, NTOPT_CALLBACK func, void *extobj)
|
---|
130 | {
|
---|
131 | int argc;
|
---|
132 | char argv[NTOPT_TEXT_MAXLEN];
|
---|
133 | char *argvp[1 + NTOPT_TEXT_MAXARGS];
|
---|
134 | int i;
|
---|
135 | int total;
|
---|
136 | char *p;
|
---|
137 |
|
---|
138 | argc = ntopt_get_count(str);
|
---|
139 | if (NTOPT_TEXT_MAXARGS <= argc) {
|
---|
140 | argc = NTOPT_TEXT_MAXARGS;
|
---|
141 | }
|
---|
142 |
|
---|
143 | memset(argvp, 0, sizeof(argvp));
|
---|
144 | total = 0;
|
---|
145 | p = &argv[0];
|
---|
146 | for (i = 0; i < argc; i++) {
|
---|
147 | int len;
|
---|
148 | argvp[i + 1] = ntopt_get_text(
|
---|
149 | str, i, p, NTOPT_TEXT_MAXLEN - total, &len);
|
---|
150 | if (total + len + 1 < NTOPT_TEXT_MAXLEN) {
|
---|
151 | p += len + 1;
|
---|
152 | total += len + 1;
|
---|
153 | }
|
---|
154 | else {
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | argvp[0] = (char *)argc;
|
---|
159 |
|
---|
160 | return func((long *)&argvp[0], extobj);
|
---|
161 | }
|
---|