Initial import from FreeBSD RELENG_4:
[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 */
28
29 /*
30 ** Interrupt dispatcher assistants.
31 */
32
33
34 /*
35 ** Declare a static, initialised array of these, with one
36 ** entry per function/subfunction.
37 **
38 ** The last element should be a dummy with a 'func' of -1
39 */
40 struct intfunc_table 
41 {
42     int         func;                           /* interrupt function number */
43     int         subfunc;                        /* subfunction number */
44     int         (* handler)(regcontext_t *REGS);/* handling function */
45     const char  *desc;                          /* textual description */
46 };
47 #define IFT_NOSUBFUNC   -1
48
49
50 /*
51 ** Declare a static array of 256 integers to use as a fast lookup 
52 ** into the table of handlers.
53 **
54 ** Call this function to initialise the lookup.  Note that the table
55 ** must be arranged with all handlers for a given function together, and
56 ** that the handler listed with IFT_NOSUBFUNC should be last.
57 */
58 static inline void
59 intfunc_init(struct intfunc_table table[], int idx[])
60 {
61     int         hn;
62
63     for (hn = 0; hn < 256; hn++)                /* initialise all no-handler state */
64         idx[hn] = -1;                           /* default to no handler */
65
66     for (hn = 0; table[hn].func >= 0; hn++)     /* walk list of handlers and add references */
67         if (idx[table[hn].func] == -1 ) /* reference first handler */
68             idx[table[hn].func] = hn;
69 }
70
71 /*
72 ** Call this to get an index matching the function/subfunction 
73 ** described by (sc), or -1 if none exist
74 */
75 static inline int
76 intfunc_find(struct intfunc_table table[], int idx[], int func, int subfunc)
77 {
78     int ent = idx[func];                                /* look for handler */
79     
80     while ((ent >= 0) &&                                /* scan entries for function */
81            (table[ent].func == func)) {
82
83         if ((table[ent].subfunc == IFT_NOSUBFUNC) ||    /* handles all */
84             (table[ent].subfunc == subfunc)) {          /* handles this one */
85             return(ent);
86         }
87         ent++;
88     }
89     return(-1);
90 }
91
92 /*
93 ** A slower lookup for a set of function handlers, but one that requires
94 ** no initialisation calls.
95 ** Again, handlers with IFT_NOSUBFUNC should be listed after any with
96 ** specific subfunction values.
97 */
98 static inline int
99 intfunc_search(struct intfunc_table table[], int func, int subfunc)
100 {
101     int         ent;
102
103     for (ent = 0; table[ent].func >= 0; ent++)
104         if ((table[ent].func == func) &&        /* matches required function */
105             ((table[ent].subfunc == IFT_NOSUBFUNC) || table[ent].subfunc == subfunc))
106             return(ent);
107     return(-1);
108 }
109
110