1 /* tr.c -- The tr command. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2010,2013 Free Software Foundation, Inc.
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.
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.
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/>.
24 #include <grub/i18n.h>
25 #include <grub/misc.h>
26 #include <grub/extcmd.h>
28 GRUB_MOD_LICENSE ("GPLv3+");
30 static const struct grub_arg_option options
[] =
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 },
38 static const char *letters_lowercase
= "abcdefghijklmnopqrstuvwxyz";
39 static const char *letters_uppercase
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
42 grub_cmd_tr (grub_extcmd_context_t ctxt
, int argc
, char **args
)
45 const char *input
= 0;
46 char *output
= 0, *optr
;
51 /* Select the defaults from options. */
52 if (ctxt
->state
[0].set
) {
53 var
= ctxt
->state
[0].arg
;
54 input
= grub_env_get (var
);
57 if (ctxt
->state
[1].set
) {
58 s1
= letters_lowercase
;
59 s2
= letters_uppercase
;
62 if (ctxt
->state
[2].set
) {
63 s1
= letters_uppercase
;
64 s2
= letters_lowercase
;
67 /* Check for arguments and update the defaults. */
75 } else if (argc
== 3) {
81 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "too many parameters");
83 if (!s1
|| !s2
|| !input
)
84 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "missing parameters");
86 if (grub_strlen (s1
) != grub_strlen (s2
))
87 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "set sizes did not match");
89 /* Translate input into output buffer. */
91 output
= grub_malloc (grub_strlen (input
) + 1);
96 for (iptr
= input
; *iptr
; iptr
++)
98 char *p
= grub_strchr (s1
, *iptr
);
100 *optr
++ = s2
[p
- s1
];
106 if (ctxt
->state
[0].set
)
107 grub_env_set (var
, output
);
109 grub_printf ("%s\n", output
);
112 return GRUB_ERR_NONE
;
115 static grub_extcmd_t cmd
;
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
);
125 grub_unregister_extcmd (cmd
);