Imported Upstream version 3.3.3
[debian/amanda] / server-src / cmdline.h
1 /*
2  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  * 
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  * 
18  * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20  *
21  * Author: Dustin J. Mitchell <dustin@zmanda.com>
22  */
23 /*
24  * $Id$
25  *
26  * Utility routines for handling command lines.
27  */
28
29 #ifndef CMDLINE_H
30 #define CMDLINE_H
31
32 #include <glib.h>
33 #include "glib-util.h"
34
35 /* A dumpspec can specify a particular dump (combining host, disk, and 
36  * datestamp), or can be less specific by leaving out some components.
37  * Missing components are NULL, except in the special case of an 
38  * "wildcard" dumpspec, as detailed below.
39  *
40  * All strings in this struct are independently malloc()ed.
41  */
42 typedef struct dumpspec_t {
43     char *host;
44     char *disk;
45     char *datestamp;
46     char *level;
47     char *write_timestamp;
48 } dumpspec_t;
49
50 /*
51  * Dumpspec list management
52  */
53
54 /* Create a new dumpspec with the given components
55  *
56  * @param host: host name
57  * @param disk: disk name
58  * @param datestamp: datestamp
59  * @param level: level (as a string, allowing regexes)
60  * @param write_timestamp: timestamp written to tape.
61  * @returns: dumpspec, or NULL on error
62  */
63 dumpspec_t *
64 dumpspec_new(
65     char *host, 
66     char *disk, 
67     char *datestamp,
68     char *level,
69     char *write_timestamp);
70
71 /* Free memory associated with a single dumpspec.  (Does not chase 
72  * next pointers)
73  *
74  * @param dumpspec: the dumpspec to free
75  */
76 void
77 dumpspec_free(
78     dumpspec_t *dumpspec);
79
80 /* Free memory associated with a list of dumpspecs.  CAUTION: do not
81  * use glib's g_slist_free directly on a dumpspec list, as it will not
82  * free the elements themselves.
83  *
84  * @param dumpspec_list: the GSList of dumpspecs to free
85  */
86 void
87 dumpspec_list_free(
88     GSList *dumpspec_list);
89
90 /*
91  * Parsing
92  */
93
94 /* Parse a command line matching the following syntax, and return
95  * the results as a linked list.  
96  *
97  *  [ host [ disk [ datestamp [ host [ disk [ datestamp .. ] ] ] ] ] ]
98  *
99  * If no results are specified, the function either returns NULL (an 
100  * empty list) or, if CMDLINE_EMPTY_TO_WILDCARD is given, a list 
101  * containing a single dumpspec with all fields set to "".
102  *
103  * Calls error() with any fatal errors, e.g., invalid regexes.
104  *
105  * @param argc: count of command line arguments
106  * @param argv: command line arguments
107  * @param flags: bitmask of the CMDLINE_* flags
108  * @returns: dumpspec list
109  */
110 GSList *
111 cmdline_parse_dumpspecs(
112     int argc,
113     char **argv,
114     int flags);
115 /* flags values (bitmask): */
116     /* parse datestamps after disks */
117 #    define CMDLINE_PARSE_DATESTAMP (1<<0)
118     /* parse levels after datestamps or disks */
119 #    define CMDLINE_PARSE_LEVEL (1<<1)
120     /* an empty argv should result in a wildcard dumpspec */
121 #    define CMDLINE_EMPTY_TO_WILDCARD (1<<2)
122     /* use exact match instead of host and disk expression */
123 #    define CMDLINE_EXACT_MATCH (1<<3)
124
125 /*
126  * Formatting
127  */
128
129 /* Format a dumpspec into a string, with shell-compatible quoting.
130  *
131  * Caller is responsible for freeing the string.
132  *
133  * @param dumpspec: the dumpspec to format
134  * @returns: newly allocated string, or NULL on error
135  */
136 char *
137 cmdline_format_dumpspec(
138     dumpspec_t *dumpspec);
139
140 /* Like cmdline_format_dumpspec, but with components supplied 
141  * individually.  Caller is responsible for freeing the 
142  * string.
143  *
144  * @param host: host name
145  * @param disk: disk name
146  * @param datestamp: datestamp
147  * @returns: newly allocated string, or NULL on error
148  */
149 char *
150 cmdline_format_dumpspec_components(
151     char *host,
152     char *disk,
153     char *datestamp,
154     char *level);
155
156 /*
157  * Searching
158  */
159
160 /* Find all holding files matching the dumpspec list.  If
161  * the dumpspec list contains a dumpspec with all blank
162  * entries, all holding files are returned.
163  *
164  * Free the resulting list with slist_free_full()
165  *
166  * @param dumpspec_list: a list of dumpspecs
167  * @returns: a list of holding disk filenames.
168  */
169 GSList *
170 cmdline_match_holding(
171     GSList *dumpspec_list);
172
173 #endif /* CMDLINE_H */
174