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