.\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 .\" %FreeBSD: src/lib/libc/stdlib/getsubopt.3,v 1.5.2.4 2001/12/14 18:33:58 ru Exp % .\" $FreeBSD$ .\" .Dd June 9, 1993 .Dt GETSUBOPT 3 .Os .Sh 名称 .Nm getsubopt .Nd 引数からサブオプションを取得 .Sh ライブラリ .Lb libc .Sh 書式 .In unistd.h .Vt extern char *suboptarg ; .Ft int .Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" .Sh 解説 .Fn getsubopt 関数は、 1 つまたは複数のタブ、スペースまたはコンマ .Pq Ql \&, キャラクタによって区切られたトークンが含まれる文字列を解析します。 ユーティリティのコマンドラインの一部として入力される、 オプション引数のまとまりを解析するのに使用するのが目的です。 .Pp 引数 .Fa optionp は、文字列へのポインタを指すポインタです。 引数 .Fa tokens は、文字列へのポインタを持つ、 .Dv NULL で終わる配列を指すポインタです。 .Pp .Fn getsubopt 関数は、文字列の最初のトークンに適合する文字列を参照する .Fa tokens 配列中のポインタの、0 始まりのオフセットを返します。 文字列にトークンが一切含まれない、または .Fa tokens 中に適合する文字列が含まれない場合には \-1 を返します。 .Pp トークンの形式が ``name=value'' である場合、 .Fa valuep で参照される先は、 トークンの ``value'' の部分の先頭を指すように設定されます。 .Pp .Fn getsubopt から戻った時、 .Fa optionp が文字列の次のトークンの先頭を指すように設定されます。 または、それ以上のトークンがない場合には、 文字列の終わりの null を指します。 外部変数 .Fa suboptarg は、現在のトークンの先頭を指すように設定されます。 トークンがなかった場合は、 .Dv NULL となります。 引数 .Fa valuep は、トークンの ``value'' の部分を示すように設定されます。 または、``value'' 部分がなかった場合は .Dv NULL となります。 .Sh 使用例 .Bd -literal -compact char *tokens[] = { #define ONE 0 "one", #define TWO 1 "two", NULL }; \&... extern char *optarg, *suboptarg; char *option, *value; while ((ch = getopt(argc, argv, "ab:")) != -1) { switch(ch) { case 'a': /* ``a'' オプションを処理 */ break; case 'b': option = optarg; while (*option) { switch(getsubopt(&option, tokens, &value)) { case ONE: /* ``one'' サブオプションを処理 */ break; case TWO: /* ``two'' サブオプションを処理 */ if (!value) error("no value for two"); i = atoi(value); break; case \-1: if (suboptarg) error("illegal sub option %s", suboptarg); else error("missing sub option"); break; } break; } .Ed .Sh 関連項目 .Xr getopt 3 , .Xr strsep 3 .Sh 歴史 .Fn getsubopt 関数は .Bx 4.4 ではじめて登場しました。