(MAKE_LOCK_NAME): Use size_byte.
[bpt/emacs.git] / src / process.c
index af4fb8f..d2779dd 100644 (file)
@@ -266,6 +266,8 @@ extern int timers_run;
 /* Maximum number of bytes to send to a pty without an eof.  */
 static int pty_max_bytes;
 
+extern Lisp_Object Vfile_name_coding_system;
+
 #ifdef HAVE_PTYS
 /* The file name of the pty opened by allocate_pty.  */
 
@@ -1157,8 +1159,9 @@ Remaining arguments are strings to give program as arguments.")
 
   /* Make the process marker point into the process buffer (if any).  */
   if (!NILP (buffer))
-    Fset_marker (XPROCESS (proc)->mark,
-                make_number (BUF_ZV (XBUFFER (buffer))), buffer);
+    set_marker_both (XPROCESS (proc)->mark, buffer,
+                    BUF_ZV (XBUFFER (buffer)),
+                    BUF_ZV_BYTE (XBUFFER (buffer)));
 
   if (!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters)
       || NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters))
@@ -1224,7 +1227,7 @@ Remaining arguments are strings to give program as arguments.")
   XPROCESS (proc)->decoding_buf = make_uninit_string (0);
   XPROCESS (proc)->encoding_buf = make_uninit_string (0);
 
-  create_process (proc, new_argv, current_dir);
+  create_process (proc, (char **) new_argv, current_dir);
 
   return unbind_to (count, proc);
 }
@@ -1279,6 +1282,7 @@ create_process_sigchld ()
 #endif
 
 #ifndef VMS /* VMS version of this function is in vmsproc.c.  */
+void
 create_process (process, new_argv, current_dir)
      Lisp_Object process;
      char **new_argv;
@@ -1397,6 +1401,35 @@ create_process (process, new_argv, current_dir)
   setup_coding_system (XPROCESS (process)->encode_coding_system,
                       proc_encode_coding_system[outchannel]);
 
+  if (CODING_REQUIRE_ENCODING (proc_encode_coding_system[outchannel]))
+    {
+      /* Here we encode arguments by the coding system used for
+        sending data to the process.  We don't support using
+        different coding systems for encoding arguments and for
+        encoding data sent to the process.  */
+      struct gcpro gcpro1;
+      int i = 1;
+      struct coding_system *coding = proc_encode_coding_system[outchannel];
+
+      coding->last_block = 1;
+      GCPRO1 (process);
+      while (new_argv[i] != 0)
+       {
+         int len = strlen (new_argv[i]);
+         int size = encoding_buffer_size (coding, len);
+         unsigned char *buf = (unsigned char *) alloca (size);
+         int produced, dmy;
+
+         produced = encode_coding (coding, new_argv[i], buf, len, size, &dmy);
+         buf[produced] = 0;
+         /* We don't have to free new_argv[i] because it points to a
+             Lisp string given as an argument to `start-process'.  */
+         new_argv[i++] = buf;
+       }
+      UNGCPRO;
+      coding->last_block = 0;
+    }
+
   /* Delay interrupts until we have a chance to store
      the new fork's pid in its process structure */
 #ifdef POSIX_SIGNALS
@@ -1453,6 +1486,9 @@ create_process (process, new_argv, current_dir)
        Protect it from permanent change.  */
     char **save_environ = environ;
 
+    current_dir
+      = Fencode_coding_string (current_dir, Vfile_name_coding_system, Qt);
+
 #ifndef WINDOWSNT
     pid = vfork ();
     if (pid == 0)
@@ -1986,6 +2022,7 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
 }
 #endif /* HAVE_SOCKETS */
 
+void
 deactivate_process (proc)
      Lisp_Object proc;
 {
@@ -2035,6 +2072,7 @@ deactivate_process (proc)
    with subprocess.  This is used in a newly-forked subprocess
    to get rid of irrelevant descriptors.  */
 
+void
 close_process_descs ()
 {
 #ifndef WINDOWSNT
@@ -2749,7 +2787,8 @@ read_process_output (proc, channel)
   /* Now set NCHARS how many bytes we must decode.  */
   nchars += coding->carryover_size;
 
-  if (! CODING_REQUIRE_NO_CONVERSION (coding))
+  if (CODING_REQUIRE_DECODING (coding)
+      || CODING_REQUIRE_DETECTION (coding))
     {
       int require = decoding_buffer_size (coding, nchars);
       int consumed, produced;
@@ -2769,13 +2808,19 @@ read_process_output (proc, channel)
              proc_decode_coding_system[channel] here.  It is done in
              detect_coding called via decode_coding above.  */
 
-         /* If coding-system for encoding is not yet decided, we set it
-            as the same as coding-system for decoding.  */
-         if (NILP (p->encode_coding_system))
+         /* If coding-system for encoding is not yet decided, we set
+            it as the same as coding-system for decoding.
+
+            But, before doing that we must check if
+            proc_encode_coding_system[p->outfd] surely points to a
+            valid memory because p->outfd will be changed once EOF is
+            sent to the process.  */
+         if (NILP (p->encode_coding_system)
+             && proc_encode_coding_system[p->outfd])
            {
              p->encode_coding_system = coding->symbol;
              setup_coding_system (coding->symbol,
-                                  proc_encode_coding_system[channel]);
+                                  proc_encode_coding_system[p->outfd]);
            }
        }
 #ifdef VMS
@@ -2804,6 +2849,8 @@ read_process_output (proc, channel)
     }
 #endif
 
