Mercurial > gnulib
annotate lib/yesno.c @ 40057:b06060465f09
maint: Run 'make update-copyright'
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 01 Jan 2019 00:25:11 +0100 |
parents | 10eb9086bea0 |
children |
rev | line source |
---|---|
5 | 1 /* yesno.c -- read a yes/no response from stdin |
5851 | 2 |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
3 Copyright (C) 1990, 1998, 2001, 2003-2019 Free Software Foundation, Inc. |
5 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
8 (at your option) any later version. |
5 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
19190 | 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
17 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
18 #include <config.h> |
5 | 19 |
5149 | 20 #include "yesno.h" |
21 | |
4684 | 22 #include <stdlib.h> |
5 | 23 #include <stdio.h> |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5149
diff
changeset
|
24 |
9157
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
25 /* Return true if we read an affirmative line from standard input. |
5 | 26 |
9157
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
27 Since this function uses stdin, it is suggested that the caller not |
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
28 use STDIN_FILENO directly, and also that the line |
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
29 atexit(close_stdin) be added to main(). */ |
666
969996bb277b
(yesno) [!HAVE_RPMATCH]: Remove function since we'll
Jim Meyering <jim@meyering.net>
parents:
661
diff
changeset
|
30 |
5149 | 31 bool |
4686
70bf7ce55a75
(main): Define with a prototype.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4684
diff
changeset
|
32 yesno (void) |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
33 { |
7726
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
34 bool yes; |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
35 |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
36 #if ENABLE_NLS |
5851 | 37 char *response = NULL; |
38 size_t response_size = 0; | |
39 ssize_t response_len = getline (&response, &response_size, stdin); | |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
40 |
5851 | 41 if (response_len <= 0) |
42 yes = false; | |
43 else | |
44 { | |
17948
88a13cbc41e7
yesno: make EOL optional in ENABLE_NLS case also
Tobias Stoeckmann <tobias@stoeckmann.org>
parents:
17848
diff
changeset
|
45 /* Remove EOL if present as that's not part of the matched response, |
88a13cbc41e7
yesno: make EOL optional in ENABLE_NLS case also
Tobias Stoeckmann <tobias@stoeckmann.org>
parents:
17848
diff
changeset
|
46 and not matched by $ for example. */ |
88a13cbc41e7
yesno: make EOL optional in ENABLE_NLS case also
Tobias Stoeckmann <tobias@stoeckmann.org>
parents:
17848
diff
changeset
|
47 if (response[response_len - 1] == '\n') |
88a13cbc41e7
yesno: make EOL optional in ENABLE_NLS case also
Tobias Stoeckmann <tobias@stoeckmann.org>
parents:
17848
diff
changeset
|
48 response[response_len - 1] = '\0'; |
5851 | 49 yes = (0 < rpmatch (response)); |
50 } | |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
51 |
5851 | 52 free (response); |
7726
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
53 #else |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
54 /* Test against "^[yY]", hardcoded to avoid requiring getline, |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
55 regex, and rpmatch. */ |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
56 int c = getchar (); |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
57 yes = (c == 'y' || c == 'Y'); |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
58 while (c != '\n' && c != EOF) |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
59 c = getchar (); |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
60 #endif |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
61 |
5851 | 62 return yes; |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
63 } |