OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / man3 / getsubopt.3
1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
3 .\"
4 .\" Permission is hereby granted, free of charge, to any person obtaining
5 .\" a copy of this software and associated documentation files (the
6 .\" "Software"), to deal in the Software without restriction, including
7 .\" without limitation the rights to use, copy, modify, merge, publish,
8 .\" distribute, sublicense, and/or sell copies of the Software, and to
9 .\" permit persons to whom the Software is furnished to do so, subject to
10 .\" the following conditions:
11 .\"
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
14 .\"
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 .\"
23 .\"*******************************************************************
24 .\"
25 .\" This file was generated with po4a. Translate the source file.
26 .\"
27 .\"*******************************************************************
28 .TH GETSUBOPT 3 2010\-09\-26 GNU "Linux Programmer's Manual"
29 .SH 名前
30 getsubopt \- 文字列中のサブオプション引き数の解釈を行う
31 .SH 書式
32 \fB#include <stdlib.h>\fP
33
34 \fBint getsubopt(char **\fP\fIoptionp\fP\fB, char * const *\fP\fItokens\fP\fB, char
35 **\fP\fIvaluep\fP\fB);\fP
36 .sp
37 .in -4n
38 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
39 .in
40 .sp
41 \fBgetsubopt\fP():
42 .ad l
43 .RS 4
44 .PD 0
45 _XOPEN_SOURCE\ >= 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
46 .br
47 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
48 .PD
49 .RE
50 .ad
51 .SH 説明
52 \fBgetsubopt\fP()  は、 \fIoptionp\fP で与えられたカンマ区切りのサブオプション・リストを解析する。
53 (このようなサブオプション・リストは \fBgetopt\fP(3)  を使ってコマンドラインを解釈した場合に現れることが多い。 例えば、
54 \fBmount\fP(8)  の \fI\-o\fP オプションを見るとよい。)  それぞれのサブオプションには対応する値を指定することができる。
55 サブオプションの名前と対応する値は等号 (\(aq=\(aq) で区切られる。 例えば、以下のような文字列を \fIoptionp\fP に渡すことができる。
56 .sp
57 .in +4n
58 \fBro,name=xyz\fP
59 .in
60
61 \fItokens\fP 引き数はトークンへのポインタの配列へのポインタで、 配列は NULL で終端される。 \fBgetsubopt\fP()
62 はこのトークンを \fIoptionp\fP 内で探す。 それぞれのトークンは、NULL 終端された 1文字以上の文字列で、
63 他のトークンと区別できる必要がある。 また、等号とカンマを含んではならない。
64
65 \fBgetsubopt\fP()  は呼び出されるたびに、 \fIoptionp\fP 中の次の未処理のサブオプションの情報を返す。
66 サブオプション内に等号があった場合、最初の等号は そのサブオプションの名前と値の区切りと解釈される。 区切りから次のカンマ
67 (最後のサブオプションの場合、文字列の末尾)  までが、サブオプションの値となる。 サブオプションの名前が \fItokens\fP
68 内の名前と一致し、値を表す文字列が見つかった場合、 \fBgetsubopt\fP()  は \fI*valuep\fP を値を表す文字列のアドレスに設定する。
69 \fIoptionp\fP 中の最初のカンマはヌルバイトで上書きされる。そのため、 \fI*valuep\fP はそのサブオプションの「値の文字列」そのものとなる。
70
71 サブオプションが認識されたが、値を表す文字列が見つからなかった場合、 *\fIvaluep\fP は NULL に設定される。
72
73 \fBgetsubopt\fP()  が返る時、 \fIoptionp\fP は次のサブオプションを指している。 ちょうど最後のサブオプションが処理された場合は、
74 文字列末尾のヌルバイト (\(aq\e0\(aq) を指している。
75 .SH 返り値
76 \fIoptionp\fP 内でサブオプションが見つかった場合、 \fBgetsubopt\fP()  は最初のサブオプションにマッチする \fItokens\fP
77 の要素の添字を返す。 見つからなかった場合、\-1 を返す。この場合、 \fI*valuep\fP は \fIname\fP\fB[=\fP\fIvalue\fP\fB]\fP
78 の文字列全体となる。
79
80 \fI*optionp\fP は変更されるので、 \fBgetsubopt\fP()  を呼び出す前の最初のサブオプションは \fBgetsubopt\fP()
81 を呼び出し後のサブオプションと必ずしも同じとは限らない。
82 .SH 準拠
83 POSIX.1\-2001.
84 .SH 注意
85
86 \fBgetsubopt\fP()  は、文字列 *\fIoptionp\fP 中に見つけたカンマを上書きするので、文字列 \fI*optionp\fP
87 は書き込み可能でなければならず、 文字列定数にすることはできない。
88 .SH 例
89 以下のプログラムは "\-o" オプションに続いてサブオプションがあることを 期待している。
90
91 .nf
92 #define _XOPEN_SOURCE 500
93 #include <stdlib.h>
94 #include <assert.h>
95 #include <stdio.h>
96
97 int main(int argc, char **argv)
98 {
99     enum {
100         RO_OPT = 0,
101         RW_OPT,
102         NAME_OPT
103     };
104     char *const token[] = {
105         [RO_OPT]   = "ro",
106         [RW_OPT]   = "rw",
107         [NAME_OPT] = "name",
108         NULL
109     };
110     char *subopts;
111     char *value;
112     int opt;
113
114     int readonly = 0;
115     int readwrite = 0;
116     char *name = NULL;
117     int errfnd = 0;
118
119     while ((opt = getopt(argc, argv, "o:")) != \-1) {
120         switch (opt) {
121         case \(aqo\(aq:
122             subopts = optarg;
123             while (*subopts != \(aq\e0\(aq && !errfnd) {
124
125             switch (getsubopt(&subopts, token, &value)) {
126             case RO_OPT:
127                 readonly = 1;
128                 break;
129
130             case RW_OPT:
131                 readwrite = 1;
132                 break;
133
134             case NAME_OPT:
135                 if (value == NULL) {
136                     fprintf(stderr, "Missing value for "
137                             "suboption \(aq%s\(aq\en", token[NAME_OPT]);
138                     errfnd = 1;
139                     continue;
140                 }
141
142                 name = value;
143                 break;
144
145             default:
146                 fprintf(stderr, "No match found "
147                         "for token: /%s/\en", value);
148                 errfnd = 1;
149                 break;
150             }
151         }
152         if (readwrite && readonly) {
153             fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
154                     "specified\en", token[RO_OPT], token[RW_OPT]);
155             errfnd = 1;
156         }
157         break;
158
159         default:
160             errfnd = 1;
161         }
162     }
163
164     if (errfnd || argc == 1) {
165         fprintf(stderr, "\enUsage: %s \-o <suboptstring>\en", argv[0]);
166         fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
167                 "and \(aqname=<value>\(aq\en");
168         exit(EXIT_FAILURE);
169     }
170
171     /* Remainder of program... */
172
173     exit(EXIT_SUCCESS);
174 }
175 .fi
176 .SH 関連項目
177 \fBgetopt\fP(3)