+  Vlast_coding_system_used = coding->symbol;
+
   outstream = p->filter;
   if (!NILP (outstream))
     {
@@ -2882,16 +2929,21 @@ read_process_output (proc, channel)
     {
       Lisp_Object old_read_only;
       int old_begv, old_zv;
+      int old_begv_byte, old_zv_byte;
       Lisp_Object odeactivate;
-      int before;
+      int before, before_byte;
+      int opoint_byte;
 
       odeactivate = Vdeactivate_mark;
 
       Fset_buffer (p->buffer);
       opoint = PT;
+      opoint_byte = PT_BYTE;
       old_read_only = current_buffer->read_only;
       old_begv = BEGV;
       old_zv = ZV;
+      old_begv_byte = BEGV_BYTE;
+      old_zv_byte = ZV_BYTE;
 
       current_buffer->read_only = Qnil;
 
@@ -2899,10 +2951,13 @@ read_process_output (proc, channel)
         at the current end-of-output marker,
         thus preserving logical ordering of input and output.  */
       if (XMARKER (p->mark)->buffer)
-       SET_PT (clip_to_bounds (BEGV, marker_position (p->mark), ZV));
+       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 (ZV);
+       SET_PT_BOTH (ZV, ZV_BYTE);
       before = PT;
+      before_byte = PT_BYTE;
 
       /* If the output marker is outside of the visible region, save
         the restriction and widen.  */
@@ -2915,18 +2970,27 @@ read_process_output (proc, channel)
        insert_from_string_before_markers (p->decoding_buf, 0, nchars, 0);
       else
        insert_before_markers (chars, nchars);
-      Fset_marker (p->mark, make_number (PT), p->buffer);
+      set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
 
       update_mode_lines++;
 
       /* Make sure opoint and the old restrictions
         float ahead of any new text just as point would.  */
       if (opoint >= before)
-       opoint += PT - before;
+       {
+         opoint += PT - before;
+         opoint_byte += PT_BYTE - before_byte;
+       }
       if (old_begv > before)
-       old_begv += PT - before;
+       {
+         old_begv += PT - before;
+         old_begv_byte += PT_BYTE - before_byte;
+       }
       if (old_zv >= before)
-       old_zv += PT - before;
+       {
+         old_zv += PT - before;
+         old_zv_byte += PT_BYTE - before_byte;
+       }
 
       /* If the restriction isn't what it should be, set it.  */
       if (old_begv != BEGV || old_zv != ZV)
@@ -2936,7 +3000,7 @@ read_process_output (proc, channel)
       Vdeactivate_mark = odeactivate;
 
       current_buffer->read_only = old_read_only;
-      SET_PT (opoint);
+      SET_PT_BOTH (opoint, opoint_byte);
       set_buffer_internal (old);
     }
 #ifdef VMS
@@ -3005,7 +3069,9 @@ send_process (proc, buf, len, object)
     error ("Output file descriptor of %s is closed", procname);
 
   coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
-  if (! CODING_MAY_REQUIRE_NO_CONVERSION (coding))
+  Vlast_coding_system_used = coding->symbol;
+
+  if (CODING_REQUIRE_ENCODING (coding))
     {
       int require = encoding_buffer_size (coding, len);
       int offset, dummy;
@@ -3015,7 +3081,7 @@ send_process (proc, buf, len, object)
          be relocated.  Setting OFFSET to -1 means we don't have to
          care relocation.  */
       offset = (BUFFERP (object)
-               ? BUF_PTR_CHAR_POS (XBUFFER (object), buf)
+               ? BUF_PTR_BYTE_POS (XBUFFER (object), buf)
                : (STRINGP (object)
                   ? offset = buf - XSTRING (object)->data
                   : -1));
@@ -3027,7 +3093,7 @@ send_process (proc, buf, len, object)
          if (offset >= 0)
            {
              if (BUFFERP (object))
-               buf = BUF_CHAR_ADDRESS (XBUFFER (object), offset);
+               buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
              else if (STRINGP (object))
                buf = offset + XSTRING (object)->data;
              /* Now we don't have to care relocation.  */
@@ -3045,7 +3111,7 @@ send_process (proc, buf, len, object)
          if (offset >= 0)
            {
              if (BUFFERP (object))
-               buf = BUF_CHAR_ADDRESS (XBUFFER (object), offset);
+               buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
              else if (STRINGP (object))
                buf = offset + XSTRING (object)->data;
            }
@@ -3142,7 +3208,7 @@ send_process (proc, buf, len, object)
                    /* Running filters might relocate buffers or strings.
                       Arrange to relocate BUF.  */
                    if (BUFFERP (object))
-                     offset = BUF_PTR_CHAR_POS (XBUFFER (object), buf);
+                     offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
                    else if (STRINGP (object))
                      offset = buf - XSTRING (object)->data;
 
@@ -3154,7 +3220,7 @@ send_process (proc, buf, len, object)
 #endif
 
                    if (BUFFERP (object))
-                     buf = BUF_CHAR_ADDRESS (XBUFFER (object), offset);
+                     buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
                    else if (STRINGP (object))
                      buf = offset + XSTRING (object)->data;
 
@@ -3205,7 +3271,7 @@ Output from processes can arrive in between bunches.")
      Lisp_Object process, start, end;
 {
   Lisp_Object proc;
-  int start1;
+  int start1, end1;
 
   proc = get_process (process);
   validate_region (&start, &end);
@@ -3213,8 +3279,9 @@ Output from processes can arrive in between bunches.")
   if (XINT (start) < GPT && XINT (end) > GPT)
     move_gap (XINT (start));
 
-  start1 = XINT (start);
-  send_process (proc, POS_ADDR (start1), XINT (end) - XINT (start),
+  start1 = CHAR_TO_BYTE (XINT (start));
+  end1 = CHAR_TO_BYTE (XINT (end));
+  send_process (proc, BYTE_POS_ADDR (start1), end1 - start1,
                Fcurrent_buffer ());
 
   return Qnil;
@@ -3694,6 +3761,7 @@ text to PROCESS after you call this function.")
 /* Kill all processes associated with `buffer'.
  If `buffer' is nil, kill all processes  */
 
+void
 kill_buffer_processes (buffer)
      Lisp_Object buffer;
 {
@@ -3979,6 +4047,7 @@ exec_sentinel (proc, reason)
    (either run the sentinel or output a message).
    This is done while Emacs is waiting for keyboard input.  */
 
+void
 status_notify ()
 {
   register Lisp_Object proc, buffer;
@@ -4049,8 +4118,8 @@ status_notify ()
            {
              Lisp_Object ro, tem;
              struct buffer *old = current_buffer;
-             int opoint;
-             int before;
+             int opoint, opoint_byte;
+             int before, before_byte;
 
              ro = XBUFFER (buffer)->read_only;
 
@@ -4061,15 +4130,17 @@ status_notify ()
              Fset_buffer (buffer);
 
              opoint = PT;
+             opoint_byte = PT_BYTE;
              /* 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 (marker_position (p->mark));
+               Fgoto_char (p->mark);
              else
-               SET_PT (ZV);
+               SET_PT_BOTH (ZV, ZV_BYTE);
 
              before = PT;
+             before_byte = PT_BYTE;
 
              tem = current_buffer->read_only;
              current_buffer->read_only = Qnil;
@@ -4078,12 +4149,13 @@ status_notify ()
              insert_string (" ");
              Finsert (1, &msg);
              current_buffer->read_only = tem;
-             Fset_marker (p->mark, make_number (PT), p->buffer);
+             set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
 
              if (opoint >= before)
-               SET_PT (opoint + (PT - before));
+               SET_PT_BOTH (opoint + (PT - before),
+                            opoint_byte + (PT_BYTE - before_byte));
              else
-               SET_PT (opoint);
+               SET_PT_BOTH (opoint, opoint_byte);
 
              set_buffer_internal (old);
            }
@@ -4099,7 +4171,7 @@ status_notify ()
 \f
 DEFUN ("set-process-coding-system", Fset_process_coding_system,
        Sset_process_coding_system, 1, 3, 0,
-  "Set coding-systems of PROCESS to DECODING (input from the process) and\n\
+  "Set coding systems of PROCESS to DECODING (input from the process) and\n\
 ENCODING (output to the process).")
   (proc, decoding, encoding)
      register Lisp_Object proc, decoding, encoding;
@@ -4125,7 +4197,7 @@ ENCODING (output to the process).")
 
 DEFUN ("process-coding-system",
        Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
-  "Return a cons of coding-system for decoding and encoding of PROCESS.")
+  "Return a cons of coding systems for decoding and encoding of PROCESS.")
   (proc)
      register Lisp_Object proc;
 {