Use BSET for write access to Lisp_Object members of struct buffer.
[bpt/emacs.git] / src / process.c
index fa16c17..a43655e 100644 (file)
@@ -173,9 +173,9 @@ extern Lisp_Object QCfamily;
 extern Lisp_Object QCfilter;
 
 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
-#define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
+#define NETCONN1_P(p) (EQ (p->type, Qnetwork))
 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
-#define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
+#define SERIALCONN1_P(p) (EQ (p->type, Qserial))
 
 #ifndef HAVE_H_ERRNO
 extern int h_errno;
@@ -249,7 +249,6 @@ static int process_output_skip;
 #define process_output_delay_count 0
 #endif
 
-static Lisp_Object Fget_process (Lisp_Object);
 static void create_process (Lisp_Object, char **, Lisp_Object);
 #ifdef SIGIO
 static int keyboard_bit_set (SELECT_TYPE *);
@@ -429,7 +428,7 @@ static void
 update_status (struct Lisp_Process *p)
 {
   eassert (p->raw_status_new);
-  p->status = status_convert (p->raw_status);
+  PSET (p, status, status_convert (p->raw_status));
   p->raw_status_new = 0;
 }
 
@@ -442,7 +441,7 @@ status_convert (int w)
   if (WIFSTOPPED (w))
     return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
   else if (WIFEXITED (w))
-    return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
+    return Fcons (Qexit, Fcons (make_number (WEXITSTATUS (w)),
                                WCOREDUMP (w) ? Qt : Qnil));
   else if (WIFSIGNALED (w))
     return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
@@ -498,7 +497,7 @@ status_message (struct Lisp_Process *p)
        {
          int c1, c2;
 
-         string = make_unibyte_string (signame, strlen (signame));
+         string = build_unibyte_string (signame);
          if (! NILP (Vlocale_coding_system))
            string = (code_convert_string_norecord
                      (string, Vlocale_coding_system, 0));
@@ -627,8 +626,8 @@ make_process (Lisp_Object name)
   p = allocate_process ();
   /* Initialize Lisp data.  Note that allocate_process initializes all
      Lisp data to nil, so do it only for slots which should not be nil.  */
-  p->status = Qrun;
-  p->mark = Fmake_marker ();
+  PSET (p, status, Qrun);
+  PSET (p, mark, Fmake_marker ());
 
   /* Initialize non-Lisp data.  Note that allocate_process zeroes out all
      non-Lisp data, so do it only for slots which should not be zero.  */
@@ -646,11 +645,10 @@ make_process (Lisp_Object name)
     {
       tem = Fget_process (name1);
       if (NILP (tem)) break;
-      sprintf (suffix, "<%"pMd">", i);
-      name1 = concat2 (name, build_string (suffix));
+      name1 = concat2 (name, make_formatted_string (suffix, "<%"pMd">", i));
     }
   name = name1;
-  p->name = name;
+  PSET (p, name, name);
   XSETPROCESS (val, p);
   Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
   return val;
@@ -747,7 +745,7 @@ nil, indicating the current buffer's process.  */)
   p->raw_status_new = 0;
   if (NETCONN1_P (p) || SERIALCONN1_P (p))
     {
-      p->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
+      PSET (p, status, Fcons (Qexit, Fcons (make_number (0), Qnil)));
       p->tick = ++process_tick;
       status_notify (p);
       redisplay_preserve_echo_area (13);
@@ -776,8 +774,7 @@ nil, indicating the current buffer's process.  */)
        {
          Fkill_process (process, Qnil);
          /* Do this now, since remove_process will make sigchld_handler do nothing.  */
-         p->status
-           = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
+         PSET (p, status, Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil)));
          p->tick = ++process_tick;
          status_notify (p);
          redisplay_preserve_echo_area (13);
@@ -904,9 +901,9 @@ Return BUFFER.  */)
   if (!NILP (buffer))
     CHECK_BUFFER (buffer);
   p = XPROCESS (process);
-  p->buffer = buffer;
+  PSET (p, buffer, buffer);
   if (NETCONN1_P (p) || SERIALCONN1_P (p))
-    p->childp = Fplist_put (p->childp, QCbuffer, buffer);
+    PSET (p, childp, Fplist_put (p->childp, QCbuffer, buffer));
   setup_process_coding_systems (process);
   return buffer;
 }
@@ -977,9 +974,9 @@ The string argument is normally a multibyte string, except:
        }
     }
 
-  p->filter = filter;
+  PSET (p, filter, filter);
   if (NETCONN1_P (p) || SERIALCONN1_P (p))
-    p->childp = Fplist_put (p->childp, QCfilter, filter);
+    PSET (p, childp, Fplist_put (p->childp, QCfilter, filter));
   setup_process_coding_systems (process);
   return filter;
 }
@@ -1006,9 +1003,9 @@ It gets two arguments: the process, and a string describing the change.  */)
   CHECK_PROCESS (process);
   p = XPROCESS (process);
 
-  p->sentinel = sentinel;
+  PSET (p, sentinel, sentinel);
   if (NETCONN1_P (p) || SERIALCONN1_P (p))
-    p->childp = Fplist_put (p->childp, QCsentinel, sentinel);
+    PSET (p, childp, Fplist_put (p->childp, QCsentinel, sentinel));
   return sentinel;
 }
 
