1173592af596437616db2d7e4260cb3ac1d0591b
[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 "amandad.h"
32 #include "util.h"
33
34 #define MAXMAXDUMPS 16
35
36 void
37 init_g_options(
38     g_option_t *        g_options)
39 {
40     g_options->str      = NULL;
41     g_options->features = NULL;
42     g_options->hostname = NULL;
43     g_options->auth     = NULL;
44     g_options->maxdumps = 0;
45     g_options->config   = NULL;
46 }
47
48
49 g_option_t *
50 parse_g_options(
51     char *      str,
52     int         verbose)
53 {
54     g_option_t *g_options;
55     char *p, *tok;
56     int new_maxdumps;
57
58     g_options = alloc(sizeof(g_option_t));
59     init_g_options(g_options);
60     g_options->str = stralloc(str);
61
62     p = stralloc(str);
63     tok = strtok(p,";");
64
65     while (tok != NULL) {
66         if(strncmp(tok,"features=", 9) == 0) {
67             char *t = tok+9;
68             char *u = strchr(t, ';');
69             if (u)
70                *u = '\0';
71             if(g_options->features != NULL) {
72                 dbprintf(_("multiple features option\n"));
73                 if(verbose) {
74                     g_printf(_("ERROR [multiple features option]\n"));
75                 }
76             }
77             if((g_options->features = am_string_to_feature(t)) == NULL) {
78                 dbprintf(_("bad features value \"%s\"\n"), t);
79                 if(verbose) {
80                     g_printf(_("ERROR [bad features value \"%s\"]\n"), t);
81                 }
82             }
83             if (u)
84                *u = ';';
85         }
86         else if(strncmp(tok,"hostname=", 9) == 0) {
87             if(g_options->hostname != NULL) {
88                 dbprintf(_("multiple hostname option\n"));
89                 if(verbose) {
90                     g_printf(_("ERROR [multiple hostname option]\n"));
91                 }
92             }
93             g_options->hostname = stralloc(tok+9);
94         }
95         else if(strncmp(tok,"auth=", 5) == 0) {
96             if(g_options->auth != NULL) {
97                 dbprintf(_("multiple auth option\n"));
98                 if(verbose) {
99                     g_printf(_("ERROR [multiple auth option]\n"));
100                 }
101             }
102             g_options->auth = stralloc(tok+5);
103         }
104         else if(strncmp(tok,"maxdumps=", 9) == 0) {
105             if(g_options->maxdumps != 0) {
106                 dbprintf(_("multiple maxdumps option\n"));
107                 if(verbose) {
108                     g_printf(_("ERROR [multiple maxdumps option]\n"));
109                 }
110             }
111             if(sscanf(tok+9, "%d;", &new_maxdumps) == 1) {
112                 if (new_maxdumps > MAXMAXDUMPS) {
113                     g_options->maxdumps = MAXMAXDUMPS;
114                 }
115                 else if (new_maxdumps > 0) {
116                     g_options->maxdumps = new_maxdumps;
117                 }
118                 else {
119                     dbprintf(_("bad maxdumps value \"%s\"\n"), tok+9);
120                     if(verbose) {
121                         g_printf(_("ERROR [bad maxdumps value \"%s\"]\n"),
122                                tok+9);
123                     }
124                 }
125             }
126             else {
127                 dbprintf(_("bad maxdumps value \"%s\"\n"), tok+9);
128                 if(verbose) {
129                     g_printf(_("ERROR [bad maxdumps value \"%s\"]\n"),
130                            tok+9);
131                 }
132             }
133         }
134         else if(strncmp(tok,"config=", 7) == 0) {
135             if(g_options->config != NULL) {
136                 dbprintf(_("multiple config option\n"));
137                 if(verbose) {
138                     g_printf(_("ERROR [multiple config option]\n"));
139                 }
140             }
141             g_options->config = stralloc(tok+7);
142             if (strchr(g_options->config, '/')) {
143                 amfree(g_options->config);
144                 dbprintf(_("invalid character in config option\n"));
145                 if(verbose) {
146                     g_printf(_("ERROR [invalid character in config option]\n"));
147                 }
148             }
149         }
150         else {
151             dbprintf(_("unknown option \"%s\"\n"), tok);
152             if(verbose) {
153                 g_printf(_("ERROR [unknown option \"%s\"]\n"), tok);
154             }
155         }
156         tok = strtok(NULL, ";");
157     }
158     if(g_options->features == NULL) {
159         g_options->features = am_set_default_feature_set();
160     }
161     if(g_options->maxdumps == 0) /* default */
162         g_options->maxdumps = 1;
163     amfree(p);
164     return g_options;
165 }
166
167 void
168 free_g_options(
169     g_option_t *        g_options)
170 {
171     if (g_options != NULL) {
172         amfree(g_options->str);
173         am_release_feature_set(g_options->features);
174         amfree(g_options->hostname);
175         amfree(g_options->auth);
176         amfree(g_options->config);
177         amfree(g_options);
178     }
179 }