Guest User

Untitled

a guest
Feb 17th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.19 KB | None | 0 0
  1. /* $Header: mview.c, 2009246515, , $ */
  2. /* jamie findlay */
  3. /* 2-13-97 */
  4. /* Joe Munson 5-10-97 Add buffering to all reads and writes for performance */
  5. /* Joe Munson 5-12-97 CTRL-W use to toggle to the next windows and CTRL-N */
  6. /* opened a new one. Now, CTRL-W is used as a handshake to */
  7. /* communicate with the applications. When CTRL-W is noticed, */
  8. /* it is sent on to the active view, and mview begins scanning */
  9. /* every character for a response from the application in the */
  10. /* (CTRL-V,CTRL-V,WINDOW #) format. */
  11. /* Joe Munson 9-9-97 Add ioctl switching to allow a windows to have a ksh */
  12. /* or a DBC program. ( -a option ) */
  13. /* compile: cc -O -o mview mview.c */
  14.  
  15. static char Ident[] = "@(#)$Revision: 2009246515$ $Date: $";
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <signal.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <termio.h>
  25. //#include <stropts.h>
  26. #include <poll.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <curses.h>
  31. #include <term.h>
  32. #ifdef __STDC__
  33. #include <wait.h>
  34. #endif
  35.  
  36. #ifndef STDIN_FILENO
  37. #define STDIN_FILENO (fileno(stdin))
  38. #endif
  39.  
  40. #ifndef STDOUT_FILENO
  41. #define STDOUT_FILENO (fileno(stdout))
  42. #endif
  43.  
  44. #ifndef FD_CLOEXEC
  45. #define FD_CLOEXEC 1
  46. #endif
  47.  
  48. #if !defined(_PID_T) & !defined(_SYS_TYPES_H) & (!defined(_H_TYPES) | !defined(_POSIX_SOURCE)) & !defined(P_MYPID)
  49. typedef int pid_t;
  50. #endif
  51.  
  52. #define VIEWMAX 16
  53. #define IOBUF 2048
  54.  
  55. #define FLAGS_TTYIN 0x00000001
  56. #define FLAGS_TTYOUT 0x00000002
  57. #define FLAGS_TTYSAME 0x00000004
  58. #define FLAGS_IOCTL 0x00000008
  59. #define FLAGS_SIGNAL 0x00000010
  60. #define FLAGS_REFRESH 0x00000020
  61. #define FLAGS_QUIT 0x00000040
  62. #define FLAGS_NEWVIEW 0x00000080
  63. #define FLAGS_IOCTLCHG 0x00000100
  64.  
  65. typedef struct {
  66. pid_t pid;
  67. int input;
  68. int output;
  69. char refresh;
  70. char vilable;
  71. char ioctltype;
  72. } VIEWINFO;
  73.  
  74. static int flags;
  75.  
  76. static char *shell[] = {
  77. "-@",
  78. "ksh",
  79. NULL
  80. };
  81.  
  82. static pid_t childpid;
  83. static VIEWINFO viewinfo[VIEWMAX];
  84. static int viewcnt;
  85. static int viewactive = 0;
  86. static struct pollfd pollttys[VIEWMAX];
  87.  
  88. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  89. static struct sigaction oldhup;
  90. static struct sigaction oldint;
  91. static struct sigaction oldpipe;
  92. static struct sigaction oldchld;
  93. #ifdef SIGTSTP
  94. static struct sigaction oldtstp;
  95. #endif
  96. #else
  97. #ifdef __STDC__
  98. static void (*oldhup)(INT);
  99. static void (*oldint)(INT);
  100. static void (*oldpipe)(INT);
  101. static void (*oldchld)(INT);
  102. #ifdef SIGTSTP
  103. static void (*oldtstp)(INT);
  104. #endif
  105. #else
  106. static void (*oldhup)();
  107. static void (*oldint)();
  108. static void (*oldpipe)();
  109. static void (*oldchld)();
  110. #ifdef SIGTSTP
  111. static void (*oldtstp)();
  112. #endif
  113. #endif
  114. #endif
  115.  
  116. static struct termio terminshell, termindbc, terminold;
  117. static struct termio termoutshell, termoutdbc, termoutold;
  118.  
  119. static char dbc_pstation[80];
  120. static char dbcvol_ram[80];
  121.  
  122. #ifdef __STDC__
  123. static void initialize(void);
  124. static void flushtowrite(int, char *, int *);
  125. static void cleanup(void);
  126. static char **getcommand(void);
  127. static int startview(char **, char *);
  128. static void checkviews(void);
  129. static void sigevent(int);
  130. #else
  131. static void initialize();
  132. static void flushtowrite();
  133. static void cleanup();
  134. static char **getcommand();
  135. static int startview();
  136. static void checkviews();
  137. static void sigevent();
  138. #endif
  139.  
  140. #define VIEW_CHG_IN 0x17 /* CTRL-W */ /* mview change view */
  141. #define VIEW_CHG_OUT 0x17 /* CTRL-V */ /* command change view */
  142. #define VIEW_ACK 0x16 /* CTRL-V */ /* acknowledgement from commmand */
  143. #define VIEW_REF 0x0C /* CTRL-L */ /* refresh to command */
  144. #define VIEW_BREAK 0x03 /* CTRL-C */ /* mview send break to command */
  145.  
  146. /* different commands need different ioctl settings */
  147. #define IOCTL_NULL 0
  148. #define IOCTL_DBC 1
  149. #define IOCTL_SHELL 2
  150.  
  151. int ASKCMD;
  152.  
  153. int main(argc, argv)
  154. int argc;
  155. char *argv[];
  156. {
  157. int i1, i2, i3, iosize, towritesize, prevview;
  158. char c1, **cmd, io[IOBUF];
  159. char towrite[IOBUF];
  160.  
  161. prevview = 0;
  162. initialize();
  163.  
  164. ASKCMD=0;
  165. if (argc > 1)
  166. if (argv[1][0] == '-' && argv[1][1] == 'a') {
  167. cmd = &argv[2];
  168. ASKCMD=1;
  169. }
  170. else
  171. cmd = &argv[1];
  172. else
  173. cmd = shell;
  174. c1 = '1';
  175.  
  176. if (startview(cmd,&c1) == -1) {
  177. fprintf(stderr, "Unable to execute '%s'\n", *cmd);
  178. cleanup();
  179. exit(1);
  180. }
  181.  
  182. while (!(flags & FLAGS_QUIT)) {
  183. /* refresh active view */
  184. if (flags & FLAGS_REFRESH) {
  185. if (viewinfo[viewactive].refresh)
  186. write(viewinfo[viewactive].output, &viewinfo[viewactive].refresh, 1);
  187. flags &= ~FLAGS_REFRESH;
  188. }
  189. if (flags & FLAGS_IOCTLCHG) {
  190. ioctlswitch(viewinfo[viewactive].ioctltype);
  191. flags &= ~FLAGS_IOCTLCHG;
  192. }
  193.  
  194. /* check for IO on each commands STDOUT & mview's STDIN */
  195. if (poll(pollttys, viewcnt, -1) > 0) {
  196.  
  197. /* cycle through each view and check for STDIO */
  198. for (i1 = 0; i1 < viewcnt && !(flags & FLAGS_QUIT); i1++) {
  199.  
  200. /* refresh active view */
  201. if (flags & FLAGS_REFRESH) {
  202. if (viewinfo[viewactive].refresh)
  203. write(viewinfo[viewactive].output, &viewinfo[viewactive].refresh, 1);
  204. flags &= ~FLAGS_REFRESH;
  205. }
  206. if (flags & FLAGS_IOCTLCHG) {
  207. ioctlswitch(viewinfo[viewactive].ioctltype);
  208. flags &= ~FLAGS_IOCTLCHG;
  209. }
  210.  
  211. if (pollttys[i1].revents & POLLIN) {
  212. /* read command's STDOUT or mview's STDIN */
  213. iosize = read(pollttys[i1].fd, io, IOBUF);
  214. if (iosize < 1) continue;
  215. /* if it's mview's STDIN */
  216. if (!i1) {
  217. towritesize=0;
  218. /* check each character to mview's IO buffer. If it's not */
  219. /* special stick it in the array of chars "towrite" */
  220. for (i2 = 0; i2 < iosize; ++i2) {
  221. switch (io[i2]) {
  222. case VIEW_CHG_IN: /* ^W - change window */
  223. flushtowrite(viewinfo[viewactive].output,towrite, &towritesize);
  224. checkviews();
  225. /* write the CTRL-W to the active view */
  226. towrite[towritesize++] = VIEW_CHG_OUT;
  227. flushtowrite(viewinfo[viewactive].output,towrite, &towritesize);
  228. flags |= FLAGS_NEWVIEW;
  229. break;
  230. case VIEW_BREAK: /* ^C - interrupt */
  231. flushtowrite(viewinfo[viewactive].output,towrite, &towritesize);
  232. #ifdef P_PID
  233. sigsend(P_PID, viewinfo[viewactive].pid, SIGINT);
  234. #else
  235. kill(viewinfo[viewactive].pid, SIGINT);
  236. #endif
  237. break;
  238. case 27:
  239. flushtowrite(viewinfo[viewactive].output,towrite, &towritesize);
  240. usleep(10000);
  241. kill(childpid, SIGALRM);
  242. break;
  243. default:
  244. towrite[towritesize++] = io[i2];
  245. break;
  246. }
  247. flushtowrite(viewinfo[viewactive].output,towrite, &towritesize);
  248. }
  249. }
  250. else if (i1 == viewactive && (flags & FLAGS_NEWVIEW)){
  251. /* get the active view's response to determine */
  252. /* the new active view */
  253. towritesize=0;
  254. for (i2 = 0; i2 < iosize; ++i2) {
  255. if (io[i2] == VIEW_ACK && io[i2 + 1] == VIEW_ACK) {
  256. flushtowrite(STDOUT_FILENO, towrite, &towritesize);
  257. if (((i2 + 2) == iosize) || io[i2 + 2] < '1' || io[i2 + 2] > '9'){
  258. /* abort the view change if the char */
  259. /* wasn't read or char < 1 or > 9 */
  260. printf("abort 1: %d %d %d\n", io[i2], io[i2 + 1], io[i2 + 2]);
  261. flags &= ~FLAGS_NEWVIEW;
  262. break;
  263. }
  264.  
  265. /* check for existing views to switch to */
  266. prevview = viewactive;
  267. viewactive = 0;
  268. for (i3 = 0; i3 < viewcnt; ++i3) {
  269. if (viewinfo[i3].vilable == io[i2 + 2]){
  270. viewactive = i3;
  271. flags |= FLAGS_REFRESH;
  272. if (viewinfo[i3].ioctltype != viewinfo[prevview].ioctltype){
  273. flags |= FLAGS_IOCTLCHG;
  274. }
  275. }
  276. }
  277. /* it must be a new one, create it */
  278. if (viewactive == 0){
  279. if (ASKCMD) cmd = getcommand();
  280. c1 = io[i2 + 2];
  281. if (cmd == NULL){
  282. flags |= FLAGS_REFRESH;
  283. viewactive=prevview;
  284. }
  285. else if (startview(cmd, &c1) == -1) {
  286. fprintf(stderr, "Unable to execute '%s'\n", *cmd);
  287. sleep(1);
  288. flags |= FLAGS_REFRESH;
  289. }
  290. }
  291. flags &= ~FLAGS_NEWVIEW;
  292. break;
  293. }
  294. else if (io[i2] == VIEW_ACK) {
  295. /* abort the view change if only one CTRL-W */
  296. printf("abort 2: %d %d %d\n", io[i2], io[i2 + 1], io[i2 + 2]);
  297. sleep(3);
  298. flags &= ~FLAGS_NEWVIEW;
  299. }
  300. else towrite[towritesize++] = io[i2];
  301. }
  302. flushtowrite(STDOUT_FILENO,towrite, &towritesize);
  303. }
  304. else if (i1 == viewactive) write(STDOUT_FILENO, io, iosize);
  305. }
  306. }
  307. }
  308. }
  309. cleanup();
  310. exit(0);
  311. return 0;
  312. }
  313.  
  314. static void initialize()
  315. {
  316. char work[128];
  317. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  318. struct sigaction act;
  319. #endif
  320.  
  321. viewinfo[0].pid = getpid();
  322. viewinfo[0].input = STDIN_FILENO;
  323. viewinfo[0].output = STDOUT_FILENO;
  324. viewinfo[0].vilable = '0';
  325. viewinfo[0].ioctltype = IOCTL_NULL;
  326. pollttys[0].fd = STDIN_FILENO;
  327. pollttys[0].events = POLLIN;
  328. pollttys[0].revents = 0;
  329. viewcnt = 1;
  330.  
  331. flags = 0;
  332.  
  333. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  334. act.sa_handler = sigevent;
  335. sigemptyset(&act.sa_mask);
  336. act.sa_flags = 0;
  337. #ifdef SA_INTERRUPT
  338. act.sa_flags |= SA_INTERRUPT;
  339. #endif
  340. sigaction(SIGHUP, &act, &oldhup);
  341. sigaction(SIGINT, &act, &oldint);
  342. sigaction(SIGPIPE, &act, &oldpipe);
  343. sigaction(SIGCHLD, &act, &oldchld);
  344. #if 0
  345. #ifdef SIGTSTP
  346. act.sa_handler = SIG_IGN;
  347. sigaction(SIGTSTP, &act, &oldtstp);
  348. if (oldtstp.sa_handler == SIG_DFL) {
  349. act.sa_handler = sigevent;
  350. sigaction(SIGTSTP, &act, NULL);
  351. }
  352. #endif
  353. #endif
  354. #else
  355. oldhup = sigset(SIGHUP, sigevent);
  356. oldint = sigset(SIGINT, sigevent);
  357. oldpipe = sigset(SIGPIPE, sigevent);
  358. oldchld = sigset(SIGCHLD, sigevent);
  359. #if 0
  360. #ifdef SIGTSTP
  361. if ((oldtstp = sigset(SIGTSTP, SIG_IGN)) == SIG_DFL) sigset(SIGTSTP, sigevent);
  362. #endif
  363. #endif
  364. #endif
  365. flags |= FLAGS_SIGNAL;
  366.  
  367. if (isatty(STDIN_FILENO)) flags |= FLAGS_TTYIN;
  368. if (isatty(STDOUT_FILENO)) flags |= FLAGS_TTYOUT;
  369. if ((flags & FLAGS_TTYIN) && (flags & FLAGS_TTYOUT)) {
  370. strcpy(work, (char *) ttyname(STDIN_FILENO));
  371. if (!strcmp(work, (char *) ttyname(STDOUT_FILENO)))
  372. flags |= FLAGS_TTYSAME;
  373. }
  374.  
  375. /* prepare terminal attributes for shell and dbc */
  376. if (flags & FLAGS_TTYIN) {
  377. ioctl(STDIN_FILENO, TCGETA, &terminold);
  378. memcpy((char *) &terminshell, (char *) &terminold, sizeof(struct termio));
  379. memcpy((char *) &termindbc, (char *) &terminold, sizeof(struct termio));
  380. termindbc.c_iflag &= ~ICRNL;
  381. termindbc.c_iflag |= IGNBRK;
  382. termindbc.c_lflag = ISIG; /* enable signals, non-canonical, no echo, etc. */
  383. /* disable the intr character so it can be sent to the active view. */
  384. termindbc.c_cc[VINTR] = fpathconf(STDIN_FILENO, _PC_VDISABLE);
  385. terminshell.c_cc[VINTR] = fpathconf(STDIN_FILENO, _PC_VDISABLE);
  386. termindbc.c_cc[VMIN] = 1;
  387. termindbc.c_cc[VTIME] = 0;
  388. if (flags & FLAGS_TTYSAME) termindbc.c_oflag = 0;
  389. }
  390. if ((flags & FLAGS_TTYOUT) && !(flags & FLAGS_TTYSAME)) {
  391. ioctl(STDOUT_FILENO, TCGETA, &termoutold);
  392. memcpy((char *) &termoutdbc, (char *) &termoutold, sizeof(struct termio));
  393. memcpy((char *) &termoutshell, (char *) &termoutold, sizeof(struct termio));
  394. termoutdbc.c_oflag = 0; /* No output translation */
  395. }
  396. flags |= FLAGS_IOCTL;
  397.  
  398. strcpy(dbc_pstation,getenv("DBC_PSTATION"));
  399. strcpy(dbcvol_ram,getenv("DBCVOL_RAM"));
  400.  
  401. }
  402.  
  403. static void cleanup()
  404. {
  405. int i1, i2, status;
  406. pid_t pid, dead;
  407.  
  408. checkviews();
  409.  
  410. /* kill all of the children */
  411. for (i1 = 1; i1 < viewcnt; i1++) {
  412. if ((pid = viewinfo[i1].pid) == -1) continue;
  413. #ifdef P_PID
  414. if (sigsend(P_PID, pid, SIGHUP) == -1) {
  415. if (sigsend(P_PID, pid, SIGHUP) == -1) continue;
  416. }
  417. #else
  418. if (kill(pid, SIGHUP) == -1) {
  419. if (kill(pid, SIGHUP) == -1) continue;
  420. }
  421. #endif
  422.  
  423. #if 0
  424. #ifdef WNOHANG
  425. while (waitpid(pid, &status, 0) == -1) if (errno != EINTR) break;
  426. #else
  427. for ( ; ; ) {
  428. dead = wait(&status);
  429. if (dead == pid) break;
  430. if (dead == -1) {
  431. if (errno == EINTR) continue;
  432. break;
  433. }
  434. for (i2 = 1; i2 < viewcnt; i2++) {
  435. if (dead == viewinfo[i2].pid) {
  436. viewinfo[i2].pid = -1;
  437. break;
  438. }
  439. }
  440. }
  441. #endif
  442. #endif
  443. }
  444.  
  445. if (flags & FLAGS_IOCTL) {
  446. if (flags & FLAGS_TTYIN) ioctl(STDIN_FILENO, TCSETAW, &terminold);
  447. if ((flags & FLAGS_TTYOUT) && !(flags & FLAGS_TTYSAME)) ioctl(STDOUT_FILENO, TCSETAW, &termoutold);
  448. }
  449.  
  450. if (flags & FLAGS_SIGNAL) {
  451. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  452. sigaction(SIGHUP, &oldhup, NULL);
  453. sigaction(SIGINT, &oldint, NULL);
  454. sigaction(SIGPIPE, &oldpipe, NULL);
  455. sigaction(SIGCHLD, &oldchld, NULL);
  456. #if 0
  457. #ifdef SIGTSTP
  458. sigaction(SIGINT, &oldtstp, NULL);
  459. #endif
  460. #endif
  461. #else
  462. sigset(SIGHUP, oldint);
  463. sigset(SIGINT, oldint);
  464. sigset(SIGPIPE, oldpipe);
  465. sigset(SIGCHLD, oldchld);
  466. #if 0
  467. #ifdef SIGTSTP
  468. sigset(SIGTSTP, oldtstp);
  469. #endif
  470. #endif
  471. #endif
  472. }
  473.  
  474. flags = 0;
  475. }
  476.  
  477. static char **getcommand()
  478. {
  479. static char *argv[50];
  480. static char args[256];
  481. int i1, i2, i3, i4, argc;
  482. char *ptr;
  483.  
  484. ptr = "\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
  485. write(STDOUT_FILENO, ptr, strlen(ptr));
  486. ptr = "Enter Command to Execute: ";
  487. write(STDOUT_FILENO, ptr, strlen(ptr));
  488. for (i2 = 0; i2 < sizeof(args) - 1; i2++) {
  489. if (read(STDIN_FILENO, &args[i2], 1) == 1) {
  490. if (args[i2] == '\n' || args[i2] == '\r') {
  491. write(STDOUT_FILENO, "\r\n", 2);
  492. break;
  493. }
  494. if (args[i2] == '\b') {
  495. if (i2) {
  496. i2--;
  497. write(STDOUT_FILENO, "\b \b", 3);
  498. }
  499. }
  500. else write(STDOUT_FILENO, &args[i2], 1);
  501. }
  502. }
  503. args[i2] = 0;
  504.  
  505. argc = 0;
  506. for (i1 = 0; i1 < i2; i1++) {
  507. if (isspace(args[i1])) continue;
  508. if (argc == sizeof(argv) / sizeof(*argv)) break;
  509. argv[argc++] = &args[i1];
  510. for (i3 = i1, i4 = FALSE; i1 < i2 && (!isspace(args[i1]) || i4); i1++) {
  511. if (args[i1] == '"') {
  512. i4 = !i4;
  513. continue;
  514. }
  515. if (args[i1] == '\\' && (args[i1 + 1] == '"' || args[i1 + 1] == '\\')) i1++;
  516. args[i3++] = args[i1];
  517. }
  518. args[i3] = 0;
  519. }
  520. if (!argc || !argv[0][0]) return(NULL);
  521. argv[argc] = NULL;
  522. return(argv);
  523. }
  524.  
  525. static int startview(argv, vilable)
  526. char *argv[];
  527. char *vilable;
  528. {
  529. int fflags; /* needed for fcntl calls */
  530. int refresh, ioctltype, input[2], output[2];
  531. char c1, tmpdbc_pstation[80], tmpdbcvol_ram[80], tmpdbc_mview[80], tmpdbc_cfg[80], homedir[80];
  532. pid_t pid;
  533.  
  534. refresh = VIEW_REF;
  535. if (argv[0][0] == '-') {
  536. c1 = toupper(argv[0][1]);
  537. if (c1 >= '@' && c1 <= '_') refresh = (char)(c1 - '@'); /* '@' turns it off */
  538. argv++;
  539. }
  540. if (argv[0] == NULL) return(-1);
  541.  
  542. checkviews();
  543.  
  544. if (viewcnt == VIEWMAX) return(-1);
  545.  
  546. if (!strcmp(*argv,"DBC") || !strcmp(*argv,"dbc") || !strcmp(*argv,"EDIT") || !strcmp(*argv,"edit"))
  547. ioctltype=IOCTL_DBC;
  548. else
  549. ioctltype=IOCTL_SHELL;
  550.  
  551. ioctlswitch(ioctltype);
  552.  
  553. if (pipe(input) == -1) return(-1);
  554. if (pipe(output) == -1) {
  555. close(input[0]);
  556. close(input[1]);
  557. return(-1);
  558. }
  559.  
  560. if ((pid = fork()) == (pid_t) -1) { /* fork failed */
  561. close(input[0]);
  562. close(input[1]);
  563. close(output[0]);
  564. close(output[1]);
  565. return(-1);
  566. }
  567. if (pid == (pid_t) 0) { /* child */
  568. /* reset signal to default */
  569. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  570. sigaction(SIGHUP, &oldhup, NULL);
  571. sigaction(SIGINT, &oldint, NULL);
  572. sigaction(SIGPIPE, &oldpipe, NULL);
  573. sigaction(SIGCHLD, &oldchld, NULL);
  574. #if 0
  575. #ifdef SIGTSTP
  576. sigaction(SIGINT, &oldtstp, NULL);
  577. #endif
  578. #endif
  579. #else
  580. sigset(SIGHUP, oldint);
  581. sigset(SIGINT, oldint);
  582. sigset(SIGPIPE, oldpipe);
  583. sigset(SIGCHLD, oldchld);
  584. #if 0
  585. #ifdef SIGTSTP
  586. sigset(SIGTSTP, oldtstp);
  587. #endif
  588. #endif
  589. #endif
  590.  
  591. /* for child, input and output are reversed. */
  592. /* close unneeded file descriptors */
  593. close(input[0]);
  594. close(output[1]);
  595. /* change descriptors to become stdin and stdout */
  596. if (output[0] != STDIN_FILENO) {
  597. if (dup2(output[0], STDIN_FILENO) == -1) goto error;
  598. close(output[0]);
  599. }
  600. if (input[1] != STDOUT_FILENO) {
  601. if (dup2(input[1], STDOUT_FILENO) == -1) goto error;
  602. close(input[1]);
  603. }
  604.  
  605. /* create a unique DBC_PSTATION, and DBCVOL_RAM variable for
  606. * this command */
  607. sprintf(tmpdbc_pstation,"DBC_PSTATION=%s_s%c",dbc_pstation,*vilable);
  608. sprintf(tmpdbcvol_ram,"DBCVOL_RAM=%s/mview%c",dbcvol_ram,*vilable);
  609. sprintf(tmpdbc_mview,"DBC_MVIEW=%c",*vilable);
  610.  
  611. sprintf(tmpdbc_cfg,"DBC_CFG=%s/mview%c/DBCDX.CFG",dbcvol_ram,*vilable);
  612.  
  613. putenv(tmpdbc_pstation);
  614. putenv(tmpdbcvol_ram);
  615. putenv(tmpdbc_mview);
  616. putenv(tmpdbc_cfg);
  617.  
  618. /* make each command start in it's own directory if it exists */
  619. sprintf(homedir,"%s/mview%c",dbcvol_ram,*vilable);
  620. chdir(homedir);
  621.  
  622. system("cvtenv");
  623. execvp(argv[0], argv);
  624. error:
  625. _exit(1);
  626. }
  627. childpid = pid;
  628. /* close unneeded file descriptors */
  629. close(input[1]);
  630. close(output[0]);
  631.  
  632. /* set pipe for reading/writing to close on exec */
  633. fcntl(input[0], F_SETFD, FD_CLOEXEC);
  634. fcntl(output[1], F_SETFD, FD_CLOEXEC);
  635. fflags = fcntl(input[0], F_GETFL, 0);
  636. fcntl(input[0], F_SETFL, fflags | O_NDELAY);
  637. viewinfo[viewcnt].pid = pid;
  638. viewinfo[viewcnt].input = input[0];
  639. viewinfo[viewcnt].output = output[1];
  640. viewinfo[viewcnt].refresh = refresh;
  641. viewinfo[viewcnt].vilable = *vilable;
  642. viewinfo[viewcnt].ioctltype = ioctltype;
  643. pollttys[viewcnt].fd = input[0];
  644. pollttys[viewcnt].events = POLLIN;
  645. pollttys[viewcnt].revents = 0;
  646. viewactive = viewcnt++;
  647.  
  648. return(0);
  649. }
  650.  
  651. static void checkviews()
  652. {
  653. int i1;
  654.  
  655. #if 0
  656. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  657. sigemptyset(&newmask);
  658. sigaddset(&newmask, SIGPIPE);
  659. sigprocmask(SIG_BLOCK, &newmask, &oldmask);
  660. #else
  661. sighold(SIGPIPE);
  662. #endif
  663. #endif
  664.  
  665. for (i1 = 1; i1 < viewcnt; i1++) {
  666. #ifdef P_PID
  667. if (sigsend(P_PID, viewinfo[i1].pid, 0) != -1 || errno != ESRCH) continue;
  668. #else
  669. if (kill(viewinfo[i1].pid, 0) != -1 || errno != ESRCH) continue;
  670. #endif
  671. close(viewinfo[i1].input);
  672. close(viewinfo[i1].output);
  673. viewcnt--;
  674. memmove((char *) &viewinfo[i1], (char *) &viewinfo[i1 + 1], (viewcnt - i1) * sizeof(VIEWINFO));
  675. memmove((char *) &pollttys[i1], (char *) &pollttys[i1 + 1], (viewcnt - i1) * sizeof(struct pollfd));
  676. if (i1 == viewactive) flags &= ~FLAGS_NEWVIEW;
  677. if (viewactive > i1){
  678. viewactive--;
  679. }
  680. i1--;
  681. }
  682. if (viewactive >= viewcnt) viewactive = viewcnt - 1;
  683. #if 0
  684. #ifdef SIGPOLL
  685. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  686. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  687. #else
  688. sigrelse(SIGPOLL);
  689. #endif
  690. #endif
  691. #endif
  692. }
  693.  
  694. static void sigevent(sig)
  695. int sig;
  696. {
  697. int oldcnt;
  698.  
  699. switch (sig) {
  700. case SIGCHLD:
  701. wait(NULL);
  702. case SIGPIPE:
  703. oldcnt = viewcnt;
  704. checkviews();
  705. if (viewcnt > 1) {
  706. if (oldcnt != viewcnt){
  707. flags |= FLAGS_REFRESH;
  708. flags |= FLAGS_IOCTLCHG;
  709. flags &= ~FLAGS_NEWVIEW;
  710. }
  711. break;
  712. }
  713. case SIGHUP:
  714. case SIGINT:
  715. flags |= FLAGS_QUIT;
  716. break;
  717. }
  718. }
  719.  
  720. static void flushtowrite(outfd,writeout,writeoutsize)
  721. int outfd;
  722. char * writeout;
  723. int * writeoutsize;
  724. {
  725. if (*writeoutsize > 0){
  726. write(outfd, writeout, *writeoutsize);
  727. *writeoutsize = 0;
  728. }
  729. }
  730.  
  731. ioctlswitch(ioctltp)
  732. int ioctltp;
  733. {
  734. if (flags & FLAGS_TTYIN) {
  735. if (ioctltp == IOCTL_DBC) {
  736. ioctl(STDIN_FILENO, TCSETAW, &termindbc);
  737. }
  738. else if (ioctltp == IOCTL_SHELL) {
  739. ioctl(STDIN_FILENO, TCSETAW, &terminshell);
  740. }
  741. }
  742. if ((flags & FLAGS_TTYOUT) && !(flags & FLAGS_TTYSAME)) {
  743. if (ioctltp == IOCTL_DBC)
  744. ioctl(STDOUT_FILENO, TCSETAW, &termoutdbc);
  745. else if (ioctltp == IOCTL_SHELL)
  746. ioctl(STDOUT_FILENO, TCSETAW, &termoutshell);
  747. }
  748. flags &= ~FLAGS_IOCTLCHG;
  749. }
Advertisement
Add Comment
Please, Sign In to add comment