@@ -1089,10 +1086,6 @@ DEFUN ("process-query-on-exit-flag",
   return (XPROCESS (process)->kill_without_query ? Qnil : Qt);
 }
 
-#ifdef DATAGRAM_SOCKETS
-static Lisp_Object Fprocess_datagram_address (Lisp_Object);
-#endif
-
 DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
        1, 2, 0,
        doc: /* Return the contact info of PROCESS; t for a real child.
@@ -1145,7 +1138,7 @@ DEFUN ("set-process-plist", Fset_process_plist, Sset_process_plist,
   CHECK_PROCESS (process);
   CHECK_LIST (plist);
 
-  XPROCESS (process)->plist = plist;
+  PSET (XPROCESS (process), plist, plist);
   return plist;
 }
 
@@ -1331,18 +1324,18 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
      itself; it's all taken care of here.  */
   record_unwind_protect (start_process_unwind, proc);
 
-  XPROCESS (proc)->childp = Qt;
-  XPROCESS (proc)->plist = Qnil;
-  XPROCESS (proc)->type = Qreal;
-  XPROCESS (proc)->buffer = buffer;
-  XPROCESS (proc)->sentinel = Qnil;
-  XPROCESS (proc)->filter = Qnil;
-  XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
+  PSET (XPROCESS (proc), childp, Qt);
+  PSET (XPROCESS (proc), plist, Qnil);
+  PSET (XPROCESS (proc), type, Qreal);
+  PSET (XPROCESS (proc), buffer, buffer);
+  PSET (XPROCESS (proc), sentinel, Qnil);
+  PSET (XPROCESS (proc), filter, Qnil);
+  PSET (XPROCESS (proc), command, Flist (nargs - 2, args + 2));
 
 #ifdef HAVE_GNUTLS
   /* AKA GNUTLS_INITSTAGE(proc).  */
   XPROCESS (proc)->gnutls_initstage = GNUTLS_STAGE_EMPTY;
-  XPROCESS (proc)->gnutls_cred_type = Qnil;
+  PSET (XPROCESS (proc), gnutls_cred_type, Qnil);
 #endif
 
 #ifdef ADAPTIVE_READ_BUFFERING
@@ -1370,7 +1363,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
     val = Vcoding_system_for_read;
     if (NILP (val))
       {
-       args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
+       args2 = alloca ((nargs + 1) * sizeof *args2);
        args2[0] = Qstart_process;
        for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
        GCPRO2 (proc, current_dir);
@@ -1382,14 +1375,14 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
        else if (CONSP (Vdefault_process_coding_system))
          val = XCAR (Vdefault_process_coding_system);
       }
-    XPROCESS (proc)->decode_coding_system = val;
+    PSET (XPROCESS (proc), decode_coding_system, val);
 
     val = Vcoding_system_for_write;
     if (NILP (val))
       {
        if (EQ (coding_systems, Qt))
          {
-           args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
+           args2 = alloca ((nargs + 1) * sizeof *args2);
            args2[0] = Qstart_process;
            for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
            GCPRO2 (proc, current_dir);
@@ -1402,7 +1395,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
        else if (CONSP (Vdefault_process_coding_system))
          val = XCDR (Vdefault_process_coding_system);
       }
-    XPROCESS (proc)->encode_coding_system = val;
+    PSET (XPROCESS (proc), encode_coding_system, val);
     /* Note: At this moment, the above coding system may leave
        text-conversion or eol-conversion unspecified.  They will be
        decided after we read output from the process and decode it by
@@ -1411,9 +1404,9 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
   }
 
 
-  XPROCESS (proc)->decoding_buf = empty_unibyte_string;
+  PSET (XPROCESS (proc), decoding_buf, empty_unibyte_string);
   XPROCESS (proc)->decoding_carryover = 0;
-  XPROCESS (proc)->encoding_buf = empty_unibyte_string;
+  PSET (XPROCESS (proc), encoding_buf, empty_unibyte_string);
 
   XPROCESS (proc)->inherit_coding_system_flag
     = !(NILP (buffer) || !inherit_process_coding_system);
@@ -1483,7 +1476,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
 
       /* Now that everything is encoded we can collect the strings into
         NEW_ARGV.  */
-      new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
+      new_argv = alloca ((nargs - 1) * sizeof *new_argv);
       new_argv[nargs - 2] = 0;
 
       for (i = nargs - 2; i-- != 0; )
@@ -1635,7 +1628,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
      more portable (see USG_SUBTTY_WORKS above).  */
 
   XPROCESS (process)->pty_flag = pty_flag;
-  XPROCESS (process)->status = Qrun;
+  PSET (XPROCESS (process), status, Qrun);
 
   /* Delay interrupts until we have a chance to store
      the new fork's pid in its process structure */
@@ -1857,10 +1850,9 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
         So have an interrupt jar it loose.  */
       {
        struct atimer *timer;
-       EMACS_TIME offset;
+       EMACS_TIME offset = make_emacs_time (1, 0);
 
        stop_polling ();
-       EMACS_SET_SECS_USECS (offset, 1, 0);
        timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
 
        if (forkin >= 0)
@@ -1875,10 +1867,10 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
 
 #ifdef HAVE_PTYS
       if (pty_flag)
-       XPROCESS (process)->tty_name = build_string (pty_name);
+       PSET (XPROCESS (process), tty_name, build_string (pty_name));
       else
 #endif
-       XPROCESS (process)->tty_name = Qnil;
+       PSET (XPROCESS (process), tty_name, Qnil);
 
 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
       /* Wait for child_setup to complete in case that vfork is
@@ -1973,7 +1965,7 @@ create_pty (Lisp_Object process)
      more portable (see USG_SUBTTY_WORKS above).  */
 
   XPROCESS (process)->pty_flag = pty_flag;
-  XPROCESS (process)->status = Qrun;
+  PSET (XPROCESS (process), status, Qrun);
   setup_process_coding_systems (process);
 
   FD_SET (inchannel, &input_wait_mask);
@@ -1984,10 +1976,10 @@ create_pty (Lisp_Object process)
   XPROCESS (process)->pid = -2;
 #ifdef HAVE_PTYS
   if (pty_flag)
-    XPROCESS (process)->tty_name = build_string (pty_name);
+    PSET (XPROCESS (process), tty_name, build_string (pty_name));
   else
 #endif
-    XPROCESS (process)->tty_name = Qnil;
+    PSET (XPROCESS (process), tty_name, Qnil);
 }
 
 \f
@@ -2376,7 +2368,7 @@ OPTION is not a supported option, return nil instead; otherwise return t.  */)
 
   if (set_socket_option (s, option, value))
     {
-      p->childp = Fplist_put (p->childp, option, value);
+      PSET (p, childp, Fplist_put (p->childp, option, value));
       return Qt;
     }
 
