質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

FreeBSD

FreeBSDは、Unix系のオープンソースのOSです。PC/AT互換機用ですが、他のプラットフォームにも移植されています。優れたネットワーク・セキュリティ・ストレージ機能で人気のOSです。ソースコードと共に無償で公開されており、多くの コミュニティによって長年に渡って開発されています。

Q&A

解決済

3回答

1575閲覧

FreeBSD getopt_long getopt_internal

Grandeur

総合スコア15

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

FreeBSD

FreeBSDは、Unix系のオープンソースのOSです。PC/AT互換機用ですが、他のプラットフォームにも移植されています。優れたネットワーク・セキュリティ・ストレージ機能で人気のOSです。ソースコードと共に無償で公開されており、多くの コミュニティによって長年に渡って開発されています。

0グッド

1クリップ

投稿2020/02/26 02:25

編集2020/02/26 04:28

FreeBSDのソースコードをSubverisonで取ってきて読んでいるんですけど、lib/libc/stdlib/getopt_long.cにある、getopt_internal()の対になって終わる、最後の"}"の括弧がないのですが、どうゆう意味があるんでしょうか?詳しい方誰か教えてください。よろしくお願い申し上げます。
versionは12.1です。

c

1static int 2getopt_internal(int nargc, char * const *nargv, const char *options, 3 const struct option *long_options, int *idx, int flags) 4{ 5 char *oli; /* option letter list index */ 6 int optchar, short_too; 7 static int posixly_correct = -1; 8 9 if (options == NULL) 10 return (-1); 11 12 /* 13 * XXX Some GNU programs (like cvs) set optind to 0 instead of 14 * XXX using optreset. Work around this braindamage. 15 */ 16 if (optind == 0) 17 optind = optreset = 1; 18 19 /* 20 * Disable GNU extensions if POSIXLY_CORRECT is set or options 21 * string begins with a '+'. 22 */ 23 if (posixly_correct == -1 || optreset) 24 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); 25 if (*options == '-') 26 flags |= FLAG_ALLARGS; 27 else if (posixly_correct || *options == '+') 28 flags &= ~FLAG_PERMUTE; 29 if (*options == '+' || *options == '-') 30 options++; 31 32 optarg = NULL; 33 if (optreset) 34 nonopt_start = nonopt_end = -1; 35start: 36 if (optreset || !*place) { /* update scanning pointer */ 37 optreset = 0; 38 if (optind >= nargc) { /* end of argument vector */ 39 place = EMSG; 40 if (nonopt_end != -1) { 41 /* do permutation, if we have to */ 42 permute_args(nonopt_start, nonopt_end, 43 optind, nargv); 44 optind -= nonopt_end - nonopt_start; 45 } 46 else if (nonopt_start != -1) { 47 /* 48 * If we skipped non-options, set optind 49 * to the first of them. 50 */ 51 optind = nonopt_start; 52 } 53 nonopt_start = nonopt_end = -1; 54 return (-1); 55 } 56 if (*(place = nargv[optind]) != '-' || 57#ifdef GNU_COMPATIBLE 58 place[1] == '\0') { 59#else 60 (place[1] == '\0' && strchr(options, '-') == NULL)) { 61#endif 62 place = EMSG; /* found non-option */ 63 if (flags & FLAG_ALLARGS) { 64 /* 65 * GNU extension: 66 * return non-option as argument to option 1 67 */ 68 optarg = nargv[optind++]; 69 return (INORDER); 70 } 71 if (!(flags & FLAG_PERMUTE)) { 72 /* 73 * If no permutation wanted, stop parsing 74 * at first non-option. 75 */ 76 return (-1); 77 } 78 /* do permutation */ 79 if (nonopt_start == -1) 80 nonopt_start = optind; 81 else if (nonopt_end != -1) { 82 permute_args(nonopt_start, nonopt_end, 83 optind, nargv); 84 nonopt_start = optind - 85 (nonopt_end - nonopt_start); 86 nonopt_end = -1; 87 } 88 optind++; 89 /* process next argument */ 90 goto start; 91 } 92 if (nonopt_start != -1 && nonopt_end == -1) 93 nonopt_end = optind; 94 95 /* 96 * If we have "-" do nothing, if "--" we are done. 97 */ 98 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { 99 optind++; 100 place = EMSG; 101 /* 102 * We found an option (--), so if we skipped 103 * non-options, we have to permute. 104 */ 105 if (nonopt_end != -1) { 106 permute_args(nonopt_start, nonopt_end, 107 optind, nargv); 108 optind -= nonopt_end - nonopt_start; 109 } 110 nonopt_start = nonopt_end = -1; 111 return (-1); 112 } 113 } 114 115 /* 116 * Check long options if: 117 * 1) we were passed some 118 * 2) the arg is not just "-" 119 * 3) either the arg starts with -- we are getopt_long_only() 120 */ 121 if (long_options != NULL && place != nargv[optind] && 122 (*place == '-' || (flags & FLAG_LONGONLY))) { 123 short_too = 0; 124#ifdef GNU_COMPATIBLE 125 dash_prefix = D_PREFIX; 126#endif 127 if (*place == '-') { 128 place++; /* --foo long option */ 129 if (*place == '\0') 130 return (BADARG); /* malformed option */ 131#ifdef GNU_COMPATIBLE 132 dash_prefix = DD_PREFIX; 133#endif 134 } else if (*place != ':' && strchr(options, *place) != NULL) 135 short_too = 1; /* could be short option too */ 136 137 optchar = parse_long_options(nargv, options, long_options, 138 idx, short_too, flags); 139 if (optchar != -1) { 140 place = EMSG; 141 return (optchar); 142 } 143 } 144 145 if ((optchar = (int)*place++) == (int)':' || 146 (optchar == (int)'-' && *place != '\0') || 147 (oli = strchr(options, optchar)) == NULL) { 148 /* 149 * If the user specified "-" and '-' isn't listed in 150 * options, return -1 (non-option) as per POSIX. 151 * Otherwise, it is an unknown option character (or ':'). 152 */ 153 if (optchar == (int)'-' && *place == '\0') 154 return (-1); 155 if (!*place) 156 ++optind; 157#ifdef GNU_COMPATIBLE 158 if (PRINT_ERROR) 159 warnx(posixly_correct ? illoptchar : gnuoptchar, 160 optchar); 161#else 162 if (PRINT_ERROR) 163 warnx(illoptchar, optchar); 164#endif 165 optopt = optchar; 166 return (BADCH); 167 } 168 if (long_options != NULL && optchar == 'W' && oli[1] == ';') { 169 /* -W long-option */ 170 if (*place) /* no space */ 171 /* NOTHING */; 172 else if (++optind >= nargc) { /* no arg */ 173 place = EMSG; 174 if (PRINT_ERROR) 175 warnx(recargchar, optchar); 176 optopt = optchar; 177 return (BADARG); 178 } else /* white space */ 179 place = nargv[optind]; 180#ifdef GNU_COMPATIBLE 181 dash_prefix = W_PREFIX; 182#endif 183 optchar = parse_long_options(nargv, options, long_options, 184 idx, 0, flags); 185 place = EMSG; 186 return (optchar); 187 } 188 if (*++oli != ':') { /* doesn't take argument */ 189 if (!*place) 190 ++optind; 191 } else { /* takes (optional) argument */ 192 optarg = NULL; 193 if (*place) /* no white space */ 194 optarg = place; 195 else if (oli[1] != ':') { /* arg not optional */ 196 if (++optind >= nargc) { /* no arg */ 197 place = EMSG; 198 if (PRINT_ERROR) 199 warnx(recargchar, optchar); 200 optopt = optchar; 201 return (BADARG); 202 } else 203 optarg = nargv[optind]; 204 } 205 place = EMSG; 206 ++optind; 207 } 208 /* dump back option letter */ 209 return (optchar); 210}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DreamTheater

2020/02/26 02:44

ソースコードを提示してください。
guest

回答3

0

ベストアンサー

odox86 さんのおっしゃることが当たっているように思います。

FreeBSD 11.3-RELEASE の該当のソースコードを確認しましたが

359行目で始まる 「}」は

c

1static int 2getopt_internal(int nargc, char * const *nargv, const char *options, 3 const struct option *long_options, int *idx, int flags) 4{

は、わたしが vim で見ているところでは、563行目で「}」で閉じていますよ。

c

1 /* dump back option letter */ 2 return (optchar); 3}

