aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/doscmd/dispatch.h
blob: 59620f1593125667f11828ece5b6dbd982c467a3 (plain) (blame)
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
/*
** Copyright (c) 1996
**	Michael Smith.  All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY Michael Smith ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO EVENT SHALL Michael Smith BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** $Id: dispatch.h,v 1.3 1996/09/22 05:52:56 miff Exp $
*/

/*
** Interrupt dispatcher assistants.
*/


/*
** Declare a static, initialised array of these, with one
** entry per function/subfunction.
**
** The last element should be a dummy with a 'func' of -1
*/
struct intfunc_table 
{
    int		func;				/* interrupt function number */
    int		subfunc;			/* subfunction number */
    int		(* handler)(regcontext_t *REGS);/* handling function */
    const char	*desc;				/* textual description */
};
#define IFT_NOSUBFUNC	-1


/*
** Declare a static array of 256 integers to use as a fast lookup 
** into the table of handlers.
**
** Call this function to initialise the lookup.  Note that the table
** must be arranged with all handlers for a given function together, and
** that the handler listed with IFT_NOSUBFUNC should be last.
*/
static inline void
intfunc_init(struct intfunc_table table[], int index[])
{
    int		hn;

    for (hn = 0; hn < 256; hn++)		/* initialise all no-handler state */
	index[hn] = -1;				/* default to no handler */

    for (hn = 0; table[hn].func >= 0; hn++)	/* walk list of handlers and add references */
	if (index[table[hn].func] == -1 )	/* reference first handler */
	    index[table[hn].func] = hn;
}

/*
** Call this to get an index matching the function/subfunction 
** described by (sc), or -1 if none exist
*/
static inline int
intfunc_find(struct intfunc_table table[], int index[], int func, int subfunc)
{
    int	ent = index[func];				/* look for handler */
    
    while ((ent >= 0) &&				/* scan entries for function */
	   (table[ent].func == func)) {

	if ((table[ent].subfunc == IFT_NOSUBFUNC) ||	/* handles all */
	    (table[ent].subfunc == subfunc)) {		/* handles this one */
	    return(ent);
	}
	ent++;
    }
    return(-1);
}

/*
** A slower lookup for a set of function handlers, but one that requires
** no initialisation calls.
** Again, handlers with IFT_NOSUBFUNC should be listed after any with
** specific subfunction values.
*/
static inline int
intfunc_search(struct intfunc_table table[], int func, int subfunc)
{
    int		ent;

    for (ent = 0; table[ent].func >= 0; ent++)
	if ((table[ent].func == func) &&	/* matches required function */
	    ((table[ent].subfunc == IFT_NOSUBFUNC) || table[ent].subfunc == subfunc))
	    return(ent);
    return(-1);
}