Simplify and avoid signal-handling races.
[bpt/emacs.git] / src / msdos.c
index 5f46829..bac6b97 100644 (file)
@@ -31,7 +31,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <time.h>
 #include <sys/param.h>
 #include <sys/time.h>
+/* gettime and settime in dos.h clash with their namesakes from
+   gnulib, so we move out of our way the prototypes in dos.h.  */
+#define gettime dos_h_gettime_
+#define settime dos_h_settime_
 #include <dos.h>
+#undef gettime
+#undef settime
 #include <errno.h>
 #include <sys/stat.h>    /* for _fixpath */
 #include <unistd.h>     /* for chdir, dup, dup2, etc. */
@@ -103,18 +109,18 @@ int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
 
 #endif /* not SYSTEM_MALLOC */
 
+/* Return the current timestamp in milliseconds since midnight.  */
 static unsigned long
 event_timestamp (void)
 {
-  struct time t;
+  struct timespec t;
   unsigned long s;
 
   gettime (&t);
-  s = t.ti_min;
-  s *= 60;
-  s += t.ti_sec;
+  s = t.tv_sec;
+  s %= 86400;
   s *= 1000;
-  s += t.ti_hund * 10;
+  s += t.tv_nsec * 1000000;
 
   return s;
 }
@@ -514,8 +520,10 @@ dos_set_window_size (int *rows, int *cols)
 
   /* If the user specified a special video mode for these dimensions,
      use that mode.  */
-  sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
-  video_mode = Fsymbol_value (Fintern_soft (build_string (video_name), Qnil));
+  video_mode
+    = Fsymbol_value (Fintern_soft (make_formatted_string
+                                  (video_name, "screen-dimensions-%dx%d",
+                                   *rows, *cols), Qnil));
 
   if (INTEGERP (video_mode)
       && (video_mode_value = XINT (video_mode)) > 0)
@@ -788,7 +796,7 @@ IT_set_face (int face)
       /* The default face for the frame should always be realized and
         cached.  */
       if (!fp)
-       abort ();
+       emacs_abort ();
     }
   screen_face = face;
   fg = fp->foreground;
@@ -1021,7 +1029,6 @@ IT_clear_end_of_line (struct frame *f, int first_unused)
 {
   char *spaces, *sp;
   int i, j, offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
-  extern int fatal_error_in_progress;
   struct tty_display_info *tty = FRAME_TTY (f);
 
   if (new_pos_X >= first_unused || fatal_error_in_progress)
@@ -1222,7 +1229,7 @@ IT_update_begin (struct frame *f)
   if (display_info->termscript)
     fprintf (display_info->termscript, "\n\n<UPDATE_BEGIN");
 
-  BLOCK_INPUT;
+  block_input ();
 
   if (f && f == mouse_face_frame)
     {
@@ -1272,7 +1279,7 @@ IT_update_begin (struct frame *f)
       hlinfo->mouse_face_mouse_frame = NULL;
     }
 
-  UNBLOCK_INPUT;
+  unblock_input ();
 }
 
 static void
@@ -1295,13 +1302,13 @@ IT_frame_up_to_date (struct frame *f)
   if (hlinfo->mouse_face_deferred_gc
       || (f && f == hlinfo->mouse_face_mouse_frame))
     {
-      BLOCK_INPUT;
+      block_input ();
       if (hlinfo->mouse_face_mouse_frame)
        note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
                              hlinfo->mouse_face_mouse_x,
                              hlinfo->mouse_face_mouse_y);
       hlinfo->mouse_face_deferred_gc = 0;
-      UNBLOCK_INPUT;
+      unblock_input ();
     }
 
   /* Set the cursor type to whatever they wanted.  In a minibuffer
@@ -1386,7 +1393,7 @@ IT_insert_glyphs (struct frame *f, struct glyph *start, int len)
 static void
 IT_delete_glyphs (struct frame *f, int n)
 {
-  abort ();
+  emacs_abort ();
 }
 
 /* set-window-configuration on window.c needs this.  */
@@ -1586,9 +1593,9 @@ IT_set_frame_parameters (struct frame *f, Lisp_Object alist)
   Lisp_Object tail;
   int i, j, length = XINT (Flength (alist));
   Lisp_Object *parms
-    = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
+    = (Lisp_Object *) alloca (length * word_size);
   Lisp_Object *values
