Fix x11font_has_glyph so it doesn't get caught out by signed chars.
[u/mdw/putty] / unix / uxpty.c
index 708e82d..a9c0676 100644 (file)
@@ -76,11 +76,11 @@ typedef struct pty_tag *Pty;
 static int pty_signal_pipe[2] = { -1, -1 };   /* obviously bogus initial val */
 
 struct pty_tag {
-    Config cfg;
+    Conf *conf;
     int master_fd, slave_fd;
     void *frontend;
     char name[FILENAME_MAX];
-    int child_pid;
+    pid_t child_pid;
     int term_width, term_height;
     int child_dead, finished;
     int exit_code;
@@ -137,7 +137,7 @@ static int pty_compare_by_pid(void *av, void *bv)
 
 static int pty_find_by_pid(void *av, void *bv)
 {
-    int a = *(int *)av;
+    pid_t a = *(pid_t *)av;
     Pty b = (Pty)bv;
 
     if (a < b->child_pid)
@@ -167,7 +167,8 @@ static tree234 *ptys_by_pid = NULL;
 static Pty single_pty = NULL;
 
 #ifndef OMIT_UTMP
-static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
+static pid_t pty_utmp_helper_pid;
+static int pty_utmp_helper_pipe;
 static int pty_stamped_utmp;
 static struct utmpx utmp_entry;
 #endif
@@ -260,7 +261,8 @@ static void cleanup_utmp(void)
 
 static void sigchld_handler(int signum)
 {
-    write(pty_signal_pipe[1], "x", 1);
+    if (write(pty_signal_pipe[1], "x", 1) <= 0)
+       /* not much we can do about it */;
 }
 
 #ifndef OMIT_UTMP
@@ -360,8 +362,10 @@ static void pty_open_master(Pty pty)
         /*
          * Set the pty master into non-blocking mode.
          */
-        int i = 1;
-        ioctl(pty->master_fd, FIONBIO, &i);
+        int fl;
+       fl = fcntl(pty->master_fd, F_GETFL);
+       if (fl != -1 && !(fl & O_NONBLOCK))
+           fcntl(pty->master_fd, F_SETFL, fl | O_NONBLOCK);
     }
 
     if (!ptys_by_fd)
@@ -584,6 +588,8 @@ int pty_real_select_result(Pty pty, int event, int status)
     }
 
     if (finished && !pty->finished) {
+       int close_on_exit;
+
        uxsel_del(pty->master_fd);
        pty_close(pty);
        pty->master_fd = -1;
@@ -596,8 +602,9 @@ int pty_real_select_result(Pty pty, int event, int status)
         * Only On Clean and it wasn't a clean exit) do we output a
         * `terminated' message.
         */
-       if (pty->cfg.close_on_exit == FORCE_OFF ||
-           (pty->cfg.close_on_exit == AUTO && pty->exit_code != 0)) {
+       close_on_exit = conf_get_int(pty->conf, CONF_close_on_exit);
+       if (close_on_exit == FORCE_OFF ||
+           (close_on_exit == AUTO && pty->exit_code != 0)) {
            char message[512];
            if (WIFEXITED(pty->exit_code))
                sprintf(message, "\r\n[pterm: process terminated with exit"
@@ -627,16 +634,16 @@ int pty_select_result(int fd, int event)
 
     if (fd == pty_signal_pipe[0]) {
        pid_t pid;
-       int ipid;
        int status;
        char c[1];
 
-       read(pty_signal_pipe[0], c, 1); /* ignore its value; it'll be `x' */
+       if (read(pty_signal_pipe[0], c, 1) <= 0)
+           /* ignore error */;
+       /* ignore its value; it'll be `x' */
 
        do {
            pid = waitpid(-1, &status, WNOHANG);
 
-           ipid = pid;
            pty = find234(ptys_by_pid, &pid, pty_find_by_pid);
 
            if (pty)
@@ -677,7 +684,7 @@ static void pty_uxsel_setup(Pty pty)
  * Also places the canonical host name into `realhost'. It must be
  * freed by the caller.
  */
-static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
+static const char *pty_init(void *frontend, void **backend_handle, Conf *conf,
                            char *host, int port, char **realhost, int nodelay,
                            int keepalive)
 {
@@ -701,9 +708,9 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
     pty->frontend = frontend;
     *backend_handle = NULL;           /* we can't sensibly use this, sadly */
 
-    pty->cfg = *cfg;                  /* structure copy */
-    pty->term_width = cfg->width;
-    pty->term_height = cfg->height;
+    pty->conf = conf_copy(conf);
+    pty->term_width = conf_get_int(conf, CONF_width);
+    pty->term_height = conf_get_int(conf, CONF_height);
 
     if (pty->master_fd < 0)
        pty_open_master(pty);
@@ -715,7 +722,8 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
     {
        struct termios attrs;
        tcgetattr(pty->master_fd, &attrs);
-       attrs.c_cc[VERASE] = cfg->bksp_is_delete ? '\177' : '\010';
+       attrs.c_cc[VERASE] = conf_get_int(conf, CONF_bksp_is_delete)
+           ? '\177' : '\010';
        tcsetattr(pty->master_fd, TCSANOW, &attrs);
     }
 
@@ -724,7 +732,7 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
      * Stamp utmp (that is, tell the utmp helper process to do so),
      * or not.
      */
-    if (!cfg->stamp_utmp) {
+    if (!conf_get_int(conf, CONF_stamp_utmp)) {
        close(pty_utmp_helper_pipe);   /* just let the child process die */
        pty_utmp_helper_pipe = -1;
     } else {
@@ -783,7 +791,8 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
        close(open(pty->name, O_WRONLY, 0));
        setpgid(pgrp, pgrp);
        {
-           char *term_env_var = dupprintf("TERM=%s", cfg->termtype);
+           char *term_env_var = dupprintf("TERM=%s",
+                                          conf_get_str(conf, CONF_termtype));
            putenv(term_env_var);
            /* We mustn't free term_env_var, as putenv links it into the
             * environment in place.
@@ -799,18 +808,12 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
        }
 #endif
        {
-           char *e = cfg->environmt;
-           char *var, *varend, *val, *varval;
-           while (*e) {
-               var = e;
-               while (*e && *e != '\t') e++;
-               varend = e;
-               if (*e == '\t') e++;
-               val = e;
-               while (*e) e++;
-               e++;
-
-               varval = dupprintf("%.*s=%s", varend-var, var, val);
+           char *key, *val;
+
+           for (val = conf_get_str_strs(conf, CONF_environmt, NULL, &key);
+                val != NULL;
+                val = conf_get_str_strs(conf, CONF_environmt, key, &key)) {
+               char *varval = dupcat(key, "=", val, NULL);
                putenv(varval);
                /*
                 * We must not free varval, since putenv links it
@@ -822,21 +825,22 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
        }
 
        /*
-        * SIGINT and SIGQUIT may have been set to ignored by our
-        * parent, particularly by things like sh -c 'pterm &' and
-        * some window managers. SIGCHLD, meanwhile, was blocked
-        * during pt_main() startup. Reverse all this for our child
-        * process.
+        * SIGINT, SIGQUIT and SIGPIPE may have been set to ignored by
+        * our parent, particularly by things like sh -c 'pterm &' and
+        * some window or session managers. SIGCHLD, meanwhile, was
+        * blocked during pt_main() startup. Reverse all this for our
+        * child process.
         */
        putty_signal(SIGINT, SIG_DFL);
        putty_signal(SIGQUIT, SIG_DFL);
+       putty_signal(SIGPIPE, SIG_DFL);
        block_signal(SIGCHLD, 0);
        if (pty_argv)
            execvp(pty_argv[0], pty_argv);
        else {
            char *shell = getenv("SHELL");
            char *shellname;
-           if (cfg->login_shell) {
+           if (conf_get_int(conf, CONF_login_shell)) {
                char *p = strrchr(shell, '/');
                shellname = snewn(2+strlen(shell), char);
                p = p ? p+1 : shell;
@@ -879,7 +883,7 @@ static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
     return NULL;
 }
 
-static void pty_reconfig(void *handle, Config *cfg)
+static void pty_reconfig(void *handle, Conf *conf)
 {
     Pty pty = (Pty)handle;
     /*
@@ -887,7 +891,7 @@ static void pty_reconfig(void *handle, Config *cfg)
      * unfortunately we do need to pick up the setting of Close On
      * Exit so we know whether to give a `terminated' message.
      */
-    pty->cfg = *cfg;                  /* structure copy */
+    conf_copy_into(pty->conf, conf);
 }
 
 /*
@@ -1085,5 +1089,7 @@ Backend pty_backend = {
     pty_provide_logctx,
     pty_unthrottle,
     pty_cfg_info,
-    1
+    "pty",
+    -1,
+    0
 };