@@ -2609,18 +2601,18 @@ usage:  (make-serial-process &rest ARGS)  */)
   if (NILP (buffer))
     buffer = name;
   buffer = Fget_buffer_create (buffer);
-  p->buffer = buffer;
-
-  p->childp = contact;
-  p->plist = Fcopy_sequence (Fplist_get (contact, QCplist));
-  p->type = Qserial;
-  p->sentinel = Fplist_get (contact, QCsentinel);
-  p->filter = Fplist_get (contact, QCfilter);
-  p->log = Qnil;
+  PSET (p, buffer, buffer);
+
+  PSET (p, childp, contact);
+  PSET (p, plist, Fcopy_sequence (Fplist_get (contact, QCplist)));
+  PSET (p, type, Qserial);
+  PSET (p, sentinel, Fplist_get (contact, QCsentinel));
+  PSET (p, filter, Fplist_get (contact, QCfilter));
+  PSET (p, log, Qnil);
   if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
     p->kill_without_query = 1;
   if (tem = Fplist_get (contact, QCstop), !NILP (tem))
-    p->command = Qt;
+    PSET (p, command, Qt);
   p->pty_flag = 0;
 
   if (!EQ (p->command, Qt))
@@ -2652,7 +2644,7 @@ usage:  (make-serial-process &rest ARGS)  */)
   else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
           || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
     val = Qnil;
-  p->decode_coding_system = val;
+  PSET (p, decode_coding_system, val);
 
   val = Qnil;
   if (!NILP (tem))
@@ -2666,12 +2658,12 @@ usage:  (make-serial-process &rest ARGS)  */)
   else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
           || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
     val = Qnil;
-  p->encode_coding_system = val;
+  PSET (p, encode_coding_system, val);
 
   setup_process_coding_systems (proc);
-  p->decoding_buf = empty_unibyte_string;
+  PSET (p, decoding_buf, empty_unibyte_string);
   p->decoding_carryover = 0;
-  p->encoding_buf = empty_unibyte_string;
+  PSET (p, encoding_buf, empty_unibyte_string);
   p->inherit_coding_system_flag
     = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
 
@@ -3019,7 +3011,9 @@ usage: (make-network-process &rest ARGS)  */)
       CHECK_STRING (service);
       memset (&address_un, 0, sizeof address_un);
       address_un.sun_family = AF_LOCAL;
-      strncpy (address_un.sun_path, SSDATA (service), sizeof address_un.sun_path);
+      if (sizeof address_un.sun_path <= SBYTES (service))
+       error ("Service name too long");
+      strcpy (address_un.sun_path, SSDATA (service));
       ai.ai_addr = (struct sockaddr *) &address_un;
       ai.ai_addrlen = sizeof address_un;
       goto open_socket;
