]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/ipp-vars.c
2 * IPP data file parsing functions.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cups-private.h"
16 #include "ipp-private.h"
17 #include "string-private.h"
18 #include "debug-internal.h"
22 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
26 _ippVarsDeinit(_ipp_vars_t
*v
) /* I - IPP variables */
34 cupsFreeOptions(v
->num_vars
, v
->vars
);
41 * '_ippVarsExpand()' - Expand variables in the source string.
45 _ippVarsExpand(_ipp_vars_t
*v
, /* I - IPP variables */
46 char *dst
, /* I - Destination buffer */
47 const char *src
, /* I - Source string */
48 size_t dstsize
) /* I - Destination buffer size */
50 char *dstptr
, /* Pointer into destination */
51 *dstend
, /* End of destination */
52 temp
[256], /* Temporary string */
53 *tempptr
; /* Pointer into temporary string */
54 const char *value
; /* Value to substitute */
58 dstend
= dst
+ dstsize
- 1;
60 while (*src
&& dstptr
< dstend
)
65 * Substitute a string/number...
68 if (!strncmp(src
, "$$", 2))
73 else if (!strncmp(src
, "$ENV[", 5))
75 strlcpy(temp
, src
+ 5, sizeof(temp
));
77 for (tempptr
= temp
; *tempptr
; tempptr
++)
85 src
+= tempptr
- temp
+ 5;
92 strlcpy(temp
, src
, sizeof(temp
));
93 if ((tempptr
= strchr(temp
, '}')) != NULL
)
96 tempptr
= temp
+ strlen(temp
);
100 strlcpy(temp
, src
+ 1, sizeof(temp
));
102 for (tempptr
= temp
; *tempptr
; tempptr
++)
103 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
110 value
= _ippVarsGet(v
, temp
);
112 src
+= tempptr
- temp
+ 1;
117 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
118 dstptr
+= strlen(dstptr
);
130 * '_ippVarsGet()' - Get a variable string.
133 const char * /* O - Value or @code NULL@ if not set */
134 _ippVarsGet(_ipp_vars_t
*v
, /* I - IPP variables */
135 const char *name
) /* I - Variable name */
139 else if (!strcmp(name
, "uri"))
141 else if (!strcmp(name
, "uriuser") || !strcmp(name
, "username"))
142 return (v
->username
[0] ? v
->username
: NULL
);
143 else if (!strcmp(name
, "scheme") || !strcmp(name
, "method"))
145 else if (!strcmp(name
, "hostname"))
147 else if (!strcmp(name
, "port"))
149 else if (!strcmp(name
, "resource"))
150 return (v
->resource
);
151 else if (!strcmp(name
, "user"))
154 return (cupsGetOption(name
, v
->num_vars
, v
->vars
));
159 * '_ippVarsInit()' - Initialize .
163 _ippVarsInit(_ipp_vars_t
*v
, /* I - IPP variables */
164 _ipp_fattr_cb_t attrcb
, /* I - Attribute (filter) callback */
165 _ipp_ferror_cb_t errorcb
, /* I - Error callback */
166 _ipp_ftoken_cb_t tokencb
) /* I - Token callback */
168 memset(v
, 0, sizeof(_ipp_vars_t
));
171 v
->errorcb
= errorcb
;
172 v
->tokencb
= tokencb
;
177 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
180 const char * /* O - Password string or @code NULL@ */
182 const char *prompt
, /* I - Prompt string (not used) */
183 http_t
*http
, /* I - HTTP connection (not used) */
184 const char *method
, /* I - HTTP method (not used) */
185 const char *resource
, /* I - Resource path (not used) */
186 void *user_data
) /* I - IPP variables */
188 _ipp_vars_t
*v
= (_ipp_vars_t
*)user_data
;
189 /* I - IPP variables */
197 if (v
->username
[0] && v
->password
&& v
->password_tries
< 3)
199 v
->password_tries
++;
201 cupsSetUser(v
->username
);
203 return (v
->password
);
213 * '_ippVarsSet()' - Set an IPP variable.
216 int /* O - 1 on success, 0 on failure */
217 _ippVarsSet(_ipp_vars_t
*v
, /* I - IPP variables */
218 const char *name
, /* I - Variable name */
219 const char *value
) /* I - Variable value */
221 if (!strcmp(name
, "uri"))
223 char uri
[1024]; /* New printer URI */
224 char resolved
[1024]; /* Resolved mDNS URI */
226 if (strstr(value
, "._tcp"))
232 if (!_httpResolveURI(value
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
238 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, v
->scheme
, sizeof(v
->scheme
), v
->username
, sizeof(v
->username
), v
->host
, sizeof(v
->host
), &(v
->port
), v
->resource
, sizeof(v
->resource
)) < HTTP_URI_STATUS_OK
)
243 if ((v
->password
= strchr(v
->username
, ':')) != NULL
)
244 *(v
->password
)++ = '\0';
247 snprintf(v
->portstr
, sizeof(v
->portstr
), "%d", v
->port
);
252 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), v
->scheme
, NULL
, v
->host
, v
->port
, v
->resource
);
253 v
->uri
= strdup(uri
);
255 return (v
->uri
!= NULL
);
259 v
->num_vars
= cupsAddOption(name
, value
, v
->num_vars
, &v
->vars
);