home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / ix_init.c < prev    next >
C/C++ Source or Header  |  1996-10-01  |  5KB  |  185 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" },
  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.     Wait(1 << 31);
  63. }
  64.  
  65. int open_libraries(void)
  66. {
  67.   int i;
  68.  
  69.   for (i = 0; ix_libs[i].base; i++)
  70.     if (!(*(ix_libs[i].base) = (void *)OpenLibrary(ix_libs[i].name, ix_libs[i].minver))
  71.         && !ix_libs[i].opt)
  72.       {
  73.     ix_panic("%s required!", ix_libs[i].name);
  74.     return 0;
  75.       }
  76.   return 1;
  77. }
  78.  
  79. void close_libraries(void)
  80. {
  81.   int i;
  82.   
  83.   for (i = 0; ix_libs[i].base; i++)
  84.     if (*ix_libs[i].base)
  85.       CloseLibrary(*ix_libs[i].base);
  86. }
  87.  
  88. struct ixemul_base *ix_init (struct ixemul_base *ixbase)
  89. {
  90.   int i;
  91.   char buf[256];
  92.   extern char hostname[];  /* in getcrap.c */
  93.  
  94.   ixemulbase = ixbase;
  95.   if (!open_libraries())
  96.     {
  97.       close_libraries();
  98.       return 0;
  99.     }
  100.  
  101.   ixbase->ix_file_tab = (struct file *)AllocMem (NOFILE * sizeof(struct file), MEMF_PUBLIC | MEMF_CLEAR);
  102.   ixbase->ix_fileNFILE = ixbase->ix_file_tab + NOFILE;
  103.   ixbase->ix_lastf = ixbase->ix_file_tab;
  104.   
  105.   memset(&ixbase->ix_notify_request, 0, sizeof(ixbase->ix_notify_request));
  106.   memset(&ixbase->ix_ptys, 0, sizeof(ixbase->ix_ptys));
  107.  
  108.   /* Read the GMT offset. This environment variable is 5 bytes long. The
  109.      first 4 form a long that contains the offset in seconds and the fifth
  110.      byte is ignored. */
  111.   if (GetVar("IXGMTOFFSET", buf, 6, GVF_BINARY_VAR) == 5 && IoErr() == 5)
  112.     ix_set_gmt_offset(*((long *)buf));
  113.   else
  114.     ix_set_gmt_offset(0);
  115.  
  116.   if (GetVar(IX_ENV_SETTINGS, buf, sizeof(struct ix_settings) + 1, GVF_BINARY_VAR) == sizeof(struct ix_settings))
  117.     ix_set_settings((struct ix_settings *)buf);
  118.   else
  119.     ix_set_settings(ix_get_default_settings());
  120.  
  121.   /* Set the hostname if the environment variable HOSTNAME exists. */
  122.   if (GetVar("HOSTNAME", buf, 64, 0) > 0)
  123.     strcpy(hostname, buf);
  124.  
  125.   /* initialize the list structures for the allocator */
  126.   init_buddy ();
  127.  
  128.   ixbase->ix_task_switcher = CreateTask("ixemul task switcher", 9, ix_task_switcher, 1000);
  129.  
  130.   /* initialize port number for AF_UNIX(localhost) sockets */
  131.   ixbase->ix_next_free_port = 1024;
  132.  
  133.   if (ixbase->ix_file_tab)
  134.     {
  135.       InitSemaphore (& ixbase->ix_semaph);
  136.  
  137.       configure_context_switch ();
  138.  
  139.       NewList ((struct List *) &ixbase->ix_socket_list);
  140.  
  141.       for (i = 0; i < IX_NUM_SLEEP_QUEUES; i++)
  142.         NewList ((struct List *) &ixbase->ix_sleep_queues[i]);
  143.  
  144.       ixbase->ix_global_environment = NULL;
  145.  
  146.       return ixbase;
  147.     }
  148.  
  149.   if (ixbase->ix_task_switcher)
  150.     {
  151.       Forbid();
  152.       DeleteTask(ixbase->ix_task_switcher);
  153.       Permit();
  154.     }
  155.  
  156.   if (ixbase->ix_file_tab)
  157.     FreeMem (ixbase->ix_file_tab, NOFILE * sizeof(struct file));
  158.   else
  159.     ix_panic ("out of memory");
  160.  
  161.   close_libraries();
  162.   
  163.   return 0;
  164. }      
  165.  
  166. void ix_lock_base (void)
  167. {
  168.   int old = u.p_sigmask;
  169.  
  170.   u.p_sigmask = ~0;
  171.   ObtainSemaphore(&ix.ix_semaph);
  172.   if (ix.ix_semaph.ss_NestCount == 1)
  173.     u.u_oldmask = old;
  174. }
  175.  
  176. void ix_unlock_base (void)
  177. {
  178.   int old = u.p_sigmask;
  179.  
  180.   if (ix.ix_semaph.ss_NestCount == 1)
  181.     old = u.u_oldmask;
  182.   ReleaseSemaphore(&ix.ix_semaph);
  183.   u.p_sigmask = old;
  184. }
  185.