home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / ix_init.c < prev    next >
C/C++ Source or Header  |  1996-12-20  |  5KB  |  194 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *  Portions Copyright (C) 1995 Jeff Shepherd
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #define _KERNEL
  23. #include "ixemul.h"
  24. #include "kprintf.h"
  25.  
  26. #include <exec/memory.h>
  27. #include <dos/var.h>
  28.  
  29. #include <string.h>
  30. #include <unistd.h>
  31.  
  32. /* not changed after the library is initialized */
  33. struct ixemul_base        *ixemulbase = NULL;
  34.  
  35. struct ExecBase            *SysBase = NULL;
  36. struct DosLibrary        *DOSBase = NULL;
  37. struct MathIeeeSingBasBase    *MathIeeeSingBasBase = NULL;
  38. struct MathIeeeDoubBasBase    *MathIeeeDoubBasBase = NULL;
  39. struct MathIeeeDoubTransBase    *MathIeeeDoubTransBase = NULL;
  40. struct Library                  *muBase = NULL;
  41.  
  42. static struct
  43. {
  44.   void        **base;
  45.   char        *name;
  46.   ULONG         minver;  /* minimal version          */
  47.   BOOL          opt;     /* no fail if not available */
  48. }
  49. ix_libs[] =
  50. {
  51.   { (void **)&DOSBase, "dos.library", 37 },
  52.   { (void **)&MathIeeeSingBasBase, "mathieeesingbas.library" },
  53.   { (void **)&MathIeeeDoubBasBase, "mathieeedoubbas.library" },
  54.   { (void **)&MathIeeeDoubTransBase, "mathieeedoubtrans.library" },
  55.   { (void **)&muBase, "multiuser.library", 39, TRUE },
  56.   { NULL, NULL }
  57. };
  58.  
  59. static void ix_task_switcher(void)
  60. {
  61.   for (;;)
  62.     if (Wait(3 << 30) & (1 << 30))
  63.       ix.ix_env_has_changed = 1;
  64. }
  65.  
  66. int open_libraries(void)
  67. {
  68.   int i;
  69.  
  70.   for (i = 0; ix_libs[i].base; i++)
  71.     if (!(*(ix_libs[i].base) = (void *)OpenLibrary(ix_libs[i].name, ix_libs[i].minver))
  72.         && !ix_libs[i].opt)
  73.       {
  74.     ix_panic("%s required!", ix_libs[i].name);
  75.     return 0;
  76.       }
  77.   return 1;
  78. }
  79.  
  80. void close_libraries(void)
  81. {
  82.   int i;
  83.   
  84.   for (i = 0; ix_libs[i].base; i++)
  85.     if (*ix_libs[i].base)
  86.       CloseLibrary(*ix_libs[i].base);
  87. }
  88.  
  89. struct ixemul_base *ix_init (struct ixemul_base *ixbase)
  90. {
  91.   int i;
  92.   char buf[256];
  93.   extern char hostname[];  /* in getcrap.c */
  94.  
  95.   ixemulbase = ixbase;
  96.   if (!open_libraries())
  97.     {
  98.       close_libraries();
  99.       return 0;
  100.     }
  101.  
  102.   ixbase->ix_file_tab = (struct file *)AllocMem (NOFILE * sizeof(struct file), MEMF_PUBLIC | MEMF_CLEAR);
  103.   ixbase->ix_fileNFILE = ixbase->ix_file_tab + NOFILE;
  104.   ixbase->ix_lastf = ixbase->ix_file_tab;
  105.   
  106.   memset(&ixbase->ix_notify_request, 0, sizeof(ixbase->ix_notify_request));
  107.   memset(&ixbase->ix_ptys, 0, sizeof(ixbase->ix_ptys));
  108.  
  109.   /* Read the GMT offset. This environment variable is 5 bytes long. The
  110.      first 4 form a long that contains the offset in seconds and the fifth
  111.      byte is ignored. */
  112.   if (GetVar("IXGMTOFFSET", buf, 6, GVF_BINARY_VAR) == 5 && IoErr() == 5)
  113.     ix_set_gmt_offset(*((long *)buf));
  114.   else
  115.     ix_set_gmt_offset(0);
  116.  
  117.   if (GetVar(IX_ENV_SETTINGS, buf, sizeof(struct ix_settings) + 1, GVF_BINARY_VAR) == sizeof(struct ix_settings))
  118.     ix_set_settings((struct ix_settings *)buf);
  119.   else
  120.     ix_set_settings(ix_get_default_settings());
  121.  
  122.   /* Set the hostname if the environment variable HOSTNAME exists. */
  123.   if (GetVar("HOSTNAME", buf, 64, 0) > 0)
  124.     strcpy(hostname, buf);
  125.  
  126.   /* initialize the list structures for the allocator */
  127.   init_buddy ();
  128.  
  129.   ixbase->ix_task_switcher = CreateTask("ixemul task switcher", 9, ix_task_switcher, 1000);
  130.  
  131.   ixbase->ix_notify_request.nr_stuff.nr_Signal.nr_Task = ixbase->ix_task_switcher;
  132.   ixbase->ix_notify_request.nr_stuff.nr_Signal.nr_SignalNum = 30;
  133.   ixbase->ix_notify_request.nr_Name = "ENV:";
  134.   ixbase->ix_notify_request.nr_Flags = NRF_SEND_SIGNAL;
  135.  
  136.   /* initialize port number for AF_UNIX(localhost) sockets */
  137.   ixbase->ix_next_free_port = 1024;
  138.  
  139.   if (ixbase->ix_file_tab)
  140.     {
  141.       InitSemaphore (& ixbase->ix_semaph);
  142.  
  143.       configure_context_switch ();
  144.  
  145.       NewList ((struct List *) &ixbase->ix_socket_list);
  146.  
  147.       for (i = 0; i < IX_NUM_SLEEP_QUEUES; i++)
  148.         NewList ((struct List *) &ixbase->ix_sleep_queues[i]);
  149.  
  150.       ixbase->ix_global_environment = NULL;
  151.       ixbase->ix_env_has_changed = 0;
  152.  
  153.       StartNotify(&ixbase->ix_notify_request);
  154.  
  155.       return ixbase;
  156.     }
  157.  
  158.   if (ixbase->ix_task_switcher)
  159.     {
  160.       Forbid();
  161.       DeleteTask(ixbase->ix_task_switcher);
  162.       Permit();
  163.     }
  164.  
  165.   if (ixbase->ix_file_tab)
  166.     FreeMem (ixbase->ix_file_tab, NOFILE * sizeof(struct file));
  167.   else
  168.     ix_panic ("out of memory");
  169.  
  170.   close_libraries();
  171.   
  172.   return 0;
  173. }      
  174.  
  175. void ix_lock_base (void)
  176. {
  177.   int old = u.p_sigmask;
  178.  
  179.   u.p_sigmask = ~0;
  180.   ObtainSemaphore(&ix.ix_semaph);
  181.   if (ix.ix_semaph.ss_NestCount == 1)
  182.     u.u_oldmask = old;
  183. }
  184.  
  185. void ix_unlock_base (void)
  186. {
  187.   int old = u.p_sigmask;
  188.  
  189.   if (ix.ix_semaph.ss_NestCount == 1)
  190.     old = u.u_oldmask;
  191.   ReleaseSemaphore(&ix.ix_semaph);
  192.   u.p_sigmask = old;
  193. }
  194.