投稿2020/02/26 03:24

showkit

総合スコア1638

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Grandeur

2020/02/26 04:26

私もvimで%で確認してみたり、一つずつ調べてみたりしたのですが、なかなか見当たりません。
dodox86

2020/02/26 04:36

>Grandeurさん #ifdef~#else~#endif を(適切に、さもプリプロセスするように)外してみれば分かります。
kgreenjp

2020/02/26 05:01 編集

ちゃんとくくってありますね。。。 dodox86さんのおっしゃっている内容をよく確認してください。 >#ifdef により、開きカッコと閉じカッコの数が必ずしも同じではないからでは?(<結構、C言語"あるある"です) 412行目あたりに下記の記述があります。 #ifdef GNU_COMPATIBLE place[1] == '\0') { #else (place[1] == '\0' && strchr(options, '-') == NULL)) { #endif これがあるため、エディタで単純に”{”の対を調べると同じ数になりません。よってエディタは対が無いよな判定になってしまっているという事です。
Grandeur

2020/02/26 05:02

理解できました、全く自分の経験不足でした。ありがとうございます。
kgreenjp

2020/02/26 05:08

いえいえ、解決できて何よりです。 私も最初あれ~?ないなぁー?って思いましたよ♪ vimで%で確認すると確かに無いように感じますよね
guest

0

こんにちは。
157~164行目の条件付きコンパイル部分の { を、#ifdef側と#else側両方有効として読んでいませんか?

投稿2020/02/26 04:38

DreamTheater

総合スコア1095

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

どこが、走行しているのか、確認したいのであれば
gcc で -D -E オプションを使用して
標準出力をファイルに書き出し
カッコの対応をとってはいかがでしょうか?

投稿2020/02/26 04:33

nanami12

総合スコア1015

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問