Initial revision
This commit is contained in:
commit
6a161fa938
80 changed files with 23264 additions and 0 deletions
279
util/genmoddep.c
Normal file
279
util/genmoddep.c
Normal file
|
@ -0,0 +1,279 @@
|
|||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define BUF_SIZE 1024
|
||||
#define SYMTAB_SIZE 509
|
||||
|
||||
struct symbol
|
||||
{
|
||||
const char *name;
|
||||
const char *mod;
|
||||
struct symbol *next;
|
||||
};
|
||||
|
||||
struct module
|
||||
{
|
||||
const char *name;
|
||||
struct module *next;
|
||||
};
|
||||
|
||||
static char buf[BUF_SIZE];
|
||||
static struct symbol *symtab[SYMTAB_SIZE];
|
||||
|
||||
static void
|
||||
err (const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf (stderr, "genmoddep: error: ");
|
||||
|
||||
va_start (ap, fmt);
|
||||
vfprintf (stderr, fmt, ap);
|
||||
va_end (ap);
|
||||
|
||||
fputc ('\n', stderr);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
static void *
|
||||
xmalloc (size_t size)
|
||||
{
|
||||
void *p;
|
||||
|
||||
p = malloc (size);
|
||||
if (! p)
|
||||
err ("out of memory");
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
static char *
|
||||
xstrdup (const char *str)
|
||||
{
|
||||
char *s;
|
||||
size_t len;
|
||||
|
||||
len = strlen (str);
|
||||
s = (char *) xmalloc (len + 1);
|
||||
memcpy (s, str, len + 1);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static void
|
||||
chomp (char *str)
|
||||
{
|
||||
int end;
|
||||
|
||||
end = strlen (str) - 1;
|
||||
if (end < 0)
|
||||
err ("empty string");
|
||||
|
||||
if (str[end] == '\n')
|
||||
str[end] = '\0';
|
||||
}
|
||||
|
||||
static unsigned
|
||||
symbol_hash (const char *s)
|
||||
{
|
||||
unsigned key = 0;
|
||||
|
||||
while (*s)
|
||||
key = key * 65599 + *s++;
|
||||
|
||||
return (key + (key >> 5)) % SYMTAB_SIZE;
|
||||
}
|
||||
|
||||
static struct symbol *
|
||||
get_symbol (const char *name)
|
||||
{
|
||||
unsigned k;
|
||||
struct symbol *sym;
|
||||
|
||||
k = symbol_hash (name);
|
||||
for (sym = symtab[k]; sym; sym = sym->next)
|
||||
if (strcmp (sym->name, name) == 0)
|
||||
return sym;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
add_symbol (const char *name, const char *mod)
|
||||
{
|
||||
unsigned k;
|
||||
struct symbol *sym;
|
||||
|
||||
if (get_symbol (name))
|
||||
err ("duplicated symbol: %s", name);
|
||||
|
||||
sym = (struct symbol *) xmalloc (sizeof (*sym));
|
||||
sym->name = xstrdup (name);
|
||||
sym->mod = xstrdup (mod);
|
||||
|
||||
k = symbol_hash (name);
|
||||
sym->next = symtab[k];
|
||||
symtab[k] = sym;
|
||||
}
|
||||
|
||||
static void
|
||||
free_symbols (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < SYMTAB_SIZE; i++)
|
||||
{
|
||||
struct symbol *p, *q;
|
||||
|
||||
p = symtab[i];
|
||||
while (p)
|
||||
{
|
||||
q = p->next;
|
||||
free ((void *) p->name);
|
||||
free ((void *) p->mod);
|
||||
free (p);
|
||||
p = q;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
read_defined_symbols (FILE *fp)
|
||||
{
|
||||
while (fgets (buf, sizeof (buf), fp))
|
||||
{
|
||||
char *p;
|
||||
|
||||
if (! *buf)
|
||||
err ("empty symbol name: %s", buf);
|
||||
|
||||
p = strchr (buf, ' ');
|
||||
if (! p)
|
||||
err ("invalid line format: %s", buf);
|
||||
|
||||
p++;
|
||||
|
||||
if (! *p)
|
||||
err ("empty module name: %s", buf);
|
||||
|
||||
*(p - 1) = '\0';
|
||||
chomp (p);
|
||||
|
||||
add_symbol (buf, p);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
add_module (struct module **head, const char *name)
|
||||
{
|
||||
struct module *mod;
|
||||
|
||||
for (mod = *head; mod; mod = mod->next)
|
||||
if (strcmp (mod->name, name) == 0)
|
||||
return;
|
||||
|
||||
mod = (struct module *) xmalloc (sizeof (*mod));
|
||||
mod->name = xstrdup (name);
|
||||
|
||||
mod->next = *head;
|
||||
*head = mod;
|
||||
}
|
||||
|
||||
static void
|
||||
free_modules (struct module *head)
|
||||
{
|
||||
struct module *next;
|
||||
|
||||
while (head)
|
||||
{
|
||||
next = head->next;
|
||||
free ((void *) head->name);
|
||||
free (head);
|
||||
head = next;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
find_dependencies (FILE *fp)
|
||||
{
|
||||
char *mod_name;
|
||||
struct module *mod_list = 0;
|
||||
struct module *mod;
|
||||
|
||||
if (! fgets (buf, sizeof (buf), fp) || buf[0] == '\n' || buf[0] == '\0')
|
||||
err ("no module name");
|
||||
|
||||
chomp (buf);
|
||||
mod_name = xstrdup (buf);
|
||||
|
||||
while (fgets (buf, sizeof (buf), fp))
|
||||
{
|
||||
struct symbol *sym;
|
||||
|
||||
chomp (buf);
|
||||
sym = get_symbol (buf);
|
||||
if (! sym)
|
||||
err ("%s in %s is not defined", buf, mod_name);
|
||||
|
||||
add_module (&mod_list, sym->mod);
|
||||
}
|
||||
|
||||
printf ("%s:", mod_name);
|
||||
|
||||
for (mod = mod_list; mod; mod = mod->next)
|
||||
if (strcmp (mod->name, "kernel") != 0)
|
||||
printf (" %s", mod->name);
|
||||
|
||||
putchar ('\n');
|
||||
|
||||
free_modules (mod_list);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
|
||||
/* First, get defined symbols. */
|
||||
read_defined_symbols (stdin);
|
||||
|
||||
/* Second, find the dependecies. */
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen (argv[i], "r");
|
||||
if (! fp)
|
||||
err ("cannot open %s", argv[i]);
|
||||
|
||||
find_dependencies (fp);
|
||||
|
||||
fclose (fp);
|
||||
}
|
||||
|
||||
/* Last, free memory. */
|
||||
free_symbols ();
|
||||
|
||||
return 0;
|
||||
}
|
223
util/i386/pc/grub-mkimage.c
Normal file
223
util/i386/pc/grub-mkimage.c
Normal file
|
@ -0,0 +1,223 @@
|
|||
/* pupa-mkimage.c - make a bootable image */
|
||||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org>
|
||||
*
|
||||
* PUPA is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PUPA; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include <pupa/types.h>
|
||||
#include <pupa/machine/boot.h>
|
||||
#include <pupa/machine/kernel.h>
|
||||
#include <pupa/kernel.h>
|
||||
#include <pupa/disk.h>
|
||||
#include <pupa/util/misc.h>
|
||||
#include <pupa/util/resolve.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define _GNU_SOURCE 1
|
||||
#include <getopt.h>
|
||||
|
||||
static void
|
||||
generate_image (const char *dir, FILE *out, char *mods[])
|
||||
{
|
||||
pupa_addr_t module_addr = 0;
|
||||
char *kernel_img, *boot_img;
|
||||
size_t kernel_size, boot_size, total_module_size;
|
||||
char *kernel_path, *boot_path;
|
||||
unsigned num;
|
||||
struct pupa_util_path_list *path_list, *p, *next;
|
||||
|
||||
path_list = pupa_util_resolve_dependencies (dir, "moddep.lst", mods);
|
||||
|
||||
kernel_path = pupa_util_get_path (dir, "kernel.img");
|
||||
kernel_size = pupa_util_get_image_size (kernel_path);
|
||||
|
||||
total_module_size = 0;
|
||||
for (p = path_list; p; p = p->next)
|
||||
total_module_size += (pupa_util_get_image_size (p->name)
|
||||
+ sizeof (struct pupa_module_header));
|
||||
|
||||
pupa_util_info ("the total module size is 0x%x", total_module_size);
|
||||
|
||||
num = ((kernel_size + total_module_size + PUPA_DISK_SECTOR_SIZE - 1)
|
||||
>> PUPA_DISK_SECTOR_BITS);
|
||||
if (num > 0xffff)
|
||||
pupa_util_error ("the core image is too big");
|
||||
|
||||
boot_path = pupa_util_get_path (dir, "diskboot.img");
|
||||
boot_size = pupa_util_get_image_size (boot_path);
|
||||
if (boot_size != PUPA_DISK_SECTOR_SIZE)
|
||||
pupa_util_error ("diskboot.img is not one sector size");
|
||||
|
||||
boot_img = pupa_util_read_image (boot_path);
|
||||
|
||||
/* i386 is a little endian architecture. */
|
||||
*((pupa_uint16_t *) (boot_img + PUPA_DISK_SECTOR_SIZE
|
||||
- PUPA_BOOT_MACHINE_LIST_SIZE + 4))
|
||||
= pupa_cpu_to_le16 (num);
|
||||
|
||||
pupa_util_write_image (boot_img, boot_size, out);
|
||||
free (boot_img);
|
||||
free (boot_path);
|
||||
|
||||
kernel_img = pupa_util_read_image (kernel_path);
|
||||
module_addr = (path_list
|
||||
? (PUPA_BOOT_MACHINE_KERNEL_ADDR + PUPA_DISK_SECTOR_SIZE
|
||||
+ kernel_size)
|
||||
: 0);
|
||||
|
||||
pupa_util_info ("the first module address is 0x%x", module_addr);
|
||||
*((pupa_uint32_t *) (kernel_img + PUPA_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
|
||||
= pupa_cpu_to_le32 (total_module_size);
|
||||
*((pupa_uint32_t *) (kernel_img + PUPA_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
|
||||
= pupa_cpu_to_le32 (kernel_size);
|
||||
|
||||
pupa_util_write_image (kernel_img, kernel_size, out);
|
||||
free (kernel_img);
|
||||
free (kernel_path);
|
||||
|
||||
while (path_list)
|
||||
{
|
||||
struct pupa_module_header header;
|
||||
size_t mod_size;
|
||||
char *mod_img;
|
||||
|
||||
next = path_list->next;
|
||||
|
||||
mod_size = pupa_util_get_image_size (path_list->name);
|
||||
|
||||
header.offset = pupa_cpu_to_le32 (sizeof (header));
|
||||
header.size = pupa_cpu_to_le32 (mod_size + sizeof (header));
|
||||
|
||||
pupa_util_info ("offset=0x%x, size=0x%x", header.offset, header.size);
|
||||
pupa_util_write_image ((char *) &header, sizeof (header), out);
|
||||
|
||||
mod_img = pupa_util_read_image (path_list->name);
|
||||
pupa_util_write_image (mod_img, mod_size, out);
|
||||
free (mod_img);
|
||||
|
||||
free ((void *) path_list->name);
|
||||
free (path_list);
|
||||
path_list = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct option options[] =
|
||||
{
|
||||
{"directory", required_argument, 0, 'd'},
|
||||
{"output", required_argument, 0, 'o'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'V'},
|
||||
{"verbose", no_argument, 0, 'v'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
static void
|
||||
usage (int status)
|
||||
{
|
||||
if (status)
|
||||
fprintf (stderr, "Try ``pupa-mkimage --help'' for more information.\n");
|
||||
else
|
||||
printf ("\
|
||||
Usage: pupa-mkimage [OPTION]... [MODULES]\n\
|
||||
\n\
|
||||
Make a bootable image of PUPA.\n\
|
||||
\n\
|
||||
-d, --directory=DIR use images and modules under DIR [default=%s]\n\
|
||||
-o, --output=FILE output a generated image to FILE [default=stdout]\n\
|
||||
-h, --help display this image and exit\n\
|
||||
-V, --version print version information and exit\n\
|
||||
-v, --verbose print verbose messages\n\
|
||||
\n\
|
||||
Report bugs to <okuji@enbug.org>.\n\
|
||||
", PUPA_DATADIR);
|
||||
|
||||
exit (status);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
char *output = 0;
|
||||
char *dir = 0;
|
||||
FILE *fp = stdout;
|
||||
|
||||
progname = "pupa-mkimage";
|
||||
|
||||
while (1)
|
||||
{
|
||||
int c = getopt_long (argc, argv, "d:o:hVv", options, 0);
|
||||
|
||||
if (c == -1)
|
||||
break;
|
||||
else
|
||||
switch (c)
|
||||
{
|
||||
case 'o':
|
||||
if (output)
|
||||
free (output);
|
||||
|
||||
output = xstrdup (optarg);
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
if (dir)
|
||||
free (dir);
|
||||
|
||||
dir = xstrdup (optarg);
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
usage (0);
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
printf ("pupa-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
|
||||
return 0;
|
||||
|
||||
case 'v':
|
||||
verbosity++;
|
||||
break;
|
||||
|
||||
default:
|
||||
usage (1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (output)
|
||||
{
|
||||
fp = fopen (output, "wb");
|
||||
if (! fp)
|
||||
pupa_util_error ("cannot open %s", output);
|
||||
}
|
||||
|
||||
generate_image (dir ? : PUPA_DATADIR, fp, argv + optind);
|
||||
|
||||
fclose (fp);
|
||||
|
||||
if (dir)
|
||||
free (dir);
|
||||
|
||||
return 0;
|
||||
}
|
137
util/misc.c
Normal file
137
util/misc.c
Normal file
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org>
|
||||
*
|
||||
* PUPA is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PUPA; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <pupa/util/misc.h>
|
||||
|
||||
char *progname = 0;
|
||||
int verbosity = 0;
|
||||
|
||||
void
|
||||
pupa_util_info (const char *fmt, ...)
|
||||
{
|
||||
if (verbosity > 0)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf (stderr, "%s: info: ", progname);
|
||||
va_start (ap, fmt);
|
||||
vfprintf (stderr, fmt, ap);
|
||||
va_end (ap);
|
||||
fputc ('\n', stderr);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
pupa_util_error (const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf (stderr, "%s: error: ", progname);
|
||||
va_start (ap, fmt);
|
||||
vfprintf (stderr, fmt, ap);
|
||||
va_end (ap);
|
||||
fputc ('\n', stderr);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
void *
|
||||
xmalloc (size_t size)
|
||||
{
|
||||
void *p;
|
||||
|
||||
p = malloc (size);
|
||||
if (! p)
|
||||
pupa_util_error ("out of memory");
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
char *
|
||||
xstrdup (const char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *dup;
|
||||
|
||||
len = strlen (str);
|
||||
dup = (char *) xmalloc (len + 1);
|
||||
memcpy (dup, str, len + 1);
|
||||
|
||||
return dup;
|
||||
}
|
||||
|
||||
char *
|
||||
pupa_util_get_path (const char *dir, const char *file)
|
||||
{
|
||||
char *path;
|
||||
|
||||
path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
|
||||
sprintf (path, "%s/%s", dir, file);
|
||||
return path;
|
||||
}
|
||||
|
||||
size_t
|
||||
pupa_util_get_image_size (const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
pupa_util_info ("getting the size of %s", path);
|
||||
|
||||
if (stat (path, &st) == -1)
|
||||
pupa_util_error ("cannot stat %s", path);
|
||||
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
char *
|
||||
pupa_util_read_image (const char *path)
|
||||
{
|
||||
char *img;
|
||||
FILE *fp;
|
||||
size_t size;
|
||||
|
||||
pupa_util_info ("reading %s", path);
|
||||
|
||||
size = pupa_util_get_image_size (path);
|
||||
img = (char *) xmalloc (size);
|
||||
|
||||
fp = fopen (path, "rb");
|
||||
if (! fp)
|
||||
pupa_util_error ("cannot open %s", path);
|
||||
|
||||
if (fread (img, 1, size, fp) != size)
|
||||
pupa_util_error ("cannot read %s", path);
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
void
|
||||
pupa_util_write_image (const char *img, size_t size, FILE *out)
|
||||
{
|
||||
pupa_util_info ("writing 0x%x bytes", size);
|
||||
if (fwrite (img, 1, size, out) != size)
|
||||
pupa_util_error ("write failed");
|
||||
}
|
||||
|
257
util/resolve.c
Normal file
257
util/resolve.c
Normal file
|
@ -0,0 +1,257 @@
|
|||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org>
|
||||
*
|
||||
* PUPA is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PUPA; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <pupa/util/resolve.h>
|
||||
#include <pupa/util/misc.h>
|
||||
|
||||
/* Module. */
|
||||
struct mod_list
|
||||
{
|
||||
const char *name;
|
||||
struct mod_list *next;
|
||||
};
|
||||
|
||||
/* Dependency. */
|
||||
struct dep_list
|
||||
{
|
||||
const char *name;
|
||||
struct mod_list *list;
|
||||
struct dep_list *next;
|
||||
};
|
||||
|
||||
static char buf[1024];
|
||||
|
||||
static void
|
||||
free_mod_list (struct mod_list *head)
|
||||
{
|
||||
while (head)
|
||||
{
|
||||
struct mod_list *next;
|
||||
|
||||
next = head->next;
|
||||
free ((void *) head->name);
|
||||
free (head);
|
||||
head = next;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
free_dep_list (struct dep_list *head)
|
||||
{
|
||||
while (head)
|
||||
{
|
||||
struct dep_list *next;
|
||||
|
||||
next = head->next;
|
||||
free ((void *) head->name);
|
||||
free_mod_list (head->list);
|
||||
free (head);
|
||||
head = next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Read the list of dependencies. */
|
||||
static struct dep_list *
|
||||
read_dep_list (FILE *fp)
|
||||
{
|
||||
struct dep_list *dep_list = 0;
|
||||
|
||||
while (fgets (buf, sizeof (buf), fp))
|
||||
{
|
||||
char *p;
|
||||
struct dep_list *dep;
|
||||
|
||||
/* Get the target name. */
|
||||
p = strchr (buf, ':');
|
||||
if (! p)
|
||||
pupa_util_error ("invalid line format: %s", buf);
|
||||
|
||||
*p++ = '\0';
|
||||
|
||||
dep = xmalloc (sizeof (*dep));
|
||||
dep->name = xstrdup (buf);
|
||||
dep->list = 0;
|
||||
|
||||
dep->next = dep_list;
|
||||
dep_list = dep;
|
||||
|
||||
/* Add dependencies. */
|
||||
while (*p)
|
||||
{
|
||||
struct mod_list *mod;
|
||||
char *name;
|
||||
|
||||
/* Skip white spaces. */
|
||||
while (*p && isspace (*p))
|
||||
p++;
|
||||
|
||||
if (! *p)
|
||||
break;
|
||||
|
||||
name = p;
|
||||
|
||||
/* Skip non-WSPs. */
|
||||
while (*p && ! isspace (*p))
|
||||
p++;
|
||||
|
||||
*p++ = '\0';
|
||||
|
||||
mod = (struct mod_list *) xmalloc (sizeof (*mod));
|
||||
mod->name = xstrdup (name);
|
||||
mod->next = dep->list;
|
||||
dep->list = mod;
|
||||
}
|
||||
}
|
||||
|
||||
return dep_list;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_module_name (const char *str)
|
||||
{
|
||||
char *base;
|
||||
char *ext;
|
||||
|
||||
base = strrchr (str, '/');
|
||||
if (! base)
|
||||
base = (char *) str;
|
||||
else
|
||||
base++;
|
||||
|
||||
ext = strrchr (base, '.');
|
||||
if (ext && strcmp (ext, ".mod") == 0)
|
||||
{
|
||||
char *name;
|
||||
|
||||
name = xmalloc (ext - base + 1);
|
||||
memcpy (name, base, ext - base);
|
||||
name[ext - base] = '\0';
|
||||
return name;
|
||||
}
|
||||
|
||||
return xstrdup (base);
|
||||
}
|
||||
|
||||
static char *
|
||||
get_module_path (const char *prefix, const char *str)
|
||||
{
|
||||
char *dir;
|
||||
char *base;
|
||||
char *ext;
|
||||
char *ret;
|
||||
|
||||
ext = strrchr (str, '.');
|
||||
if (ext && strcmp (ext, ".mod") == 0)
|
||||
base = xstrdup (str);
|
||||
else
|
||||
{
|
||||
base = xmalloc (strlen (str) + 4 + 1);
|
||||
sprintf (base, "%s.mod", str);
|
||||
}
|
||||
|
||||
dir = strchr (str, '/');
|
||||
if (dir)
|
||||
return base;
|
||||
|
||||
ret = pupa_util_get_path (prefix, base);
|
||||
free (base);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
add_module (const char *dir,
|
||||
struct dep_list *dep_list,
|
||||
struct mod_list **mod_head,
|
||||
struct pupa_util_path_list **path_head,
|
||||
const char *name)
|
||||
{
|
||||
char *mod_name;
|
||||
struct pupa_util_path_list *path;
|
||||
struct mod_list *mod;
|
||||
struct dep_list *dep;
|
||||
|
||||
mod_name = get_module_name (name);
|
||||
|
||||
/* Check if the module has already been added. */
|
||||
for (mod = *mod_head; mod; mod = mod->next)
|
||||
if (strcmp (mod->name, mod_name) == 0)
|
||||
{
|
||||
free (mod_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Resolve dependencies. */
|
||||
for (dep = dep_list; dep; dep = dep->next)
|
||||
if (strcmp (dep->name, mod_name) == 0)
|
||||
{
|
||||
for (mod = dep->list; mod; mod = mod->next)
|
||||
add_module (dir, dep_list, mod_head, path_head, mod->name);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Add this module. */
|
||||
mod = (struct mod_list *) xmalloc (sizeof (*mod));
|
||||
mod->name = mod_name;
|
||||
mod->next = *mod_head;
|
||||
*mod_head = mod;
|
||||
|
||||
/* Add this path. */
|
||||
path = (struct pupa_util_path_list *) xmalloc (sizeof (*path));
|
||||
path->name = get_module_path (dir, name);
|
||||
path->next = *path_head;
|
||||
*path_head = path;
|
||||
}
|
||||
|
||||
struct pupa_util_path_list *
|
||||
pupa_util_resolve_dependencies (const char *prefix,
|
||||
const char *dep_list_file,
|
||||
char *modules[])
|
||||
{
|
||||
char *path;
|
||||
FILE *fp;
|
||||
struct dep_list *dep_list;
|
||||
struct mod_list *mod_list = 0;
|
||||
struct pupa_util_path_list *path_list = 0;
|
||||
|
||||
path = pupa_util_get_path (prefix, dep_list_file);
|
||||
fp = fopen (path, "r");
|
||||
if (! fp)
|
||||
pupa_util_error ("cannot open %s", path);
|
||||
|
||||
free (path);
|
||||
dep_list = read_dep_list (fp);
|
||||
fclose (fp);
|
||||
|
||||
while (*modules)
|
||||
{
|
||||
add_module (prefix, dep_list, &mod_list, &path_list, *modules);
|
||||
modules++;
|
||||
}
|
||||
|
||||
free_dep_list (dep_list);
|
||||
free_mod_list (mod_list);
|
||||
|
||||
return path_list;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue