2 Bacula® - The Network Backup Solution
4 Copyright (C) 2011-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * Kern Sibbald, January MMXII
19 * Selection list. A string of integers separated by commas
20 * representing items selected. Ranges of the form nn-mm
28 * error if returns -1 and errmsg set
29 * end of items if returns -1 and errmsg NULL
31 int64_t sellist::next()
41 * As we walk the list, we set EOF in
42 * the end of the next item to ease scanning,
43 * but save and then restore the character.
45 for (p=e; p && *p; p=e) {
48 if (e) { /* have list */
55 h = strchr(p, '-'); /* range? */
57 errmsg = _("Negative numbers not permitted.\n");
60 if (h) { /* have range */
63 if (!is_an_integer(h)) {
64 errmsg = _("Range end is not integer.\n");
68 if (!is_an_integer(p)) {
69 errmsg = _("Range start is not an integer.\n");
72 beg = str_to_int64(p);
73 end = str_to_int64(h);
75 errmsg = _("Range end not bigger than start.\n");
78 } else { /* not list, not range */
81 /* Check for abort (.) */
83 errmsg = _("User cancel requested.\n");
86 /* Check for all keyword */
87 if (strncasecmp(p, "all", 3) == 0) {
92 if (!is_an_integer(p)) {
93 errmsg = _("Input value is not an integer.\n");
96 beg = end = str_to_int64(p);
104 if (beg <= 0 || end <= 0) {
105 errmsg = _("Selection items must be be greater than zero.\n");
109 errmsg = _("Selection item too large.\n");
117 errmsg = NULL; /* No error */
121 return -1; /* Error, errmsg set */
126 * Set selection string and optionally scan it
127 * returns false on error in string
130 bool sellist::set_string(char *string, bool scan=true)
133 * Copy string, because we write into it,
134 * then scan through it once to find any
144 e = str = bstrdup(string);
149 while (next() >= 0) {
162 /* Get the expanded list of all ids, very useful for SQL queries */
163 char *sellist::get_expanded_list()
165 int32_t expandedsize = 512;
172 p = expanded = (char *)malloc(expandedsize * sizeof(char));
175 while ((val = next()) >= 0) {
176 edit_int64(val, ed1);
179 /* Alloc more space if needed */
180 if ((p + len + 1) > (expanded + expandedsize)) {
181 expandedsize = expandedsize * 2;
183 tmp = (char *) realloc(expanded, expandedsize);
185 /* Compute new addresses for p and expanded */
186 p = tmp + (p - expanded);
190 /* If not at the begining of the string, add a "," */
204 int main(int argc, char **argv, char **env)
211 msg = _("No input string given.\n");
214 Dmsg1(000, "argv[1]=%s\n", argv[1]);
216 strip_trailing_junk(argv[1]);
217 sl.set_string(argv[1]);
219 //Dmsg1(000, "get_list=%s\n", sl.get_list());
221 /* If the list is very long, Dmsg will truncate it */
222 Dmsg1(000, "get_expanded_list=%s\n", NPRT(sl.get_expanded_list()));
224 if ((msg = sl.get_errmsg())) {
227 while ((i=sl.next()) > 0) {
228 Dmsg1(000, "rtn=%d\n", i);
230 if ((msg = sl.get_errmsg())) {
233 printf("\nPass 2 argv[1]=%s\n", argv[1]);
234 sl.set_string(argv[1]);
235 while ((i=sl.next()) > 0) {
236 Dmsg1(000, "rtn=%d\n", i);
238 msg = sl.get_errmsg();
239 Dmsg2(000, "rtn=%d msg=%s\n", i, NPRT(msg));
246 Dmsg1(000, "Error: %s\n", NPRT(msg));
250 #endif /* TEST_PROGRAM */