-    = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
+    = (Lisp_Object *) alloca (length * word_size);
   /* Do we have to reverse the foreground and background colors?  */
   int reverse = EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt);
   int redraw = 0, fg_set = 0, bg_set = 0;
@@ -1610,11 +1617,9 @@ IT_set_frame_parameters (struct frame *f, Lisp_Object alist)
 
   /* Extract parm names and values into those vectors.  */
   i = 0;
-  for (tail = alist; CONSP (tail); tail = Fcdr (tail))
+  for (tail = alist; CONSP (tail); tail = XCDR (tail))
     {
-      Lisp_Object elt;
-
-      elt = Fcar (tail);
+      Lisp_Object elt = XCAR (tail);
       parms[i] = Fcar (elt);
       CHECK_SYMBOL (parms[i]);
       values[i] = Fcdr (elt);
@@ -1795,7 +1800,7 @@ internal_terminal_init (void)
     }
 
   tty = FRAME_TTY (sf);
-  KVAR (current_kboard, Vwindow_system) = Qpc;
+  kset_window_system (current_kboard, Qpc);
   sf->output_method = output_msdos_raw;
   if (init_needed)
     {
@@ -1813,7 +1818,7 @@ internal_terminal_init (void)
        }
 
       Vinitial_window_system = Qpc;
-      Vwindow_system_version = make_number (23); /* RE Emacs version */
+      Vwindow_system_version = make_number (24); /* RE Emacs version */
       tty->terminal->type = output_msdos_raw;
 
       /* If Emacs was dumped on DOS/V machine, forget the stale VRAM
@@ -2428,10 +2433,10 @@ and then the scan code.  */)
   else
     {
       val = Fvector (NUM_RECENT_DOSKEYS, keys);
-      memcpy (XVECTOR (val)->contents, keys + recent_doskeys_index,
-             (NUM_RECENT_DOSKEYS - recent_doskeys_index) * sizeof (Lisp_Object));
-      memcpy (XVECTOR (val)->contents + NUM_RECENT_DOSKEYS - recent_doskeys_index,
-             keys, recent_doskeys_index * sizeof (Lisp_Object));
+      vcopy (val, 0, keys + recent_doskeys_index,
+            NUM_RECENT_DOSKEYS - recent_doskeys_index);
+      vcopy (val, NUM_RECENT_DOSKEYS - recent_doskeys_index,
+            keys, recent_doskeys_index);
       return val;
     }
 }
@@ -2466,12 +2471,12 @@ dos_rawgetc (void)
       sc = regs.h.ah;
 
       total_doskeys += 2;
-      XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
-       = make_number (c);
+      ASET (recent_doskeys, recent_doskeys_index, make_number (c));
+      recent_doskeys_index++;
       if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
        recent_doskeys_index = 0;
-      XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
-       = make_number (sc);
+      ASET (recent_doskeys, recent_doskeys_index, make_number (sc));
+      recent_doskeys_index++;
       if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
        recent_doskeys_index = 0;
 
@@ -2822,7 +2827,7 @@ IT_menu_create (void)
 {
   XMenu *menu;
 
-  menu = (XMenu *) xmalloc (sizeof (XMenu));
+  menu = xmalloc (sizeof (XMenu));
   menu->allocated = menu->count = menu->panecount = menu->width = 0;
   return menu;
 }
