Libinfo-78.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_utils.h
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Useful macros and other stuff for generic lookups
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28
29 #import <netinfo/lookup_types.h>
30 #include <netinfo/ni.h>
31 #include <stdarg.h>
32
33 extern mach_port_t _lu_port;
34 extern unit *_lookup_buf;
35 extern int _lu_running(void);
36
37 int lookupd_query(ni_proplist *l, ni_proplist ***out);
38 ni_proplist *lookupd_make_query(char *cat, char *fmt, ...);
39 void ni_property_merge(ni_property *a, ni_property *b);
40 void ni_proplist_merge(ni_proplist *a, ni_proplist *b);
41
42 typedef enum lookup_state {
43 LOOKUP_CACHE,
44 LOOKUP_FILE,
45 } lookup_state;
46
47 #define SETSTATE(_lu_set, _old_set, state, stayopen) \
48 { \
49 if (_lu_running()) { \
50 _lu_set(stayopen); \
51 *state = LOOKUP_CACHE; \
52 } else { \
53 _old_set(stayopen); \
54 *state = LOOKUP_FILE; \
55 } \
56 }
57
58 #define SETSTATEVOID(_lu_set, _old_set, state) \
59 { \
60 if (_lu_running()) { \
61 _lu_set(); \
62 *state = LOOKUP_CACHE; \
63 } else { \
64 _old_set(); \
65 *state = LOOKUP_FILE; \
66 } \
67 }
68
69 #define INTSETSTATEVOID(_lu_set, _old_set, state) \
70 { \
71 int result; \
72 if (_lu_running()) { \
73 result = _lu_set(); \
74 *state = LOOKUP_CACHE; \
75 } else { \
76 result = _old_set(); \
77 *state = LOOKUP_FILE; \
78 } \
79 return result; \
80 }
81
82 #define UNSETSTATE(_lu_unset, _old_unset, state) \
83 { \
84 if (_lu_running()) { \
85 _lu_unset(); \
86 } else { \
87 _old_unset(); \
88 } \
89 *state = LOOKUP_CACHE; \
90 }
91
92 #define GETENT(_lu_get, _old_get, state, res_type) \
93 { \
94 res_type *res; \
95 \
96 if (_lu_running()) { \
97 if (*state == LOOKUP_CACHE) { \
98 res = _lu_get(); \
99 } else { \
100 res = _old_get(); \
101 } \
102 } else { \
103 res = _old_get(); \
104 } \
105 return (res); \
106 }
107
108 #define LOOKUP1(_lu_lookup, _old_lookup, arg, res_type) \
109 { \
110 res_type *res; \
111 \
112 if (_lu_running()) { \
113 res = _lu_lookup(arg); \
114 } else { \
115 res = _old_lookup(arg); \
116 } \
117 return (res); \
118 }
119
120 #define LOOKUP2(_lu_lookup, _old_lookup, arg1, arg2, res_type) \
121 { \
122 res_type *res; \
123 \
124 if (_lu_running()) { \
125 res = _lu_lookup(arg1, arg2); \
126 } else { \
127 res = _old_lookup(arg1, arg2); \
128 } \
129 return (res); \
130 }