3 * @author Shinichiro Nakamura
4 * @brief NT-Shell用オプション解析モジュールの実装。
8 * ===============================================================
9 * Natural Tiny Shell (NT-Shell)
10 * ===============================================================
11 * Copyright (c) 2010-2012 Shinichiro Nakamura
13 * Permission is hereby granted, free of charge, to any person
14 * obtaining a copy of this software and associated documentation
15 * files (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use,
17 * copy, modify, merge, publish, distribute, sublicense, and/or
18 * sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
27 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
29 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 * OTHER DEALINGS IN THE SOFTWARE.
33 * ===============================================================
39 * @brief 与えられた文字がデリミタであるかどうかを判定する。
41 * @retval true デリミタである。
42 * @retval false デリミタでない。
45 (((c) == '\r') || ((c) == '\n') || ((c) == '\t') || ((c) == '\0') || ((c) == ' '))
47 static int ntopt_get_count(const char *str);
48 static char *ntopt_get_text(
49 const char *str, const int n, char *buf, int siz, int *len);
52 * @brief 与えられた文字列のセンテンス数を取得する。
56 static int ntopt_get_count(const char *str)
60 char *p = (char *)str;
76 * @brief 与えられた文字列のセンテンスを取得する。
78 * @param n センテンス。(0からntopt-get_count(str) - 1までの値。)
80 * @param siz 格納バッファのサイズ。
81 * @param len 格納した文字列のサイズ。
82 * @retval !NULL 取得成功。格納バッファへのポインタ。
85 static char *ntopt_get_text(
86 const char *str, const int n, char *buf, int siz, int *len)
90 char *p = (char *)str;
99 while (!IS_DELIM(*p)) {
123 * @brief 与えられた文字列をデリミタで分割する。
125 * @param func コールバック関数。
126 * @param extobj 拡張オブジェクト。
127 * @return コールバック関数が返した値。
129 int ntopt_parse(const char *str, NTOPT_CALLBACK func, void *extobj)
132 char argv[NTOPT_TEXT_MAXLEN];
133 char *argvp[NTOPT_TEXT_MAXARGS];
138 argc = ntopt_get_count(str);
139 if (NTOPT_TEXT_MAXARGS <= argc) {
140 argc = NTOPT_TEXT_MAXARGS;
145 for (i = 0; i < argc; i++) {
147 argvp[i] = ntopt_get_text(
148 str, i, p, NTOPT_TEXT_MAXLEN - total, &len);
149 if (total + len + 1 < NTOPT_TEXT_MAXLEN) {
157 return func(argc, &argvp[0], extobj);