home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / ntenv.c < prev    next >
C/C++ Source or Header  |  2000-12-05  |  12KB  |  467 lines

  1. /* -*-C-*-
  2.  
  3. $Id: ntenv.c,v 1.19 2000/12/05 21:23:45 cph Exp $
  4.  
  5. Copyright (c) 1992-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "scheme.h"
  23. #include "nt.h"
  24. #include "osenv.h"
  25. #include "ntscreen.h"
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. extern unsigned long file_time_to_unix_time (FILETIME *);
  30. extern void unix_time_to_file_time (unsigned long, FILETIME *);
  31.  
  32. static unsigned long
  33. system_time_to_unix_time (SYSTEMTIME * st)
  34. {
  35.   FILETIME ft;
  36.   (void) SystemTimeToFileTime (st, (&ft));
  37.   return (file_time_to_unix_time (&ft));
  38. }
  39.  
  40. #if 0
  41. static void
  42. unix_time_to_system_time (unsigned long ut, SYSTEMTIME * st)
  43. {
  44.   FILETIME ft;
  45.   unix_time_to_file_time (ut, (&ft));
  46.   (void) FileTimeToSystemTime ((&ft), st);
  47. }
  48. #endif
  49.  
  50. time_t
  51. DEFUN_VOID (OS_encoded_time)
  52. {
  53.   SYSTEMTIME t;
  54.   GetSystemTime (&t);
  55.   return (system_time_to_unix_time (&t));
  56. }
  57.  
  58. void
  59. OS_decode_time (time_t t, struct time_structure * buffer)
  60. {
  61.   struct tm * ts;
  62.   STD_PTR_UNIX_CALL (ts, localtime, (&t));
  63.   (buffer -> year) = ((ts -> tm_year) + 1900);
  64.   (buffer -> month) = ((ts -> tm_mon) + 1);
  65.   (buffer -> day) = (ts -> tm_mday);
  66.   (buffer -> hour) = (ts -> tm_hour);
  67.   (buffer -> minute) = (ts -> tm_min);
  68.   (buffer -> second) = (ts -> tm_sec);
  69.   (buffer -> daylight_savings_time) = (ts -> tm_isdst);
  70.   /* I'm assuming that `timezone' is implemented by the C library;
  71.      this might need conditionalization.  -- cph */
  72.   (buffer -> time_zone) = timezone;
  73.   {
  74.     /* In localtime() encoding, 0 is Sunday; in ours, it's Monday. */
  75.     int wday = (ts -> tm_wday);
  76.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  77.   }
  78. }
  79.  
  80. void
  81. OS_decode_utc (time_t t, struct time_structure * buffer)
  82. {
  83.   struct tm * ts;
  84.   STD_PTR_UNIX_CALL (ts, gmtime, (&t));
  85.   (buffer -> year) = ((ts -> tm_year) + 1900);
  86.   (buffer -> month) = ((ts -> tm_mon) + 1);
  87.   (buffer -> day) = (ts -> tm_mday);
  88.   (buffer -> hour) = (ts -> tm_hour);
  89.   (buffer -> minute) = (ts -> tm_min);
  90.   (buffer -> second) = (ts -> tm_sec);
  91.   (buffer -> daylight_savings_time) = (ts -> tm_isdst);
  92.   (buffer -> time_zone) = 0;
  93.   {
  94.     /* In gmtime() encoding, 0 is Sunday; in ours, it's Monday. */
  95.     int wday = (ts -> tm_wday);
  96.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  97.   }
  98. }
  99.  
  100. #if 0
  101. /* This nice implementation can't be used because it only works under
  102.    Windows NT.  */
  103. void
  104. OS_decode_time (time_t t, struct time_structure * buffer)
  105. {
  106.   SYSTEMTIME st;
  107.   SYSTEMTIME lst;
  108.   TIME_ZONE_INFORMATION tzi;
  109.   unix_time_to_system_time (t, (&st));
  110.   switch (GetTimeZoneInformation (&tzi))
  111.     {
  112.     case TIME_ZONE_ID_STANDARD:
  113.       (buffer -> daylight_savings_time) = 0;
  114.       (buffer -> time_zone) = (tzi . Bias);
  115.       break;
  116.     case TIME_ZONE_ID_DAYLIGHT:
  117.       (buffer -> daylight_savings_time) = 1;
  118.       (buffer -> time_zone) = (tzi . Bias);
  119.       break;
  120.     default:
  121.       (buffer -> daylight_savings_time) = -1;
  122.       (buffer -> time_zone) = 0;
  123.       break;
  124.     }
  125.   if (((buffer -> daylight_savings_time) == 0) && ((buffer -> time_zone) == 0))
  126.     lst = st;
  127.   else
  128.     (void) SystemTimeToTzSpecificLocalTime ((&tzi), (&st), (&lst));
  129.   (buffer -> year) = (lst . wYear);
  130.   (buffer -> month) = (lst . wMonth);
  131.   (buffer -> day) = (lst . wDay);
  132.   (buffer -> hour) = (lst . wHour);
  133.   (buffer -> minute) = (lst . wMinute);
  134.   (buffer -> second) = (lst . wSecond);
  135.   {
  136.     /* In SYSTEMTIME encoding, 0 is Sunday; in ours, it's Monday. */
  137.     int wday = (lst . wDayOfWeek);
  138.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  139.   }
  140. }
  141. #endif
  142.  
  143. time_t
  144. OS_encode_time (struct time_structure * buffer)
  145. {
  146.   time_t t;
  147.   struct tm ts_s, * ts;
  148.   ts = &ts_s;
  149.   (ts -> tm_year) = ((buffer -> year) - 1900);
  150.   (ts -> tm_mon) = ((buffer -> month) - 1);
  151.   (ts -> tm_mday) = (buffer -> day);
  152.   (ts -> tm_hour) = (buffer -> hour);
  153.   (ts -> tm_min) = (buffer -> minute);
  154.   (ts -> tm_sec) = (buffer -> second);
  155.   (ts -> tm_isdst) = (buffer -> daylight_savings_time);
  156.   STD_UINT_UNIX_CALL (t, mktime, (ts));
  157.   /* mktime assumes its argument is local time, and converts it to
  158.      UTC; if the specified time zone is different, adjust the result.  */
  159.   if (((buffer -> time_zone) != INT_MAX)
  160.       && ((buffer -> time_zone) != timezone))
  161.     t = ((t - timezone) + (buffer -> time_zone));
  162.   return (t);
  163. }
  164.  
  165. #define FILETIME_TO_MSECS(ft) \
  166.   (((4294967296.0 * (double)  ft.dwHighDateTime) + ft.dwLowDateTime)*100e-6)
  167.  
  168. double
  169. DEFUN_VOID (OS_process_clock)
  170. {
  171.   /* This must not signal an error in normal use. */
  172.   /* Return answer in milliseconds, was in 1/100th seconds */
  173.  
  174.   FILETIME  creation_time, exit_time, kernel_time, user_time;
  175.   if(GetProcessTimes(GetCurrentProcess(),
  176.              &creation_time, &exit_time, &kernel_time, &user_time)) {
  177.     return  FILETIME_TO_MSECS(user_time) + FILETIME_TO_MSECS(kernel_time);
  178.   } else {
  179.     return ((((double) (clock ())) * 1000.0) / ((double) CLOCKS_PER_SEC));
  180.   }
  181. }
  182.  
  183. double
  184. DEFUN_VOID (OS_real_time_clock)
  185. {
  186.   return ((((double) (clock ())) * 1000.0) / ((double) CLOCKS_PER_SEC));
  187. }
  188.  
  189. /* The timers are all the same.
  190.    This just provides three distinct timers.
  191.  */
  192.  
  193. #define TIMER_ID_REAL        (TIMER_ID_BASE + 2)
  194. #define TIMER_ID_PROFILE    (TIMER_ID_BASE + 1)
  195. #define TIMER_ID_PROCESS    (TIMER_ID_BASE + 0)
  196.  
  197. #ifdef USE_WM_TIMER
  198.  
  199. #define TIMER_ID_BASE        0x100
  200.  
  201. enum timer_next
  202. {
  203.   timer_next_none,
  204.   timer_next_normal,
  205.   timer_next_disable,
  206.   timer_next_set_period
  207. };
  208.  
  209. struct timer_state_s
  210. {
  211.   int local_id;
  212.   int global_id;
  213.   clock_t period;
  214.   enum timer_next next;
  215. };
  216.  
  217. struct timer_state_s scheme_timers[3] =
  218. {
  219.   { TIMER_ID_PROCESS, 0, 0, timer_next_none, },
  220.   { TIMER_ID_PROFILE, 0, 0, timer_next_none, },
  221.   { TIMER_ID_REAL,    0, 0, timer_next_none, },
  222. };
  223.  
  224. extern HANDLE master_tty_window;
  225.  
  226. static void
  227. DEFUN (clear_timer, (timer_id), int timer_id)
  228. {
  229.   struct timer_state_s * timer = &scheme_timers[timer_id - TIMER_ID_BASE];
  230.   if (timer->global_id != 0)
  231.     KillTimer (master_tty_window, timer->global_id);
  232.   timer->global_id = 0;
  233.   timer->next = timer_next_none;
  234.   return;
  235. }
  236.  
  237. extern VOID /* CALLBACK */ EXFUN (TimerProc, (HWND, UINT, UINT, DWORD));
  238.  
  239. #define THE_TIMER_PROC ((TIMERPROC) NULL) /* TimerProc */
  240.  
  241. VOID /* CALLBACK */
  242. DEFUN (TimerProc, (hwnd, umsg, timer_id, dwtime),
  243.        HWND hwnd AND UINT umsg AND UINT timer_id AND DWORD dwtime)
  244. {
  245.   if (hwnd == master_tty_window)
  246.   {
  247.     struct timer_state_s * timer;
  248.  
  249.     REQUEST_INTERRUPT (INT_Timer);
  250.     timer = &scheme_timers[timer_id - TIMER_ID_BASE];
  251.     switch (timer->next)
  252.     {
  253.     case timer_next_set_period:
  254.       // clear_timer (timer_id);
  255.       timer->global_id = (SetTimer (master_tty_window,
  256.                     timer->local_id,
  257.                     timer->period,
  258.                     THE_TIMER_PROC));
  259.       timer->next = timer_next_normal;
  260.       break;
  261.       
  262.     case timer_next_normal:
  263.       break;
  264.  
  265.     case timer_next_none:
  266.     case timer_next_disable:
  267.     default:      
  268.       clear_timer (timer_id);
  269.       break;
  270.     }
  271.   }
  272.   return;
  273. }
  274.  
  275. static void
  276. DEFUN (set_timer, (timer_id, first, interval),
  277.        int timer_id AND clock_t first AND clock_t interval)
  278. {
  279.   struct timer_state_s * timer = &scheme_timers[timer_id - TIMER_ID_BASE];
  280.   if (timer->global_id != 0)
  281.   {
  282.     KillTimer (master_tty_window, timer->global_id);
  283.     timer->global_id = 0;
  284.   }
  285.   
  286.   timer->period = interval;
  287.   if ((first == 0) || (interval == first))
  288.     timer->next = timer_next_normal;
  289.   else if (interval == 0)
  290.     timer->next = timer_next_disable;
  291.   else
  292.     timer->next = timer_next_set_period;
  293.   timer->global_id = (SetTimer (master_tty_window,
  294.                 timer->local_id,
  295.                 ((first == 0) ? interval : first),
  296.                 THE_TIMER_PROC));
  297.   if (timer->global_id == 0)
  298.   {
  299.     timer->next = timer_next_none;
  300.     NT_error_api_call ((GetLastError ()), apicall_SetTimer);
  301.   }
  302.   return;
  303. }
  304.  
  305. #else /* not USE_WM_TIMER */
  306.  
  307. #define TIMER_ID_BASE        0
  308.  
  309. struct timer_state_s
  310. {
  311.   int counter;
  312.   int reload;
  313. };
  314.  
  315. struct timer_state_s scheme_timers[3] = { { 0, 0, }, { 0, 0, }, { 0, 0, } };
  316.  
  317. extern void EXFUN (low_level_timer_tick, (void));
  318.  
  319. void
  320. DEFUN_VOID (low_level_timer_tick)
  321. {
  322.   int i;
  323.   int number_signalled = 0;
  324.  
  325.   for (i = 0; i < 3; i++)
  326.     if (scheme_timers[i].counter != 0)
  327.     {
  328.       scheme_timers[i].counter -= 1;
  329.       if (scheme_timers[i].counter == 0)
  330.       {
  331.     scheme_timers[i].counter = scheme_timers[i].reload;
  332.     number_signalled += 1;
  333.       }
  334.     }
  335.  
  336.   if (number_signalled != 0)
  337.     REQUEST_INTERRUPT (INT_Timer);
  338.   return;
  339. }
  340.  
  341. static void
  342. DEFUN (set_timer, (timer_id, first, interval),
  343.        int timer_id AND clock_t first AND clock_t interval)
  344. {
  345.   struct timer_state_s * timer = &scheme_timers[timer_id];
  346.  
  347.   /* Round up. */ 
  348.   timer->counter = ((first + 49) / 50);
  349.   timer->reload = ((interval + 49) / 50);
  350.   return;
  351. }
  352.  
  353. static void
  354. DEFUN (clear_timer, (timer_id), int timer_id)
  355. {
  356.   struct timer_state_s * timer = &scheme_timers[timer_id];
  357.  
  358.   timer->counter = 0;
  359.   timer->reload = 0;
  360.   return;
  361. }
  362.  
  363. #endif /* USE_WM_TIMER */
  364.  
  365. void
  366. DEFUN (OS_process_timer_set, (first, interval),
  367.        clock_t first AND clock_t interval)
  368. {
  369.   set_timer (TIMER_ID_PROCESS, first, interval);
  370.   return;
  371. }
  372.  
  373. void
  374. DEFUN_VOID (OS_process_timer_clear)
  375. {
  376.   clear_timer (TIMER_ID_PROCESS);
  377.   return;
  378. }
  379.  
  380. void
  381. DEFUN (OS_profile_timer_set, (first, interval),
  382.        clock_t first AND clock_t interval)
  383. {
  384.   set_timer (TIMER_ID_PROFILE, first, interval);
  385.   return;
  386. }
  387.  
  388. void
  389. DEFUN_VOID (OS_profile_timer_clear)
  390. {
  391.   clear_timer (TIMER_ID_PROFILE);
  392.   return;
  393. }
  394.  
  395. void
  396. DEFUN (OS_real_timer_set, (first, interval),
  397.        clock_t first AND clock_t interval)
  398. {
  399.   set_timer (TIMER_ID_REAL, first, interval);
  400.   return;
  401. }
  402.  
  403. void
  404. DEFUN_VOID (OS_real_timer_clear)
  405. {
  406.   clear_timer (TIMER_ID_REAL);
  407.   return;
  408. }
  409.  
  410. static size_t current_dir_path_size = 0;
  411. static char * current_dir_path = 0;
  412.  
  413. CONST char *
  414. DEFUN_VOID (OS_working_dir_pathname)
  415. {
  416.   if (current_dir_path) {
  417.     return (current_dir_path);
  418.   }
  419.   if (current_dir_path_size == 0)
  420.     {
  421.       current_dir_path = (OS_malloc (1024));
  422.       current_dir_path_size = 1024;
  423.     }
  424.   while (1)
  425.     {
  426.       if ((getcwd (current_dir_path, current_dir_path_size)) != 0)
  427.       {
  428.     strlwr (current_dir_path);
  429.     return (current_dir_path);
  430.       }
  431. #ifdef ERANGE
  432.       if (errno != ERANGE)
  433.     NT_error_unix_call (errno, syscall_getcwd);
  434. #endif
  435.       current_dir_path_size *= 2;
  436.       {
  437.     char * new_current_dir_path
  438.       = (OS_realloc (current_dir_path, current_dir_path_size));
  439.     current_dir_path = new_current_dir_path;
  440.       }
  441.     }
  442. }
  443.  
  444. void
  445. DEFUN (OS_set_working_dir_pathname, (name), CONST char * name)
  446. {
  447.   size_t name_size = (strlen (name));
  448.   CONST char * filename = name;
  449.  
  450.   STD_BOOL_API_CALL (SetCurrentDirectory, (filename));
  451.  
  452.   while (1)
  453.   {
  454.     if (name_size < current_dir_path_size)
  455.     {
  456.       strcpy(current_dir_path, name);
  457.       return;
  458.     }
  459.     current_dir_path_size *= 2;
  460.     {
  461.       char * new_current_dir_path
  462.     = (OS_realloc (current_dir_path, current_dir_path_size));
  463.       current_dir_path = new_current_dir_path;
  464.     }
  465.   }
  466. }
  467.