Imported Upstream version 3.3.3
[debian/amanda] / amandad-src / amandad_util.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /* 
28  * $Id: amandad_util.c,v 1.5 2006/07/19 17:46:07 martinea Exp $
29  *
30  */
31
32 #include "amanda.h"
33 #include "amandad.h"
34 #include "util.h"
35
36 #define MAXMAXDUMPS 16
37
38 void
39 init_g_options(
40     g_option_t *        g_options)
41 {
42     g_options->str      = NULL;
43     g_options->features = NULL;
44     g_options->hostname = NULL;
45     g_options->auth     = NULL;
46     g_options->maxdumps = 0;
47     g_options->config   = NULL;
48 }
49
50
51 g_option_t *
52 parse_g_options(
53     char *      str,
54     int         verbose)
55 {
56     g_option_t *g_options;
57     char *p, *tok;
58     int new_maxdumps;
59
60     g_options = alloc(sizeof(g_option_t));
61     init_g_options(g_options);
62     g_options->str = stralloc(str);
63
64     p = stralloc(str);
65     tok = strtok(p,";");
66
67     while (tok != NULL) {
68         if(strncmp(tok,"features=", 9) == 0) {
69             char *t = tok+9;
70             char *u = strchr(t, ';');
71             if (u)
72                *u = '\0';
73             if(g_options->features != NULL) {
74                 dbprintf(_("multiple features option\n"));
75                 if(verbose) {
76                     g_printf(_("ERROR [multiple features option]\n"));
77                 }
78             }
79             if((g_options->features = am_string_to_feature(t)) == NULL) {
80                 dbprintf(_("bad features value \"%s\"\n"), t);
81                 if(verbose) {
82                     g_printf(_("ERROR [bad features value \"%s\"]\n"), t);
83                 }
84             }
85             if (u)
86                *u = ';';
87         }
88         else if(strncmp(tok,"hostname=", 9) == 0) {
89             if(g_options->hostname != NULL) {
90                 dbprintf(_("multiple hostname option\n"));
91                 if(verbose) {
92                     g_printf(_("ERROR [multiple hostname option]\n"));
93                 }
94             }
95             g_options->hostname = stralloc(tok+9);
96         }
97         else if(strncmp(tok,"auth=", 5) == 0) {
98             if(g_options->auth != NULL) {
99                 dbprintf(_("multiple auth option\n"));
100                 if(verbose) {
101                     g_printf(_("ERROR [multiple auth option]\n"));
102                 }
103             }
104             g_options->auth = stralloc(tok+5);
105         }
106         else if(strncmp(tok,"maxdumps=", 9) == 0) {
107             if(g_options->maxdumps != 0) {
108                 dbprintf(_("multiple maxdumps option\n"));
109                 if(verbose) {
110                     g_printf(_("ERROR [multiple maxdumps option]\n"));
111                 }
112             }
113             if(sscanf(tok+9, "%d;", &new_maxdumps) == 1) {
114                 if (new_maxdumps > MAXMAXDUMPS) {
115                     g_options->maxdumps = MAXMAXDUMPS;
116                 }
117                 else if (new_maxdumps > 0) {
118                     g_options->maxdumps = new_maxdumps;
119                 }
120                 else {
121                     dbprintf(_("bad maxdumps value \"%s\"\n"), tok+9);
122                     if(verbose) {
123                         g_printf(_("ERROR [bad maxdumps value \"%s\"]\n"),
124                                tok+9);
125                     }
126                 }
127             }
128             else {
129                 dbprintf(_("bad maxdumps value \"%s\"\n"), tok+9);
130                 if(verbose) {
131                     g_printf(_("ERROR [bad maxdumps value \"%s\"]\n"),
132                            tok+9);
133                 }
134             }
135         }
136         else if(strncmp(tok,"config=", 7) == 0) {
137             if(g_options->config != NULL) {
138                 dbprintf(_("multiple config option\n"));
139                 if(verbose) {
140                     g_printf(_("ERROR [multiple config option]\n"));
141                 }
142             }
143             g_options->config = stralloc(tok+7);
144             if (strchr(g_options->config, '/')) {
145                 amfree(g_options->config);
146                 dbprintf(_("invalid character in config option\n"));
147                 if(verbose) {
148                     g_printf(_("ERROR [invalid character in config option]\n"));
149                 }
150             }
151         }
152         else {
153             dbprintf(_("unknown option \"%s\"\n"), tok);
154             if(verbose) {
155                 g_printf(_("ERROR [unknown option \"%s\"]\n"), tok);
156             }
157         }
158         tok = strtok(NULL, ";");
159     }
160     if(g_options->features == NULL) {
161         g_options->features = am_set_default_feature_set();
162     }
163     if(g_options->maxdumps == 0) /* default */
164         g_options->maxdumps = 1;
165     amfree(p);
166     return g_options;
167 }
168
169 void
170 free_g_options(
171     g_option_t *        g_options)
172 {
173     if (g_options != NULL) {
174         amfree(g_options->str);
175         am_release_feature_set(g_options->features);
176         amfree(g_options->hostname);
177         amfree(g_options->auth);
178         amfree(g_options->config);
179         amfree(g_options);
180     }
181 }