Skip to content

Commit f4e878e

Browse files
committed
Add 'end' argument to cupsParseOptions.
1 parent 193cbd6 commit f4e878e

14 files changed

Lines changed: 46 additions & 37 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ libcups v3.0rc1 (TBD)
88
strings.
99
- Added support for per-user instances of `cups-locald` (Issue #69)
1010
- Added `httpConnectURI` API.
11+
- Added "end" argument to `cupsParseOptions` API.
1112
- Renamed `httpReconnect` to `httpConnectAgain`.
1213
- Updated `cupsDestInfo` to accept a `cups_dest_flags_t` argument.
1314
- Updated `cupsCopyString` and `cupsConcatString` APIs to safely terminate UTF-8

cups/cups.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ extern const char *cupsLocalizeDestValue(http_t *http, cups_dest_t *dest, cups_d
381381
extern char *cupsLocalizeNotifySubject(cups_lang_t *lang, ipp_t *event) _CUPS_PUBLIC;
382382
extern char *cupsLocalizeNotifyText(cups_lang_t *lang, ipp_t *event) _CUPS_PUBLIC;
383383

384-
extern size_t cupsParseOptions(const char *arg, size_t num_options, cups_option_t **options) _CUPS_PUBLIC;
384+
extern size_t cupsParseOptions(const char *arg, const char **end, size_t num_options, cups_option_t **options) _CUPS_PUBLIC;
385385
extern http_status_t cupsPutFd(http_t *http, const char *resource, int fd) _CUPS_PUBLIC;
386386
extern http_status_t cupsPutFile(http_t *http, const char *resource, const char *filename) _CUPS_PUBLIC;
387387

cups/dest-options.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Destination option/media support for CUPS.
33
//
4-
// Copyright © 2021-2023 by OpenPrinting.
4+
// Copyright © 2021-2024 by OpenPrinting.
55
// Copyright © 2012-2019 by Apple Inc.
66
//
77
// Licensed under Apache License v2.0. See the file "LICENSE" for more
@@ -1835,11 +1835,11 @@ cups_create_media_db(
18351835

18361836
if (!mdb.key)
18371837
{
1838-
pwg_media_t pwg; // PWG media info
1838+
pwg_media_t temppwg; // PWG media info
18391839
char keyword[128], // PWG size keyword
18401840
ppdname[41]; // PPD size name
18411841

1842-
if (!mdb.size_name && _pwgMediaNearSize(&pwg, keyword, sizeof(keyword), ppdname, sizeof(ppdname), mdb.width, mdb.length, _PWG_EPSILON))
1842+
if (!mdb.size_name && _pwgMediaNearSize(&temppwg, keyword, sizeof(keyword), ppdname, sizeof(ppdname), mdb.width, mdb.length, _PWG_EPSILON))
18431843
mdb.size_name = keyword;
18441844

18451845
if (!mdb.size_name)

cups/dest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3209,7 +3209,7 @@ cups_get_dests(
32093209
}
32103210

32113211
// Add options until we hit the end of the line...
3212-
dest->num_options = cupsParseOptions(lineptr, dest->num_options, &(dest->options));
3212+
dest->num_options = cupsParseOptions(lineptr, /*end*/NULL, dest->num_options, &(dest->options));
32133213

32143214
// If we found what we were looking for, stop now...
32153215
if (match_name)

cups/encode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Option encoding routines for CUPS.
33
//
4-
// Copyright © 2021-2023 by OpenPrinting.
4+
// Copyright © 2021-2024 by OpenPrinting.
55
// Copyright © 2007-2019 by Apple Inc.
66
// Copyright © 1997-2007 by Easy Software Products.
77
//
@@ -498,7 +498,7 @@ _cupsEncodeOption(
498498

499499
case IPP_TAG_BEGIN_COLLECTION :
500500
// Collection value
501-
num_cols = cupsParseOptions(val, 0, &cols);
501+
num_cols = cupsParseOptions(val, /*end*/NULL, 0, &cols);
502502
if ((collection = ippNew()) == NULL)
503503
{
504504
cupsFreeOptions(num_cols, cols);

cups/options.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Option routines for CUPS.
33
//
4-
// Copyright © 2022-2023 by OpenPrinting.
4+
// Copyright © 2022-2024 by OpenPrinting.
55
// Copyright © 2007-2017 by Apple Inc.
66
// Copyright © 1997-2007 by Easy Software Products.
77
//
@@ -201,10 +201,14 @@ cupsGetOption(const char *name, // I - Name of option
201201
// intact - use @code cupsParseOptions@ on the value to extract the
202202
// collection attributes.
203203
//
204+
// The "end" argument, if not `NULL`, receives a pointer to the end of the
205+
// options.
206+
//
204207

205208
size_t // O - Number of options found
206209
cupsParseOptions(
207210
const char *arg, // I - Argument to parse
211+
const char **end, // O - Pointer to end of options or `NULL` for "don't care"
208212
size_t num_options, // I - Number of options
209213
cups_option_t **options) // O - Options found
210214
{
@@ -217,6 +221,9 @@ cupsParseOptions(
217221

218222

219223
// Range check input...
224+
if (end)
225+
*end = NULL;
226+
220227
if (!arg)
221228
return (num_options);
222229

@@ -231,20 +238,9 @@ cupsParseOptions(
231238
}
232239

233240
if (*copyarg == '{')
234-
{
235-
// Remove surrounding {} so we can parse "{name=value ... name=value}"...
236-
if ((ptr = copyarg + strlen(copyarg) - 1) > copyarg && *ptr == '}')
237-
{
238-
*ptr = '\0';
239-
ptr = copyarg + 1;
240-
}
241-
else
242-
ptr = copyarg;
243-
}
241+
ptr = copyarg + 1;
244242
else
245-
{
246243
ptr = copyarg;
247-
}
248244

249245
// Skip leading spaces...
250246
while (_cups_isspace(*ptr))
@@ -262,6 +258,13 @@ cupsParseOptions(
262258
if (ptr == name)
263259
break;
264260

261+
// End after the closing brace...
262+
if (*ptr == '}' && *copyarg == '{')
263+
{
264+
ptr ++;
265+
break;
266+
}
267+
265268
// Skip trailing spaces...
266269
while (_cups_isspace(*ptr))
267270
*ptr++ = '\0';
@@ -356,6 +359,10 @@ cupsParseOptions(
356359
num_options = cupsAddOption(name, value, num_options, options);
357360
}
358361

362+
// Save the progress in the input string...
363+
if (end)
364+
*end = arg + (ptr - copyarg);
365+
359366
// Free the copy of the argument we made and return the number of options found.
360367
free(copyarg);
361368

cups/testdest.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ main(int argc, // I - Number of command-line arguments
170170
cups_option_t *options = NULL;// Options
171171

172172
for (i ++; i < argc; i ++)
173-
num_options = cupsParseOptions(argv[i], num_options, &options);
173+
num_options = cupsParseOptions(argv[i], /*end*/NULL, num_options, &options);
174174

175175
show_conflicts(http, dest, dinfo, num_options, options);
176176
}
@@ -219,7 +219,7 @@ main(int argc, // I - Number of command-line arguments
219219
const char *filename = argv[i + 1];
220220

221221
for (i += 2; i < argc; i ++)
222-
num_options = cupsParseOptions(argv[i], num_options, &options);
222+
num_options = cupsParseOptions(argv[i], /*end*/NULL, num_options, &options);
223223

224224
print_file(http, dest, dinfo, filename, num_options, options);
225225
}

cups/testdnssd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ main(int argc, // I - Number of command-line arguments
230230
puts("IfIdx Service Name");
231231
puts("----- ----------------------------------------------------------------");
232232

233-
if ((browse = cupsDNSSDBrowseNew(dnssd, CUPS_DNSSD_IF_INDEX_ANY, argv[2], NULL, browse_print_cb, &testdata)) == NULL)
233+
if (cupsDNSSDBrowseNew(dnssd, CUPS_DNSSD_IF_INDEX_ANY, argv[2], NULL, browse_print_cb, &testdata) == NULL)
234234
{
235235
cupsDNSSDDelete(dnssd);
236236
return (1);
@@ -318,6 +318,9 @@ browse_print_cb(
318318
// Test data
319319

320320

321+
(void)browse;
322+
(void)flags;
323+
321324
printf("%5u %s.%s.%s\n", if_index, name, regtype, domain);
322325

323326
cupsMutexLock(&data->mutex);

cups/testform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ main(int argc, // I - Number of command-line arguments
203203
break;
204204
}
205205

206-
num_vars = cupsParseOptions(argv[i], 0, &vars);
206+
num_vars = cupsParseOptions(argv[i], /*end*/NULL, 0, &vars);
207207
data = cupsFormEncode(url, num_vars, vars);
208208

209209
if (data)

cups/testoptions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ main(int argc, // I - Number of command-line arguments
4141
"foobar=FOO\\ BAR "
4242
"barfoo=barfoo "
4343
"barfoo=\"\'BAR FOO\'\" "
44-
"auth-info=user,pass\\\\,word\\\\\\\\", 0, &options);
44+
"auth-info=user,pass\\\\,word\\\\\\\\", /*end*/NULL, 0, &options);
4545

4646
if (num_options != 6)
4747
{
@@ -146,7 +146,7 @@ main(int argc, // I - Number of command-line arguments
146146
cups_option_t *option; // Current option
147147

148148

149-
num_options = cupsParseOptions(argv[1], 0, &options);
149+
num_options = cupsParseOptions(argv[1], /*end*/NULL, 0, &options);
150150

151151
for (i = 0, option = options; i < num_options; i ++, option ++)
152152
printf("options[%u].name=\"%s\", value=\"%s\"\n", (unsigned)i, option->name, option->value);

0 commit comments

Comments
 (0)