~ubuntu-branches/ubuntu/lucid/grub2/lucid

« back to all changes in this revision

Viewing changes to util/genmoddep.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060105152040-1ab076d4n3y2o5yf
Tags: 1.92-1
* New upstream release.
  - Add support for GPT partition table format.
  - Add a new command "play" to play an audio file on PC.
  - Add support for Linux/ADFS partition table format.
  - Add support for BASH-like scripting.
  - Add support for Apple HFS+ filesystems.
* 01_fix_grub-install.patch: Added. Fix grub-install to use
  /bin/grub-mkimage instead of /sbin/grub-mkimage. Closes: #338824
* Do not use CDBS tarball mode anymore. Closes: #344272  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2002  Free Software Foundation, Inc.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
#include <stdlib.h>
 
23
#include <stdarg.h>
 
24
 
 
25
#define BUF_SIZE        1024
 
26
#define SYMTAB_SIZE     509
 
27
 
 
28
struct symbol
 
29
{
 
30
  const char *name;
 
31
  const char *mod;
 
32
  struct symbol *next;
 
33
};
 
34
 
 
35
struct module
 
36
{
 
37
  const char *name;
 
38
  struct module *next;
 
39
};
 
40
 
 
41
static char buf[BUF_SIZE];
 
42
static struct symbol *symtab[SYMTAB_SIZE];
 
43
 
 
44
static void
 
45
err (const char *fmt, ...)
 
46
{
 
47
  va_list ap;
 
48
 
 
49
  fprintf (stderr, "genmoddep: error: ");
 
50
  
 
51
  va_start (ap, fmt);
 
52
  vfprintf (stderr, fmt, ap);
 
53
  va_end (ap);
 
54
 
 
55
  fputc ('\n', stderr);
 
56
  exit (1);
 
57
}
 
58
 
 
59
static void *
 
60
xmalloc (size_t size)
 
61
{
 
62
  void *p;
 
63
 
 
64
  p = malloc (size);
 
65
  if (! p)
 
66
    err ("out of memory");
 
67
 
 
68
  return p;
 
69
}
 
70
 
 
71
static char *
 
72
xstrdup (const char *str)
 
73
{
 
74
  char *s;
 
75
  size_t len;
 
76
 
 
77
  len = strlen (str);
 
78
  s = (char *) xmalloc (len + 1);
 
79
  memcpy (s, str, len + 1);
 
80
 
 
81
  return s;
 
82
}
 
83
 
 
84
static void
 
85
chomp (char *str)
 
86
{
 
87
  int end;
 
88
  
 
89
  end = strlen (str) - 1;
 
90
  if (end < 0)
 
91
    err ("empty string");
 
92
 
 
93
  if (str[end] == '\n')
 
94
    str[end] = '\0';
 
95
}
 
96
 
 
97
static unsigned
 
98
symbol_hash (const char *s)
 
99
{
 
100
  unsigned key = 0;
 
101
 
 
102
  while (*s)
 
103
    key = key * 65599 + *s++;
 
104
 
 
105
  return (key + (key >> 5)) % SYMTAB_SIZE;
 
106
}
 
107
 
 
108
static struct symbol *
 
109
get_symbol (const char *name)
 
110
{
 
111
  unsigned k;
 
112
  struct symbol *sym;
 
113
  
 
114
  k = symbol_hash (name);
 
115
  for (sym = symtab[k]; sym; sym = sym->next)
 
116
    if (strcmp (sym->name, name) == 0)
 
117
      return sym;
 
118
 
 
119
  return 0;
 
120
}
 
121
 
 
122
static void
 
123
add_symbol (const char *name, const char *mod)
 
124
{
 
125
  unsigned k;
 
126
  struct symbol *sym;
 
127
 
 
128
  if (get_symbol (name))
 
129
    err ("duplicated symbol: %s", name);
 
130
  
 
131
  sym = (struct symbol *) xmalloc (sizeof (*sym));
 
132
  sym->name = xstrdup (name);
 
133
  sym->mod = xstrdup (mod);
 
134
 
 
135
  k = symbol_hash (name);
 
136
  sym->next = symtab[k];
 
137
  symtab[k] = sym;
 
138
}
 
139
 
 
140
static void
 
