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