]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.c
Fix lib/sellist.c
[bacula/bacula] / bacula / src / lib / sellist.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2011-2012 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula(R) is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Kern Sibbald, January  MMXII
30  *
31  *  Selection list. A string of integers separated by commas
32  *   representing items selected. Ranges of the form nn-mm
33  *   are also permitted.
34  */
35
36 #include "bacula.h"
37
38 /*
39  * Returns next item
40  *   error if returns -1 and errmsg set
41  *   end of items if returns -1 and errmsg NULL
42  */
43 int64_t sellist::next()
44 {
45    errmsg = NULL;
46    if (beg <= end) {
47       return beg++;
48    }
49    if (e == NULL) {
50       goto bail_out;
51    }
52    /*
53     * As we walk the list, we set EOF in
54     *   the end of the next item to ease scanning,
55     *   but save and then restore the character.
56     */
57    for (p=e; p && *p; p=e) {
58       /* Check for list */
59       e = strchr(p, ',');
60       if (e) {
61          esave = *e;
62          *e++ = 0;
63       } else {
64          esave = 0;
65       }
66       /* Check for range */
67       h = strchr(p, '-');             /* range? */
68       if (h == p) {
69          errmsg = _("Negative numbers not permitted.\n");
70          goto bail_out;
71       }
72       if (h) {
73          hsave = *h;
74          *h++ = 0;
75          if (!is_an_integer(h)) {
76             errmsg = _("Range end is not integer.\n");
77             goto bail_out;
78          }
79          skip_spaces(&p);
80          if (!is_an_integer(p)) {
81             errmsg = _("Range start is not an integer.\n");
82             goto bail_out;
83          }
84          beg = str_to_int64(p);
85          end = str_to_int64(h);
86          if (end < beg) {
87             errmsg = _("Range end not bigger than start.\n");
88             goto bail_out;
89          }
90       } else {
91          hsave = 0;
92          skip_spaces(&p);
93          if (!is_an_integer(p)) {
94             errmsg = _("Input value is not an integer.\n");
95             goto bail_out;
96          }
97          beg = end = str_to_int64(p);
98       }
99       if (esave) {
100          *(e-1) = esave;
101       }
102       if (hsave) {
103          *(h-1) = hsave;
104       }
105       if (beg <= 0 || end <= 0) {
106          errmsg = _("Selection items must be be greater than zero.\n");
107          goto bail_out;
108       }
109       if (end > max) {
110          errmsg = _("Selection item too large.\n");
111          goto bail_out;
112       }
113       if (beg <= end) {
114          return beg++;
115       }
116    }
117    /* End of items */
118    errmsg = NULL;      /* No error */
119    return -1;
120
121 bail_out:
122    return -1;          /* Error, errmsg set */
123 }
124
125
126 /*
127  * Set selection string and optionally scan it
128  *   returns false on error in string
129  *   returns true if OK
130  */
131 bool sellist::set_string(char *string, bool scan=true)
132 {
133    /*
134     * Copy string, because we write into it,
135     *  then scan through it once to find any
136     *  errors.
137     */
138    if (str) {
139       free(str);
140    }
141    e = str = bstrdup(string);
142    end = 0;
143    beg = 1;
144    num_items = 0;
145    if (scan) {
146       while (next() >= 0) {
147          num_items++;
148       }
149       if (get_errmsg()) {
150          return false;
151       }
152       e = str;
153       end = 0;
154       beg = 1;
155    }
156    return true;
157 }
158
159 #ifdef TEST_PROGRAM
160 int main(int argc, char **argv, char **env)
161 {
162    char *msg;
163    sellist sl;
164    int i;
165
166    if (!argv[1]) {
167       msg = _("No input string given.\n");
168       goto bail_out;
169    }
170    Dmsg1(000, "argv[1]=%s\n", argv[1]);
171
172    strip_trailing_junk(argv[1]);
173    sl.set_string(argv[1]);
174    if ((msg = sl.get_errmsg())) {
175       goto bail_out;
176    }
177    while ((i=sl.next()) >= 0) {
178       Dmsg1(000, "%d\n", i);
179    }
180    if ((msg = sl.get_errmsg())) {
181       goto bail_out;
182    }
183    printf("\nPass 2\n");
184    sl.set_string(argv[1]);
185    while ((i=sl.next()) >= 0) {
186       Dmsg1(000, "%d\n", i);
187    }
188    if ((msg = sl.get_errmsg())) {
189       goto bail_out;
190    }
191    return 0;
192
193 bail_out:
194    Dmsg1(000, "Error: %s\n", msg);
195    return 1;
196
197 }
198 #endif /* TEST_PROGRAM */