kernel: Use hashdestroy() to free hash tables allocated with hashinit().
[dragonfly.git] / usr.bin / doscmd / dispatch.h
1 /*
2 ** Copyright (c) 1996
3 **      Michael Smith.  All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 **    notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 **    notice, this list of conditions and the following disclaimer in the
12 **    documentation and/or other materials provided with the distribution.
13 **
14 ** THIS SOFTWARE IS PROVIDED BY Michael Smith ``AS IS'' AND
15 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED.  IN NO EVENT SHALL Michael Smith BE LIABLE
18 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 ** SUCH DAMAGE.
25 **
26 ** $FreeBSD: src/usr.bin/doscmd/dispatch.h,v 1.2.2.1 2001/08/02 02:17:15 obrien Exp $
27 ** $DragonFly: src/usr.bin/doscmd/dispatch.h,v 1.2 2003/06/17 04:29:25 dillon Exp $
28 */
29
30 /*
31 ** Interrupt dispatcher assistants.
32 */
33
34
35 /*
36 ** Declare a static, initialised array of these, with one
37 ** entry per function/subfunction.
38 **
39 ** The last element should be a dummy with a 'func' of -1
40 */
41 struct intfunc_table 
42 {
43     int         func;                           /* interrupt function number */
44     int         subfunc;                        /* subfunction number */
45     int         (* handler)(regcontext_t *REGS);/* handling function */
46     const char  *desc;                          /* textual description */
47 };
48 #define IFT_NOSUBFUNC   -1
49
50
51 /*
52 ** Declare a static array of 256 integers to use as a fast lookup 
53 ** into the table of handlers.
54 **
55 ** Call this function to initialise the lookup.  Note that the table
56 ** must be arranged with all handlers for a given function together, and
57 ** that the handler listed with IFT_NOSUBFUNC should be last.
58 */
59 static inline void
60 intfunc_init(struct intfunc_table table[], int idx[])
61 {
62     int         hn;
63
64     for (hn = 0; hn < 256; hn++)                /* initialise all no-handler state */
65         idx[hn] = -1;                           /* default to no handler */
66
67     for (hn = 0; table[hn].func >= 0; hn++)     /* walk list of handlers and add references */
68         if (idx[table[hn].func] == -1 ) /* reference first handler */
69             idx[table[hn].func] = hn;
70 }
71
72 /*
73 ** Call this to get an index matching the function/subfunction 
74 ** described by (sc), or -1 if none exist
75 */
76 static inline int
77 intfunc_find(struct intfunc_table table[], int idx[], int func, int subfunc)
78 {
79     int ent = idx[func];                                /* look for handler */
80     
81     while ((ent >= 0) &&                                /* scan entries for function */
82            (table[ent].func == func)) {
83
84         if ((table[ent].subfunc == IFT_NOSUBFUNC) ||    /* handles all */
85             (table[ent].subfunc == subfunc)) {          /* handles this one */
86             return(ent);
87         }
88         ent++;
89     }
90     return(-1);
91 }
92
93 /*
94 ** A slower lookup for a set of function handlers, but one that requires
95 ** no initialisation calls.
96 ** Again, handlers with IFT_NOSUBFUNC should be listed after any with
97 ** specific subfunction values.
98 */
99 static inline int
100 intfunc_search(struct intfunc_table table[], int func, int subfunc)
101 {
102     int         ent;
103
104     for (ent = 0; table[ent].func >= 0; ent++)
105         if ((table[ent].func == func) &&        /* matches required function */
106             ((table[ent].subfunc == IFT_NOSUBFUNC) || table[ent].subfunc == subfunc))
107             return(ent);
108     return(-1);
109 }
110
111