added linesize argument to readline()
[unix-history] / usr / src / usr.bin / window / wwenviron.c
index adab561..8dc4956 100644 (file)
@@ -1,68 +1,66 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)wwenviron.c 3.3 83/08/31";
-#endif
+static char sccsid[] = "@(#)wwenviron.c        3.20 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "ww.h"
 
 #include "ww.h"
-
-extern char **environ;
+#include <sys/signal.h>
 
 /*
  * Set up the environment of this process to run in window 'wp'.
 
 /*
  * Set up the environment of this process to run in window 'wp'.
- * Can't report errors in any intelligent way, so don't.
  */
 wwenviron(wp)
 register struct ww *wp;
 {
        register i;
  */
 wwenviron(wp)
 register struct ww *wp;
 {
        register i;
-       register char **p, **q;
-       char **termcap = 0;
-       char **env;
-       char *tbuf;
+       int pgrp = getpid();
+       char buf[1024];
 
 
-       (void) dup2(wp->ww_tty, 0);
-       (void) dup2(wp->ww_tty, 1);
-       (void) dup2(wp->ww_tty, 2);
+       if ((i = open("/dev/tty", 0)) < 0)
+               goto bad;
+       if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
+               goto bad;
+       (void) close(i);
+       if ((i = wp->ww_socket) < 0 && (i = open(wp->ww_ttyname, 2)) < 0)
+               goto bad;
+       (void) dup2(i, 0);
+       (void) dup2(i, 1);
+       (void) dup2(i, 2);
        for (i = wwdtablesize - 1; i > 2; i--)
                (void) close(i);
        for (i = wwdtablesize - 1; i > 2; i--)
                (void) close(i);
-
-       i = open("/dev/tty", 0);
-       if (i < 0) {
-               perror("/dev/tty");
-               return;
-       }
-       if (ioctl(i, (int)TIOCNOTTY, (char *)0) < 0) {
-               perror("ioctl(TIOCNOTTY)");
-               return;
-       }
-       (void) close(i);
-       if (open(wp->ww_ttyname, 0) < 0) {
-               perror(wp->ww_ttyname);
-               return;
-       }
-
-       for (i = 0, p = environ; *p; p++, i++)
-               ;
-       if ((env = (char **)malloc((unsigned)(i + 3) * sizeof (char *))) == 0)
-               return;
-       if ((tbuf = malloc((unsigned) 1024)) == 0)
-               return;
-       for (p = environ, q = env; *p; p++, q++) {
-               if (strncmp(*p, "TERM=", 5) == 0)
-                       *q = WWT_TERM;
-               else if (strncmp(*p, "TERMCAP=", 8) == 0)
-                       termcap = q;
-               else
-                       *q = *p;
-       }
-       if (termcap == 0)
-               termcap = q++;
-       *q = 0;
-       *termcap = sprintf(tbuf, "TERMCAP=%sco#%d:li#%d:",
-               WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr);
-       if (wwavailmodes & WWM_REV)
-               (void) strcat(tbuf, WWT_REV);
-       if (wwavailmodes & WWM_UL)
-               (void) strcat(tbuf, WWT_UL);
-       (void) strcat(tbuf, wwkeys);
-       environ = env;
+       (void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
+       (void) setpgrp(pgrp, pgrp);
+       /* SIGPIPE is the only one we ignore */
+       (void) signal(SIGPIPE, SIG_DFL);
+       (void) sigsetmask(0);
+       /*
+        * Two conditions that make destructive setenv ok:
+        * 1. setenv() copies the string,
+        * 2. we've already called tgetent which copies the termcap entry.
+        */
+       (void) sprintf(buf, "%sco#%d:li#%d:%s",
+               WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
+       (void) setenv("TERMCAP", buf, 1);
+       (void) sprintf(buf, "%d", wp->ww_id + 1);
+       (void) setenv("WINDOW_ID", buf, 1);
+       return 0;
+bad:
+       wwerrno = WWE_SYS;
+       return -1;
 }
 }