1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/* virt-rescue
* Copyright (C) 2010-2020 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <locale.h>
#include <libintl.h>
#include "c-ctype.h"
#include "guestfs.h"
#include "guestfs-utils.h"
#include "rescue.h"
static void print_help (void);
static void print_inspector (void);
static void crlf (void);
static void print_escape_key (void);
/* Parse the -e parameter from the command line. */
int
parse_escape_key (const char *arg)
{
size_t len;
if (STREQ (arg, "none"))
return 0;
len = strlen (arg);
if (len == 0)
return -1;
switch (arg[0]) {
case '^':
if (len == 2 &&
((arg[1] >= 'a' && arg[1] <= 'z') ||
(arg[1] >= 'A' && arg[1] <= '_'))) {
return c_toupper (arg[1]) - '@';
}
else
return -1;
break;
}
return -1;
}
/* Print one-line end user description of the escape key.
*
* This is printed when virt-rescue starts.
*/
void
print_escape_key_help (void)
{
crlf ();
/* Difficult to translate this string. XXX */
printf ("The virt-rescue escape key is ‘");
print_escape_key ();
printf ("’. Type ‘");
print_escape_key ();
printf (" h’ for help.");
crlf ();
}
void
init_escape_state (struct escape_state *state)
{
state->in_escape = false;
}
/* Process escapes in the tty input buffer.
*
* This function has a state parameter so that we can handle an escape
* sequence split over the end of the buffer.
*
* Escape sequences are removed from the buffer.
*
* Returns true iff virt-rescue should exit.
*/
bool
process_escapes (struct escape_state *state, char *buf, size_t *len)
{
size_t i;
for (i = 0; i < *len; ++i) {
#define DROP_CURRENT_CHAR() \
memmove (&buf[i], &buf[i+1], --(*len))
#define PRINT_ESC() \
do { print_escape_key (); putchar (buf[i]); crlf (); } while (0)
if (!state->in_escape) {
if (buf[i] == escape_key) {
/* Drop the escape key from the buffer and go to escape mode. */
DROP_CURRENT_CHAR ();
state->in_escape = true;
}
}
else /* in escape sequence */ {
if (buf[i] == escape_key) /* ^] ^] means send ^] to rescue shell */
state->in_escape = false;
else {
switch (buf[i]) {
case '?': case 'h':
PRINT_ESC ();
print_help ();
break;
case 'i':
PRINT_ESC ();
print_inspector ();
break;
case 'q': case 'x':
PRINT_ESC ();
return true /* exit virt-rescue at once */;
case 's':
PRINT_ESC ();
printf (_("attempting to sync filesystems ..."));
crlf ();
guestfs_sync (g);
break;
case 'u':
PRINT_ESC ();
printf (_("unmounting filesystems ..."));
crlf ();
guestfs_umount_all (g);
break;
case 'z':
PRINT_ESC ();
raise (SIGTSTP);
break;
default:
/* Any unrecognized escape sequence will be dropped. We
* could be obnoxious and ring the bell, but I hate it when
* programs do that.
*/
break;
}
/* Drop the escape key and return to non-escape mode. */
DROP_CURRENT_CHAR ();
state->in_escape = false;
/* The output is line buffered, this is just to make sure
* everything gets written to stdout before we continue
* writing to STDOUT_FILENO.
*/
fflush (stdout);
}
} /* in escape sequence */
} /* for */
return false /* don't exit */;
}
/* This is called when the user types ^] h */
static void
print_help (void)
{
printf (_("virt-rescue escape sequences:"));
crlf ();
putchar (' ');
print_escape_key ();
printf (_(" ? - print this message"));
crlf ();
putchar (' ');
print_escape_key ();
printf (_(" h - print this message"));
crlf ();
if (inspector) {
putchar (' ');
print_escape_key ();
printf (_(" i - print inspection data"));
crlf ();
}
putchar (' ');
print_escape_key ();
printf (_(" q - quit virt-rescue"));
crlf ();
putchar (' ');
print_escape_key ();
printf (_(" s - sync the filesystems"));
crlf ();
putchar (' ');
print_escape_key ();
printf (_(" u - unmount filesystems"));
crlf ();
putchar (' ');
print_escape_key ();
printf (_(" x - quit virt-rescue"));
crlf ();
putchar (' ');
print_escape_key ();
printf (_(" z - suspend virt-rescue"));
crlf ();
printf (_("to pass the escape key through to the rescue shell, type it twice"));
crlf ();
}
/* This is called when the user types ^] i */
static void
print_inspector (void)
{
CLEANUP_FREE_STRING_LIST char **roots = NULL;
size_t i;
const char *root;
char *str;
if (inspector) {
roots = guestfs_inspect_get_roots (g);
if (roots) {
crlf ();
for (i = 0; roots[i] != NULL; ++i) {
root = roots[i];
printf (_("root device: %s"), root);
crlf ();
str = guestfs_inspect_get_product_name (g, root);
if (str) {
printf (_(" product name: %s"), str);
crlf ();
}
free (str);
str = guestfs_inspect_get_type (g, root);
if (str) {
printf (_(" type: %s"), str);
crlf ();
}
free (str);
str = guestfs_inspect_get_distro (g, root);
if (str) {
printf (_(" distro: %s"), str);
crlf ();
}
free (str);
}
}
}
}
/* Because the terminal is in raw mode, we have to send CR LF instead
* of printing just \n.
*/
static void
crlf (void)
{
putchar ('\r');
putchar ('\n');
}
static void
print_escape_key (void)
{
switch (escape_key) {
case 0:
printf ("none");
break;
case '\x1'...'\x1f':
putchar ('^');
putchar (escape_key + '@');
break;
default:
abort ();
}
}
|