Guest User

Untitled

a guest
Feb 17th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.07 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. default:
  243. towrite[towritesize++] = io[i2];
  244. break;
  245. }
  246. flushtowrite(viewinfo[viewactive].output,towrite, &towritesize);
  247. }
  248. }
  249. else if (i1 == viewactive && (flags & FLAGS_NEWVIEW)){
  250. /* get the active view's response to determine */
  251. /* the new active view */
  252. towritesize=0;
  253. for (i2 = 0; i2 < iosize; ++i2) {
  254. if (io[i2] == VIEW_ACK && io[i2 + 1] == VIEW_ACK) {
  255. flushtowrite(STDOUT_FILENO, towrite, &towritesize);
  256. if (((i2 + 2) == iosize) || io[i2 + 2] < '1' || io[i2 + 2] > '9'){
  257. /* abort the view change if the char */
  258. /* wasn't read or char < 1 or > 9 */
  259. printf("abort 1: %d %d %d\n", io[i2], io[i2 + 1], io[i2 + 2]);
  260. flags &= ~FLAGS_NEWVIEW;
  261. break;
  262. }
  263.  
  264. /* check for existing views to switch to */
  265. prevview = viewactive;
  266. viewactive = 0;
  267. for (i3 = 0; i3 < viewcnt; ++i3) {
  268. if (viewinfo[i3].vilable == io[i2 + 2]){
  269. viewactive = i3;
  270. flags |= FLAGS_REFRESH;
  271. if (viewinfo[i3].ioctltype != viewinfo[prevview].ioctltype){
  272. flags |= FLAGS_IOCTLCHG;
  273. }
  274. }
  275. }
  276. /* it must be a new one, create it */
  277. if (viewactive == 0){
  278. if (ASKCMD) cmd = getcommand();
  279. c1 = io[i2 + 2];
  280. if (cmd == NULL){
  281. flags |= FLAGS_REFRESH;
  282. viewactive=prevview;
  283. }
  284. else if (startview(cmd, &c1) == -1) {
  285. fprintf(stderr, "Unable to execute '%s'\n", *cmd);
  286. sleep(1);
  287. flags |= FLAGS_REFRESH;
  288. }
  289. }
  290. flags &= ~FLAGS_NEWVIEW;
  291. break;
  292. }
  293. else if (io[i2] == VIEW_ACK) {
  294. /* abort the view change if only one CTRL-W */
  295. printf("abort 2: %d %d %d\n", io[i2], io[i2 + 1], io[i2 + 2]);
  296. sleep(3);
  297. flags &= ~FLAGS_NEWVIEW;
  298. }
  299. else towrite[towritesize++] = io[i2];
  300. }
  301. flushtowrite(STDOUT_FILENO,towrite, &towritesize);
  302. }
  303. else if (i1 == viewactive) write(STDOUT_FILENO, io, iosize);
  304. }
  305. }
  306. }
  307. }
  308. cleanup();
  309. exit(0);
  310. return 0;
  311. }
  312.  
  313. static void initialize()
  314. {
  315. char work[128];
  316. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  317. struct sigaction act;
  318. #endif
  319.  
  320. viewinfo[0].pid = getpid();
  321. viewinfo[0].input = STDIN_FILENO;
  322. viewinfo[0].output = STDOUT_FILENO;
  323. viewinfo[0].vilable = '0';
  324. viewinfo[0].ioctltype = IOCTL_NULL;
  325. pollttys[0].fd = STDIN_FILENO;
  326. pollttys[0].events = POLLIN;
  327. pollttys[0].revents = 0;
  328. viewcnt = 1;
  329.  
  330. flags = 0;
  331.  
  332. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  333. act.sa_handler = sigevent;
  334. sigemptyset(&act.sa_mask);
  335. act.sa_flags = 0;
  336. #ifdef SA_INTERRUPT
  337. act.sa_flags |= SA_INTERRUPT;
  338. #endif
  339. sigaction(SIGHUP, &act, &oldhup);
  340. sigaction(SIGINT, &act, &oldint);
  341. sigaction(SIGPIPE, &act, &oldpipe);
  342. sigaction(SIGCHLD, &act, &oldchld);
  343. #if 0
  344. #ifdef SIGTSTP
  345. act.sa_handler = SIG_IGN;
  346. sigaction(SIGTSTP, &act, &oldtstp);
  347. if (oldtstp.sa_handler == SIG_DFL) {
  348. act.sa_handler = sigevent;
  349. sigaction(SIGTSTP, &act, NULL);
  350. }
  351. #endif
  352. #endif
  353. #else
  354. oldhup = sigset(SIGHUP, sigevent);
  355. oldint = sigset(SIGINT, sigevent);
  356. oldpipe = sigset(SIGPIPE, sigevent);
  357. oldchld = sigset(SIGCHLD, sigevent);
  358. #if 0
  359. #ifdef SIGTSTP
  360. if ((oldtstp = sigset(SIGTSTP, SIG_IGN)) == SIG_DFL) sigset(SIGTSTP, sigevent);
  361. #endif
  362. #endif
  363. #endif
  364. flags |= FLAGS_SIGNAL;
  365.  
  366. if (isatty(STDIN_FILENO)) flags |= FLAGS_TTYIN;
  367. if (isatty(STDOUT_FILENO)) flags |= FLAGS_TTYOUT;
  368. if ((flags & FLAGS_TTYIN) && (flags & FLAGS_TTYOUT)) {
  369. strcpy(work, (char *) ttyname(STDIN_FILENO));
  370. if (!strcmp(work, (char *) ttyname(STDOUT_FILENO)))
  371. flags |= FLAGS_TTYSAME;
  372. }
  373.  
  374. /* prepare terminal attributes for shell and dbc */
  375. if (flags & FLAGS_TTYIN) {
  376. ioctl(STDIN_FILENO, TCGETA, &terminold);
  377. memcpy((char *) &terminshell, (char *) &terminold, sizeof(struct termio));
  378. memcpy((char *) &termindbc, (char *) &terminold, sizeof(struct termio));
  379. termindbc.c_iflag &= ~ICRNL;
  380. termindbc.c_iflag |= IGNBRK;
  381. termindbc.c_lflag = ISIG; /* enable signals, non-canonical, no echo, etc. */
  382. /* disable the intr character so it can be sent to the active view. */
  383. termindbc.c_cc[VINTR] = fpathconf(STDIN_FILENO, _PC_VDISABLE);
  384. terminshell.c_cc[VINTR] = fpathconf(STDIN_FILENO, _PC_VDISABLE);
  385. termindbc.c_cc[VMIN] = 1;
  386. termindbc.c_cc[VTIME] = 0;
  387. if (flags & FLAGS_TTYSAME) termindbc.c_oflag = 0;
  388. }
  389. if ((flags & FLAGS_TTYOUT) && !(flags & FLAGS_TTYSAME)) {
  390. ioctl(STDOUT_FILENO, TCGETA, &termoutold);
  391. memcpy((char *) &termoutdbc, (char *) &termoutold, sizeof(struct termio));
  392. memcpy((char *) &termoutshell, (char *) &termoutold, sizeof(struct termio));
  393. termoutdbc.c_oflag = 0; /* No output translation */
  394. }
  395. flags |= FLAGS_IOCTL;
  396.  
  397. }
  398.  
  399. static void cleanup()
  400. {
  401. int i1, i2, status;
  402. pid_t pid, dead;
  403.  
  404. checkviews();
  405.  
  406. /* kill all of the children */
  407. for (i1 = 1; i1 < viewcnt; i1++) {
  408. if ((pid = viewinfo[i1].pid) == -1) continue;
  409. #ifdef P_PID
  410. if (sigsend(P_PID, pid, SIGHUP) == -1) {
  411. if (sigsend(P_PID, pid, SIGHUP) == -1) continue;
  412. }
  413. #else
  414. if (kill(pid, SIGHUP) == -1) {
  415. if (kill(pid, SIGHUP) == -1) continue;
  416. }
  417. #endif
  418.  
  419. #if 0
  420. #ifdef WNOHANG
  421. while (waitpid(pid, &status, 0) == -1) if (errno != EINTR) break;
  422. #else
  423. for ( ; ; ) {
  424. dead = wait(&status);
  425. if (dead == pid) break;
  426. if (dead == -1) {
  427. if (errno == EINTR) continue;
  428. break;
  429. }
  430. for (i2 = 1; i2 < viewcnt; i2++) {
  431. if (dead == viewinfo[i2].pid) {
  432. viewinfo[i2].pid = -1;
  433. break;
  434. }
  435. }
  436. }
  437. #endif
  438. #endif
  439. }
  440.  
  441. if (flags & FLAGS_IOCTL) {
  442. if (flags & FLAGS_TTYIN) ioctl(STDIN_FILENO, TCSETAW, &terminold);
  443. if ((flags & FLAGS_TTYOUT) && !(flags & FLAGS_TTYSAME)) ioctl(STDOUT_FILENO, TCSETAW, &termoutold);
  444. }
  445.  
  446. if (flags & FLAGS_SIGNAL) {
  447. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  448. sigaction(SIGHUP, &oldhup, NULL);
  449. sigaction(SIGINT, &oldint, NULL);
  450. sigaction(SIGPIPE, &oldpipe, NULL);
  451. sigaction(SIGCHLD, &oldchld, NULL);
  452. #if 0
  453. #ifdef SIGTSTP
  454. sigaction(SIGINT, &oldtstp, NULL);
  455. #endif
  456. #endif
  457. #else
  458. sigset(SIGHUP, oldint);
  459. sigset(SIGINT, oldint);
  460. sigset(SIGPIPE, oldpipe);
  461. sigset(SIGCHLD, oldchld);
  462. #if 0
  463. #ifdef SIGTSTP
  464. sigset(SIGTSTP, oldtstp);
  465. #endif
  466. #endif
  467. #endif
  468. }
  469.  
  470. flags = 0;
  471. }
  472.  
  473. static char **getcommand()
  474. {
  475. static char *argv[50];
  476. static char args[256];
  477. int i1, i2, i3, i4, argc;
  478. char *ptr;
  479.  
  480. 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";
  481. write(STDOUT_FILENO, ptr, strlen(ptr));
  482. ptr = "Enter Command to Execute: ";
  483. write(STDOUT_FILENO, ptr, strlen(ptr));
  484. for (i2 = 0; i2 < sizeof(args) - 1; i2++) {
  485. if (read(STDIN_FILENO, &args[i2], 1) == 1) {
  486. if (args[i2] == '\n' || args[i2] == '\r') {
  487. write(STDOUT_FILENO, "\r\n", 2);
  488. break;
  489. }
  490. if (args[i2] == '\b') {
  491. if (i2) {
  492. i2--;
  493. write(STDOUT_FILENO, "\b \b", 3);
  494. }
  495. }
  496. else write(STDOUT_FILENO, &args[i2], 1);
  497. }
  498. }
  499. args[i2] = 0;
  500.  
  501. argc = 0;
  502. for (i1 = 0; i1 < i2; i1++) {
  503. if (isspace(args[i1])) continue;
  504. if (argc == sizeof(argv) / sizeof(*argv)) break;
  505. argv[argc++] = &args[i1];
  506. for (i3 = i1, i4 = FALSE; i1 < i2 && (!isspace(args[i1]) || i4); i1++) {
  507. if (args[i1] == '"') {
  508. i4 = !i4;
  509. continue;
  510. }
  511. if (args[i1] == '\\' && (args[i1 + 1] == '"' || args[i1 + 1] == '\\')) i1++;
  512. args[i3++] = args[i1];
  513. }
  514. args[i3] = 0;
  515. }
  516. if (!argc || !argv[0][0]) return(NULL);
  517. argv[argc] = NULL;
  518. return(argv);
  519. }
  520.  
  521. static int startview(argv, vilable)
  522. char *argv[];
  523. char *vilable;
  524. {
  525. int fflags; /* needed for fcntl calls */
  526. int refresh, ioctltype, input[2], output[2];
  527. char c1, tmpdbc_pstation[80], tmpdbcvol_ram[80], tmpdbc_mview[80], tmpdbc_cfg[80], homedir[80];
  528. pid_t pid;
  529.  
  530. refresh = VIEW_REF;
  531. if (argv[0][0] == '-') {
  532. c1 = toupper(argv[0][1]);
  533. if (c1 >= '@' && c1 <= '_') refresh = (char)(c1 - '@'); /* '@' turns it off */
  534. argv++;
  535. }
  536. if (argv[0] == NULL) return(-1);
  537.  
  538. checkviews();
  539.  
  540. if (viewcnt == VIEWMAX) return(-1);
  541.  
  542. if (!strcmp(*argv,"DBC") || !strcmp(*argv,"dbc") || !strcmp(*argv,"EDIT") || !strcmp(*argv,"edit"))
  543. ioctltype=IOCTL_DBC;
  544. else
  545. ioctltype=IOCTL_SHELL;
  546.  
  547. ioctlswitch(ioctltype);
  548.  
  549. if (pipe(input) == -1) return(-1);
  550. if (pipe(output) == -1) {
  551. close(input[0]);
  552. close(input[1]);
  553. return(-1);
  554. }
  555.  
  556. if ((pid = fork()) == (pid_t) -1) { /* fork failed */
  557. close(input[0]);
  558. close(input[1]);
  559. close(output[0]);
  560. close(output[1]);
  561. return(-1);
  562. }
  563. if (pid == (pid_t) 0) { /* child */
  564. /* reset signal to default */
  565. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  566. sigaction(SIGHUP, &oldhup, NULL);
  567. sigaction(SIGINT, &oldint, NULL);
  568. sigaction(SIGPIPE, &oldpipe, NULL);
  569. sigaction(SIGCHLD, &oldchld, NULL);
  570. #if 0
  571. #ifdef SIGTSTP
  572. sigaction(SIGINT, &oldtstp, NULL);
  573. #endif
  574. #endif
  575. #else
  576. sigset(SIGHUP, oldint);
  577. sigset(SIGINT, oldint);
  578. sigset(SIGPIPE, oldpipe);
  579. sigset(SIGCHLD, oldchld);
  580. #if 0
  581. #ifdef SIGTSTP
  582. sigset(SIGTSTP, oldtstp);
  583. #endif
  584. #endif
  585. #endif
  586.  
  587. /* for child, input and output are reversed. */
  588. /* close unneeded file descriptors */
  589. close(input[0]);
  590. close(output[1]);
  591. /* change descriptors to become stdin and stdout */
  592. if (output[0] != STDIN_FILENO) {
  593. if (dup2(output[0], STDIN_FILENO) == -1) goto error;
  594. close(output[0]);
  595. }
  596. if (input[1] != STDOUT_FILENO) {
  597. if (dup2(input[1], STDOUT_FILENO) == -1) goto error;
  598. close(input[1]);
  599. }
  600.  
  601. /* create a unique DBC_PSTATION, and DBCVOL_RAM variable for
  602. * this command */
  603. sprintf(tmpdbc_pstation,"DBC_PSTATION=%s_s%c",dbc_pstation,*vilable);
  604. sprintf(tmpdbcvol_ram,"DBCVOL_RAM=%s/mview%c",dbcvol_ram,*vilable);
  605. sprintf(tmpdbc_mview,"DBC_MVIEW=%c",*vilable);
  606.  
  607. sprintf(tmpdbc_cfg,"DBC_CFG=%s/mview%c/DBCDX.CFG",dbcvol_ram,*vilable);
  608.  
  609. putenv(tmpdbc_pstation);
  610. putenv(tmpdbcvol_ram);
  611. putenv(tmpdbc_mview);
  612. putenv(tmpdbc_cfg);
  613.  
  614. /* make each command start in it's own directory if it exists */
  615. sprintf(homedir,"%s/mview%c",dbcvol_ram,*vilable);
  616. chdir(homedir);
  617.  
  618. system("cvtenv");
  619. execvp(argv[0], argv);
  620. error:
  621. _exit(1);
  622. }
  623. childpid = pid;
  624. /* close unneeded file descriptors */
  625. close(input[1]);
  626. close(output[0]);
  627.  
  628. /* set pipe for reading/writing to close on exec */
  629. fcntl(input[0], F_SETFD, FD_CLOEXEC);
  630. fcntl(output[1], F_SETFD, FD_CLOEXEC);
  631. fflags = fcntl(input[0], F_GETFL, 0);
  632. fcntl(input[0], F_SETFL, fflags | O_NDELAY);
  633. viewinfo[viewcnt].pid = pid;
  634. viewinfo[viewcnt].input = input[0];
  635. viewinfo[viewcnt].output = output[1];
  636. viewinfo[viewcnt].refresh = refresh;
  637. viewinfo[viewcnt].vilable = *vilable;
  638. viewinfo[viewcnt].ioctltype = ioctltype;
  639. pollttys[viewcnt].fd = input[0];
  640. pollttys[viewcnt].events = POLLIN;
  641. pollttys[viewcnt].revents = 0;
  642. viewactive = viewcnt++;
  643.  
  644. return(0);
  645. }
  646.  
  647. static void checkviews()
  648. {
  649. int i1;
  650.  
  651. #if 0
  652. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  653. sigemptyset(&newmask);
  654. sigaddset(&newmask, SIGPIPE);
  655. sigprocmask(SIG_BLOCK, &newmask, &oldmask);
  656. #else
  657. sighold(SIGPIPE);
  658. #endif
  659. #endif
  660.  
  661. for (i1 = 1; i1 < viewcnt; i1++) {
  662. #ifdef P_PID
  663. if (sigsend(P_PID, viewinfo[i1].pid, 0) != -1 || errno != ESRCH) continue;
  664. #else
  665. if (kill(viewinfo[i1].pid, 0) != -1 || errno != ESRCH) continue;
  666. #endif
  667. close(viewinfo[i1].input);
  668. close(viewinfo[i1].output);
  669. viewcnt--;
  670. memmove((char *) &viewinfo[i1], (char *) &viewinfo[i1 + 1], (viewcnt - i1) * sizeof(VIEWINFO));
  671. memmove((char *) &pollttys[i1], (char *) &pollttys[i1 + 1], (viewcnt - i1) * sizeof(struct pollfd));
  672. if (i1 == viewactive) flags &= ~FLAGS_NEWVIEW;
  673. if (viewactive > i1){
  674. viewactive--;
  675. }
  676. i1--;
  677. }
  678. if (viewactive >= viewcnt) viewactive = viewcnt - 1;
  679. #if 0
  680. #ifdef SIGPOLL
  681. #if defined(SA_RESTART) | defined(SA_INTERRUPT) | defined(SA_RESETHAND)
  682. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  683. #else
  684. sigrelse(SIGPOLL);
  685. #endif
  686. #endif
  687. #endif
  688. }
  689.  
  690. static void sigevent(sig)
  691. int sig;
  692. {
  693. int oldcnt;
  694.  
  695. switch (sig) {
  696. case SIGCHLD:
  697. wait(NULL);
  698. case SIGPIPE:
  699. oldcnt = viewcnt;
  700. checkviews();
  701. if (viewcnt > 1) {
  702. if (oldcnt != viewcnt){
  703. flags |= FLAGS_REFRESH;
  704. flags |= FLAGS_IOCTLCHG;
  705. flags &= ~FLAGS_NEWVIEW;
  706. }
  707. break;
  708. }
  709. case SIGHUP:
  710. case SIGINT:
  711. flags |= FLAGS_QUIT;
  712. break;
  713. }
  714. }
  715.  
  716. static void flushtowrite(outfd,writeout,writeoutsize)
  717. int outfd;
  718. char * writeout;
  719. int * writeoutsize;
  720. {
  721. if (*writeoutsize > 0){
  722. write(outfd, writeout, *writeoutsize);
  723. *writeoutsize = 0;
  724. }
  725. }
  726.  
  727. ioctlswitch(ioctltp)
  728. int ioctltp;
  729. {
  730. if (flags & FLAGS_TTYIN) {
  731. if (ioctltp == IOCTL_DBC) {
  732. ioctl(STDIN_FILENO, TCSETAW, &termindbc);
  733. }
  734. else if (ioctltp == IOCTL_SHELL) {
  735. ioctl(STDIN_FILENO, TCSETAW, &terminshell);
  736. }
  737. }
  738. if ((flags & FLAGS_TTYOUT) && !(flags & FLAGS_TTYSAME)) {
  739. if (ioctltp == IOCTL_DBC)
  740. ioctl(STDOUT_FILENO, TCSETAW, &termoutdbc);
  741. else if (ioctltp == IOCTL_SHELL)
  742. ioctl(STDOUT_FILENO, TCSETAW, &termoutshell);
  743. }
  744. flags &= ~FLAGS_IOCTLCHG;
  745. }
Advertisement
Add Comment
Please, Sign In to add comment