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
|
/* exec.et
** Adapted from
** D. Richard Hipp
**
** For the public domain...
*/
#include <tk.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
/* Interval between process check in milliseconds */
#define CHKPID_INTERVAL 100
/* Maximal number of registered processes */
#define MAXPROC 20
int InstallCommandsExec()
{
ET_INSTALL_COMMANDS;
return 0;
}
/* The following is the file descriptor used for communicating with the
** pseudo-tty and the process running on the other side of the pty. */
static int iChildFd;
/* Pids of Processes, 0 is Tty foreground process */
struct ProcessList {
pid_t pid;
char command[50];
} processList[MAXPROC];
int noSigChld = 0; /* Number of sigchld Processes */
pid_t pidSigChld[10]; /* Pids of sigchld Processes */
/* File descriptors for background processes */
static int iFdOut[2]; /* Stdout pipe */
static int iFdErr[2]; /* Stderr pipe */
/* The following variables hold the width and height of the visible
** terminal screen, in characters. These variables are linked to
** variables with the same name (expect the leading "i") in the Tcl/Tk
** code */
static int iWidth = 80;
static int iHeight = 24;
/* The following variables hold information about the current position
** of the cursor and the state of the virtual VT100 terminal.
**
** iCurX, iCurY The location of the cursor in the text widget. The
** text widgets address space is used. This means that
** the top line is where iCurY==1 and the leftmost
** character is iCurX==0. (The cursor is actually in the
** space immediately prior to iCurX,iCurY.)
**
** iBufX, iBufY The place in the text widget where the text currently
** accumulating in zLineBuf[] should be written.
**
** iCurSavedX, The saved location of the cursor. Used by
** iCurSavedY ESC-7 and ESC-8
**
** iBtm The bottom line of text in the whole text widget.
** (This is NOT the bottom line of visible text. The
** bottom line of visible text is iTop + iHeight). This
** is always the same as "ET_INT(.t index end) - 1".
**
** iTop The first line above the topmost visible line of text.
** This value can be 0 if the text widget is scrolled
** all the way to the top. (This variable doesn't really
** exists. It is used for commenting only.)
**
** xProcess Pointer to the routine used to process the next
** character which is input to the vt100 emulator.
**
** iScrollTop, The top and bottom of the scrolling region on the
** iScrollBtm screen. The numbers are stored as vt100 indices,
** so 1 means the top line and iHeight means the bottom
** line. iScrollTop==1 && iScrollBtm==iHeight means
** the whole screen scrolls.
**
** bReplaceMode If True, then new characters overwrite old ones. When
** False, the new characters are inserted.
**
** iMaxLines The number of lines of text to preserve in the scrollback.
*/
static int iBtm; /* Bottom line of text in the text widget */
static int iCurX, iCurY; /* X and Y coordinates of the cursor */
static int iBufX, iBufY; /* X and Y coordinates at the beginning of
** the line insertion buffer */
static int iCurSavedX, /* Saved X and Y coordinates */
iCurSavedY;
static void (*xProcess)(char); /* Use this routine to process the next char */
static int iScrollTop = 1, /* The bounds of the scrolling region */
iScrollBtm = 24;
static int bReplaceMode = 1; /* True to overwrite characters */
static int iMaxLines = 1000; /* Number of lines to preserve */
/* A NOTE ON INDEXING:
**
** The text widget calls its top line 1 and its leftmost character 0, but
** The vt100 escape codes use 1 for both the topmost and leftmost position.
** Furthermore, the vt100 position is relative to the topmost visible
** line of the screen, where the text widget refer to the topmost line
** in the scrollback text.
**
** The following are invariants:
**
** TextWidgetY = iCurY
** Vt100Y = iCurY + iTop
**
** TextWidgetX = iCurX
** Vt100X = iCurX + 1
*/
/* Sanity checking
*/
#ifdef DEBUG
#define ASSERT(X) if(!(X)){ShouldntHappen(__LINE__,__FILE__);}
#else
#define ASSERT(X)
#endif
/* The virtual VT100 mechanism uses line buffering to reduce the amount
** of calls to the text widget. The following variables hold the line
** buffer.
*/
static char zLineBuf[200];
static int iBufWidth = 0; /* Number of characters in the buffer */
/* The next variables are used to hold parameters specified in an
** ESC[Ps;Ps;... control sequence.
*/
static int nParam; /* Number of parameters seen */
static int aiParam[10]; /* Value of each parameter. -1==default */
/* If the following variable is true, each character received by
** the vt100 emulator is printed on standard output. This variable
** is linked to the Tcl/Tk variable named "Debug" so that it can
** be turned on and off using the "send" command. */
#ifdef DEBUG
static int bDebug = 0;
#endif
/* The following variables holds attributes of all characters which are
** to be inserted into the text widget
*/
static enum _BufTag {
BT_Normal,
BT_Underline,
BT_Bold,
BT_Inverse,
} eBufTag = BT_Normal;
#ifdef DEBUG
/* This routine should never be called */
static void ShouldntHappen(int line, char *file){
fprintf(stderr,"Assertion failed on line %d of %s\n",line,file);
}
#endif
/* Forward declarations */
static void HandleTerm(void*,int);
static void HandleStdOut(void*,int);
static void HandleStdErr(void*,int);
/*###static Tcl_AsyncHandler asSigchld;*/
/*###static void CatchDeathOfChild(int nonsense);*/
/*###static Tcl_AsyncProc HandleSigchld;*/
static void chkpids(ClientData);
void CleanKill(void);
static int StopTty();
/* Procedures defined in getpty.c */
extern int RunCommand(char *, char **, int, int*);
extern void KillChildProcess(void);
void DebugProcessList()
{
int i;
fprintf(stderr, "Process list:\n");
for (i = 0; i < 20; i++) {
if (processList[i].pid)
fprintf(stderr, "%6d %s\n", processList[i].pid, processList[i].command);
}
fprintf(stderr, "-------------\n");
}
void NewProcess(pid_t pid, char *command)
{
int i;
#ifdef DEBUG
fprintf(stderr, "NewProcess: %d %s\n", pid, command);
#endif
for (i = 1; i < 20 && processList[i].pid; i++);
if (i < 20) {
processList[i].pid = pid;
strncpy(processList[i].command, command, 49);
processList[i].command[49] = '\0';
return;
}
#ifdef DEBUG
DebugProcessList();
#endif
}
void FreeProcess(pid_t pid)
{
int i;
#ifdef DEBUG
fprintf(stderr, "FreeProcess: %d\n", pid);
DebugProcessList();
#endif
for (i = 0; i < MAXPROC; i++) {
if (processList[i].pid == pid) {
processList[i].pid = 0;
#ifdef DEBUG
DebugProcessList();
#endif
return;
}
}
}
void KillAllProcesses()
{
int i;
for (i = 0; i < MAXPROC; i++) {
if (processList[i].pid > 0) {
#ifdef DEBUG
fprintf(stderr, "KillAllProcesses: Kill %d %s\n",
processList[i].pid, processList[i].command);
#endif
kill(processList[i].pid, SIGKILL);
}
}
}
/* Tcl/Tk will sometimes attempt to change environment variables. Hence,
** the following string must be writeable -- it cannot be a string literal
** because string literals are read-only.
*/
static char zTermEnv[] = "TERM=xterm";
int CreateTerm()
{
static void ProcessNormal(char);
int i;
Tcl_LinkVar(Et_Interp,"Width",(char*)&iWidth,TCL_LINK_INT);
Tcl_LinkVar(Et_Interp,"Height",(char*)&iHeight,TCL_LINK_INT);
Tcl_LinkVar(Et_Interp,"CurX",(char*)&iCurX,TCL_LINK_INT);
Tcl_LinkVar(Et_Interp,"CurY",(char*)&iCurY,TCL_LINK_INT);
#ifdef DEBUG
Tcl_LinkVar(Et_Interp,"Debug",(char*)&bDebug,TCL_LINK_BOOLEAN);
Tcl_LinkVar(Et_Interp,"Btm",(char*)&iBtm,TCL_LINK_INT);
Tcl_LinkVar(Et_Interp,"ScrollTop",(char*)&iScrollTop,TCL_LINK_INT);
Tcl_LinkVar(Et_Interp,"ScrollBtm",(char*)&iScrollBtm,TCL_LINK_INT);
#endif
iScrollTop = iCurY = iBtm = 1;
iScrollBtm = iHeight;
ET( CreateTermWin );
putenv(zTermEnv);
for (i = 0; i < MAXPROC; i++)
processList[i].pid = 0;
/*
* Install signal handler *
asSigchld = Tcl_AsyncCreate(HandleSigchld, 0);
signal(SIGCHLD, CatchDeathOfChild);
*/
/* Install idle handler */
Tk_CreateTimerHandler(CHKPID_INTERVAL, chkpids, (ClientData)0);
return 0;
}
/*#############################################
static void CatchDeathOfChild(int nonsense)
{
int pid;
pid = wait((int *)0);
if (pid > 0)
pidSigChld[noSigChld++] = pid;
signal(SIGCHLD, CatchDeathOfChild);
Tcl_AsyncMark(asSigchld);
}
*/
/*##############################################
int HandleSigchld(ClientData clientData,
Tcl_Interp *interp, int code)
{
int pid;
while (noSigChld > 0) {
pid = pidSigChld[--noSigChld];
#ifdef DEBUG
fprintf(stderr, "Process %d died\n", pid);
#endif
if (pid == processList[0].pid) {
processList[0].pid = 0;
HandleTerm((ClientData)&iChildFd, 0);
CleanKill();
Tcl_SetVar(Et_Interp, "sigchld", "1", TCL_GLOBAL_ONLY);
} else {
FreeProcess(pid);
HandleStdOut((ClientData)&iFdOut[0], 0);
HandleStdErr((ClientData)&iFdErr[0], 0);
ET( OutMsg "Process %d(pid) died.");
}
}
return code;
}
*/
void chkpids(ClientData clientdata)
{
int status, i;
if (processList[0].pid > 0) {
if (waitpid(processList[0].pid, &status, WNOHANG) != 0) {
Tcl_SetVar(Et_Interp, "sigchld", "1", TCL_GLOBAL_ONLY);
processList[0].pid = 0;
HandleTerm((ClientData)&iChildFd, 0);
CleanKill();
}
}
for (i = 1; i < 20; i++) {
if (processList[i].pid > 0) {
if (waitpid(processList[i].pid, &status, WNOHANG) != 0) {
HandleStdOut((ClientData)&iFdOut[0], 0);
HandleStdErr((ClientData)&iFdErr[0], 0);
ET( OutMsg "Process %d(processList[i].pid) died.");
processList[i].pid = 0;
}
}
}
Tk_CreateTimerHandler(CHKPID_INTERVAL, chkpids, (ClientData)0);
}
ET_PROC( ExecCmd )
{
static void ProcessNormal(char);
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " cmdlist\"", 0);
return ET_ERROR;
}
if (processList[0].pid > 0) {
ET( OutMsg "Cannot start new command because process is running.");
return ET_OK;
}
iChildFd = RunCommand(argv[1], &argv[1], 0, &processList[0].pid);
#ifdef DEBUG
fprintf(stderr, "RunCommand pid = %d\n", processList[0].pid);
#endif
xProcess = ProcessNormal;
if( iChildFd>=0 ){
ET(TermBind);
Tcl_CreateFileHandler(iChildFd,TK_READABLE,HandleTerm,
(ClientData)&iChildFd);
return ET_OK;
} else {
Tcl_AppendResult(interp, "RunCommand failed", 0);
return ET_ERROR;
}
}
/* This routine is called when the child process dies in order to
** do a clean shutdown of the application */
void CleanKill(void){
ET(TermUnbind);
Tcl_DeleteFileHandler(iChildFd);
close(iChildFd);
KillChildProcess();
}
void CleanShutdown()
{
StopTty();
}
/* Make sure the text position iX,iY really exists. If the line iY
** doesn't exist, then add newlines until it does. If character
** iX doesn't exists on line iY, then add spaces until it does.
** Return the actual length of line iY.
*/
static int MakePositionExist(int iX, int iY){
int lineNum;
int lineLen;
int i;
char zBuf[1000]; /* Number of elements must be greater than max width
** and height of the text widget */
if( iX > iWidth ) iX = iWidth;
ASSERT( iY >= 1 && iX >= 0 );
/* If line iY doesn't exists, then we'll have to create it. */
if( iBtm < iY ){
for(i=0; i<iY - iBtm; i++) zBuf[i] = '\n';
zBuf[i] = 0;
ET(.term insert end {%s(zBuf)});
iBtm = iY;
lineLen = 0;
}else{
/* Find the amount of text on line iY. */
sscanf( ET_STR(.term index {%d(iY).0 lineend}), "%d.%d", &lineNum,&lineLen);
ASSERT( lineNum==iY );
ASSERT( ET_INT(.term index end)==iBtm+1 );
}
/* If character position iX doesn't exists, add spaces until it does */
if( iX > lineLen ){
for(i=0; i<iX - lineLen; i++) zBuf[i] = ' ';
zBuf[i] = 0;
ET(.term insert %d(iY).%d(lineLen) {%s(zBuf)});
lineLen = iX;
}
/* Return the actual length of line iY. */
return lineLen;
}
/* Adjust the text widget so that it is scrolled all the way to the
** bottom.
*/
static void JumpToBottom(void){
ET( .term yview moveto 1 );
}
/* Clip the number of lines in the text widget to keep the number
** of lines at or below iMaxLines
*/
static void ClipScrollback(void){
if( iBtm > iMaxLines ){
int extra = iBtm - iMaxLines - 1;
iCurY -= extra;
iBtm -= extra;
if( iCurY<1 ) iCurY = 1;
ET(.term delete 1.0 %d(extra+1).0);
}
}
/* Transfer the text in zLineBuf[0..iBufWidth] into the text widget.
** at position iBufY.iBufX.
**
** If the number of lines in the text widget exceeds iMaxLines because of
** this operation, then lines are deleted from the beginning of the
** widget to get the total number of lines back to iMaxLines.
**
** This is the only way to remove characters from zLineBuf. This is
** also the only way to add non-white-space characters to the screen.
*/
static void FlushBuffer(void){
int lineLen; /* The length of the line iBufY */
/* Early out if nothing to do */
if( iBufWidth==0 ) return;
/* Make sure the position iBufX,iBufY really exists */
lineLen = MakePositionExist(iBufX,iBufY);
/* Prepare the line to receive text. This may involve deleting some
** of the existing text (in overwrite mode), or deleting some text
** that will scroll off the right side of the screen in insert mode.
*/
if( iBufX < lineLen ){
if( bReplaceMode ){
int end = iBufX + iBufWidth;
if( end>lineLen ) end = lineLen;
ET(.term delete %d(iBufY).%d(iBufX) %d(iBufY).%d(end));
}else if( lineLen + iBufWidth > iWidth ){
ET(.term delete %d(iBufY).%d(lineLen-iBufWidth) %d(iBufY).%d(lineLen));
}
}
/* Add zLineBuf to the text widget at the appropriate spot.
*/
zLineBuf[iBufWidth] = 0;
ET(.term insert %d(iBufY).%d(iBufX) "%q(zLineBuf)");
/* Tag the newly inserted text according to the current character mode. */
/* ET(
foreach t [.term tag names %d(iBufY).%d(iBufX)] {
.term tag remove $t %d(iBufY).%d(iBufX) %d(iBufY).%d(iBufX+iBufWidth)
}
); */
switch( eBufTag ){
case BT_Normal:
break;
case BT_Underline:
ET( .term tag add ul %d(iBufY).%d(iBufX) %d(iBufY).%d(iBufX+iBufWidth) );
break;
case BT_Bold:
ET( .term tag add bd %d(iBufY).%d(iBufX) %d(iBufY).%d(iBufX+iBufWidth) );
break;
case BT_Inverse:
ET( .term tag add iv %d(iBufY).%d(iBufX) %d(iBufY).%d(iBufX+iBufWidth) );
break;
}
iBufWidth = 0;
}
/* Display the cursor at its appropriate position
*/
static void DisplayCursor(void){
MakePositionExist(iCurX,iCurY);
ET(.term mark set insert %d(iCurY).%d(iCurX));
}
/* Index forward. This means that the cursor needs to move down
** by one line. Things can get complex here, due to the VT100
** scrolling region logic. Only the lines between iScrollTop and
** iScrollBtm, inclusive, should scroll.
**
** Remember that both iScrollTop and iScrollBtm are vt100-indices.
** That is to say, the top line of the visible part of text is
** line 1. The cursor is on the top line of the scrolling region if:
**
** iScrollTop == iCurY - iTop
**
** This routine prevents texts from flowing into the scrollback region
** (of the top of the viewing area) unless the scrolling region is the
** whole viewing area.
**
** The buffer should have already been flushed by the time this
** function is called.
*/
static void IndexForward(void){
int vt100Y; /* Cursor Y coordinate in VT100 space */
int iTop;
iTop = iBtm - iHeight;
if( iTop < 0 ) iTop = 0;
vt100Y = iCurY - iTop;
if( vt100Y<iScrollBtm ){
iCurY++;
}else if( iScrollBtm==vt100Y ){
ET(.term insert {%d(iCurY).0 lineend} \n);
if( iScrollTop>1 || iScrollBtm<iHeight ){
int y = iScrollTop + iTop;
ET(.term delete %d(y).0 %d(y+1).0);
}else{
iBtm++;
iCurY++;
}
}else if( vt100Y<iHeight ){
iCurY++;
}else{
/* Do nothing. Let the next line of text overwrite the bottom line. */
}
}
/* Index backwards. Like the previous function, just in the opposite
** direction.
*/
static void IndexBackward(void){
int vt100Y; /* Y coordinate in VT100 space */
int iTop;
iTop = iBtm - iHeight;
if( iTop < 0 ) iTop = 0;
vt100Y = iCurY - iTop;
if( vt100Y>iScrollTop ){
iCurY--;
}else if( vt100Y==iScrollTop ){
int y;
ET(.term insert %d(iCurY).0 \n);
y = iScrollBtm + iTop;
ET(.term delete {%d(y).0 lineend} {%d(y+1).0 lineend});
}else if( vt100Y>1 ){
iCurY--;
}else{
/* Do nothing. Let the next line overwrite the top line */
}
}
/* Add printable characters to the cache. "c" is always a printable
** character here -- never a newline or tab or other control character.
**
** This is the only way in which text can be added to zLineBuf[].
*/
static void InsertChar(char c){
static void ProcessNormal(char c);
if( iCurX>=iWidth ){
FlushBuffer();
IndexForward();
iCurX = 0;
}
if( iBufWidth >= sizeof(zLineBuf)-1 ){
FlushBuffer();
}
if( iBufWidth==0 ){
iBufX = iCurX;
iBufY = iCurY;
}
zLineBuf[iBufWidth++] = c;
iCurX++;
}
/* Process a character received while in the normal mode of operation.
**
** Add the character to the character cache. If the character cache is
** full, then add the cache to the text widget.
*/
static void ProcessNormal(char c){
static void ProcessEsc(char c);
switch( c ){
case 033: /* ESC -- Escape */
xProcess = ProcessEsc;
FlushBuffer();
break;
case 007: /* BEL -- Bell */
XBell(Et_Display,0);
break;
case 010: /* BS -- Backspace */
if( iCurX>0 ){
FlushBuffer();
iCurX--;
}
break;
case 011: /* HT -- Horizontal Tab */
FlushBuffer();
iCurX = (iCurX+8)&~0x7;
if( iCurX >= iWidth ){
FlushBuffer();
IndexForward();
iCurX = 0;
}
break;
case 012: /* NL -- New line */
case 013: /* VT -- Vertical Tab */
case 014: /* FF -- Form feed */
FlushBuffer();
IndexForward();
break;
case 015: /* CR -- Carriage return */
FlushBuffer();
iCurX = 0;
break;
default:
if( c>=' ' && c<='~' ){
InsertChar(c);
}
break;
}
}
/* Process a character of a background process
** No Emulation mode, \n goes to begin of a new line
*/
static void ProcessBackground(char c){
switch( c ){
case 007: /* BEL -- Bell */
XBell(Et_Display,0);
break;
case 010: /* BS -- Backspace */
if( iCurX>0 ){
FlushBuffer();
iCurX--;
}
break;
case 011: /* HT -- Horizontal Tab */
FlushBuffer();
iCurX = (iCurX+8)&~0x7;
if( iCurX >= iWidth ){
FlushBuffer();
IndexForward();
iCurX = 0;
}
break;
case 012: /* NL -- New line */
case 013: /* VT -- Vertical Tab */
case 014: /* FF -- Form feed */
case 015: /* CR -- Carriage return */
FlushBuffer();
IndexForward();
iCurX = 0;
break;
default:
if( c>=' ' && c<='~' ){
InsertChar(c);
}
break;
}
}
/* Ingore one character
*/
static void IgnoreOne(char c){
xProcess = ProcessNormal;
}
/* Wait until an ESC '\\' is seen
*/
static void WaitEscSlash(char c){
static int escSeen = 0;
if( c==033 ){
escSeen = 1;
}else if( c=='\\' && escSeen ){
escSeen = 0;
xProcess = ProcessNormal;
}else{
escSeen = 0;
}
}
/* Wait until the BEL character is seen
*/
static void WaitBel(char c){
if( c==007 ) xProcess = ProcessNormal;
}
/* The previous character was an ESC (escape). Process the next
** character.
**
** When this function is called, we know that the buffer has been
** flushed.
*/
static void ProcessEsc(char c){
int i;
static void ProcessEscBrace(char c);
ASSERT( iBufWidth==0 );
xProcess = ProcessNormal;
switch( c ){
case '[':
xProcess = ProcessEscBrace;
nParam = 0;
for(i=0; i<sizeof(aiParam)/sizeof(aiParam[0]); i++) aiParam[i] = -1;
break;
case '#': /* DEC test sequence */
case '(': /* Designate G0 Character Set */
case ')': /* Designate G1 Character Set */
case '*': /* Designate G2 Character Set */
case '+': /* Designate G3 Character Set */
xProcess = IgnoreOne;
break;
case '7': /* Save Cursor */
iCurSavedX = iCurX;
iCurSavedY = iCurY;
break;
case '8': /* Restore Cursor */
iCurX = iCurSavedX;
iCurY = iCurSavedY;
break;
case '=': /* Application Keypad */
case '>': /* Normal Keypad */
break;
case 'D': /* Index */
case 'E': /* NextLine */
IndexForward();
break;
case 'F': /* An HP bug */
break;
case 'H': /* Tab set */
break;
case 'M': /* Reverse index */
IndexBackward();
break;
case 'N': /* Use G2 for next character only */
break;
case 'O': /* Use G3 for next character only */
break;
case 'P': /* "ESC P text ESC \" implements Device Control String */
xProcess = WaitEscSlash;
break;
case 'Z': /* Return the Terminal ID */
ET( SendToTTY \033\[?1\;2c );
break;
case ']':
xProcess = WaitBel;
break;
case '^':
case '_':
xProcess = WaitEscSlash;
break;
default:
break;
}
}
/* We have seen an ESC followed by a '['. Process subsequent characters.
**
** When this function is called, we know that the buffer has been
** flushed.
*/
static void ProcessEscBrace(char c){
int n, x, y;
int i;
int end;
int iTop;
char zBuf[100];
static void ProcessEscBraceQuestion(char);
ASSERT( iBufWidth==0 );
xProcess = ProcessNormal;
switch( c ){
case '?':
xProcess = ProcessEscBraceQuestion;
break;
case '@':
n = aiParam[0];
if( n<0 ) n = 1;
for(i=0; i<n; i++) InsertChar(' ');
break;
case 'A':
n = aiParam[0];
if( n<0 ) n = 1;
iCurY -= n;
if( iCurY < 1 ) iCurY = 1;
break;
case 'B':
n = aiParam[0];
if( n<0 ) n = 1;
iCurY += n;
if( iCurY > iBtm && iCurY > iHeight ) iCurY = iBtm;
break;
case 'C':
n = aiParam[0];
if( n<0 ) n = 1;
iCurX += n;
if( iCurX > iWidth ) iCurX = iWidth;
break;
case 'D':
n = aiParam[0];
if( n<0 ) n = 1;
iCurX -= n;
if( iCurX<0 ) iCurX = 0;
break;
case 'H':
y = aiParam[0];
if( y<1 ) y = 1;
x = aiParam[1];
if( x<1 ) x = 1;
iCurX = x-1;
iTop = iBtm - iHeight;
if( iTop<0 ) iTop = 0;
iCurY = iTop + y;
break;
case 'J':
iTop = iBtm - iHeight;
if( iTop<0 ) iTop = 0;
switch( aiParam[0] ){
case 1: /* Clear above the cursor */
if( iCurY > iTop + 1 ){
for(i=0; i<iCurY - iTop - 1; i++) zBuf[i] = '\n';
zBuf[i] = 0;
ET( .term delete %d(iTop+1).0 %d(iCurY).0 );
if( zBuf[0] ) ET( .term insert %d(iTop+1).0 {%s(zBuf)} );
}
ET(.term delete %d(iCurY).0 %d(iCurY).%d(iCurX));
break;
case 2: /* Clear everything */
for(i=0; i<iBtm - iTop - 1; i++) zBuf[i] = '\n';
zBuf[i] = 0;
ET(.term delete %d(iTop+1).0 {%d(iBtm).0 lineend});
if( zBuf[0] ) ET(.term insert %d(iTop+1).0 {%s(zBuf)});
break;
default: /* Clear below the cursor */
for(i=0; i<iBtm - iCurY; i++) zBuf[i] = '\n';
zBuf[i] = 0;
ET( .term delete %d(iCurY).%d(iCurX) {%d(iBtm).0 lineend} );
if( zBuf[0] ) ET( .term insert end {%s(zBuf)} );
break;
}
break;
case 'K':
switch( aiParam[0] ){
case 1: /* Clear to left of cursor */
ET(.term delete %d(iCurY).0 %d(iCurY).%d(iCurX));
break;
case 2: /* Clear the whole line */
ET(.term delete %d(iCurY).0 {%d(iCurY).0 lineend});
break;
default: /* Clear to the right of the cursor */
ET(.term delete %d(iCurY).%d(iCurX) {%d(iCurY).0 lineend});
break;
}
break;
case 'L': /* Insert lines */
n = aiParam[0];
if( n<1 ) n = 1;
for(i=0; i<n; i++) zBuf[i] = '\n';
zBuf[i] = 0;
ET(.term insert %d(iCurY).0 {%s(zBuf)});
iTop = iBtm - iHeight;
if( iTop<0 ) iTop = 0;
y = iScrollBtm + iTop;
ET(.term delete {%d(y).0 lineend} {%d(y+n).0 lineend});
iBtm = ET_INT(.term index end) - 1;
break;
case 'M': /* Delete lines */
n = aiParam[0];
if( n<1 ) n = 1;
for(i=0; i<n; i++) zBuf[i] = '\n';
zBuf[i] = 0;
ET(.term delete %d(iCurY).0 %d(iCurY+n).0);
iTop = iBtm - iHeight;
if( iTop<0 ) iTop = 0;
y = iScrollBtm + iTop;
if( iCurY < y ){
ET(.term insert {%d(y-n).0 lineend} {%s(zBuf)});
}else{
ET(.term insert end {%s(zBuf)});
}
break;
case 'P': /* Delete characters */
n = aiParam[0];
if( n<0 ) n = 1;
sscanf( ET_STR(.term index {%d(iCurY).0 lineend}), "%*d.%d", &end);
if( iCurX + n > end ) n = end - iCurX;
if( n>0 ){
ET(.term delete %d(iCurY).%d(iCurX) %d(iCurY).%d(iCurX+n));
}
break;
case 'T': /* Initiate hilite mouse tracking */
break;
case 'c': /* Send device attributes */
break;
case 'f': /* horizontal and vertical position */
break;
case 'g': /* Tab clear */
break;
case 'h': /* Set mode */
switch( aiParam[0] ){
case 4:
bReplaceMode = 0;
default:
break;
}
break;
case 'l': /* reset mode */
switch( aiParam[0] ){
case 4:
bReplaceMode = 1;
default:
break;
}
break;
case 'm': /* character attributes */
switch( aiParam[0] ){
case 1: /* Bold */
case 5: /* Blink (appears as bold) */
eBufTag = BT_Bold;
break;
case 4: /* Underscore */
eBufTag = BT_Underline;
break;
case 7: /* Inverse */
eBufTag = BT_Inverse;
break;
default: /* Normal */
eBufTag = BT_Normal;
break;
}
break;
case 'n': /* Device status report */
break;
case 'r': /* Set scrolling region */
iScrollTop = aiParam[0];
if( iScrollTop<1 ) iScrollTop = 1;
if( iScrollTop>iHeight ) iScrollTop = iHeight;
iScrollBtm = aiParam[1];
if( iScrollBtm<iScrollTop ) iScrollBtm = iScrollTop;
if( iScrollBtm>iHeight ) iScrollBtm = iHeight;
iTop = iBtm - iHeight;
if( iTop<0 ) iTop = 0;
iCurY = iTop + 1;
iCurX = 0;
break;
case 'x': /* Request terminal parameters */
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if( aiParam[nParam]<0 ){
aiParam[nParam] = c - '0';
}else{
aiParam[nParam] = aiParam[nParam]*10 + c - '0';
}
xProcess = ProcessEscBrace;
break;
case ';':
if( nParam < sizeof(aiParam)/sizeof(aiParam[0]) - 1 ){
nParam++;
}
xProcess = ProcessEscBrace;
break;
default:
break;
}
}
/* We have seen an ESC followed by a '['. Process subsequent characters.
**
** When this function is called, we know that the buffer has been
** flushed and the text widget has scrolled all the way to the
** bottom.
*/
static void ProcessEscBraceQuestion(char c){
switch( c ){
case 'h': /* DEC private mode set */
xProcess = ProcessNormal;
break;
case 'l': /* DEC private mode reset */
xProcess = ProcessNormal;
break;
case 'r': /* Restore DEC private mode values */
xProcess = ProcessNormal;
break;
case 's': /* Save DEC private mode values */
xProcess = ProcessNormal;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ' ':
case ';':
break;
default:
xProcess = ProcessNormal;
break;
}
}
/* This routine is called to deal with data written by the child process
** on the other side of the pty. Its job is to interpret characters as
** if it where a VT100 terminal, and make appropriate changes to the
** text widget.
*/
static void HandleTerm(void *clientData, int mask){
int *fd = (int *)clientData;
int nByte;
char zBuf[2000];
nByte = read(*fd,zBuf,sizeof(zBuf)-1);
if( nByte>0 ){
int i;
/* Process all characters received */
for(i=0; i<nByte; i++){
#ifdef DEBUG
if( bDebug ){
printf("Recv: %03o %c\n",(int)zBuf[i],
(zBuf[i]>=' ' && zBuf[i]<='~')? zBuf[i] : ' ');
}
#endif
(*xProcess)(zBuf[i]);
}
/* It might be a while before we process additional input, so
** flush the buffer to synchronize the screen display. */
FlushBuffer();
ClipScrollback();
DisplayCursor();
JumpToBottom();
/* Tk4.0 requires two "update idletasks" in order to redisplay
** the text widget. This line is not strictly necessary. If you
** remove it, the terminal will scroll faster, but you won't get to
** watch is scroll by. With this line in place, the screen will
** update at least every 2000 characters.
*/
ET(update idletasks; update idletasks);
}else if( nByte<0 ){
/* Perhaps the child process has died */
extern int IsChildAlive();
if( !IsChildAlive() ){
CleanKill();
}
}
}
static void HandleStdOut(void *clientData, int mask){
int *fd = (int *)clientData;
int nByte;
char zBuf[2000];
nByte = read(*fd,zBuf,sizeof(zBuf)-1);
if( nByte>0 ){
int i;
/* Process all characters received */
for(i=0; i<nByte; i++){
#ifdef DEBUG
if( bDebug ){
printf("Recv: %03o %c\n",(int)zBuf[i],
(zBuf[i]>=' ' && zBuf[i]<='~')? zBuf[i] : ' ');
}
#endif
ProcessBackground(zBuf[i]);
}
/* It might be a while before we process additional input, so
** flush the buffer to synchronize the screen display. */
FlushBuffer();
ClipScrollback();
DisplayCursor();
JumpToBottom();
/* Tk4.0 requires two "update idletasks" in order to redisplay
** the text widget. This line is not strictly necessary. If you
** remove it, the terminal will scroll faster, but you won't get to
** watch is scroll by. With this line in place, the screen will
** update at least every 2000 characters.
*/
ET(update idletasks; update idletasks);
}else if( nByte<0 ){
/* Perhaps the child process has died */
extern int IsChildAlive();
if( !IsChildAlive() ){
CleanKill();
}
}
}
static void HandleStdErr(void *clientData, int mask){
HandleStdOut(clientData, mask);
}
/* Process the text of the first argument as if it had been received
** through the pseudo-TTY.
*/
ET_PROC( SimulatedInput ){
int i;
if( argc!=2 ){
interp->result = "wrong # args";
return ET_ERROR;
}
for(i=0; argv[1][i]; i++){
#ifdef DEBUG
if( bDebug ){
printf("Sim: %03o %c\n",(int)argv[1][i],
(argv[1][i]>=' ' && argv[1][i]<='~')? argv[1][i] : ' ');
}
#endif
(*xProcess)(argv[1][i]);
}
FlushBuffer();
ClipScrollback();
DisplayCursor();
JumpToBottom();
return ET_OK;
}
/* This procedure is used to send characters to the process running
** on the other size of the pseudo-TTY.
*/
ET_PROC( SendToTTY ){
int c;
char z[1];
if( argc!=2 ){
interp->result = "wrong # args";
return ET_ERROR;
}
Tcl_GetInt(interp,argv[1],&c);
*z = c;
if( c>0 && c<=0x7f ){
while( write(iChildFd,z,1)<1 ){}
}
return ET_OK;
}
/* A special procedure is needed to send zeros.
*/
ET_PROC( SendZeroToTTY ){
char z[1];
*z = 0;
while( write(iChildFd,z,1)<1 ){}
return ET_OK;
}
/* Do a paste operation
*/
ET_PROC( Paste ){
int i;
if( argc!=2 ){
interp->result = "wrong # args";
return ET_ERROR;
}
for(i=0; argv[1][i]; i++){
char c = argv[1][i];
while( write(iChildFd,&c,1)<1 ){}
/* Some Pseudo-TTYs are broken (ex: Linux) and can't receive too
** many characters too quickly. Hence we need to pause every so
** often on a big paste to let the Pseudo-TTY catch up. */
if( i%100 == 99 ) ET( after 10; update);
}
return ET_OK;
}
/* Ring the bell. Needed in Tk4.0 because the built-in "bell" causes
** the screen-saver to be reset, which in turn causes an unsightly flicker
** on the screen. */
ET_PROC( bell ){
XBell(Et_Display,0);
return ET_OK;
}
/* This procedure is called after the window changes size. Its
** job is to tell the Pseudo-TTY about the new window size.
*/
ET_PROC( WindowSizeChangeNotify ){
static int previousHeight = 24;
extern void SetSizeOfTTY(int,int,int);
JumpToBottom();
SetSizeOfTTY(iChildFd,iWidth,iHeight);
if( iScrollBtm == previousHeight ){
iScrollBtm = iHeight;
}
previousHeight = iHeight;
return ET_OK;
}
ET_PROC( Out )
{
int i;
if( argc!=3 ){
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " win string\"", 0);
return ET_ERROR;
}
xProcess = ProcessNormal;
for(i=0; argv[2][i]; i++){
#ifdef DEBUG
if( bDebug ){
printf("Sim: %03o %c\n",(int)argv[1][i],
(argv[1][i]>=' ' && argv[1][i]<='~')? argv[1][i] : ' ');
}
#endif
(*xProcess)(argv[2][i]);
}
ProcessNormal('\r');
ProcessNormal('\n');
FlushBuffer();
ClipScrollback();
DisplayCursor();
JumpToBottom();
return ET_OK;
}
int StartTty()
{
pipe(iFdOut);
pipe(iFdErr);
Tcl_CreateFileHandler(iFdOut[0],TK_READABLE,HandleStdOut,
(ClientData)&iFdOut[0]);
Tcl_CreateFileHandler(iFdErr[0],TK_READABLE,HandleStdErr,
(ClientData)&iFdErr[0]);
fcntl(iFdOut[0], F_SETFL, O_NONBLOCK);
fcntl(iFdErr[0], F_SETFL, O_NONBLOCK);
return 0;
}
static int StopTty()
{
Tcl_DeleteFileHandler(iFdErr[0]);
Tcl_DeleteFileHandler(iFdOut[0]);
close(iFdErr[0]);
close(iFdErr[1]);
close(iFdOut[0]);
close(iFdOut[1]);
return 0;
}
ET_PROC(ExecBg)
{
pid_t pid;
char *cmdline;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " arg ...\"", 0);
return ET_ERROR;
}
if ((pid = fork()) < 0) {
sprintf(interp->result, "%s: fork failed with error code %d",
argv[0], pid);
return ET_ERROR;
}
else if (pid == 0) { /* Child */
close(0);
if (iFdOut[1] != 1) {
dup2(iFdOut[1], 1);
close(iFdOut[1]);
}
if (iFdErr[1] != 2) {
dup2(iFdErr[1], 2);
close(iFdErr[1]);
}
execvp(argv[1], &argv[1]);
exit(1);
}
else { /* Parent */
cmdline = Tcl_Merge(argc-1, argv+1);
NewProcess(pid, cmdline);
ET(OutMsg "Process %d(pid) started: %s(cmdline)");
free(cmdline);
sprintf(interp->result, "%d", pid);
return ET_OK;
}
}
ET_PROC( Kill )
{
int i, j;
char spid[60];
pid_t pids[20];
ET(set pid {});
for (i = 0, j = 0; i < 20; i++) {
if (processList[i].pid) {
sprintf(spid, "{%5d %s}", processList[i].pid, processList[i].command);
pids[j++] = processList[i].pid;
ET(lappend pid %s(spid));
}
}
if (j > 0) {
j = ET_INT(ListDlg .ldlg 300x150$params(dlg_geom) "Kill Process:" $pid);
#ifdef DEBUG
fprintf(stderr, "Kill: %d %d\n", j, pids[j]);
#endif
kill(pids[j], SIGKILL);
}
return ET_OK;
}
ET_PROC( CleanShutdown)
{
KillAllProcesses();
StopTty();
return ET_OK;
}
|