2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2002 Free Software Foundation, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
#define SYMTAB_SIZE 509
41
static char buf[BUF_SIZE];
42
static struct symbol *symtab[SYMTAB_SIZE];
45
err (const char *fmt, ...)
49
fprintf (stderr, "genmoddep: error: ");
52
vfprintf (stderr, fmt, ap);
66
err ("out of memory");
72
xstrdup (const char *str)
78
s = (char *) xmalloc (len + 1);
79
memcpy (s, str, len + 1);
89
end = strlen (str) - 1;
98
symbol_hash (const char *s)
103
key = key * 65599 + *s++;
105
return (key + (key >> 5)) % SYMTAB_SIZE;
108
static struct symbol *
109
get_symbol (const char *name)
114
k = symbol_hash (name);
115
for (sym = symtab[k]; sym; sym = sym->next)
116
if (strcmp (sym->name, name) == 0)
123
add_symbol (const char *name, const char *mod)
128
if (get_symbol (name))
129
err ("duplicated symbol: %s", name);
131
sym = (struct symbol *) xmalloc (sizeof (*sym));
132
sym->name = xstrdup (name);
133
sym->mod = xstrdup (mod);
135
k = symbol_hash (name);
136
sym->next = symtab[k];
145
for (i = 0; i < SYMTAB_SIZE; i++)
147
struct symbol *p, *q;
153
free ((void *) p->name);
154
free ((void *) p->mod);
162
read_defined_symbols (FILE *fp)
164
while (fgets (buf, sizeof (buf), fp))
169
err ("empty symbol name: %s", buf);
171
p = strchr (buf, ' ');
173
err ("invalid line format: %s", buf);
178
err ("empty module name: %s", buf);
188
add_module (struct module **head, const char *name)
192
for (mod = *head; mod; mod = mod->next)
193
if (strcmp (mod->name, name) == 0)
196
mod = (struct module *) xmalloc (sizeof (*mod));
197
mod->name = xstrdup (name);
204
free_modules (struct module *head)
211
free ((void *) head->name);
218
find_dependencies (FILE *fp)
221
struct module *mod_list = 0;
224
if (! fgets (buf, sizeof (buf), fp) || buf[0] == '\n' || buf[0] == '\0')
225
err ("no module name");
228
mod_name = xstrdup (buf);
230
while (fgets (buf, sizeof (buf), fp))
235
sym = get_symbol (buf);
237
err ("%s in %s is not defined", buf, mod_name);
239
add_module (&mod_list, sym->mod);
242
printf ("%s:", mod_name);
244
for (mod = mod_list; mod; mod = mod->next)
245
if (strcmp (mod->name, "kernel") != 0)
246
printf (" %s", mod->name);
250
free_modules (mod_list);
254
main (int argc, char *argv[])
258
/* First, get defined symbols. */
259
read_defined_symbols (stdin);
261
/* Second, find the dependecies. */
262
for (i = 1; i < argc; i++)
266
fp = fopen (argv[i], "r");
268
err ("cannot open %s", argv[i]);
270
find_dependencies (fp);
275
/* Last, free memory. */