llow parsing of usage strings.

This commit is contained in:
augustss 2000-09-24 02:17:52 +00:00
parent 8d014ac8a4
commit cb9f491cfe
2 changed files with 56 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: usage.c,v 1.6 2000/08/13 22:22:02 augustss Exp $ */
/* $NetBSD: usage.c,v 1.7 2000/09/24 02:17:52 augustss Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
@ -192,3 +192,40 @@ hid_usage_in_page(unsigned int u)
sprintf(b, "0x%04x", i);
return b;
}
int
hid_parse_usage_page(const char *name)
{
int k;
if (!pages)
errx(1, "no hid table\n");
for (k = 0; k < npages; k++)
if (strcmp(pages[k].name, name) == 0)
return pages[k].usage;
return -1;
}
/* XXX handle hex */
int
hid_parse_usage_in_page(const char *name)
{
const char *sep = strchr(name, ':');
int k, j;
unsigned int l;
if (sep == NULL)
return -1;
l = sep - name;
for (k = 0; k < npages; k++)
if (strncmp(pages[k].name, name, l) == 0)
goto found;
return -1;
found:
sep++;
for (j = 0; j < pages[k].pagesize; j++)
if (strcmp(pages[k].page_contents[j].name, sep) == 0)
return (pages[k].usage << 16) | pages[k].page_contents[j].usage;
return (-1);
}

View File

@ -1,4 +1,4 @@
.\" $NetBSD: usb.3,v 1.12 2000/09/24 02:13:24 augustss Exp $
.\" $NetBSD: usb.3,v 1.13 2000/09/24 02:17:52 augustss Exp $
.\"
.\" Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
.\" All rights reserved.
@ -67,6 +67,10 @@
.Fn hid_usage_page "int i"
.Ft char *
.Fn hid_usage_in_page "u_int u"
.Ft int
.Fn hid_parse_usage_page "const char *"
.Ft char *
.Fn hid_parse_usage_in_page "const char *"
.Ft void
.Fn hid_init "char *file"
.Ft int
@ -162,7 +166,19 @@ will return the symbolic name of a usage page, and the function
.Fn hid_usage_in_page
will return the symbolic name of the usage within the page.
Both these functions may return a pointer to static data.
Before either of these functions can be called the usage table
.Pp
The functions
.Fn hid_parse_usage_page
and
.Fn hid_parse_usage_in_page
are the inverses of
.Fn hid_usage_page
and
.Fn hid_usage_in_page .
They take a usage string and return the number of the usage, or -1
if it cannot be found.
.Pp
Before any of these functions can be called the usage table
must be parsed, this is done by calling
.Fn hid_init
with the name of the table. Passing