8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / print / libpapi-lpd / common / service.c
blobc39ea0cbb50a3319012fb2e9cfa09526ebd74104
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: service.c 163 2006-05-09 15:07:45Z njacobs $ */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <alloca.h>
37 #include <uri.h>
38 #include <papi_impl.h>
40 papi_status_t
41 service_fill_in(service_t *svc, char *name)
43 papi_status_t status = PAPI_OK;
44 uri_t *uri = NULL;
46 if (svc == NULL)
47 return (PAPI_BAD_ARGUMENT);
49 if (name == NULL)
50 return (PAPI_OK);
53 * valid URIs are in the form:
54 * lpd://server[:port]/.../queue[#extensions]
55 * rfc-1179://server[:port]/.../queue[#extensions]
56 * any authentication information supplied the URI is ignored.
58 if (uri_from_string((char *)name, &uri) != -1) {
59 if ((strcasecmp(uri->scheme, "lpd") == 0) ||
60 (strcasecmp(uri->scheme, "rfc-1179") == 0)) {
61 if (svc->uri != NULL)
62 uri_free(svc->uri);
63 svc->uri = uri;
64 } else {
65 uri_free(uri);
66 status = PAPI_URI_SCHEME;
70 return (status);
73 papi_status_t
74 papiServiceCreate(papi_service_t *handle, char *service_name,
75 char *user_name, char *password,
76 int (*authCB)(papi_service_t svc, void *app_data),
77 papi_encryption_t encryption, void *app_data)
79 papi_status_t status;
80 service_t *svc = NULL;
82 if (handle == NULL)
83 return (PAPI_BAD_ARGUMENT);
85 if ((*handle = svc = (service_t *)calloc(1, sizeof (*svc))) == NULL)
86 return (PAPI_TEMPORARY_ERROR);
88 if (service_name != NULL)
89 papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
90 "service-name", service_name);
92 (void) papiServiceSetUserName(svc, user_name);
93 (void) papiServiceSetPassword(svc, password);
94 (void) papiServiceSetAuthCB(svc, authCB);
95 (void) papiServiceSetAppData(svc, app_data);
96 (void) papiServiceSetEncryption(svc, encryption);
98 status = service_fill_in(svc, service_name);
100 return (status);
103 void
104 papiServiceDestroy(papi_service_t handle)
106 if (handle != NULL) {
107 service_t *svc = handle;
109 #ifdef DEADBEEF
110 if (svc->cache != NULL)
111 cache_free(svc->cache);
112 #endif
113 if (svc->uri != NULL)
114 uri_free(svc->uri);
115 if (svc->attributes != NULL)
116 papiAttributeListFree(svc->attributes);
117 free(svc);
121 papi_status_t
122 papiServiceSetUserName(papi_service_t handle, char *user_name)
124 service_t *svc = handle;
126 if (svc == NULL)
127 return (PAPI_BAD_ARGUMENT);
129 return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
130 "user-name", user_name));
133 papi_status_t
134 papiServiceSetPassword(papi_service_t handle, char *password)
136 service_t *svc = handle;
138 if (svc == NULL)
139 return (PAPI_BAD_ARGUMENT);
141 return (papiAttributeListAddString(&svc->attributes,
142 PAPI_ATTR_REPLACE, "password", password));
145 papi_status_t
146 papiServiceSetEncryption(papi_service_t handle,
147 papi_encryption_t encryption)
149 service_t *svc = handle;
151 if (svc == NULL)
152 return (PAPI_BAD_ARGUMENT);
154 return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
155 "encryption", (int)encryption));
158 papi_status_t
159 papiServiceSetAuthCB(papi_service_t handle,
160 int (*authCB)(papi_service_t svc, void *app_data))
162 service_t *svc = handle;
164 if (svc == NULL)
165 return (PAPI_BAD_ARGUMENT);
167 svc->authCB = (int (*)(papi_service_t svc, void *))authCB;
169 return (PAPI_OK);
172 papi_status_t
173 papiServiceSetAppData(papi_service_t handle, void *app_data)
175 service_t *svc = handle;
177 if (svc == NULL)
178 return (PAPI_BAD_ARGUMENT);
180 svc->app_data = (void *)app_data;
182 return (PAPI_OK);
185 char *
186 papiServiceGetServiceName(papi_service_t handle)
188 service_t *svc = handle;
189 char *result = NULL;
191 if (svc != NULL)
192 papiAttributeListGetString(svc->attributes, NULL,
193 "service-name", &result);
195 return (result);
198 char *
199 papiServiceGetUserName(papi_service_t handle)
201 service_t *svc = handle;
202 char *result = NULL;
204 if (svc != NULL)
205 papiAttributeListGetString(svc->attributes, NULL,
206 "user-name", &result);
208 return (result);
212 char *
213 papiServiceGetPassword(papi_service_t handle)
215 service_t *svc = handle;
216 char *result = NULL;
218 if (svc != NULL)
219 papiAttributeListGetString(svc->attributes, NULL,
220 "password", &result);
222 return (result);
225 papi_encryption_t
226 papiServiceGetEncryption(papi_service_t handle)
228 service_t *svc = handle;
229 papi_encryption_t result = PAPI_ENCRYPT_NEVER;
231 if (svc != NULL)
232 papiAttributeListGetInteger(svc->attributes, NULL,
233 "encryption", (int *)&result);
235 return (result);
238 void *
239 papiServiceGetAppData(papi_service_t handle)
241 service_t *svc = handle;
242 void *result = NULL;
244 if (svc != NULL) {
245 result = svc->app_data;
248 return (result);
252 papi_attribute_t **
253 papiServiceGetAttributeList(papi_service_t handle)
255 service_t *svc = handle;
256 papi_attribute_t **result = NULL;
258 if (svc != NULL)
259 result = svc->attributes;
261 return (result);
264 char *
265 papiServiceGetStatusMessage(papi_service_t handle)
267 service_t *svc = handle;
268 char *result = NULL;
270 if (svc != NULL) {
271 papiAttributeListGetString(svc->attributes, NULL,
272 "detailed-status-message", &result);
275 return (result);
278 void
279 detailed_error(service_t *svc, char *fmt, ...)
281 if ((svc != NULL) && (fmt != NULL)) {
282 va_list ap;
283 size_t size;
284 char *message = alloca(BUFSIZ);
286 va_start(ap, fmt);
288 * fill in the message. If the buffer is too small, allocate
289 * one that is large enough and fill it in.
291 if ((size = vsnprintf(message, BUFSIZ, fmt, ap)) >= BUFSIZ)
292 if ((message = alloca(size)) != NULL)
293 vsnprintf(message, size, fmt, ap);
294 va_end(ap);
296 papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
297 "detailed-status-message", message);