- drain_input (state);
- return state->delay;
-}
-
-\f
-/* Subprocess.
- */
-
-static void
-subproc_cb (XtPointer closure, int *source, XtInputId *id)
-{
- p_state *state = (p_state *) closure;
- state->input_available_p = True;
-}
-
-
-static void
-launch_text_generator (p_state *state)
-{
- XtAppContext app = XtDisplayToApplicationContext (state->dpy);
- Display *dpy = state->dpy;
- char buf[255];
- char *oprogram = get_string_resource (dpy, "program", "Program");
- char *program = (char *) malloc (strlen (oprogram) + 50);
-
- strcpy (program, "( ");
- strcat (program, oprogram);
-
- /* Kludge! Special-case "xscreensaver-text" to tell it how wide
- the screen is. We used to do this by just always feeding
- `program' through sprintf() and setting the default value to
- "xscreensaver-text --cols %d", but that makes things blow up
- if someone ever uses a --program that includes a % anywhere.
- */
- if (!strcmp (oprogram, "xscreensaver-text"))
- sprintf (program + strlen(program), " --cols %d", state->grid_width-1);
-
- strcat (program, " ) 2>&1");
-
-#ifdef HAVE_FORKPTY
- if(state->mode == 1)
- {
- int fd;
- struct winsize ws;
-
- ws.ws_row = state->grid_height - 1;
- ws.ws_col = state->grid_width - 2;
- ws.ws_xpixel = state->xgwa.width;
- ws.ws_ypixel = state->xgwa.height;
-
- state->pipe = NULL;
- if((state->pid = forkpty(&fd, NULL, NULL, &ws)) < 0)
- {
- /* Unable to fork */
- sprintf (buf, "%.100s: forkpty", progname);
- perror(buf);
- }
- else if(!state->pid)
- {
- /* This is the child fork. */
- char *av[10];
- int i = 0;
- if (putenv("TERM=vt100"))
- abort();
- av[i++] = "/bin/sh";
- av[i++] = "-c";
- av[i++] = program;
- av[i] = 0;
- execvp (av[0], av);
- sprintf (buf, "%.100s: %.100s", progname, oprogram);
- perror(buf);
- exit(1);
- }
- else
- {
- /* This is the parent fork. */
- state->pipe = fdopen(fd, "r+");
- state->pipe_id =
- XtAppAddInput (app, fileno (state->pipe),
- (XtPointer) (XtInputReadMask | XtInputExceptMask),
- subproc_cb, (XtPointer) state);
- }
- }
- else
-#endif /* HAVE_FORKPTY */
- {
- /* don't mess up controlling terminal if someone dumbly does
- "-pipe -program tcsh". */
- static int protected_stdin_p = 0;
- if (! protected_stdin_p) {
- fclose (stdin);
- open ("/dev/null", O_RDWR); /* re-allocate fd 0 */
- protected_stdin_p = 1;
- }
-
- if ((state->pipe = popen (program, "r")))
- {
- state->pipe_id =
- XtAppAddInput (app, fileno (state->pipe),
- (XtPointer) (XtInputReadMask | XtInputExceptMask),
- subproc_cb, (XtPointer) state);
- }
- else
- {
- sprintf (buf, "%.100s: %.100s", progname, program);
- perror (buf);
- }
- }
-
- free (program);
-}
-
-
-static void
-relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
-{
- p_state *state = (p_state *) closure;
- if (!state->pipe_timer) abort();
- state->pipe_timer = 0;
- launch_text_generator (state);
-}
-
-
-static void
-drain_input (p_state *state)
-{
- XtAppContext app = XtDisplayToApplicationContext (state->dpy);
- if (state->input_available_p && state->pipe)
- {
- unsigned char s[2];
- int n = read (fileno (state->pipe), (void *) s, 1);
- if (n == 1)
- {
- print_char (state, s[0]);
- }
- else
- {
- XtRemoveInput (state->pipe_id);
- state->pipe_id = 0;
- if (state->pid)
- {
- waitpid(state->pid, NULL, 0);
- fclose (state->pipe);
- }
- else
- {
- pclose (state->pipe);
- }
- state->pipe = 0;