]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/tr.c
tpm: Fix bug in GRUB2 TPM module
[grub2.git] / grub-core / commands / tr.c
1 /* tr.c -- The tr command. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2010,2013 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/dl.h>
21 #include <grub/mm.h>
22 #include <grub/err.h>
23 #include <grub/env.h>
24 #include <grub/i18n.h>
25 #include <grub/misc.h>
26 #include <grub/extcmd.h>
27
28 GRUB_MOD_LICENSE ("GPLv3+");
29
30 static const struct grub_arg_option options[] =
31 {
32 { "set", 's', 0, N_("Set a variable to return value."), N_("VARNAME"), ARG_TYPE_STRING },
33 { "upcase", 'U', 0, N_("Translate to upper case."), 0, 0 },
34 { "downcase", 'D', 0, N_("Translate to lower case."), 0, 0 },
35 { 0, 0, 0, 0, 0, 0 }
36 };
37
38 static const char *letters_lowercase = "abcdefghijklmnopqrstuvwxyz";
39 static const char *letters_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
40
41 static grub_err_t
42 grub_cmd_tr (grub_extcmd_context_t ctxt, int argc, char **args)
43 {
44 char *var = 0;
45 const char *input = 0;
46 char *output = 0, *optr;
47 const char *s1 = 0;
48 const char *s2 = 0;
49 const char *iptr;
50
51 /* Select the defaults from options. */
52 if (ctxt->state[0].set) {
53 var = ctxt->state[0].arg;
54 input = grub_env_get (var);
55 }
56
57 if (ctxt->state[1].set) {
58 s1 = letters_lowercase;
59 s2 = letters_uppercase;
60 }
61
62 if (ctxt->state[2].set) {
63 s1 = letters_uppercase;
64 s2 = letters_lowercase;
65 }
66
67 /* Check for arguments and update the defaults. */
68 if (argc == 1)
69 input = args[0];
70
71 else if (argc == 2) {
72 s1 = args[0];
73 s2 = args[1];
74
75 } else if (argc == 3) {
76 s1 = args[0];
77 s2 = args[1];
78 input = args[2];
79
80 } else if (argc > 3)
81 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters");
82
83 if (!s1 || !s2 || !input)
84 return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing parameters");
85
86 if (grub_strlen (s1) != grub_strlen (s2))
87 return grub_error (GRUB_ERR_BAD_ARGUMENT, "set sizes did not match");
88
89 /* Translate input into output buffer. */
90
91 output = grub_malloc (grub_strlen (input) + 1);
92 if (! output)
93 return grub_errno;
94
95 optr = output;
96 for (iptr = input; *iptr; iptr++)
97 {
98 char *p = grub_strchr (s1, *iptr);
99 if (p)
100 *optr++ = s2[p - s1];
101 else
102 *optr++ = *iptr;
103 }
104 *optr = '\0';
105
106 if (ctxt->state[0].set)
107 grub_env_set (var, output);
108 else
109 grub_printf ("%s\n", output);
110
111 grub_free (output);
112 return GRUB_ERR_NONE;
113 }
114
115 static grub_extcmd_t cmd;
116
117 GRUB_MOD_INIT(tr)
118 {
119 cmd = grub_register_extcmd ("tr", grub_cmd_tr, 0, N_("[OPTIONS] [SET1] [SET2] [STRING]"),
120 N_("Translate SET1 characters to SET2 in STRING."), options);
121 }
122
123 GRUB_MOD_FINI(tr)
124 {
125 grub_unregister_extcmd (cmd);
126 }