]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ipp-vars.c
Finish re-tooling of ipptool to use new parser.
[thirdparty/cups.git] / cups / ipp-vars.c
1 /*
2 * IPP data file parsing functions.
3 *
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 /*
12 * Include necessary headers...
13 */
14
15 #include <cups/cups.h>
16 #include "ipp-private.h"
17 #include "string-private.h"
18
19
20 /*
21 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
22 */
23
24 void
25 _ippVarsDeinit(_ipp_vars_t *v) /* I - IPP variables */
26 {
27 if (v->uri)
28 {
29 free(v->uri);
30 v->uri = NULL;
31 }
32
33 cupsFreeOptions(v->num_vars, v->vars);
34 v->num_vars = 0;
35 v->vars = NULL;
36 }
37
38
39 /*
40 * '_ippVarsExpand()' - Expand variables in the source string.
41 */
42
43 void
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 */
48 {
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 */
54
55
56 dstptr = dst;
57 dstend = dst + dstsize - 1;
58
59 while (*src && dstptr < dstend)
60 {
61 if (*src == '$')
62 {
63 /*
64 * Substitute a string/number...
65 */
66
67 if (!strncmp(src, "$$", 2))
68 {
69 value = "$";
70 src += 2;
71 }
72 else if (!strncmp(src, "$ENV[", 5))
73 {
74 strlcpy(temp, src + 5, sizeof(temp));
75
76 for (tempptr = temp; *tempptr; tempptr ++)
77 if (*tempptr == ']')
78 break;
79
80 if (*tempptr)
81 *tempptr++ = '\0';
82
83 value = getenv(temp);
84 src += tempptr - temp + 5;
85 }
86 else
87 {
88 if (src[1] == '{')
89 {
90 src += 2;
91 strlcpy(temp, src, sizeof(temp));
92 if ((tempptr = strchr(temp, '}')) != NULL)
93 *tempptr = '\0';
94 else
95 tempptr = temp + strlen(temp);
96 }
97 else
98 {
99 strlcpy(temp, src + 1, sizeof(temp));
100
101 for (tempptr = temp; *tempptr; tempptr ++)
102 if (!isalnum(*tempptr & 255) && *tempptr != '-' && *tempptr != '_')
103 break;
104
105 if (*tempptr)
106 *tempptr = '\0';
107 }
108
109 value = _ippVarsGet(v, temp);
110
111 src += tempptr - temp + 1;
112 }
113
114 if (value)
115 {
116 strlcpy(dstptr, value, (size_t)(dstend - dstptr + 1));
117 dstptr += strlen(dstptr);
118 }
119 }
120 else
121 *dstptr++ = *src++;
122 }
123
124 *dstptr = '\0';
125 }
126
127
128 /*
129 * '_ippVarsGet()' - Get a variable string.
130 */
131
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 */
135 {
136 if (!strcmp(name, "uri"))
137 return (v->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"))
141 return (v->scheme);
142 else if (!strcmp(name, "hostname"))
143 return (v->host);
144 else if (!strcmp(name, "port"))
145 return (v->portstr);
146 else if (!strcmp(name, "resource"))
147 return (v->resource);
148 else if (!strcmp(name, "user"))
149 return (cupsUser());
150 else
151 return (cupsGetOption(name, v->num_vars, v->vars));
152 }
153
154
155 /*
156 * '_ippVarsInit()' - Initialize .
157 */
158
159 void
160 _ippVarsInit(_ipp_vars_t *v) /* I - IPP variables */
161 {
162 memset(v, 0, sizeof(_ipp_vars_t));
163 }
164
165
166 /*
167 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
168 */
169
170 const char * /* O - Password string or @code NULL@ */
171 _ippVarsPasswordCB(
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 */
177 {
178 _ipp_vars_t *v = (_ipp_vars_t *)user_data;
179 /* I - IPP variables */
180
181
182 (void)prompt;
183 (void)http;
184 (void)method;
185 (void)resource;
186
187 if (v->username[0] && v->password && v->password_tries < 3)
188 {
189 v->password_tries ++;
190
191 cupsSetUser(v->username);
192
193 return (v->password);
194 }
195 else
196 {
197 return (NULL);
198 }
199 }
200
201
202 /*
203 * '_ippVarsSet()' - Set an IPP variable.
204 */
205
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 */
210 {
211 if (!strcmp(name, "uri"))
212 {
213 char uri[1024]; /* New printer URI */
214 http_uri_status_t uri_status; /* URI status */
215
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)
217 return (0);
218
219 if (v->username[0])
220 {
221 if ((v->password = strchr(v->username, ':')) != NULL)
222 *(v->password)++ = '\0';
223 }
224
225 snprintf(v->portstr, sizeof(v->portstr), "%d", v->port);
226
227 if (v->uri)
228 free(v->uri);
229
230 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), v->scheme, NULL, v->host, v->port, v->resource);
231 v->uri = strdup(uri);
232
233 return (v->uri != NULL);
234 }
235 else
236 {
237 v->num_vars = cupsAddOption(name, value, v->num_vars, &v->vars);
238 return (1);
239 }
240 }