@@ -3321,7 +3315,7 @@ usage: (make-network-process &rest ARGS)  */)
        {
          if (datagram_address[s].sa)
            abort ();
-         datagram_address[s].sa = (struct sockaddr *) xmalloc (lres->ai_addrlen);
+         datagram_address[s].sa = xmalloc (lres->ai_addrlen);
          datagram_address[s].len = lres->ai_addrlen;
          if (is_server)
            {
@@ -3406,23 +3400,23 @@ usage: (make-network-process &rest ARGS)  */)
 
   p = XPROCESS (proc);
 
-  p->childp = contact;
-  p->plist = Fcopy_sequence (Fplist_get (contact, QCplist));
-  p->type = Qnetwork;
+  PSET (p, childp, contact);
+  PSET (p, plist, Fcopy_sequence (Fplist_get (contact, QCplist)));
+  PSET (p, type, Qnetwork);
 
-  p->buffer = buffer;
-  p->sentinel = sentinel;
-  p->filter = filter;
-  p->log = Fplist_get (contact, QClog);
+  PSET (p, buffer, buffer);
+  PSET (p, sentinel, sentinel);
+  PSET (p, filter, filter);
+  PSET (p, log, Fplist_get (contact, QClog));
   if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
     p->kill_without_query = 1;
   if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
-    p->command = Qt;
+    PSET (p, command, Qt);
   p->pid = 0;
   p->infd  = inch;
   p->outfd = outch;
   if (is_server && socktype != SOCK_DGRAM)
-    p->status = Qlisten;
+    PSET (p, status, Qlisten);
 
   /* Make the process marker point into the process buffer (if any).  */
   if (BUFFERP (buffer))
@@ -3436,7 +3430,7 @@ usage: (make-network-process &rest ARGS)  */)
       /* We may get here if connect did succeed immediately.  However,
         in that case, we still need to signal this like a non-blocking
         connection.  */
-      p->status = Qconnect;
+      PSET (p, status, Qconnect);
       if (!FD_ISSET (inch, &connect_wait_mask))
        {
          FD_SET (inch, &connect_wait_mask);
@@ -3503,7 +3497,7 @@ usage: (make-network-process &rest ARGS)  */)
        else
          val = Qnil;
       }
-    p->decode_coding_system = val;
+    PSET (p, decode_coding_system, val);
 
     if (!NILP (tem))
       {
@@ -3537,13 +3531,13 @@ usage: (make-network-process &rest ARGS)  */)
        else
          val = Qnil;
       }
-    p->encode_coding_system = val;
+    PSET (p, encode_coding_system, val);
   }
   setup_process_coding_systems (proc);
 
-  p->decoding_buf = empty_unibyte_string;
+  PSET (p, decoding_buf, empty_unibyte_string);
   p->decoding_carryover = 0;
-  p->encoding_buf = empty_unibyte_string;
+  PSET (p, encoding_buf, empty_unibyte_string);
 
   p->inherit_coding_system_flag
     = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
@@ -3723,8 +3717,9 @@ FLAGS is the current flags of the interface.  */)
 
   CHECK_STRING (ifname);
 
-  memset (rq.ifr_name, 0, sizeof rq.ifr_name);
-  strncpy (rq.ifr_name, SSDATA (ifname), sizeof (rq.ifr_name));
+  if (sizeof rq.ifr_name <= SBYTES (ifname))
+    error ("interface name too long");
+  strcpy (rq.ifr_name, SSDATA (ifname));
 
   s = socket (AF_INET, SOCK_STREAM, 0);
   if (s < 0)
@@ -4155,18 +4150,18 @@ server_accept_connection (Lisp_Object server, int channel)
                          conv_sockaddr_to_lisp (&saddr.sa, len));
 #endif
 
-  p->childp = contact;
-  p->plist = Fcopy_sequence (ps->plist);
-  p->type = Qnetwork;
+  PSET (p, childp, contact);
+  PSET (p, plist, Fcopy_sequence (ps->plist));
+  PSET (p, type, Qnetwork);
 
-  p->buffer = buffer;
-  p->sentinel = ps->sentinel;
-  p->filter = ps->filter;
-  p->command = Qnil;
+  PSET (p, buffer, buffer);
+  PSET (p, sentinel, ps->sentinel);
+  PSET (p, filter, ps->filter);
+  PSET (p, command, Qnil);
   p->pid = 0;
   p->infd  = s;
   p->outfd = s;
-  p->status = Qrun;
+  PSET (p, status, Qrun);
 
   /* Client processes for accepted connections are not stopped initially.  */
   if (!EQ (p->filter, Qt))
@@ -4183,13 +4178,13 @@ server_accept_connection (Lisp_Object server, int channel)
      of the new process should reflect the settings at the time the
      server socket was opened; not the current settings. */
 
-  p->decode_coding_system = ps->decode_coding_system;
-  p->encode_coding_system = ps->encode_coding_system;
+  PSET (p, decode_coding_system, ps->decode_coding_system);
+  PSET (p, encode_coding_system, ps->encode_coding_system);
   setup_process_coding_systems (proc);
 
-  p->decoding_buf = empty_unibyte_string;
+  PSET (p, decoding_buf, empty_unibyte_string);
   p->decoding_carryover = 0;
-  p->encoding_buf = empty_unibyte_string;
+  PSET (p, encoding_buf, empty_unibyte_string);
 
   p->inherit_coding_system_flag
     = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag);
@@ -4245,7 +4240,7 @@ wait_reading_process_output_1 (void)
        If NSECS > 0, the timeout consists of NSECS only.
        If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
 
-   READ_KBD is a lisp value:
+   READ_KBD is:
      0 to ignore keyboard input, or
      1 to return when input is available, or
      -1 meaning caller will actually read the input, so don't throw to
@@ -4291,7 +4286,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
   FD_ZERO (&Writeok);
 
   if (time_limit == 0 && nsecs == 0 && wait_proc && !NILP (Vinhibit_quit)
-      && !(CONSP (wait_proc->status) && EQ (XCAR (wait_proc->status), Qexit)))
+      && !(CONSP (wait_proc->status)
+          && EQ (XCAR (wait_proc->status), Qexit)))
     message ("Blocking call to accept-process-output with quit inhibited!!");
 
   /* If wait_proc is a process to watch, set wait_channel accordingly.  */
