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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
pconsole WJ101
Copyright (C) 2001 Walter de Jong <walter@heiho.net>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
command.c WJ101
*/
#include "config.h"
#include "commands.h"
#include "pconsole.h"
#include "cstring.h"
#include "terminal.h"
#include "Conn.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_FCNTL_H
#include "fcntl.h"
#endif
Command commands[] = {
{ "help", cmd_help, "Give help about the available commands", },
{ "?", cmd_help, "short-cut for 'help'", },
{ "version", cmd_version, "Display version information", },
{ "echo", cmd_echo, "Turn echo on or off", },
{ "attach", cmd_attach, "Attach to a tty device", },
{ "detach", cmd_detach, "Detach from a tty device", },
{ "list", cmd_list, "Show devices currently attached to", },
{ "connect", cmd_connect, "Leave command mode", },
{ "quit", cmd_exit, "Exit pconsole", },
{ "exit", cmd_exit, "Exit pconsole", },
{ NULL, NULL, NULL, },
};
int pcommand(char *buf) {
char cmd[4096], **arr, *cmd_str;
int i, len;
strcpy(cmd, buf);
cstrip_line(cmd);
if (!*cmd)
return 0;
if ((arr = cstrsplit(cmd, ' ')) == NULL) {
fprintf(stderr, "pconsole: out of memory (?)\n");
return 0;
}
len = strlen(arr[0]);
for(i = 0; commands[i].cmd != NULL; i++) {
cmd_str = commands[i].cmd;
if (*cmd_str == '|')
cmd_str++;
if (!strncmp(cmd_str, arr[0], len)) {
if (commands[i].func == NULL)
printf("pconsole: not yet implemented\n");
else
commands[i].func(&arr[1]);
break;
}
}
if (commands[i].cmd == NULL)
printf("pconsole: unknown command '%s'\n", arr[0]);
free(arr);
return 0;
}
int cmd_help(char **argv) {
int i;
if (argv == NULL || argv[0] == NULL) {
for(i = 0; commands[i].cmd != NULL; i++)
if (commands[i].cmd[0] == '|')
printf("\n %-14s %s\n", commands[i].cmd+1, commands[i].help);
else
printf(" %-14s %s\n", commands[i].cmd, commands[i].help);
} else {
int j, len;
char *cmd;
for(j = 0; argv[j] != NULL; j++) {
/*
Special command 'help warranty' to satisfy the GNU people
It is formatted at 60 chars because the pconsole.sh script opens the
window by default at only 60 chars. The message is too long so it
still scrolls off the screen, but oh well...
*/
if (!strcmp(argv[j], "warranty")) {
printf(
"Copyright (C) 2001 Walter de Jong <walter@heiho.net>\n"
"\n");
printf(
"This program is free software; you can redistribute it\n"
"and/or modify it under the terms of the GNU General Public\n"
"License as published by the Free Software Foundation;\n"
"either version 2 of the License, or (at your option)\n"
"any later version.\n"
"\n");
printf(
"This program is distributed in the hope that it will be\n"
"useful, but WITHOUT ANY WARRANTY; without even the\n"
"implied warranty of MERCHANTABILITY or FITNESS FOR A\n"
"PARTICULAR PURPOSE. See the GNU General Public License\n"
"for more details.\n"
"\n");
printf(
"You should have received a copy of the GNU General Public\n"
"License along with this program; if not, write to the\n"
"Free Software Foundation, Inc., 59 Temple Place,\n"
"Suite 330, Boston, MA 02111-1307 USA\n");
continue;
}
/*
give help on specified commands
*/
len = strlen(argv[j]);
for(i = 0; commands[i].cmd != NULL; i++) {
cmd = commands[i].cmd;
if (*cmd == '|')
cmd++;
if (!strncmp(argv[j], cmd, len)) {
printf(" %-14s %s\n", cmd, commands[i].help);
break;
}
}
if (commands[i].cmd == NULL)
printf("pconsole: unknown command '%s'\n", argv[j]);
}
}
return 0;
}
int cmd_version(char **argv) {
printf("pconsole " VERSION " WJ101\n"
"Copyright (C) 2001 Walter de Jong <walter@heiho.net>\n"
"\n"
"This is free software with ABSOLUTELY NO WARRANTY.\n"
"For details type 'help warranty'.\n"
"\n"
"The distribution page is at http://www.heiho.net/pconsole/\n"
);
return 0;
}
int cmd_echo(char **argv) {
if (argv == NULL || argv[0] == NULL)
flags ^= FLAGS_ECHO;
else
if (!strcmp(argv[0], "on"))
flags |= FLAGS_ECHO;
else
if (!strcmp(argv[0], "off"))
flags &= ~FLAGS_ECHO;
else {
printf("usage: echo [on|off]\n");
return 1;
}
printf("pconsole: echo is now toggled %s\n", (flags & FLAGS_ECHO) ? "on" : "off");
return 0;
}
int cmd_exit(char **argv) {
Conn *c, *c_next;
char *arr[2];
/*
exit: detach from all attached terminals
abuse the cmd_detach() command to do this
*/
arr[1] = NULL;
for(c = AllConns; c != NULL; c = c_next) {
c_next = c->next;
arr[0] = c->dev;
cmd_detach(arr);
}
terminal_mode(TERMINAL_COOKED);
exit(0);
return -1;
}
int cmd_connect(char **argv) {
if (AllConns == NULL) {
printf("pconsole: currently not attached to any terminal device\n");
return 0;
}
flags &= ~FLAGS_CMD_MODE; /* drop out of command mode */
return 0;
}
int cmd_attach(char **argv) {
int i, len;
char *tty, *devname, buf[256];
struct stat ttystat, ttystat2;
Conn *c;
if (argv == NULL || argv[0] == NULL) {
printf("usage: attach [hostname#]<tty device name> [...]\n");
return 1;
}
if ((tty = ttyname(fileno(stdin))) == NULL) {
printf("pconsole: not on a tty\n");
cmd_exit(NULL);
return -1;
}
if (stat(tty, &ttystat)) {
printf("failed to stat my tty %s: %s\n", tty, strerror(errno));
cmd_exit(NULL);
return -1;
}
sprintf(buf, "\r\n[pconsole attached from tty %s]\r\n", tty);
len = strlen(buf);
for(i = 0; argv[i] != NULL; i++) {
printf("attaching %s : ", argv[i]);
if ((devname = strchr(argv[i], '#')) != NULL) {
*devname = 0;
devname++;
if (!*devname) {
printf("invalid terminal device name '%s:'\n", argv[i]);
continue;
}
} else
devname = argv[i];
/*
do a couple of safety checks
the given name should be a character device, and it should not be the same
as the controlling tty
*/
if (stat(devname, &ttystat2)) {
printf("%s\n", strerror(errno));
continue;
}
if (((ttystat2.st_mode & S_IFMT) & S_IFCHR) != S_IFCHR) {
printf("not a character device\n");
continue;
}
#ifdef HAVE_ST_RDEV
if (ttystat2.st_rdev == ttystat.st_rdev) {
printf("cannot create a pconsole loop\n");
continue;
}
if (find_Conn_by_rdev(ttystat2.st_rdev) != NULL) {
printf("already attached\n");
continue;
}
#else
if (find_Conn_by_name(devname) != NULL) {
printf("already attached\n");
continue;
}
#endif
/*
allocate connection object
*/
if ((c = new_Conn()) == NULL) {
printf("out of memory (?)\n");
return -1;
}
c->dev = strdup(devname);
if (devname != argv[i])
c->hostname = strdup(argv[i]);
#ifdef HAVE_ST_RDEV
c->rdev = ttystat2.st_rdev;
#endif
/*
open the device
*/
if ((c->fd = open(devname, O_RDWR)) == -1) {
printf("%s\n", strerror(errno));
destroy_Conn(c);
continue;
}
add_Conn(c);
printf("Ok\n");
write(c->fd, buf, len); /* tell target we're attached */
}
return 0;
}
int cmd_detach(char **argv) {
int i, len;
char *tty, buf[256];
Conn *c;
if (argv == NULL || argv[0] == NULL) {
printf("usage: detach [hostname#]<tty device name> [...]\n");
return 1;
}
tty = ttyname(fileno(stdin));
sprintf(buf, "\r\n[pconsole detached from tty %s]\r\n", (tty == NULL) ? "(unknown)" : tty);
len = strlen(buf);
for(i = 0; argv[i] != NULL; i++) {
if ((c = find_Conn_by_dev(argv[i])) == NULL
&& (c = find_Conn_by_hostname(argv[i])) == NULL) {
printf("not attached to %s\n", argv[i]);
continue;
}
do {
if (c->hostname != NULL)
printf("detaching from %s#%s : ", c->hostname, c->dev);
else
printf("detaching from %s : ", c->dev);
if (c->fd > 0)
write(c->fd, buf, len);
remove_Conn(c);
destroy_Conn(c);
printf("Ok\n");
} while((c = find_Conn_by_hostname(argv[i])) != NULL
|| (c = find_Conn_by_dev(argv[i])) != NULL);
}
return 0;
}
int cmd_list(char **argv) {
Conn *c;
if (AllConns == NULL) {
printf("pconsole: currently not attached to any terminal device\n");
return 0;
}
printf("Currently attached to:\n");
for(c = AllConns; c != NULL; c = c->next) {
#ifdef HAVE_ST_RDEV
if (c->hostname != NULL)
printf(" %-16s %-20s (device no %d, %d)\n", c->dev, c->hostname, ((int)c->rdev >> 8) & 0xff, (int)c->rdev & 0xff);
else
printf(" %-16s %-20s (device no %d, %d)\n", c->dev, " ", ((int)c->rdev >> 8) & 0xff, (int)c->rdev & 0xff);
#else
if (c->hostname != NULL)
printf(" %-16s %s\n", c->dev, c->hostname);
else
printf(" %-16s\n", c->dev);
#endif
}
return 0;
}
/* EOB */
|