File: names.c

package info (click to toggle)
zorroutils 0.05-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 136 kB
  • ctags: 118
  • sloc: ansic: 666; perl: 44; makefile: 23
file content (241 lines) | stat: -rw-r--r-- 4,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 *	$Id: names.c,v 1.3 2000-08-12 12:01:19 geert Exp $
 *
 *	Linux Zorro Utilities -- Device ID to Name Translation
 *
 *	Copyright (C) 1998--2000 Geert Uytterhoeven
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include "zorroutils.h"

int show_numeric_ids;

char *zorro_ids = ZORRO_ID_DB;

static byte *name_list;
static int name_list_loaded;

struct nl_entry {
  struct nl_entry *next;
  int id1, id2;
  byte *name;
};

#define ID1_VENDOR -1
#define ID1_ERROR -2

#define HASH_SIZE 1024

static struct nl_entry *nl_hash[HASH_SIZE];

static inline unsigned int nl_calc_hash(int id1, int id2)
{
  unsigned int h;

  h = id1 ^ id2;
  h ^= (h >> 6);
  return h & (HASH_SIZE-1);
}

static struct nl_entry *nl_lookup(int id1, int id2)
{
  unsigned int h = nl_calc_hash(id1, id2);
  struct nl_entry *n = nl_hash[h];

  while (n && (n->id1 != id1 || n->id2 != id2))
    n = n->next;
  return n;
}

static int nl_add(int id1, int id2, byte *text)
{
  unsigned int h = nl_calc_hash(id1, id2);
  struct nl_entry *n = nl_hash[h];

  while (n && (n->id1 != id1 || n->id2 != id2))
    n = n->next;
  if (n)
    return 1;
  n = xmalloc(sizeof(struct nl_entry));
  n->id1 = id1;
  n->id2 = id2;
  n->name = text;
  n->next = nl_hash[h];
  nl_hash[h] = n;
  return 0;
}

static void
err_name_list(char *msg)
{
  fprintf(stderr, "%s: %s: %m\n", zorro_ids, msg);
  exit(1);
}

static void
parse_name_list(void)
{
  byte *p = name_list;
  byte *q, *r;
  int lino = 0;
  int id1 = ID1_ERROR;
  int id2 = 0;
  int i, j;

  while (*p)
    {
      lino++;
      q = p;
      while (*p && *p != '\n')
	{
	  if (*p == '#')
	    {
	      *p++ = 0;
	      while (*p && *p != '\n')
		p++;
	      break;
	    }
	  if (*p == '\t')
	    *p = ' ';
	  p++;
	}
      if (*p == '\n')
	*p++ = 0;
      if (!*q)
	continue;
      r = p;
      while (r > q && r[-1] == ' ')
	*--r = 0;
      r = q;
      while (*q == ' ')
	q++;
      if (strlen(q) < 5 || q[4] != ' ')
	goto parserr;
      if (r == q)
	{
	  if (sscanf(q, "%x", &j) != 1)
	    goto parserr;
	  i = ID1_VENDOR;
	  id1 = i;
	  id2 = j;
	}
      else
	{
	  if (sscanf(q, "%x", &j) != 1)
	    goto parserr;
	  if (id1 == ID1_ERROR)
	    goto parserr;
	  i = id2;
	}
      q += 4;
      while (*q == ' ')
	q++;
      if (!*q)
	goto parserr;
      if (nl_add(i, j, q))
	{
	  fprintf(stderr, "%s, line %d: duplicate entry\n", zorro_ids, lino);
	  exit(1);
	}
    }
  return;

parserr:
  fprintf(stderr, "%s, line %d: parse error\n", zorro_ids, lino);
  exit(1);
}

static void
load_name_list(void)
{
  int fd;
  struct stat st;

  fd = open(zorro_ids, O_RDONLY);
  if (fd < 0)
    {
      show_numeric_ids = 1;
      return;
    }
  if (fstat(fd, &st) < 0)
    err_name_list("stat");
  name_list = xmalloc(st.st_size + 1);
  if (read(fd, name_list, st.st_size) != st.st_size)
    err_name_list("read");
  name_list[st.st_size] = 0;
  parse_name_list();
  close(fd);
  name_list_loaded = 1;
}

char *
lookup_vendor(word i)
{
  static char vendbuf[6];

  if (!show_numeric_ids && !name_list_loaded)
    load_name_list();
  if (!show_numeric_ids)
    {
      struct nl_entry *e;

      e = nl_lookup(ID1_VENDOR, i);
      if (e)
	return e->name;
    }
  sprintf(vendbuf, "%04x", i);
  return vendbuf;
}

char *
lookup_device(word v, byte i, byte j)
{
  static char devbuf[6];

  if (!show_numeric_ids && !name_list_loaded)
    load_name_list();
  if (!show_numeric_ids)
    {
      struct nl_entry *e;

      e = nl_lookup(v, (i<<8) | j);
      if (e)
	return e->name;
    }
  sprintf(devbuf, "%02x:%02x", i, j);
  return devbuf;
}

char *
lookup_device_full(word v, byte i, byte j)
{
  static char fullbuf[256];

  if (!show_numeric_ids && !name_list_loaded)
    load_name_list();
  if (!show_numeric_ids)
    {
      struct nl_entry *e, *e2;

      e = nl_lookup(ID1_VENDOR, v);
      e2 = nl_lookup(v, (i<<8) | j);
      if (!e)
	sprintf(fullbuf, "Unknown device %04x:%02x:%02x", v, i, j);
      else if (!e2)
	sprintf(fullbuf, "%s: Unknown device %02x:%02x", e->name, i, j);
      else
	sprintf(fullbuf, "%s %s", e->name, e2->name);
    }
  else
    sprintf(fullbuf, "%04x:%02x:%02x", v, i, j);
  return fullbuf;
}