2 * IPP data file parsing functions.
4 * Copyright © 2007-2018 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/cups.h>
16 #include "ipp-private.h"
17 #include "string-private.h"
21 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
25 _ippVarsDeinit(_ipp_vars_t
*v
) /* I - IPP variables */
33 cupsFreeOptions(v
->num_vars
, v
->vars
);
40 * '_ippVarsExpand()' - Expand variables in the source string.
44 _ippVarsExpand(_ipp_vars_t
*v
, /* I - IPP variables */
45 char *dst
, /* I - Destination buffer */
46 const char *src
, /* I - Source string */
47 size_t dstsize
) /* I - Destination buffer size */
49 char *dstptr
, /* Pointer into destination */
50 *dstend
, /* End of destination */
51 temp
[256], /* Temporary string */
52 *tempptr
; /* Pointer into temporary string */
53 const char *value
; /* Value to substitute */
57 dstend
= dst
+ dstsize
- 1;
59 while (*src
&& dstptr
< dstend
)
64 * Substitute a string/number...
67 if (!strncmp(src
, "$$", 2))
72 else if (!strncmp(src
, "$ENV[", 5))
74 strlcpy(temp
, src
+ 5, sizeof(temp
));
76 for (tempptr
= temp
; *tempptr
; tempptr
++)
84 src
+= tempptr
- temp
+ 5;
91 strlcpy(temp
, src
, sizeof(temp
));
92 if ((tempptr
= strchr(temp
, '}')) != NULL
)
95 tempptr
= temp
+ strlen(temp
);
99 strlcpy(temp
, src
+ 1, sizeof(temp
));
101 for (tempptr
= temp
; *tempptr
; tempptr
++)
102 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
109 value
= _ippVarsGet(v
, temp
);
111 src
+= tempptr
- temp
+ 1;
116 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
117 dstptr
+= strlen(dstptr
);
129 * '_ippVarsGet()' - Get a variable string.
132 const char * /* O - Value or @code NULL@ if not set */
133 _ippVarsGet(_ipp_vars_t
*v
, /* I - IPP variables */
134 const char *name
) /* I - Variable name */
136 if (!strcmp(name
, "uri"))
138 else if (!strcmp(name
, "uriuser") || !strcmp(name
, "username"))
139 return (v
->username
[0] ? v
->username
: NULL
);
140 else if (!strcmp(name
, "scheme") || !strcmp(name
, "method"))
142 else if (!strcmp(name
, "hostname"))
144 else if (!strcmp(name
, "port"))
146 else if (!strcmp(name
, "resource"))
147 return (v
->resource
);
148 else if (!strcmp(name
, "user"))
151 return (cupsGetOption(name
, v
->num_vars
, v
->vars
));
156 * '_ippVarsInit()' - Initialize .
160 _ippVarsInit(_ipp_vars_t
*v
) /* I - IPP variables */
162 memset(v
, 0, sizeof(_ipp_vars_t
));
167 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
170 const char * /* O - Password string or @code NULL@ */
172 const char *prompt
, /* I - Prompt string (not used) */
173 http_t
*http
, /* I - HTTP connection (not used) */
174 const char *method
, /* I - HTTP method (not used) */
175 const char *resource
, /* I - Resource path (not used) */
176 void *user_data
) /* I - IPP variables */
178 _ipp_vars_t
*v
= (_ipp_vars_t
*)user_data
;
179 /* I - IPP variables */
187 if (v
->username
[0] && v
->password
&& v
->password_tries
< 3)
189 v
->password_tries
++;
191 cupsSetUser(v
->username
);
193 return (v
->password
);
203 * '_ippVarsSet()' - Set an IPP variable.
206 int /* O - 1 on success, 0 on failure */
207 _ippVarsSet(_ipp_vars_t
*v
, /* I - IPP variables */
208 const char *name
, /* I - Variable name */
209 const char *value
) /* I - Variable value */
211 if (!strcmp(name
, "uri"))
213 char uri
[1024]; /* New printer URI */
214 http_uri_status_t uri_status
; /* URI status */
216 if ((uri_status
= 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
)
221 if ((v
->password
= strchr(v
->username
, ':')) != NULL
)
222 *(v
->password
)++ = '\0';
225 snprintf(v
->portstr
, sizeof(v
->portstr
), "%d", v
->port
);
230 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), v
->scheme
, NULL
, v
->host
, v
->port
, v
->resource
);
231 v
->uri
= strdup(uri
);
233 return (v
->uri
!= NULL
);
237 v
->num_vars
= cupsAddOption(name
, value
, v
->num_vars
, &v
->vars
);