@@ -2836,10 +2841,10 @@ IT_menu_make_room (XMenu *menu)
   if (menu->allocated == 0)
     {
       int count = menu->allocated = 10;
-      menu->text = (char **) xmalloc (count * sizeof (char *));
-      menu->submenu = (XMenu **) xmalloc (count * sizeof (XMenu *));
-      menu->panenumber = (int *) xmalloc (count * sizeof (int));
-      menu->help_text = (const char **) xmalloc (count * sizeof (char *));
+      menu->text = xmalloc (count * sizeof (char *));
+      menu->submenu = xmalloc (count * sizeof (XMenu *));
+      menu->panenumber = xmalloc (count * sizeof (int));
+      menu->help_text = xmalloc (count * sizeof (char *));
     }
   else if (menu->allocated == menu->count)
     {
@@ -2920,7 +2925,7 @@ IT_menu_display (XMenu *menu, int y, int x, int pn, int *faces, int disp_help)
   width = menu->width;
   /* We multiply width by 2 to account for possible control characters.
      FIXME: cater to non-ASCII characters in menus.  */
-  text = (struct glyph *) xmalloc ((width * 2 + 2) * sizeof (struct glyph));
+  text = xmalloc ((width * 2 + 2) * sizeof (struct glyph));
   ScreenGetCursor (&row, &col);
   mouse_get_xy (&mx, &my);
   IT_update_begin (sf);
@@ -3008,7 +3013,7 @@ XMenuAddPane (Display *foo, XMenu *menu, const char *txt, int enable)
   const char *p;
 
   if (!enable)
-    abort ();
+    emacs_abort ();
 
   IT_menu_make_room (menu);
   menu->submenu[menu->count] = IT_menu_create ();
@@ -4072,13 +4077,6 @@ sigprocmask (int how, const sigset_t *new_set, sigset_t *old_set)
 #ifndef HAVE_SELECT
 #include "sysselect.h"
 
-#ifndef EMACS_TIME_ZERO_OR_NEG_P
-#define EMACS_TIME_ZERO_OR_NEG_P(time) \
-  ((long)(time).tv_sec < 0             \
-   || ((time).tv_sec == 0              \
-       && (long)(time).tv_usec <= 0))
-#endif
-
 /* This yields the rest of the current time slice to the task manager.
    It should be called by any code which knows that it has nothing
    useful to do except idle.
@@ -4104,10 +4102,10 @@ dos_yield_time_slice (void)
    because wait_reading_process_output takes care of that.  */
 int
 sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
-           EMACS_TIME *timeout)
+           EMACS_TIME *timeout, void *ignored)
 {
   int check_input;
-  struct time t;
+  struct timespec t;
 
   check_input = 0;
   if (rfds)
@@ -4121,7 +4119,7 @@ sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
     FD_ZERO (efds);
 
   if (nfds != 1)
-    abort ();
+    emacs_abort ();
 
   /* If we are looking only for the terminal, with no timeout,
      just read it and wait -- that's more efficient.  */
@@ -4137,22 +4135,17 @@ sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
       EMACS_TIME clnow, cllast, cldiff;
 
       gettime (&t);
-      EMACS_SET_SECS_USECS (cllast, t.ti_sec, t.ti_hund * 10000L);
+      cllast = make_emacs_time (t.tv_sec, t.tv_nsec);
 
       while (!check_input || !detect_input_pending ())
        {
          gettime (&t);
-         EMACS_SET_SECS_USECS (clnow, t.ti_sec, t.ti_hund * 10000L);
-         EMACS_SUB_TIME (cldiff, clnow, cllast);
-
-         /* When seconds wrap around, we assume that no more than
-            1 minute passed since last `gettime'.  */
-         if (EMACS_TIME_NEG_P (cldiff))
-           EMACS_SET_SECS (cldiff, EMACS_SECS (cldiff) + 60);
-         EMACS_SUB_TIME (*timeout, *timeout, cldiff);
+         clnow = make_emacs_time (t.tv_sec, t.tv_nsec);
+         cldiff = sub_emacs_time (clnow, cllast);
+         *timeout = sub_emacs_time (*timeout, cldiff);
 
          /* Stop when timeout value crosses zero.  */
-         if (EMACS_TIME_ZERO_OR_NEG_P (*timeout))
+         if (EMACS_TIME_SIGN (*timeout) <= 0)
            return 0;
          cllast = clnow;
          dos_yield_time_slice ();
@@ -4221,26 +4214,8 @@ init_gettimeofday (void)
 }
 #endif
 
-#ifdef abort
-#undef abort
 void
-dos_abort (char *file, int line)
-{
-  char buffer1[200], buffer2[400];
-  int i, j;
-
-  sprintf (buffer1, "<EMACS FATAL ERROR IN %s LINE %d>", file, line);
-  for (i = j = 0; buffer1[i]; i++) {
-    buffer2[j++] = buffer1[i];
-    buffer2[j++] = 0x70;
-  }
-  dosmemput (buffer2, j, (int)ScreenPrimary);
-  ScreenSetCursor (2, 0);
-  abort ();
-}
-#else
-void
-abort (void)
+emacs_abort (void)
 {
   dos_ttcooked ();
   ScreenSetCursor (10, 0);
@@ -4256,7 +4231,6 @@ abort (void)
 #endif /* __DJGPP_MINOR__ >= 2 */
   exit (2);
 }
-#endif
 
 void
 syms_of_msdos (void)