/* * sscr_input -- * Read any waiting shell input. * * PUBLIC: int sscr_input(SCR *); */ int sscr_input(SCR *sp) { GS *gp; struct pollfd *pfd; int nfds, rval; gp = sp->gp; rval = 0; /* Allocate space for pfd. */ nfds = 0; TAILQ_FOREACH(sp, &gp->dq, q) if (F_ISSET(sp, SC_SCRIPT)) nfds++; if (nfds == 0) return (0); pfd = calloc(nfds, sizeof(struct pollfd)); if (pfd == NULL) { msgq(sp, M_SYSERR, "malloc"); return (1); } /* Setup events bitmasks. */ nfds = 0; TAILQ_FOREACH(sp, &gp->dq, q) if (F_ISSET(sp, SC_SCRIPT)) { pfd[nfds].fd = sp->script->sh_master; pfd[nfds].events = POLLIN; nfds++; } loop: /* Check for input. */ switch (poll(pfd, nfds, 0)) { case -1: msgq(sp, M_SYSERR, "poll"); rval = 1; /* FALLTHROUGH */ case 0: goto done; default: break; } /* Read the input. */ nfds = 0; TAILQ_FOREACH(sp, &gp->dq, q) if (F_ISSET(sp, SC_SCRIPT)) { if ((pfd[nfds].revents & POLLHUP) && sscr_end(sp)) goto done; if ((pfd[nfds].revents & POLLIN) && sscr_insert(sp)) goto done; nfds++; } goto loop; done: free(pfd); return (rval); }
/* * sscr_getprompt -- * Eat lines printed by the shell until a line with no trailing * carriage return comes; set the prompt from that line. */ static int sscr_getprompt(SCR *sp) { struct timeval tv; fd_set fdset; int master; /* Wait up to a second for characters to read. */ tv.tv_sec = 5; tv.tv_usec = 0; master = sp->script->sh_master; FD_ZERO(&fdset); FD_SET(master, &fdset); switch (select(master + 1, &fdset, NULL, NULL, &tv)) { case -1: /* Error or interrupt. */ msgq(sp, M_SYSERR, "select"); break; case 0: /* Timeout */ msgq(sp, M_ERR, "Error: timed out"); break; case 1: /* Characters to read. */ return (sscr_insert(sp) || sp->script == NULL); } sscr_end(sp); return (1); }
/* * sscr_check_input - * Check for input from command input or scripting windows. * * PUBLIC: int sscr_check_input(SCR *sp); */ int sscr_check_input(SCR *sp) { GS *gp; SCR *tsp; struct pollfd *pfd; int nfds, rval; gp = sp->gp; rval = 0; /* Allocate space for pfd. */ nfds = 1; TAILQ_FOREACH(tsp, &gp->dq, q) if (F_ISSET(sp, SC_SCRIPT)) nfds++; pfd = calloc(nfds, sizeof(struct pollfd)); if (pfd == NULL) { msgq(sp, M_SYSERR, "malloc"); return (1); } /* Setup events bitmasks. */ pfd[0].fd = STDIN_FILENO; pfd[0].events = POLLIN; nfds = 1; TAILQ_FOREACH(tsp, &gp->dq, q) if (F_ISSET(sp, SC_SCRIPT)) { pfd[nfds].fd = sp->script->sh_master; pfd[nfds].events = POLLIN; nfds++; } loop: /* Check for input. */ switch (poll(pfd, nfds, INFTIM)) { case -1: msgq(sp, M_SYSERR, "poll"); rval = 1; /* FALLTHROUGH */ case 0: goto done; default: break; } /* Only insert from the scripting windows if no command input */ if (!(pfd[0].revents & POLLIN)) { nfds = 1; TAILQ_FOREACH(tsp, &gp->dq, q) if (F_ISSET(sp, SC_SCRIPT)) { if ((pfd[nfds].revents & POLLHUP) && sscr_end(sp)) goto done; if ((pfd[nfds].revents & POLLIN) && sscr_insert(sp)) goto done; nfds++; } goto loop; }
/* * sscr_input -- * Read any waiting shell input. * * PUBLIC: int sscr_input __P((SCR *)); */ int sscr_input(SCR *sp) { WIN *wp; struct timeval poll; fd_set rdfd; int maxfd; wp = sp->wp; loop: maxfd = 0; FD_ZERO(&rdfd); poll.tv_sec = 0; poll.tv_usec = 0; /* Set up the input mask. */ TAILQ_FOREACH(sp, &wp->scrq, q) if (F_ISSET(sp, SC_SCRIPT)) { FD_SET(sp->script->sh_master, &rdfd); if (sp->script->sh_master > maxfd) maxfd = sp->script->sh_master; } /* Check for input. */ switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) { case -1: msgq(sp, M_SYSERR, "select"); return (1); case 0: return (0); default: break; } /* Read the input. */ TAILQ_FOREACH(sp, &wp->scrq, q) if (F_ISSET(sp, SC_SCRIPT) && FD_ISSET(sp->script->sh_master, &rdfd) && sscr_insert(sp)) return (1); goto loop; }