1 /* agent-opt.c - Helper for certain agent options
2 * Copyright (C) 2013 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 <string.h>
33
34 #include "shareddefs.h"
35
36
37 /* Parse VALUE and return an integer representing a pinentry_mode_t.
38 (-1) is returned for an invalid VALUE. */
39 int
parse_pinentry_mode(const char * value)40 parse_pinentry_mode (const char *value)
41 {
42 int result;
43
44 if (!strcmp (value, "ask") || !strcmp (value, "default"))
45 result = PINENTRY_MODE_ASK;
46 else if (!strcmp (value, "cancel"))
47 result = PINENTRY_MODE_CANCEL;
48 else if (!strcmp (value, "error"))
49 result = PINENTRY_MODE_ERROR;
50 else if (!strcmp (value, "loopback"))
51 result = PINENTRY_MODE_LOOPBACK;
52 else
53 result = -1;
54
55 return result;
56 }
57
58 /* Return the string representation for the pinentry MODE. Returns
59 "?" for an invalid mode. */
60 const char *
str_pinentry_mode(pinentry_mode_t mode)61 str_pinentry_mode (pinentry_mode_t mode)
62 {
63 switch (mode)
64 {
65 case PINENTRY_MODE_ASK: return "ask";
66 case PINENTRY_MODE_CANCEL: return "cancel";
67 case PINENTRY_MODE_ERROR: return "error";
68 case PINENTRY_MODE_LOOPBACK: return "loopback";
69 }
70 return "?";
71 }
72
73
74 /* Parse VALUE and return an integer representing a request_origin_t.
75 * (-1) is returned for an invalid VALUE. */
76 int
parse_request_origin(const char * value)77 parse_request_origin (const char *value)
78 {
79 int result;
80
81 if (!strcmp (value, "none") || !strcmp (value, "local"))
82 result = REQUEST_ORIGIN_LOCAL;
83 else if (!strcmp (value, "remote"))
84 result = REQUEST_ORIGIN_REMOTE;
85 else if (!strcmp (value, "browser"))
86 result = REQUEST_ORIGIN_BROWSER;
87 else
88 result = -1;
89
90 return result;
91 }
92
93
94 /* Return the string representation for the request origin. Returns
95 * "?" for an invalid mode. */
96 const char *
str_request_origin(request_origin_t mode)97 str_request_origin (request_origin_t mode)
98 {
99 switch (mode)
100 {
101 case REQUEST_ORIGIN_LOCAL: return "local";
102 case REQUEST_ORIGIN_REMOTE: return "remote";
103 case REQUEST_ORIGIN_BROWSER: return "browser";
104 }
105 return "?";
106 }
107