Coverage Report

Created: 2022-12-08 06:09

/src/gnupg/common/yesno.c
Line
Count
Source (jump to first uncovered line)
1
/* yesno.c - Yes/No questions
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3
 *
4
 * This file is part of GnuPG.
5
 *
6
 * This file is free software; you can redistribute it and/or modify
7
 * it under the terms of either
8
 *
9
 *   - the GNU Lesser General Public License as published by the Free
10
 *     Software Foundation; either version 3 of the License, or (at
11
 *     your option) any later version.
12
 *
13
 * or
14
 *
15
 *   - the GNU General Public License as published by the Free
16
 *     Software Foundation; either version 2 of the License, or (at
17
 *     your option) any later version.
18
 *
19
 * or both in parallel, as here.
20
 *
21
 * This file is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
28
 */
29
30
#include <config.h>
31
#include <stdlib.h>
32
#include <errno.h>
33
34
#include "i18n.h"
35
#include "util.h"
36
37
38
/* Check the string S for a YES or NO answer and take care of
39
   localization.  If no valid string is given the value of DEF_ANSWER
40
   is returned.  Returns 1 for yes and 0 for no.  */
41
int
42
answer_is_yes_no_default (const char *s, int def_answer)
43
0
{
44
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
45
0
  const char *long_yes = _("yes");
46
0
  const char *short_yes = _("yY");
47
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
48
0
  const char *long_no = _("no");
49
0
  const char *short_no = _("nN");
50
51
  /* Note: we have to use the local dependent compare here. */
52
0
  if ( match_multistr(long_yes,s) )
53
0
    return 1;
54
0
  if ( *s && strchr( short_yes, *s ) && !s[1] )
55
0
    return 1;
56
  /* Test for "no" strings to catch ambiguities for the next test. */
57
0
  if ( match_multistr(long_no,s) )
58
0
    return 0;
59
0
  if ( *s && strchr( short_no, *s ) && !s[1] )
60
0
    return 0;
61
  /* Test for the english version (for those who are used to type yes). */
62
0
  if ( !ascii_strcasecmp(s, "yes" ) )
63
0
    return 1;
64
0
  if ( *s && strchr( "yY", *s ) && !s[1] )
65
0
    return 1;
66
0
  return def_answer;
67
0
}
68
69
int
70
answer_is_yes ( const char *s )
71
0
{
72
0
  return answer_is_yes_no_default(s,0);
73
0
}
74
75
/****************
76
 * Return 1 for yes, -1 for quit, or 0 for no
77
 */
78
int
79
answer_is_yes_no_quit ( const char *s )
80
0
{
81
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
82
0
  const char *long_yes = _("yes");
83
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
84
0
  const char *long_no = _("no");
85
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
86
0
  const char *long_quit = _("quit");
87
0
  const char *short_yes = _("yY");
88
0
  const char *short_no = _("nN");
89
0
  const char *short_quit = _("qQ");
90
91
  /* Note: we have to use a local dependent compare here. */
92
0
  if ( match_multistr(long_no,s) )
93
0
    return 0;
94
0
  if ( match_multistr(long_yes,s) )
95
0
    return 1;
96
0
  if ( match_multistr(long_quit,s) )
97
0
    return -1;
98
0
  if ( *s && strchr( short_no, *s ) && !s[1] )
99
0
    return 0;
100
0
  if ( *s && strchr( short_yes, *s ) && !s[1] )
101
0
      return 1;
102
0
  if ( *s && strchr( short_quit, *s ) && !s[1] )
103
0
    return -1;
104
  /* but not here. */
105
0
  if ( !ascii_strcasecmp(s, "yes" ) )
106
0
    return 1;
107
0
  if ( !ascii_strcasecmp(s, "quit" ) )
108
0
      return -1;
109
0
  if ( *s && strchr( "yY", *s ) && !s[1] )
110
0
    return 1;
111
0
  if ( *s && strchr( "qQ", *s ) && !s[1] )
112
0
    return -1;
113
0
  return 0;
114
0
}
115
116
/*
117
   Return 1 for okay, 0 for cancel or DEF_ANSWER for default.
118
 */
119
int
120
answer_is_okay_cancel (const char *s, int def_answer)
121
0
{
122
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
123
0
  const char *long_okay = _("okay|okay");
124
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
125
0
  const char *long_cancel = _("cancel|cancel");
126
0
  const char *short_okay = _("oO");
127
0
  const char *short_cancel = _("cC");
128
129
  /* Note: We have to use the locale dependent compare. */
130
0
  if ( match_multistr(long_okay,s) )
131
0
    return 1;
132
0
  if ( match_multistr(long_cancel,s) )
133
0
    return 0;
134
0
  if ( *s && strchr( short_okay, *s ) && !s[1] )
135
0
    return 1;
136
0
  if ( *s && strchr( short_cancel, *s ) && !s[1] )
137
0
    return 0;
138
  /* Always test for the English values (not locale here). */
139
0
  if ( !ascii_strcasecmp(s, "okay" ) )
140
0
    return 1;
141
0
  if ( !ascii_strcasecmp(s, "ok" ) )
142
0
    return 1;
143
0
  if ( !ascii_strcasecmp(s, "cancel" ) )
144
0
    return 0;
145
0
  if ( *s && strchr( "oO", *s ) && !s[1] )
146
0
    return 1;
147
0
  if ( *s && strchr( "cC", *s ) && !s[1] )
148
0
    return 0;
149
0
  return def_answer;
150
0
}