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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
|
/* ----------------------------------------------------------------------- *
*
* Copyright 2008-2011 Gene Cumm - All Rights Reserved
*
* 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, Inc., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* rosh.c
*
* Read-Only shell; Simple shell system designed for SYSLINUX-derivitives.
* Provides minimal commands utilizing the console via stdout/stderr as the
* sole output devices. Designed to compile for Linux for testing/debugging.
*/
/*
* ToDos:
* prompt: Allow left/right arrow, home/end and more?
* commands Break into argv/argc-like array
* rosh_cfg: allow -s <file> to change config
* rosh_ls(): sorted; then multiple columns
* prompt: Possibly honor timeout on initial entry for usage as UI
* Also possibly honor totaltimeout
*/
/*#define DO_DEBUG 1
//*/
/* Uncomment the above line for debugging output; Comment to remove */
/*#define DO_DEBUG2 1
//*/
/* Uncomment the above line for super-debugging output; Must have regular
* debugging enabled; Comment to remove.
*/
#include "rosh.h"
#include "version.h"
#define APP_LONGNAME "Read-Only Shell"
#define APP_NAME "rosh"
#define APP_AUTHOR "Gene Cumm"
#define APP_YEAR "2010"
#define APP_VER "beta-b090"
/* Print version information to stdout
*/
void rosh_version(int vtype)
{
char env[256];
env[0] = 0;
printf("%s v %s; (c) %s %s.\n\tFrom Syslinux %s, %s\n", APP_LONGNAME, APP_VER, APP_YEAR, APP_AUTHOR, VERSION_STR, DATE);
switch (vtype) {
case 1:
rosh_get_env_ver(env, 256);
printf("\tRunning on %s\n", env);
}
}
/* Print beta message and if DO_DEBUG/DO_DEBUG2 are active
*/
void print_beta(void)
{
puts(rosh_beta_str);
ROSH_DEBUG("DO_DEBUG active\n");
ROSH_DEBUG2("DO_DEBUG2 active\n");
}
/* Search a string for first non-space (' ') character, starting at ipos
* istr input string to parse
* ipos input position to start at
*/
int rosh_search_nonsp(const char *istr, const int ipos)
{
int curpos;
char c;
curpos = ipos;
c = istr[curpos];
while (c && isspace(c))
c = istr[++curpos];
return curpos;
}
/* Search a string for space (' '), returning the position of the next space
* or the '\0' at end of string
* istr input string to parse
* ipos input position to start at
*/
int rosh_search_sp(const char *istr, const int ipos)
{
int curpos;
char c;
curpos = ipos;
c = istr[curpos];
while (c && !(isspace(c)))
c = istr[++curpos];
return curpos;
}
/* Parse a string for the first non-space string, returning the end position
* from src
* dest string to contain the first non-space string
* src string to parse
* ipos Position to start in src
*/
int rosh_parse_sp_1(char *dest, const char *src, const int ipos)
{
int bpos, epos; /* beginning and ending position of source string
to copy to destination string */
bpos = 0;
epos = 0;
/* //HERE-error condition checking */
bpos = rosh_search_nonsp(src, ipos);
epos = rosh_search_sp(src, bpos);
if (epos > bpos) {
memcpy(dest, src + bpos, epos - bpos);
if (dest[epos - bpos] != 0)
dest[epos - bpos] = 0;
} else {
epos = strlen(src);
dest[0] = 0;
}
return epos;
}
/*
* parse_args1: Try 1 at parsing a string to an argc/argv pair. use free_args1 to free memory malloc'd
*
* Derived from com32/lib/sys/argv.c:__parse_argv()
* Copyright 2004-2009 H. Peter Anvin - All Rights Reserved
* Copyright 2009 Intel Corporation; author: H. Peter Anvin
*/
int parse_args1(char ***iargv, const char *istr)
{
int argc = 0;
const char *p;
char *q, *r, *args, **arg;
int sp = 1; //, qt = 0; /* Was a space; inside a quote */
/* Scan 1: Length */
/* I could eliminate this if I knew a max length, like strncpy() */
int len = strlen(istr);
/* Scan 2: Copy, nullify and make argc */
if (!(args = malloc(len + 1)))
goto fail_args;
q = args;
for (p = istr;; p++) {
if (*p <= ' ') {
if (!sp) {
sp = 1;
*q++ = '\0';
}
} else {
if (sp) {
argc++;
sp = 0;
}
*q++ = *p;
}
if (!*p)
break;
}
q--; /* Point q to final null */
/* Scan 3: Build array of pointers */
if (!(*iargv = malloc((argc + 1) * sizeof(char *))))
goto fail_args_ptr;
arg = *iargv;
arg[argc] = NULL; /* Nullify the last pointer */
if (*args != '\0')
*arg++ = args;
for (r = args; r < q ; r++) {
if (*r == '\0') {
*arg++ = r + 1;
}
}
fail_args:
return argc;
fail_args_ptr:
free(args);
return 0;
}
/* Free argv created by parse_args1()
* argv Argument Values
*/
void free_args1(char ***argv)
{
char *s;
s = **argv;
free(*argv);
free(s);
}
/* Convert a string to an argc/argv pair
* str String to parse
* argv Argument Values
* returns Argument Count
*/
int rosh_str2argv(char ***argv, const char *str)
{
return parse_args1(argv, str);
}
/* Free an argv created by rosh_str2argv()
* argv Argument Values to free
*/
void rosh_free_argv(char ***argv)
{
free_args1(argv);
}
/* Print the contents of an argc/argv pair
* argc Argument Count
* argv Argument Values
*/
void rosh_pr_argv(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++) {
printf("%s%s", argv[i], (i < argc)? " " : "");
}
puts("");
}
/* Print the contents of an argc/argv pair verbosely
* argc Argument Count
* argv Argument Values
*/
void rosh_pr_argv_v(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++) {
printf("%4d '%s'\n", i, argv[i]);
}
}
/* Reset the getopt() environment
*/
void rosh_getopt_reset(void)
{
optind = 0;
optopt = 0;
}
/* Display help
* type Help type
* cmdstr Command for which help is requested
*/
void rosh_help(int type, const char *cmdstr)
{
switch (type) {
case 2:
if ((cmdstr == NULL) || (strcmp(cmdstr, "") == 0)) {
rosh_version(0);
puts(rosh_help_str2);
} else {
switch (cmdstr[0]) {
case 'c':
puts(rosh_help_cd_str);
break;
case 'l':
puts(rosh_help_ls_str);
break;
default:
printf(rosh_help_str_adv, cmdstr);
}
}
break;
case 1:
default:
if (cmdstr)
printf("%s: %s: unknown command\n", APP_NAME, cmdstr);
rosh_version(0);
puts(rosh_help_str1);
}
}
/* Handle most/all errors
* ierrno Input Error number
* cmdstr Command being executed to cause error
* filestr File/parameter causing error
*/
void rosh_error(const int ierrno, const char *cmdstr, const char *filestr)
{
printf("--ERROR: %s '%s': ", cmdstr, filestr);
switch (ierrno) {
case 0:
puts("NO ERROR");
break;
case ENOENT:
puts("not found");
/* SYSLinux-3.72 COM32 API returns this for a
directory or empty file */
ROSH_COM32(" (COM32) could be a directory or empty file\n");
break;
case EIO:
puts("I/O Error");
break;
case EBADF:
puts("Bad File Descriptor");
break;
case EACCES:
puts("Access DENIED");
break;
case ENOTDIR:
puts("not a directory");
ROSH_COM32(" (COM32) could be directory\n");
break;
case EISDIR:
puts("IS a directory");
break;
case ENOSYS:
puts("not implemented");
break;
default:
printf("returns error; errno=%d\n", ierrno);
}
} /* rosh_error */
/* Concatenate command line arguments into one string
* cmdstr Output command string
* cmdlen Length of cmdstr
* argc Argument Count
* argv Argument Values
* barg Beginning Argument
*/
int rosh_argcat(char *cmdstr, const int cmdlen, const int argc, char *argv[],
const int barg)
{
int i, arglen, curpos; /* index, argument length, current position
in cmdstr */
curpos = 0;
cmdstr[0] = '\0'; /* Nullify string just to be sure */
for (i = barg; i < argc; i++) {
arglen = strlen(argv[i]);
/* Theoretically, this should never be met in SYSLINUX */
if ((curpos + arglen) > (cmdlen - 1))
arglen = (cmdlen - 1) - curpos;
memcpy(cmdstr + curpos, argv[i], arglen);
curpos += arglen;
if (curpos >= (cmdlen - 1)) {
/* Hopefully, curpos should not be greater than
(cmdlen - 1) */
/* Still need a '\0' at the last character */
cmdstr[(cmdlen - 1)] = 0;
break; /* Escape out of the for() loop;
We can no longer process anything more */
} else {
cmdstr[curpos] = ' ';
curpos += 1;
cmdstr[curpos] = 0;
}
}
/* If there's a ' ' at the end, remove it. This is normal unless
the maximum length is met/exceeded. */
if (cmdstr[curpos - 1] == ' ')
cmdstr[--curpos] = 0;
return curpos;
} /* rosh_argcat */
/*
* Prints a lot of the data in a struct termios
*/
/*
void rosh_print_tc(struct termios *tio)
{
printf(" -- termios: ");
printf(".c_iflag=%04X ", tio->c_iflag);
printf(".c_oflag=%04X ", tio->c_oflag);
printf(".c_cflag=%04X ", tio->c_cflag);
printf(".c_lflag=%04X ", tio->c_lflag);
printf(".c_cc[VTIME]='%d' ", tio->c_cc[VTIME]);
printf(".c_cc[VMIN]='%d'", tio->c_cc[VMIN]);
printf("\n");
}
*/
/*
* Attempts to get a single key from the console
* returns key pressed
*/
int rosh_getkey(void)
{
int inc;
inc = KEY_NONE;
while (inc == KEY_NONE)
inc = get_key(stdin, 6000);
return inc;
} /* rosh_getkey */
/*
* Qualifies a filename relative to the working directory
* filestr Filename to qualify
* pwdstr working directory
* returns qualified file name string
*/
void rosh_qualify_filestr(char *filestr, const char *ifilstr,
const char *pwdstr)
{
int filepos = 0;
if ((filestr) && (pwdstr) && (ifilstr)) {
if (ifilstr[0] != SEP) {
strcpy(filestr, pwdstr);
filepos = strlen(pwdstr);
if (filestr[filepos - 1] != SEP)
filestr[filepos++] = SEP;
}
strcpy(filestr + filepos, ifilstr);
ROSH_DEBUG("--'%s'\n", filestr);
}
}
/* Concatenate multiple files to stdout
* argc Argument Count
* argv Argument Values
*/
void rosh_cat(int argc, char *argv[])
{
FILE *f;
char buf[ROSH_BUF_SZ];
int i, numrd;
for (i = 0; i < argc; i++) {
printf("--File = '%s'\n", argv[i]);
errno = 0;
f = fopen(argv[i], "r");
if (f != NULL) {
numrd = fread(buf, 1, ROSH_BUF_SZ, f);
while (numrd > 0) {
fwrite(buf, 1, numrd, stdout);
numrd = fread(buf, 1, ROSH_BUF_SZ, f);
}
fclose(f);
} else {
rosh_error(errno, "cat", argv[i]);
errno = 0;
}
}
} /* rosh_cat */
/* Change PWD (Present Working Directory)
* argc Argument count
* argv Argument values
* ipwdstr Initial PWD
*/
void rosh_cd(int argc, char *argv[], const char *ipwdstr)
{
int rv = 0;
#ifdef DO_DEBUG
char filestr[ROSH_PATH_SZ];
#endif /* DO_DEBUG */
ROSH_DEBUG("CMD: \n");
ROSH_DEBUG_ARGV_V(argc, argv);
errno = 0;
if (argc == 2)
rv = chdir(argv[1]);
else if (argc == 1)
rv = chdir(ipwdstr);
else
rosh_help(2, argv[0]);
if (rv != 0) {
if (argc == 2)
rosh_error(errno, "cd", argv[1]);
else
rosh_error(errno, "cd", ipwdstr);
errno = 0;
} else {
#ifdef DO_DEBUG
if (getcwd(filestr, ROSH_PATH_SZ))
ROSH_DEBUG(" %s\n", filestr);
#endif /* DO_DEBUG */
}
} /* rosh_cd */
/* Print the syslinux config file name
*/
void rosh_cfg(void)
{
printf("CFG: '%s'\n", syslinux_config_file());
} /* rosh_cfg */
/* Echo a string back to the screen
* cmdstr command string to process
*/
void rosh_echo(const char *cmdstr)
{
int bpos = 0;
ROSH_DEBUG("CMD: '%s'\n", cmdstr);
bpos = rosh_search_nonsp(cmdstr, rosh_search_sp(cmdstr, 0));
if (bpos > 1) {
ROSH_DEBUG(" bpos=%d\n", bpos);
printf("'%s'\n", cmdstr + bpos);
} else {
puts("");
}
} /* rosh_echo */
/* Process argc/argv to optarr
* argc Argument count
* argv Argument values
* optarr option array to populate
*/
void rosh_ls_arg_opt(int argc, char *argv[], int optarr[])
{
int rv = 0;
optarr[0] = -1;
optarr[1] = -1;
optarr[2] = -1;
rosh_getopt_reset();
while (rv != -1) {
ROSH_DEBUG2("getopt optind=%d rv=%d\n", optind, rv);
rv = getopt(argc, argv, rosh_ls_opt_str);
switch (rv) {
case 'l':
case 0:
optarr[0] = 1;
break;
case 'F':
case 1:
optarr[1] = 1;
break;
case 'i':
case 2:
optarr[2] = 1;
break;
case '?':
case -1:
default:
ROSH_DEBUG2("getopt optind=%d rv=%d\n", optind, rv);
break;
}
}
ROSH_DEBUG2(" end getopt optind=%d rv=%d\n", optind, rv);
ROSH_DEBUG2("\tIn rosh_ls_arg_opt() opt[0]=%d\topt[1]=%d\topt[2]=%d\n", optarr[0], optarr[1],
optarr[2]);
} /* rosh_ls_arg_opt */
/* Retrieve the size of a file argument
* filestr directory name of directory entry
* de directory entry
*/
int rosh_ls_de_size(const char *filestr, struct dirent *de)
{
int de_size;
char filestr2[ROSH_PATH_SZ];
int fd2, file2pos;
struct stat fdstat;
filestr2[0] = 0;
file2pos = -1;
if (filestr) {
file2pos = strlen(filestr);
memcpy(filestr2, filestr, file2pos);
filestr2[file2pos] = '/';
}
strcpy(filestr2 + file2pos + 1, de->d_name);
fd2 = open(filestr2, O_RDONLY);
fstat(fd2, &fdstat);
fd2 = close(fd2);
de_size = (int)fdstat.st_size;
return de_size;
} /* rosh_ls_de_size */
/* Retrieve the size and mode of a file
* filestr directory name of directory entry
* de directory entry
*/
int rosh_ls_de_size_mode(const char *filestr, struct dirent *de, mode_t * st_mode)
{
int de_size;
char filestr2[ROSH_PATH_SZ];
int file2pos;
struct stat fdstat;
int status;
filestr2[0] = 0;
file2pos = -1;
memset(&fdstat, 0, sizeof fdstat);
ROSH_DEBUG2("ls:dsm(%s, %s) ", filestr, de->d_name);
if (filestr) {
/* FIXME: prevent string overflow */
file2pos = strlen(filestr);
memcpy(filestr2, filestr, file2pos);
if (( filestr2[file2pos - 1] == SEP )) {
file2pos--;
} else {
filestr2[file2pos] = SEP;
}
}
strcpy(filestr2 + file2pos + 1, de->d_name);
errno = 0;
ROSH_DEBUG2("stat(%s) ", filestr2);
status = stat(filestr2, &fdstat);
(void)status;
ROSH_DEBUG2("\t--stat()=%d\terr=%d\n", status, errno);
if (errno) {
rosh_error(errno, "ls:szmd.stat", de->d_name);
errno = 0;
}
de_size = (int)fdstat.st_size;
*st_mode = fdstat.st_mode;
return de_size;
} /* rosh_ls_de_size_mode */
/* Returns the Inode number if fdstat contains it
* fdstat struct to extract inode from if not COM32, for now
*/
long rosh_ls_d_ino(struct stat *fdstat)
{
long de_ino;
#ifdef __COM32__
if (fdstat)
de_ino = -1;
else
de_ino = 0;
#else /* __COM32__ */
de_ino = fdstat->st_ino;
#endif /* __COM32__ */
return de_ino;
}
/* Convert a d_type to a single char in human readable format
* d_type d_type to convert
* returns human readable single character; a space if other
*/
char rosh_d_type2char_human(unsigned char d_type)
{
char ret;
switch (d_type) {
case DT_UNKNOWN:
ret = 'U';
break; /* Unknown */
case DT_FIFO:
ret = 'F';
break; /* FIFO */
case DT_CHR:
ret = 'C';
break; /* Char Dev */
case DT_DIR:
ret = 'D';
break; /* Directory */
case DT_BLK:
ret = 'B';
break; /* Block Dev */
case DT_REG:
ret = 'R';
break; /* Regular File */
case DT_LNK:
ret = 'L';
break; /* Link, Symbolic */
case DT_SOCK:
ret = 'S';
break; /* Socket */
case DT_WHT:
ret = 'W';
break; /* UnionFS Whiteout */
default:
ret = ' ';
}
return ret;
} /* rosh_d_type2char_human */
/* Convert a d_type to a single char by ls's prefix standards for -l
* d_type d_type to convert
* returns ls style single character; a space if other
*/
char rosh_d_type2char_lspre(unsigned char d_type)
{
char ret;
switch (d_type) {
case DT_FIFO:
ret = 'p';
break;
case DT_CHR:
ret = 'c';
break;
case DT_DIR:
ret = 'd';
break;
case DT_BLK:
ret = 'b';
break;
case DT_REG:
ret = '-';
break;
case DT_LNK:
ret = 'l';
break;
case DT_SOCK:
ret = 's';
break;
default:
ret = '?';
}
return ret;
} /* rosh_d_type2char_lspre */
/* Convert a d_type to a single char by ls's classify (-F) suffix standards
* d_type d_type to convert
* returns ls style single character; a space if other
*/
char rosh_d_type2char_lssuf(unsigned char d_type)
{
char ret;
switch (d_type) {
case DT_FIFO:
ret = '|';
break;
case DT_DIR:
ret = '/';
break;
case DT_LNK:
ret = '@';
break;
case DT_SOCK:
ret = '=';
break;
default:
ret = ' ';
}
return ret;
} /* rosh_d_type2char_lssuf */
/* Converts data in the "other" place of st_mode to a ls-style string
* st_mode Mode in other to analyze
* st_mode_str string to hold converted string
*/
void rosh_st_mode_am2str(mode_t st_mode, char *st_mode_str)
{
st_mode_str[0] = ((st_mode & S_IROTH) ? 'r' : '-');
st_mode_str[1] = ((st_mode & S_IWOTH) ? 'w' : '-');
st_mode_str[2] = ((st_mode & S_IXOTH) ? 'x' : '-');
}
/* Converts st_mode to an ls-style string
* st_mode mode to convert
* st_mode_str string to hold converted string
*/
void rosh_st_mode2str(mode_t st_mode, char *st_mode_str)
{
st_mode_str[0] = rosh_d_type2char_lspre(IFTODT(st_mode));
rosh_st_mode_am2str((st_mode & S_IRWXU) >> 6, st_mode_str + 1);
rosh_st_mode_am2str((st_mode & S_IRWXG) >> 3, st_mode_str + 4);
rosh_st_mode_am2str(st_mode & S_IRWXO, st_mode_str + 7);
st_mode_str[10] = 0;
} /* rosh_st_mode2str */
/* Output a single entry
* filestr directory name to list
* de directory entry
* optarr Array of options
*/
void rosh_ls_arg_dir_de(const char *filestr, struct dirent *de, const int *optarr)
{
int de_size;
mode_t st_mode;
char st_mode_str[11];
st_mode = 0;
ROSH_DEBUG2("+");
if (optarr[2] > -1)
printf("%10d ", (int)(de->d_ino));
if (optarr[0] > -1) {
de_size = rosh_ls_de_size_mode(filestr, de, &st_mode);
rosh_st_mode2str(st_mode, st_mode_str);
ROSH_DEBUG2("%04X ", st_mode);
printf("%s %10d ", st_mode_str, de_size);
}
ROSH_DEBUG("'");
printf("%s", de->d_name);
ROSH_DEBUG("'");
if (optarr[1] > -1)
printf("%c", rosh_d_type2char_lssuf(de->d_type));
printf("\n");
} /* rosh_ls_arg_dir_de */
/* Output listing of a regular directory
* filestr directory name to list
* d the open DIR
* optarr Array of options
NOTE:This is where I could use qsort
*/
void rosh_ls_arg_dir(const char *filestr, DIR * d, const int *optarr)
{
struct dirent *de;
int filepos;
filepos = 0;
errno = 0;
while ((de = readdir(d))) {
filepos++;
rosh_ls_arg_dir_de(filestr, de, optarr);
}
if (errno) {
rosh_error(errno, "ls:arg_dir", filestr);
errno = 0;
} else { if (filepos == 0)
ROSH_DEBUG("0 files found");
}
} /* rosh_ls_arg_dir */
/* Simple directory listing for one argument (file/directory) based on
* filestr and pwdstr
* ifilstr input filename/directory name to list
* pwdstr Present Working Directory string
* optarr Option Array
*/
void rosh_ls_arg(const char *filestr, const int *optarr)
{
struct stat fdstat;
int status;
// char filestr[ROSH_PATH_SZ];
// int filepos;
DIR *d;
struct dirent de;
/* Initialization; make filestr based on leading character of ifilstr
and pwdstr */
// rosh_qualify_filestr(filestr, ifilstr, pwdstr);
fdstat.st_mode = 0;
fdstat.st_size = 0;
ROSH_DEBUG("\topt[0]=%d\topt[1]=%d\topt[2]=%d\n", optarr[0], optarr[1],
optarr[2]);
/* Now, the real work */
errno = 0;
status = stat(filestr, &fdstat);
if (status == 0) {
if (S_ISDIR(fdstat.st_mode)) {
ROSH_DEBUG("PATH '%s' is a directory\n", filestr);
if ((d = opendir(filestr))) {
rosh_ls_arg_dir(filestr, d, optarr);
closedir(d);
} else {
rosh_error(errno, "ls", filestr);
errno = 0;
}
} else {
de.d_ino = rosh_ls_d_ino(&fdstat);
de.d_type = (IFTODT(fdstat.st_mode));
strcpy(de.d_name, filestr);
if (S_ISREG(fdstat.st_mode)) {
ROSH_DEBUG("PATH '%s' is a regular file\n", filestr);
} else {
ROSH_DEBUG("PATH '%s' is some other file\n", filestr);
}
rosh_ls_arg_dir_de(NULL, &de, optarr);
/* if (ifilstr[0] == SEP)
rosh_ls_arg_dir_de(NULL, &de, optarr);
else
rosh_ls_arg_dir_de(pwdstr, &de, optarr);*/
}
} else {
rosh_error(errno, "ls", filestr);
errno = 0;
}
return;
} /* rosh_ls_arg */
/* Parse options that may be present in the cmdstr
* filestr Possible option string to parse
* optstr Current options
* returns 1 if filestr does not begin with '-' else 0
*/
int rosh_ls_parse_opt(const char *filestr, char *optstr)
{
int ret;
if (filestr[0] == '-') {
ret = 0;
if (optstr)
strcat(optstr, filestr + 1);
} else {
ret = 1;
}
ROSH_DEBUG("ParseOpt: '%s'\n\topt: '%s'\n\tret: %d\n", filestr, optstr,
ret);
return ret;
} /* rosh_ls_parse_opt */
/* List Directory
* argc Argument count
* argv Argument values
*/
void rosh_ls(int argc, char *argv[])
{
int optarr[3];
int i;
rosh_ls_arg_opt(argc, argv, optarr);
ROSH_DEBUG2("In ls()\n");
ROSH_DEBUG2_ARGV_V(argc, argv);
#ifdef DO_DEBUG
optarr[0] = 2;
#endif /* DO_DEBUG */
ROSH_DEBUG2(" argc=%d; optind=%d\n", argc, optind);
if (optind >= argc)
rosh_ls_arg(".", optarr);
for (i = optind; i < argc; i++) {
rosh_ls_arg(argv[i], optarr);
}
} /* rosh_ls */
/* Simple directory listing; calls rosh_ls()
* argc Argument count
* argv Argument values
*/
void rosh_dir(int argc, char *argv[])
{
ROSH_DEBUG(" dir implemented as ls\n");
rosh_ls(argc, argv);
} /* rosh_dir */
/* Page through a buffer string
* buf Buffer to page through
*/
void rosh_more_buf(char *buf, int buflen, int rows, int cols, char *scrbuf)
{
char *bufp, *bufeol, *bufeol2; /* Pointer to current and next
end-of-line position in buffer */
int bufpos, bufcnt; /* current position, count characters */
int inc;
int i, numln; /* Index, Number of lines */
int elpl; /* Extra lines per line read */
(void)cols;
bufpos = 0;
bufp = buf + bufpos;
bufeol = bufp;
numln = rows - 1;
ROSH_DEBUG("--(%d)\n", buflen);
while (bufpos < buflen) {
for (i = 0; i < numln; i++) {
bufeol2 = strchr(bufeol, '\n');
if (bufeol2 == NULL) {
bufeol = buf + buflen;
i = numln;
} else {
elpl = ((bufeol2 - bufeol - 1) / cols);
if (elpl < 0)
elpl = 0;
i += elpl;
ROSH_DEBUG2(" %d/%d ", elpl, i+1);
/* If this will not push too much, use it */
/* but if it's the first line, use it */
/* //HERE: We should probably snip the line off */
if ((i < numln) || (i == elpl))
bufeol = bufeol2 + 1;
}
}
ROSH_DEBUG2("\n");
bufcnt = bufeol - bufp;
printf("--(%d/%d @%d)\n", bufcnt, buflen, bufpos);
memcpy(scrbuf, bufp, bufcnt);
scrbuf[bufcnt] = 0;
printf("%s", scrbuf);
bufp = bufeol;
bufpos += bufcnt;
if (bufpos == buflen)
break;
inc = rosh_getkey();
numln = 1;
switch (inc) {
case KEY_CTRL('c'):
case 'q':
case 'Q':
bufpos = buflen;
break;
case ' ':
numln = rows - 1;
}
}
} /* rosh_more_buf */
/* Page through a single file using the open file stream
* fd File Descriptor
*/
void rosh_more_fd(int fd, int rows, int cols, char *scrbuf)
{
struct stat fdstat;
char *buf;
int bufpos;
int numrd;
FILE *f;
fstat(fd, &fdstat);
if (S_ISREG(fdstat.st_mode)) {
buf = malloc((int)fdstat.st_size);
if (buf != NULL) {
f = fdopen(fd, "r");
bufpos = 0;
numrd = fread(buf, 1, (int)fdstat.st_size, f);
while (numrd > 0) {
bufpos += numrd;
numrd = fread(buf + bufpos, 1,
((int)fdstat.st_size - bufpos), f);
}
fclose(f);
rosh_more_buf(buf, bufpos, rows, cols, scrbuf);
}
} else {
}
} /* rosh_more_fd */
/* Page through a file like the more command
* argc Argument Count
* argv Argument Values
*/
void rosh_more(int argc, char *argv[])
{
int fd, i;
/* char filestr[ROSH_PATH_SZ];
int cmdpos;*/
int rows, cols;
char *scrbuf;
int ret;
ROSH_DEBUG_ARGV_V(argc, argv);
ret = getscreensize(1, &rows, &cols);
if (ret) {
ROSH_DEBUG("getscreensize() fail(%d); fall back\n", ret);
ROSH_DEBUG("\tROWS='%d'\tCOLS='%d'\n", rows, cols);
/* If either fail, go under normal size, just in case */
if (!rows)
rows = 20;
if (!cols)
cols = 75;
}
ROSH_DEBUG("\tUSE ROWS='%d'\tCOLS='%d'\n", rows, cols);
/* 32 bit align beginning of row and over allocate */
scrbuf = malloc(rows * ((cols+3)&(INT_MAX - 3)));
if (!scrbuf)
return;
if (argc) {
/* There is no need to mess up the console if we don't have a
file */
rosh_console_raw();
for (i = 0; i < argc; i++) {
printf("--File = '%s'\n", argv[i]);
errno = 0;
fd = open(argv[i], O_RDONLY);
if (fd != -1) {
rosh_more_fd(fd, rows, cols, scrbuf);
close(fd);
} else {
rosh_error(errno, "more", argv[i]);
errno = 0;
}
}
rosh_console_std();
}
free(scrbuf);
} /* rosh_more */
/* Page a file with rewind
* argc Argument Count
* argv Argument Values
*/
void rosh_less(int argc, char *argv[])
{
printf(" less implemented as more (for now)\n");
rosh_more(argc, argv);
} /* rosh_less */
/* Show PWD
*/
void rosh_pwd(void)
{
char pwdstr[ROSH_PATH_SZ];
errno = 0;
if (getcwd(pwdstr, ROSH_PATH_SZ)) {
printf("%s\n", pwdstr);
} else {
rosh_error(errno, "pwd", "");
errno = 0;
}
} /* rosh_pwd */
/* Reboot; use warm reboot if one of certain options set
* argc Argument count
* argv Argument values
*/
void rosh_reboot(int argc, char *argv[])
{
int rtype = 0;
if (argc) {
/* For now, just use the first */
switch (argv[0][0]) {
case '1':
case 's':
case 'w':
rtype = 1;
break;
case '-':
switch (argv[0][1]) {
case '1':
case 's':
case 'w':
rtype = 1;
break;
}
break;
}
}
syslinux_reboot(rtype);
} /* rosh_reboot */
/* Run a boot string, calling syslinux_run_command
* argc Argument count
* argv Argument values
*/
void rosh_run(int argc, char *argv[])
{
char cmdstr[ROSH_CMD_SZ];
int len;
len = rosh_argcat(cmdstr, ROSH_CMD_SZ, argc, argv, 0);
if (len) {
printf("--run: '%s'\n", cmdstr);
syslinux_run_command(cmdstr);
} else {
printf(APP_NAME ":run: No arguments\n");
}
} /* rosh_run */
/* Process an argc/argv pair and call handling function
* argc Argument count
* argv Argument values
* ipwdstr Initial Present Working Directory string
* returns Whether to exit prompt
*/
char rosh_command(int argc, char *argv[], const char *ipwdstr)
{
char do_exit = false;
int tlen;
tlen = strlen(argv[0]);
ROSH_DEBUG_ARGV_V(argc, argv);
switch (argv[0][0]) {
case 'e':
case 'E':
case 'q':
case 'Q':
switch (argv[0][1]) {
case 0:
case 'x':
case 'X':
case 'u':
case 'U':
if ((strncasecmp("exit", argv[0], tlen) == 0) ||
(strncasecmp("quit", argv[0], tlen) == 0))
do_exit = true;
else
rosh_help(1, argv[0]);
break;
case 'c':
case 'C':
if (strncasecmp("echo", argv[0], tlen) == 0)
rosh_pr_argv(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
default:
rosh_help(1, argv[0]);
}
break;
case 'c':
case 'C': /* run 'cd' 'cat' 'cfg' */
switch (argv[0][1]) {
case 'a':
case 'A':
if (strncasecmp("cat", argv[0], tlen) == 0)
rosh_cat(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
case 'd':
case 'D':
if (strncasecmp("cd", argv[0], tlen) == 0)
rosh_cd(argc, argv, ipwdstr);
else
rosh_help(1, argv[0]);
break;
case 'f':
case 'F':
if (strncasecmp("cfg", argv[0], tlen) == 0)
rosh_cfg();
else
rosh_help(1, argv[0]);
break;
default:
rosh_help(1, argv[0]);
}
break;
case 'd':
case 'D': /* run 'dir' */
if (strncasecmp("dir", argv[0], tlen) == 0)
rosh_dir(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
case 'h':
case 'H':
case '?':
if ((strncasecmp("help", argv[0], tlen) == 0) || (tlen == 1))
rosh_help(2, argv[1]);
else
rosh_help(1, NULL);
break;
case 'l':
case 'L': /* run 'ls' 'less' */
switch (argv[0][1]) {
case 0:
case 's':
case 'S':
if (strncasecmp("ls", argv[0], tlen) == 0)
rosh_ls(argc, argv);
else
rosh_help(1, argv[0]);
break;
case 'e':
case 'E':
if (strncasecmp("less", argv[0], tlen) == 0)
rosh_less(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
default:
rosh_help(1, argv[0]);
}
break;
case 'm':
case 'M':
switch (argv[0][1]) {
case 'a':
case 'A':
if (strncasecmp("man", argv[0], tlen) == 0)
rosh_help(2, argv[1]);
else
rosh_help(1, argv[0]);
break;
case 'o':
case 'O':
if (strncasecmp("more", argv[0], tlen) == 0)
rosh_more(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
default:
rosh_help(1, argv[0]);
}
break;
case 'p':
case 'P': /* run 'pwd' */
if (strncasecmp("pwd", argv[0], tlen) == 0)
rosh_pwd();
else
rosh_help(1, argv[0]);
break;
case 'r':
case 'R': /* run 'run' */
switch (argv[0][1]) {
case 0:
case 'e':
case 'E':
if (strncasecmp("reboot", argv[0], tlen) == 0)
rosh_reboot(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
case 'u':
case 'U':
if (strncasecmp("run", argv[0], tlen) == 0)
rosh_run(argc - 1, &argv[1]);
else
rosh_help(1, argv[0]);
break;
default:
rosh_help(1, argv[0]);
}
break;
case 'v':
case 'V':
if (strncasecmp("version", argv[0], tlen) == 0)
rosh_version(1);
else
rosh_help(1, argv[0]);
break;
case 0:
case '\n':
break;
default:
rosh_help(1, argv[0]);
} /* switch(argv[0][0]) */
return do_exit;
} /* rosh_command */
/* Process the prompt for commands as read from stdin and call rosh_command
* to process command line string
* icmdstr Initial command line string
* returns Exit status
*/
int rosh_prompt(int iargc, char *iargv[])
{
int rv;
char cmdstr[ROSH_CMD_SZ];
char ipwdstr[ROSH_PATH_SZ];
char do_exit;
char **argv;
int argc;
rv = 0;
do_exit = false;
if (!getcwd(ipwdstr, ROSH_PATH_SZ))
strcpy(ipwdstr, "./");
if (iargc > 1)
do_exit = rosh_command(iargc - 1, &iargv[1], ipwdstr);
while (!(do_exit)) {
/* Extra preceeding newline */
printf("\nrosh: ");
/* Read a line from console */
if (fgets(cmdstr, ROSH_CMD_SZ, stdin)) {
argc = rosh_str2argv(&argv, cmdstr);
do_exit = rosh_command(argc, argv, ipwdstr);
rosh_free_argv(&argv);
} else {
do_exit = false;
}
}
return rv;
}
int main(int argc, char *argv[])
{
int rv;
/* Initialization */
rv = 0;
rosh_console_std();
if (argc == 1) {
rosh_version(0);
print_beta();
} else {
#ifdef DO_DEBUG
char cmdstr[ROSH_CMD_SZ];
rosh_argcat(cmdstr, ROSH_CMD_SZ, argc, argv, 1);
ROSH_DEBUG("arg='%s'\n", cmdstr);
#endif
}
rv = rosh_prompt(argc, argv);
printf("--Exiting '" APP_NAME "'\n");
return rv;
}
|