1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
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:
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
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.
23 .\" Japanese Version Copyright (c) 2007 Akihiro MOTOKI
24 .\" all rights reserved.
25 .\" Translated 2007-06-02, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
27 .TH GETSUBOPT 3 2008-05-29 "GNU" "Linux Programmer's Manual"
30 .\"O getsubopt \- parse suboption arguments from a string
31 getsubopt \- ʸ»úÎóÃæ¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó°ú¤¿ô¤Î²ò¼á¤ò¹Ô¤¦
34 .B #define _XOPEN_SOURCE 500
36 .B #include <stdlib.h>
38 .BI "int getsubopt(char **"optionp ", char * const *" tokens \
43 .\"O parses the list of comma-separated suboptions provided in
45 .\"O (Such a suboption list is typically produced when
47 .\"O is used to parse a command line;
48 .\"O see for example the \fI-o\fP option of
50 .\"O Each suboption may include an associated value,
51 .\"O which is separated from the suboption name by an equal sign.
52 .\"O The following is an example of the kind of string
53 .\"O that might be passed in
58 ¤ÇÍ¿¤¨¤é¤ì¤¿¥«¥ó¥Þ¶èÀÚ¤ê¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¡¦¥ê¥¹¥È¤ò²òÀϤ¹¤ë¡£
59 (¤³¤Î¤è¤¦¤Ê¥µ¥Ö¥ª¥×¥·¥ç¥ó¡¦¥ê¥¹¥È¤Ï
61 ¤ò»È¤Ã¤Æ¥³¥Þ¥ó¥É¥é¥¤¥ó¤ò²ò¼á¤·¤¿¾ì¹ç¤Ë¸½¤ì¤ë¤³¤È¤¬Â¿¤¤¡£
64 ¤Î \fI-o\fP ¥ª¥×¥·¥ç¥ó¤ò¸«¤ë¤È¤è¤¤¡£)
65 ¤½¤ì¤¾¤ì¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Ë¤ÏÂбþ¤¹¤ëÃͤò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
66 ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î̾Á°¤ÈÂбþ¤¹¤ëÃͤÏÅù¹æ (\(aq=\(aq) ¤Ç¶èÀÚ¤é¤ì¤ë¡£
67 Î㤨¤Ð¡¢°Ê²¼¤Î¤è¤¦¤Êʸ»úÎó¤ò
77 .\"O argument is a pointer to a NULL-terminated list of the tokens that
81 .\"O The tokens should be distinct, null-terminated strings containing at
82 .\"O least one character, with no embedded equal signs or commas.
84 °ú¤¿ô¤Ï¥È¡¼¥¯¥ó¤Î¥ê¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¡¢¥ê¥¹¥È¤Ï NULL ¤Ç½ªÃ¼¤µ¤ì¤ë¡£
89 ¤½¤ì¤¾¤ì¤Î¥È¡¼¥¯¥ó¤Ï¡¢NULL ½ªÃ¼¤µ¤ì¤¿ 1ʸ»ú°Ê¾å¤Îʸ»úÎó¤Ç¡¢
90 ¾¤Î¥È¡¼¥¯¥ó¤È¶èÊ̤Ǥ¤ëɬÍפ¬¤¢¤ë¡£
91 ¤Þ¤¿¡¢Åù¹æ¤È¥«¥ó¥Þ¤ò´Þ¤ó¤Ç¤Ï¤Ê¤é¤Ê¤¤¡£
95 .\"O returns information about the next unprocessed suboption in
98 ¤Ï¸Æ¤Ó½Ð¤µ¤ì¤ë¤¿¤Ó¤Ë¡¢
100 Ãæ¤Î¼¡¤Î̤½èÍý¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î¾ðÊó¤òÊÖ¤¹¡£
101 .\"O The first equal sign in a suboption (if any) is interpreted as a
102 .\"O separator between the name and the value of that suboption.
103 .\"O The value extends to the next comma,
104 .\"O or (for the last suboption) to the end of the string.
105 ¥µ¥Ö¥ª¥×¥·¥ç¥óÆâ¤ËÅù¹æ¤¬¤¢¤Ã¤¿¾ì¹ç¡¢ºÇ½é¤ÎÅù¹æ¤Ï
106 ¤½¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î̾Á°¤ÈÃͤζèÀÚ¤ê¤È²ò¼á¤µ¤ì¤ë¡£
107 ¶èÀڤ꤫¤é¼¡¤Î¥«¥ó¥Þ (ºÇ¸å¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î¾ì¹ç¡¢Ê¸»úÎó¤ÎËöÈø)
108 ¤Þ¤Ç¤¬¡¢¥µ¥Ö¥ª¥×¥·¥ç¥ó¤ÎÃͤȤʤ롣
109 .\"O If the name of the suboption matches a known name from
111 .\"O and a value string was found,
112 .\"O .BR getsubopt ()
115 .\"O to the address of that string.
116 ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î̾Á°¤¬
118 Æâ¤Î̾Á°¤È°ìÃפ·¡¢Ãͤòɽ¤¹Ê¸»úÎ󤬸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
122 ¤òÃͤòɽ¤¹Ê¸»úÎó¤Î¥¢¥É¥ì¥¹¤ËÀßÄꤹ¤ë¡£
123 .\"O The first comma in
125 .\"O is overwritten with a null byte, so
127 .\"O is precisely the "value string" for that suboption.
129 Ãæ¤ÎºÇ½é¤Î¥«¥ó¥Þ¤Ï¥Ì¥ë¥Ð¥¤¥È¤Ç¾å½ñ¤¤µ¤ì¤ë¡£¤½¤Î¤¿¤á¡¢
131 ¤Ï¤½¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î¡ÖÃͤÎʸ»úÎó¡×¤½¤Î¤â¤Î¤È¤Ê¤ë¡£
133 .\"O If the suboption is recognized, but no value string was found,
136 ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤¬Ç§¼±¤µ¤ì¤¿¤¬¡¢Ãͤòɽ¤¹Ê¸»úÎ󤬸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
138 ¤Ï NULL ¤ËÀßÄꤵ¤ì¤ë¡£
141 .\"O .BR getsubopt ()
144 .\"O points to the next suboption, or to the null character at the end of the
145 .\"O string if the last suboption was just processed.
149 ¤Ï¼¡¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤ò»Ø¤·¤Æ¤¤¤ë¡£
150 ¤Á¤ç¤¦¤ÉºÇ¸å¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤¬½èÍý¤µ¤ì¤¿¾ì¹ç¤Ï¡¢
151 ʸ»úÎóËöÈø¤Î¥Ì¥ëʸ»ú¤ò»Ø¤·¤Æ¤¤¤ë¡£
152 .\"O .SH RETURN VALUE
154 .\"O If the first suboption in
157 .\"O .BR getsubopt ()
158 .\"O returns the index of the matching suboption element in
160 .\"O Otherwise, \-1 is returned and
163 .\"O .IB name [= value ]
166 Æâ¤Ç¥µ¥Ö¥ª¥×¥·¥ç¥ó¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
168 ¤ÏºÇ½é¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Ë¥Þ¥Ã¥Á¤¹¤ë
171 ¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢\-1 ¤òÊÖ¤¹¡£¤³¤Î¾ì¹ç¡¢
179 .\"O is changed, the first suboption before the call to
180 .\"O .BR getsubopt ()
181 .\"O is not (necessarily) the same as the first suboption after
182 .\"O .BR getsubopt ().
186 ¤ò¸Æ¤Ó½Ð¤¹Á°¤ÎºÇ½é¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Ï
188 ¤ò¸Æ¤Ó½Ð¤·¸å¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Èɬ¤º¤·¤âƱ¤¸¤È¤Ï¸Â¤é¤Ê¤¤¡£
189 .\"O .SH CONFORMING TO
196 .\"O .BR getsubopt ()
197 .\"O overwrites any commas it finds in the string
199 .\"O that string must be writable; it cannot be a string constant.
203 Ãæ¤Ë¸«¤Ä¤±¤¿¥«¥ó¥Þ¤ò¾å½ñ¤¤¹¤ë¤Î¤Ç¡¢Ê¸»úÎó
205 ¤Ï½ñ¤¹þ¤ß²Äǽ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤º¡¢
206 ʸ»úÎóÄê¿ô¤Ë¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£
209 .\"O The following program expects suboptions following a "\-o" option.
210 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï "\-o" ¥ª¥×¥·¥ç¥ó¤Ë³¤¤¤Æ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤¬¤¢¤ë¤³¤È¤ò
214 #define _XOPEN_SOURCE 500
219 int main(int argc, char **argv)
226 char *const token[] = {
241 while ((opt = getopt(argc, argv, "o:")) != \-1) {
245 while (*subopts != \(aq\\0\(aq && !errfnd) {
247 switch (getsubopt(&subopts, token, &value)) {
258 fprintf(stderr, "Missing value for "
259 "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
268 fprintf(stderr, "No match found "
269 "for token: /%s/\\n", value);
274 if (readwrite && readonly) {
275 fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
276 "specified\\n", token[RO_OPT], token[RW_OPT]);
286 if (errfnd || argc == 1) {
287 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
288 fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
289 "and \(aqname=<value>\(aq\\n");
293 /* Remainder of program... */
301 .BR feature_test_macros (7)