141
free_symbols (void)
 
142
{
 
143
  int i;
 
144
 
 
145
  for (i = 0; i < SYMTAB_SIZE; i++)
 
146
    {
 
147
      struct symbol *p, *q;
 
148
 
 
149
      p = symtab[i];
 
150
      while (p)
 
151
        {
 
152
          q = p->next;
 
153
          free ((void *) p->name);
 
154
          free ((void *) p->mod);
 
155
          free (p);
 
156
          p = q;
 
157
        }
 
158
    }
 
159
}
 
160
 
 
161
static void
 
162
read_defined_symbols (FILE *fp)
 
163
{
 
164
  while (fgets (buf, sizeof (buf), fp))
 
165
    {
 
166
      char *p;
 
167
 
 
168
      if (! *buf)
 
169
        err ("empty symbol name: %s", buf);
 
170
      
 
171
      p = strchr (buf, ' ');
 
172
      if (! p)
 
173
        err ("invalid line format: %s", buf);
 
174
 
 
175
      p++;
 
176
      
 
177
      if (! *p)
 
178
        err ("empty module name: %s", buf);
 
179
 
 
180
      *(p - 1) = '\0';
 
181
      chomp (p);
 
182
      
 
183
      add_symbol (buf, p);
 
184
    }
 
185
}
 
186
 
 
187
static void
 
188
add_module (struct module **head, const char *name)
 
189
{
 
190
  struct module *mod;
 
191
 
 
192
  for (mod = *head; mod; mod = mod->next)
 
193
    if (strcmp (mod->name, name) == 0)
 
194
      return;
 
195
 
 
196
  mod = (struct module *) xmalloc (sizeof (*mod));
 
197
  mod->name = xstrdup (name);
 
198
 
 
199
  mod->next = *head;
 
200
  *head = mod;
 
201
}
 
202
 
 
203
static void
 
204
free_modules (struct module *head)
 
205
{
 
206
  struct module *next;
 
207
 
 
208
  while (head)
 
209
    {
 
210
      next = head->next;
 
211
      free ((void *) head->name);
 
212
      free (head);
 
213
      head = next;
 
214
    }
 
215
}
 
216
 
 
217
static void
 
218
find_dependencies (FILE *fp)
 
219
{
 
220
  char *mod_name;
 
221
  struct module *mod_list = 0;
 
222
  struct module *mod;
 
223
  
 
224
  if (! fgets (buf, sizeof (buf), fp) || buf[0] == '\n' || buf[0] == '\0')
 
225
    err ("no module name");
 
226
 
 
227
  chomp (buf);
 
228
  mod_name = xstrdup (buf);
 
229
 
 
230
  while (fgets (buf, sizeof (buf), fp))
 
231
    {
 
232
      struct symbol *sym;
 
233
      
 
234
      chomp (buf);
 
235
      sym = get_symbol (buf);
 
236
      if (! sym)
 
237
        err ("%s in %s is not defined", buf, mod_name);
 
238
 
 
239
      add_module (&mod_list, sym->mod);
 
240
    }
 
241
 
 
242
  printf ("%s:", mod_name);
 
243
  
 
244
  for (mod = mod_list; mod; mod = mod->next)
 
245
    if (strcmp (mod->name, "kernel") != 0)
 
246
      printf (" %s", mod->name);
 
247
  
 
248
  putchar ('\n');
 
249
 
 
250
  free_modules (mod_list);
 
251
}
 
252
 
 
253
int
 
254
main (int argc, char *argv[])
 
255
{
 
256
  int i;
 
257
  
 
258
  /* First, get defined symbols.  */
 
259
  read_defined_symbols (stdin);
 
260
 
 
261
  /* Second, find the dependecies.  */
 
262
  for (i = 1; i < argc; i++)
 
263
    {
 
264
      FILE *fp;
 
265
 
 
266
      fp = fopen (argv[i], "r");
 
267
      if (! fp)
 
268
        err ("cannot open %s", argv[i]);
 
269
 
 
270
      find_dependencies (fp);
 
271
 
 
272
      fclose (fp);
 
273
    }
 
274
 
 
275
  /* Last, free memory.  */
 
276
  free_symbols ();
 
277
  
 
278
  return 0;
 
279
}