]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_printer.c
0290e105966ab1a56c679d523ae676dc167668ff
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (C) 1989 by NeXT, Inc.
29 #include <mach/mach.h>
33 #include <rpc/types.h>
35 #include "_lu_types.h"
36 #include "printerdb.h"
39 extern struct prdb_ent
*_old_prdb_get();
40 extern struct prdb_ent
*_old_prdb_getbyname();
41 extern void _old_prdb_set();
42 extern void _old_prdb_end();
44 static lookup_state prdb_state
= LOOKUP_CACHE
;
45 static struct prdb_ent global_prdb
;
46 static int global_free
= 1;
47 static char *prdb_data
= NULL
;
48 static unsigned prdb_datalen
= 0;
49 static int prdb_nentries
;
50 static int prdb_start
= 1;
51 static XDR prdb_xdr
= { 0 };
59 if (global_free
== 1) return;
61 names
= global_prdb
.pe_name
;
64 while (*names
) free(*names
++);
65 free(global_prdb
.pe_name
);
68 for (i
= 0; i
< global_prdb
.pe_nprops
; i
++)
70 free(global_prdb
.pe_prop
[i
].pp_key
);
71 free(global_prdb
.pe_prop
[i
].pp_value
);
74 free(global_prdb
.pe_prop
);
81 convert_prdb(_lu_prdb_ent
*lu_prdb
)
87 len
= lu_prdb
->pe_names
.pe_names_len
;
88 global_prdb
.pe_name
= (char **)malloc((len
+ 1) * sizeof(char *));
89 for (i
= 0; i
< len
; i
++)
91 global_prdb
.pe_name
[i
] = strdup(lu_prdb
->pe_names
.pe_names_val
[i
]);
94 global_prdb
.pe_name
[len
] = NULL
;
96 len
= lu_prdb
->pe_props
.pe_props_len
;
97 global_prdb
.pe_prop
= (prdb_property
*)malloc(len
* sizeof(prdb_property
));
98 for (i
= 0; i
< len
; i
++)
100 global_prdb
.pe_prop
[i
].pp_key
=
101 strdup(lu_prdb
->pe_props
.pe_props_val
[i
].pp_key
);
103 global_prdb
.pe_prop
[i
].pp_value
=
104 strdup(lu_prdb
->pe_props
.pe_props_val
[i
].pp_value
);
107 global_prdb
.pe_nprops
= lu_prdb
->pe_props
.pe_props_len
;
116 if (prdb_data
!= NULL
)
119 vm_deallocate(mach_task_self(), (vm_address_t
)prdb_data
, prdb_datalen
);
131 static struct prdb_ent
*
132 lu_prdb_getbyname(const char *name
)
135 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
138 _lu_prdb_ent_ptr lu_prdb
;
139 static int proc
= -1;
140 unit lookup_buf
[MAX_INLINE_UNITS
];
144 if (_lookup_link(_lu_port
, "prdb_getbyname", &proc
) != KERN_SUCCESS
)
150 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
151 if (!xdr__lu_string(&outxdr
, &name
))
153 xdr_destroy(&outxdr
);
157 datalen
= MAX_INLINE_UNITS
;
158 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
159 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
162 xdr_destroy(&outxdr
);
166 xdr_destroy(&outxdr
);
168 datalen
*= BYTES_PER_XDR_UNIT
;
169 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
172 if (!xdr__lu_prdb_ent_ptr(&inxdr
, &lu_prdb
) || (lu_prdb
== NULL
))
180 convert_prdb(lu_prdb
);
181 xdr_free(xdr__lu_prdb_ent_ptr
, &lu_prdb
);
182 return (&global_prdb
);
188 static int proc
= -1;
189 _lu_prdb_ent lu_prdb
;
197 if (_lookup_link(_lu_port
, "prdb_get", &proc
) != KERN_SUCCESS
)
203 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &prdb_data
, &prdb_datalen
)
211 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
212 prdb_datalen
*= BYTES_PER_XDR_UNIT
;
215 xdrmem_create(&prdb_xdr
, prdb_data
, prdb_datalen
,
217 if (!xdr_int(&prdb_xdr
, &prdb_nentries
))
219 xdr_destroy(&prdb_xdr
);
225 if (prdb_nentries
== 0)
227 xdr_destroy(&prdb_xdr
);
232 bzero(&lu_prdb
, sizeof(lu_prdb
));
233 if (!xdr__lu_prdb_ent(&prdb_xdr
, &lu_prdb
))
235 xdr_destroy(&prdb_xdr
);
241 convert_prdb(&lu_prdb
);
242 xdr_free(xdr__lu_prdb_ent
, &lu_prdb
);
243 return (&global_prdb
);
247 prdb_getbyname(const char *name
)
249 LOOKUP1(lu_prdb_getbyname
, _old_prdb_getbyname
, name
, prdb_ent
);
255 GETENT(lu_prdb_get
, _old_prdb_get
, &prdb_state
, prdb_ent
);
259 prdb_set(const char *name
)
261 SETSTATE(lu_prdb_set
, _old_prdb_set
, &prdb_state
, name
);
267 UNSETSTATE(lu_prdb_end
, _old_prdb_end
, &prdb_state
);