@@ -4312,11 +4308,10 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
 
   /* Since we may need to wait several times,
      compute the absolute time to return at.  */
-  if (time_limit || nsecs) /* FIXME neither should be negative, no? */
+  if (time_limit || 0 < nsecs)
     {
-      EMACS_GET_TIME (end_time);
-      EMACS_SET_SECS_NSECS (timeout, time_limit, nsecs);
-      EMACS_ADD_TIME (end_time, end_time, timeout);
+      timeout = make_emacs_time (time_limit, nsecs);
+      end_time = add_emacs_time (current_emacs_time (), timeout);
     }
 
   while (1)
@@ -4341,22 +4336,22 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
       /* Exit if already run out */
       if (nsecs < 0)
        {
-         /* -1 specified for timeout means
+         /* A negative timeout means
             gobble output available now
             but don't wait at all. */
 
-         EMACS_SET_SECS_USECS (timeout, 0, 0);
+         timeout = make_emacs_time (0, 0);
        }
-      else if (time_limit || nsecs)
+      else if (time_limit || 0 < nsecs)
        {
-         EMACS_GET_TIME (timeout);
-         if (EMACS_TIME_LE (end_time, timeout))
+         EMACS_TIME now = current_emacs_time ();
+         if (EMACS_TIME_LE (end_time, now))
            break;
-         EMACS_SUB_TIME (timeout, end_time, timeout);
+         timeout = sub_emacs_time (end_time, now);
        }
       else
        {
-         EMACS_SET_SECS_USECS (timeout, 100000, 0);
+         timeout = make_emacs_time (100000, 0);
        }
 
       /* Normally we run timers here.
@@ -4398,7 +4393,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
              && requeued_events_pending_p ())
            break;
 
-         /* If time_limit is negative, we are not going to wait at all.  */
+         /* A negative timeout means do not wait at all.  */
          if (0 <= nsecs)
            {
              if (EMACS_TIME_VALID_P (timer_delay))
@@ -4441,7 +4436,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
             Atemp = input_wait_mask;
          Ctemp = write_mask;
 
-         EMACS_SET_SECS_USECS (timeout, 0, 0);
+         timeout = make_emacs_time (0, 0);
          if ((pselect (max (max_process_desc, max_input_desc) + 1,
                        &Atemp,
 #ifdef NON_BLOCKING_CONNECT
@@ -4593,7 +4588,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
                        nsecs = XPROCESS (proc)->read_output_delay;
                    }
                }
-             EMACS_SET_SECS_NSECS (timeout, 0, nsecs);
+             timeout = make_emacs_time (0, nsecs);
              process_output_skip = 0;
            }
 #endif
@@ -4666,7 +4661,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
       do_pending_window_change (0);
 
       if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers)
-       /* We wanted the full specified time, so return now.  */
+       /* We waited the full specified time, so return now.  */
        break;
       if (nfds < 0)
        {
@@ -4891,10 +4886,10 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
                  if (p->pid == -2)
                    {
                      /* If the EIO occurs on a pty, sigchld_handler's
-                        wait3() will not find the process object to
+                        waitpid() will not find the process object to
                         delete.  Do it here.  */
                      p->tick = ++process_tick;
-                     p->status = Qfailed;
+                     PSET (p, status, Qfailed);
                    }
                   else
                    kill (getpid (), SIGCHLD);
@@ -4914,8 +4909,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
                  if (XPROCESS (proc)->raw_status_new)
                    update_status (XPROCESS (proc));
                  if (EQ (XPROCESS (proc)->status, Qrun))
-                   XPROCESS (proc)->status
-                     = Fcons (Qexit, Fcons (make_number (256), Qnil));
+                   PSET (XPROCESS (proc), status,
+                         Fcons (Qexit, Fcons (make_number (256), Qnil)));
                }
            }
 #ifdef NON_BLOCKING_CONNECT
