Imported Upstream version 2.5.1
[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             if(g_options->features != NULL) {
68                 dbprintf(("%s: multiple features option\n", 
69                           debug_prefix(NULL)));
70                 if(verbose) {
71                     printf("ERROR [multiple features option]\n");
72                 }
73             }
74             if((g_options->features = am_string_to_feature(tok+9)) == NULL) {
75                 dbprintf(("%s: bad features value \"%s\n",
76                           debug_prefix(NULL), tok+10));
77                 if(verbose) {
78                     printf("ERROR [bad features value \"%s\"]\n", tok+10);
79                 }
80             }
81         }
82         else if(strncmp(tok,"hostname=", 9) == 0) {
83             if(g_options->hostname != NULL) {
84                 dbprintf(("%s: multiple hostname option\n", 
85                           debug_prefix(NULL)));
86                 if(verbose) {
87                     printf("ERROR [multiple hostname option]\n");
88                 }
89             }
90             g_options->hostname = stralloc(tok+9);
91         }
92         else if(strncmp(tok,"auth=", 5) == 0) {
93             if(g_options->auth != NULL) {
94                 dbprintf(("%s: multiple auth option\n", 
95                           debug_prefix(NULL)));
96                 if(verbose) {
97                     printf("ERROR [multiple auth option]\n");
98                 }
99             }
100             g_options->auth = stralloc(tok+5);
101         }
102         else if(strncmp(tok,"maxdumps=", 9) == 0) {
103             if(g_options->maxdumps != 0) {
104                 dbprintf(("%s: multiple maxdumps option\n", 
105                           debug_prefix(NULL)));
106                 if(verbose) {
107                     printf("ERROR [multiple maxdumps option]\n");
108                 }
109             }
110             if(sscanf(tok+9, "%d;", &new_maxdumps) == 1) {
111                 if (new_maxdumps > MAXMAXDUMPS) {
112                     g_options->maxdumps = MAXMAXDUMPS;
113                 }
114                 else if (new_maxdumps > 0) {
115                     g_options->maxdumps = new_maxdumps;
116                 }
117                 else {
118                     dbprintf(("%s: bad maxdumps value \"%s\"\n",
119                               debug_prefix(NULL), tok+9));
120                     if(verbose) {
121                         printf("ERROR [bad maxdumps value \"%s\"]\n",
122                                tok+9);
123                     }
124                 }
125             }
126             else {
127                 dbprintf(("%s: bad maxdumps value \"%s\"\n",
128                           debug_prefix(NULL), tok+9));
129                 if(verbose) {
130                     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(("%s: multiple config option\n",
138                           debug_prefix(NULL)));
139                 if(verbose) {
140                     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(("%s: invalid character in config option\n",
147                           debug_prefix(NULL)));
148                 if(verbose) {
149                     printf("ERROR [invalid character in config option]\n");
150                 }
151             }
152         }
153         else {
154             dbprintf(("%s: unknown option \"%s\"\n",
155                                   debug_prefix(NULL), tok));
156             if(verbose) {
157                 printf("ERROR [unknown option \"%s\"]\n", tok);
158             }
159         }
160         tok = strtok(NULL, ";");
161     }
162     if(g_options->features == NULL) {
163         g_options->features = am_set_default_feature_set();
164     }
165     if(g_options->maxdumps == 0) /* default */
166         g_options->maxdumps = 1;
167     amfree(p);
168     return g_options;
169 }
170
171 void
172 free_g_options(
173     g_option_t *        g_options)
174 {
175     amfree(g_options->str);
176     am_release_feature_set(g_options->features);
177     amfree(g_options->hostname);
178     amfree(g_options->auth);
179     amfree(g_options->config);
180     amfree(g_options);
181 }