/*
 * The Spar Library - modular math parser
 * Copyright (C) 2000,2001 Davide Angelocola <davide178@inwind.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#include <spar/sl_conf.h>
#include <spar/sl_module_manager.h>
#include <spar/sl_module_loader.h>
#include <spar/sl_module.h>
#include <spar/sl_io.h>
#include <spar/sl_error.h>
#include <spar/sl_util.h>

#define SL_MODULE_BUFFER_FILE	512

static int __module_ID_counter;

/* 
 * Strip whitespace from the start and end of STRING.  Return a pointer
 * into STRING. 
 */
static char *
__stripwhite (char *string)
{
  register char *s, *t;

  for (s = string; sl_isblank (*s); s++)
    ;

  if (*s == 0)
    return (s);

  t = s + strlen (s) - 1;

  while (t > s && sl_isblank (*t))
    t--;

  *++t = '\0';

  return s;
}

int
sl_module_loader_load_config (const char *filename)
{
  FILE *modules;
  char *line;

  sl_assert (filename != NULL);

  if ((modules = fopen (filename, "r")) == NULL)
    {
      sl_error_throw (SL_ERROR_CLASS_ERROR, SL_ERROR_CUSTOM,
		      "cannot open modules configuration file '%s'.",
		      filename);
      return SL_ERROR;
    }

  line = (char *) sl_malloc (SL_MODULE_BUFFER_FILE + 1);

  for (fgets (line, SL_MODULE_BUFFER_FILE, modules); !feof (modules);
       fgets (line, SL_MODULE_BUFFER_FILE, modules))
    {
      char *new = __stripwhite (line);
      new[strlen (new) - 1] = '\0';

      if ((sl_iscomment (*new)) || (*new == 0))
	continue;

      sl_module_loader_load (new);
    }

  sl_free (line, sizeof (line));
  fclose (modules);

  return SL_SUCCESS;
}

int
sl_module_loader_load (const char *filename)
{
  sl_module *(*__get_module) (void);
  void *handle;

  if ((handle = dlopen (filename, RTLD_NOW)) == NULL)
    {
      sl_error_throw (SL_ERROR_CLASS_ERROR, SL_ERROR_CUSTOM, "dlopen(): %s.",
		      dlerror ());
      return SL_ERROR;
    }

  dlerror ();
  if ((__get_module = (sl_module * (*)(void)) dlsym (handle, "sl_get_module"))
      == NULL)
    {
      sl_error_throw (SL_ERROR_CLASS_ERROR, SL_ERROR_CUSTOM, "dlsym(): %s.",
		      dlerror ());

      return SL_ERROR;
    }
  else
    {
      sl_module *m = (sl_module *) __get_module ();

      if (m->name == NULL)
	sl_error_throw (SL_ERROR_CLASS_WARNING, SL_ERROR_CUSTOM,
			"no module name.");

      if (m->description == NULL)
	sl_error_throw (SL_ERROR_CLASS_WARNING, SL_ERROR_CUSTOM,
			"no module description.");

      if (m->version == NULL)
	sl_error_throw (SL_ERROR_CLASS_WARNING, SL_ERROR_CUSTOM,
			"no module version.");

      sl_module_manager_add (m, __module_ID_counter++, handle);

      return SL_SUCCESS;
    }

}

void
sl_module_loader_init (void)
{
  /* 
   * Inits the module manager binary tree
   * Don't checks the return code  
   */
  sl_module_manager_init ();
  __module_ID_counter = 0;
}


syntax highlighted by Code2HTML, v. 0.9.1