@@ -4963,12 +4958,13 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
              if (xerrno)
                {
                  p->tick = ++process_tick;
-                 p->status = Fcons (Qfailed, Fcons (make_number (xerrno), Qnil));
+                 PSET (p, status,
+                       Fcons (Qfailed, Fcons (make_number (xerrno), Qnil)));
                  deactivate_process (proc);
                }
              else
                {
-                 p->status = Qrun;
+                 PSET (p, status, Qrun);
                  /* Execute the sentinel here.  If we had relied on
                     status_notify to do it later, it will read input
                     from the process before calling the sentinel.  */
@@ -5042,7 +5038,7 @@ read_process_output (Lisp_Object proc, register int channel)
   ptrdiff_t count = SPECPDL_INDEX ();
   Lisp_Object odeactivate;
 
-  chars = (char *) alloca (carryover + readmax);
+  chars = alloca (carryover + readmax);
   if (carryover)
     /* See the comment above.  */
     memcpy (chars, SDATA (p->decoding_buf), carryover);
@@ -5168,7 +5164,7 @@ read_process_output (Lisp_Object proc, register int channel)
       /* A new coding system might be found.  */
       if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
        {
-         p->decode_coding_system = Vlast_coding_system_used;
+         PSET (p, decode_coding_system, Vlast_coding_system_used);
 
          /* Don't call setup_coding_system for
             proc_decode_coding_system[channel] here.  It is done in
@@ -5184,8 +5180,8 @@ read_process_output (Lisp_Object proc, register int channel)
          if (NILP (p->encode_coding_system)
              && proc_encode_coding_system[p->outfd])
            {
-             p->encode_coding_system
-               = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
+             PSET (p, encode_coding_system,
+                   coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
              setup_coding_system (p->encode_coding_system,
                                   proc_encode_coding_system[p->outfd]);
            }
@@ -5194,7 +5190,7 @@ read_process_output (Lisp_Object proc, register int channel)
       if (coding->carryover_bytes > 0)
        {
          if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
-           p->decoding_buf = make_uninit_string (coding->carryover_bytes);
+           PSET (p, decoding_buf, make_uninit_string (coding->carryover_bytes));
          memcpy (SDATA (p->decoding_buf), coding->carryover,
                  coding->carryover_bytes);
          p->decoding_carryover = coding->carryover_bytes;
@@ -5250,14 +5246,16 @@ read_process_output (Lisp_Object proc, register int channel)
       old_begv_byte = BEGV_BYTE;
       old_zv_byte = ZV_BYTE;
 
-      BVAR (current_buffer, read_only) = Qnil;
+      BSET (current_buffer, read_only, Qnil);
 
       /* Insert new output into buffer
         at the current end-of-output marker,
         thus preserving logical ordering of input and output.  */
       if (XMARKER (p->mark)->buffer)
-       SET_PT_BOTH (clip_to_bounds (BEGV, marker_position (p->mark), ZV),
-                    clip_to_bounds (BEGV_BYTE, marker_byte_position (p->mark),
+       SET_PT_BOTH (clip_to_bounds (BEGV,
+                                    marker_position (p->mark), ZV),
+                    clip_to_bounds (BEGV_BYTE,
+                                    marker_byte_position (p->mark),
                                     ZV_BYTE));
       else
        SET_PT_BOTH (ZV, ZV_BYTE);
@@ -5276,12 +5274,12 @@ read_process_output (Lisp_Object proc, register int channel)
         similar code in the previous `if' block.  */
       if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
        {
-         p->decode_coding_system = Vlast_coding_system_used;
+         PSET (p, decode_coding_system, Vlast_coding_system_used);
          if (NILP (p->encode_coding_system)
              && proc_encode_coding_system[p->outfd])
            {
-             p->encode_coding_system
-               = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
+             PSET (p, encode_coding_system,
+                   coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
              setup_coding_system (p->encode_coding_system,
                                   proc_encode_coding_system[p->outfd]);
            }
@@ -5289,7 +5287,7 @@ read_process_output (Lisp_Object proc, register int channel)
       if (coding->carryover_bytes > 0)
        {
          if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
-           p->decoding_buf = make_uninit_string (coding->carryover_bytes);
+           PSET (p, decoding_buf, make_uninit_string (coding->carryover_bytes));
          memcpy (SDATA (p->decoding_buf), coding->carryover,
                  coding->carryover_bytes);
          p->decoding_carryover = coding->carryover_bytes;
@@ -5339,7 +5337,7 @@ read_process_output (Lisp_Object proc, register int channel)
        Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
 
 
-      BVAR (current_buffer, read_only) = old_read_only;
+      BSET (current_buffer, read_only, old_read_only);
       SET_PT_BOTH (opoint, opoint_byte);
     }
   /* Handling the process output should not deactivate the mark.  */
@@ -5406,9 +5404,9 @@ write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj,
   entry = Fcons (obj, Fcons (make_number (offset), make_number (len)));
 
   if (front)
-    p->write_queue = Fcons (entry, p->write_queue);
+    PSET (p, write_queue, Fcons (entry, p->write_queue));
   else
-    p->write_queue = nconc2 (p->write_queue, Fcons (entry, Qnil));
+    PSET (p, write_queue, nconc2 (p->write_queue, Fcons (entry, Qnil)));
 }
 
 /* Remove the first element in the write_queue of process P, put its
@@ -5426,7 +5424,7 @@ write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj,
     return 0;
 
   entry = XCAR (p->write_queue);
-  p->write_queue = XCDR (p->write_queue);
+  PSET (p, write_queue, XCDR (p->write_queue));
 
   *obj = XCAR (entry);
   offset_length = XCDR (entry);
@@ -5473,8 +5471,8 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
          && !NILP (BVAR (XBUFFER (object), enable_multibyte_characters)))
       || EQ (object, Qt))
     {
-      p->encode_coding_system
-       = complement_process_encoding_system (p->encode_coding_system);
+      PSET (p, encode_coding_system,
+           complement_process_encoding_system (p->encode_coding_system));
       if (!EQ (Vlast_coding_system_used, p->encode_coding_system))
        {
          /* The coding system for encoding was changed to raw-text
@@ -5695,7 +5693,7 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
       proc = process_sent_to;
       p = XPROCESS (proc);
       p->raw_status_new = 0;
-      p->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
+      PSET (p, status, Fcons (Qexit, Fcons (make_number (256), Qnil)));
       p->tick = ++process_tick;
       deactivate_process (proc);
       error ("SIGPIPE raised on process %s; closed it", SDATA (p->name));
@@ -5924,7 +5922,7 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
 #ifdef SIGCONT
     case SIGCONT:
       p->raw_status_new = 0;
-      p->status = Qrun;
+      PSET (p, status, Qrun);
       p->tick = ++process_tick;
       if (!nomsg)
        {
@@ -6020,7 +6018,7 @@ traffic.  */)
          FD_CLR (p->infd, &input_wait_mask);
          FD_CLR (p->infd, &non_keyboard_wait_mask);
        }
-      p->command = Qt;
+      PSET (p, command, Qt);
       return process;
     }
 #ifndef SIGTSTP
@@ -6056,7 +6054,7 @@ traffic.  */)
          tcflush (p->infd, TCIFLUSH);
 #endif /* not WINDOWSNT */
        }
-      p->command = Qnil;
+      PSET (p, command, Qnil);
       return process;
     }
 #ifdef SIGCONT
@@ -6291,7 +6289,7 @@ process has been transmitted to the serial port.  */)
 
       if (!proc_encode_coding_system[new_outfd])
        proc_encode_coding_system[new_outfd]
-         = (struct coding_system *) xmalloc (sizeof (struct coding_system));
+         = xmalloc (sizeof (struct coding_system));
       memcpy (proc_encode_coding_system[new_outfd],
              proc_encode_coding_system[old_outfd],
              sizeof (struct coding_system));
@@ -6352,7 +6350,7 @@ sigchld_handler (int signo)
       do
        {
          errno = 0;
-         pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
+         pid = waitpid (-1, &w, WNOHANG | WUNTRACED);
        }
       while (pid < 0 && errno == EINTR);
 
@@ -6429,7 +6427,7 @@ sigchld_handler (int signo)
          /* Tell wait_reading_process_output that it needs to wake up and
             look around.  */
          if (input_available_clear_time)
-           EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
+           *input_available_clear_time = make_emacs_time (0, 0);
        }
 
       /* There was no asynchronous process found for that pid: we have
@@ -6440,14 +6438,14 @@ sigchld_handler (int signo)
 
          /* Report the status of the synchronous process.  */
          if (WIFEXITED (w))
-           synch_process_retcode = WRETCODE (w);
+           synch_process_retcode = WEXITSTATUS (w);
          else if (WIFSIGNALED (w))
            synch_process_termsig = WTERMSIG (w);
 
          /* Tell wait_reading_process_output that it needs to wake up and
             look around.  */
          if (input_available_clear_time)
-           EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
+           *input_available_clear_time = make_emacs_time (0, 0);
        }
 
     sigchld_end_of_loop:
@@ -6472,7 +6470,7 @@ sigchld_handler (int signo)
 static Lisp_Object
 exec_sentinel_unwind (Lisp_Object data)
 {
-  XPROCESS (XCAR (data))->sentinel = XCDR (data);
+  PSET (XPROCESS (XCAR (data)), sentinel, XCDR (data));
   return Qnil;
 }
 
@@ -6518,7 +6516,7 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
 
   /* Zilch the sentinel while it's running, to avoid recursive invocations;
      assure that it gets restored no matter how the sentinel exits.  */
-  p->sentinel = Qnil;
+  PSET (p, sentinel, Qnil);
   record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
   /* Inhibit quit so that random quits don't screw up a running filter.  */
   specbind (Qinhibit_quit, Qt);
@@ -6673,12 +6671,13 @@ status_notify (struct Lisp_Process *deleting_process)
              before_byte = PT_BYTE;
 
              tem = BVAR (current_buffer, read_only);
-             BVAR (current_buffer, read_only) = Qnil;
+             BSET (current_buffer, read_only, Qnil);
              insert_string ("\nProcess ");
-             Finsert (1, &p->name);
+             { /* FIXME: temporary kludge */
+               Lisp_Object tem2 = p->name; Finsert (1, &tem2); }
              insert_string (" ");
              Finsert (1, &msg);
-             BVAR (current_buffer, read_only) = tem;
+             BSET (current_buffer, read_only, tem);
              set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
 
              if (opoint >= before)
@@ -6715,8 +6714,8 @@ encode subprocess input.  */)
   Fcheck_coding_system (decoding);
   Fcheck_coding_system (encoding);
   encoding = coding_inherit_eol_type (encoding, Qnil);
-  p->decode_coding_system = decoding;
-  p->encode_coding_system = encoding;
+  PSET (p, decode_coding_system, decoding);
+  PSET (p, encode_coding_system, encoding);
   setup_process_coding_systems (process);
 
   return Qnil;
@@ -6746,7 +6745,8 @@ suppressed.  */)
   CHECK_PROCESS (process);
   p = XPROCESS (process);
   if (NILP (flag))
-    p->decode_coding_system = raw_text_coding_system (p->decode_coding_system);
+    PSET (p, decode_coding_system,
+         raw_text_coding_system (p->decode_coding_system));
   setup_process_coding_systems (process);
 
   return Qnil;
@@ -6826,7 +6826,7 @@ extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
        If NSECS > 0, the timeout consists of NSECS only.
        If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
 
-   READ_KBD is a Lisp_Object:
+   READ_KBD is:
      0 to ignore keyboard input, or
      1 to return when input is available, or
      -1 means caller will actually read the input, so don't throw to
@@ -6848,8 +6848,6 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
 {
   register int nfds;
   EMACS_TIME end_time, timeout;
-  SELECT_TYPE waitchannels;
-  int xerrno;
 
   if (time_limit < 0)
     {
@@ -6860,11 +6858,10 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
     time_limit = TYPE_MAXIMUM (time_t);
 
   /* What does time_limit really mean?  */
-  if (time_limit || nsecs) /* FIXME: what if negative? */
+  if (time_limit || 0 < nsecs)
     {
-      EMACS_GET_TIME (end_time);
-      EMACS_SET_SECS_NSECS (timeout, time_limit, nsecs);
-      EMACS_ADD_TIME (end_time, end_time, timeout);
+      timeout = make_emacs_time (time_limit, nsecs);
+      end_time = add_emacs_time (current_emacs_time (), timeout);
     }
 
   /* Turn off periodic alarms (in case they are in use)
@@ -6876,6 +6873,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
   while (1)
     {
       int timeout_reduced_for_timers = 0;
+      SELECT_TYPE waitchannels;
+      int xerrno;
 
       /* If calling from keyboard input, do not quit
         since we want to return C-g as an input character.
@@ -6891,22 +6890,22 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
       /* Exit if already run out */
       if (nsecs < 0)
        {
-         /* -1 specified for timeout means
+         /* A negative timeout means
             gobble output available now
             but don't wait at all. */
 
-         EMACS_SET_SECS_USECS (timeout, 0, 0);
+         timeout = make_emacs_time (0, 0);
        }
-      else if (time_limit || nsecs)
+      else if (time_limit || 0 < nsecs)
        {
-         EMACS_GET_TIME (timeout);
-         if (EMACS_TIME_LE (end_time, timeout))
+         EMACS_TIME now = current_emacs_time ();
+         if (EMACS_TIME_LE (end_time, now))
            break;
-         EMACS_SUB_TIME (timeout, end_time, timeout);
+         timeout = sub_emacs_time (end_time, now);
        }
       else
        {
-         EMACS_SET_SECS_USECS (timeout, 100000, 0);
+         timeout = make_emacs_time (100000, 0);
        }
 
       /* If our caller will not immediately handle keyboard events,
@@ -6950,13 +6949,6 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
       if (read_kbd < 0)
        set_waiting_for_input (&timeout);
 
-      /* Wait till there is something to do.  */
-
-      if (! read_kbd && NILP (wait_for_cell))
-       FD_ZERO (&waitchannels);
-      else
-       FD_SET (0, &waitchannels);
-
       /* If a frame has been newly mapped and needs updating,
         reprocess its display stuff.  */
       if (frame_garbaged && do_display)
@@ -6967,13 +6959,16 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
            set_waiting_for_input (&timeout);
        }
 
+      /* Wait till there is something to do.  */
+      FD_ZERO (&waitchannels);
       if (read_kbd && detect_input_pending ())
+       nfds = 0;
+      else
        {
-         nfds = 0;
-         FD_ZERO (&waitchannels);
+         if (read_kbd || !NILP (wait_for_cell))
+           FD_SET (0, &waitchannels);
+         nfds = pselect (1, &waitchannels, NULL, NULL, &timeout, NULL);
        }
-      else
-       nfds = pselect (1, &waitchannels, NULL, NULL, &timeout, NULL);
 
       xerrno = errno;
 
@@ -7088,8 +7083,7 @@ setup_process_coding_systems (Lisp_Object process)
     return;
 
   if (!proc_decode_coding_system[inch])
-    proc_decode_coding_system[inch]
-      = (struct coding_system *) xmalloc (sizeof (struct coding_system));
+    proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system));
   coding_system = p->decode_coding_system;
   if (! NILP (p->filter))
     ;
@@ -7101,8 +7095,7 @@ setup_process_coding_systems (Lisp_Object process)
   setup_coding_system (coding_system, proc_decode_coding_system[inch]);
 
   if (!proc_encode_coding_system[outch])
-    proc_encode_coding_system[outch]
-      = (struct coding_system *) xmalloc (sizeof (struct coding_system));
+    proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system));
   setup_coding_system (p->encode_coding_system,
                       proc_encode_coding_system[outch]);
 #endif
@@ -7310,8 +7303,10 @@ integer or floating point values.
 }
 
 \f
+/* This is not called "init_process" because that is the name of a
+   Mach system call, so it would cause problems on Darwin systems.  */
 void
-init_process (void)
+init_process_emacs (void)
 {
 #ifdef subprocesses
   register int i;
@@ -7408,8 +7403,7 @@ init_process (void)
     char const *release = (STRINGP (Voperating_system_release)
                           ? SSDATA (Voperating_system_release)
                           : 0);
-    if (!release || !release[0] || (release[0] < MIN_PTY_KERNEL_VERSION
-                                   && release[1] == '.')) {
+    if (!release || !release[0] || (release[0] < '7' && release[1] == '.')) {
       Vprocess_connection_type = Qnil;
     }
   }