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");
113 errmsg = NULL; /* No error */
117 return -1; /* Error, errmsg set */
122 * Set selection string and optionally scan it
123 * returns false on error in string
126 bool sellist::set_string(char *string, bool scan=true)
129 * Copy string, because we write into it,
130 * then scan through it once to find any
140 e = str = bstrdup(string);
145 while (next() >= 0) {
158 /* Get the expanded list of all ids, very useful for SQL queries */
159 char *sellist::get_expanded_list()
161 int32_t expandedsize = 512;
168 p = expanded = (char *)malloc(expandedsize * sizeof(char));
171 while ((val = next()) >= 0) {
172 edit_int64(val, ed1);
175 /* Alloc more space if needed */
176 if ((p + len + 1) > (expanded + expandedsize)) {
177 expandedsize = expandedsize * 2;
179 tmp = (char *) realloc(expanded, expandedsize);
181 /* Compute new addresses for p and expanded */
182 p = tmp + (p - expanded);
186 /* If not at the begining of the string, add a "," */
200 int main(int argc, char **argv, char **env)
207 msg = _("No input string given.\n");
210 Dmsg1(000, "argv[1]=%s\n", argv[1]);
212 strip_trailing_junk(argv[1]);
213 sl.set_string(argv[1]);
215 //Dmsg1(000, "get_list=%s\n", sl.get_list());
217 /* If the list is very long, Dmsg will truncate it */
218 Dmsg1(000, "get_expanded_list=%s\n", NPRT(sl.get_expanded_list()));
220 if ((msg = sl.get_errmsg())) {
223 while ((i=sl.next()) > 0) {
224 Dmsg1(000, "rtn=%d\n", i);
226 if ((msg = sl.get_errmsg())) {
229 printf("\nPass 2 argv[1]=%s\n", argv[1]);
230 sl.set_string(argv[1]);
231 while ((i=sl.next()) > 0) {
232 Dmsg1(000, "rtn=%d\n", i);
234 msg = sl.get_errmsg();
235 Dmsg2(000, "rtn=%d msg=%s\n", i, NPRT(msg));
242 Dmsg1(000, "Error: %s\n", NPRT(msg));
246 #endif /* TEST_PROGRAM */