Imported Upstream version 3.2.0
[debian/amanda] / server-src / cmdline.h
1 /*
2  * Copyright (c) 2007,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. Mathilda 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     char *write_timestamp;
47 } dumpspec_t;
48
49 /*
50  * Dumpspec list management
51  */
52
53 /* Create a new dumpspec with the given components
54  *
55  * @param host: host name
56  * @param disk: disk name
57  * @param datestamp: datestamp
58  * @param level: level (as a string, allowing regexes)
59  * @param write_timestamp: timestamp written to tape.
60  * @returns: dumpspec, or NULL on error
61  */
62 dumpspec_t *
63 dumpspec_new(
64     char *host, 
65     char *disk, 
66     char *datestamp,
67     char *level,
68     char *write_timestamp);
69
70 /* Free memory associated with a single dumpspec.  (Does not chase 
71  * next pointers)
72  *
73  * @param dumpspec: the dumpspec to free
74  */
75 void
76 dumpspec_free(
77     dumpspec_t *dumpspec);
78
79 /* Free memory associated with a list of dumpspecs.  CAUTION: do not
80  * use glib's g_slist_free directly on a dumpspec list, as it will not
81  * free the elements themselves.
82  *
83  * @param dumpspec_list: the GSList of dumpspecs to free
84  */
85 void
86 dumpspec_list_free(
87     GSList *dumpspec_list);
88
89 /*
90  * Parsing
91  */
92
93 /* Parse a command line matching the following syntax, and return
94  * the results as a linked list.  
95  *
96  *  [ host [ disk [ datestamp [ host [ disk [ datestamp .. ] ] ] ] ] ]
97  *
98  * If no results are specified, the function either returns NULL (an 
99  * empty list) or, if CMDLINE_EMPTY_TO_WILDCARD is given, a list 
100  * containing a single dumpspec with all fields set to "".
101  *
102  * Calls error() with any fatal errors, e.g., invalid regexes.
103  *
104  * @param argc: count of command line arguments
105  * @param argv: command line arguments
106  * @param flags: bitmask of the CMDLINE_* flags
107  * @returns: dumpspec list
108  */
109 GSList *
110 cmdline_parse_dumpspecs(
111     int argc,
112     char **argv,
113     int flags);
114 /* flags values (bitmask): */
115     /* parse datestamps after disks */
116 #    define CMDLINE_PARSE_DATESTAMP (1<<0)
117     /* parse levels after datestamps or disks */
118 #    define CMDLINE_PARSE_LEVEL (1<<1)
119     /* an empty argv should result in a wildcard dumpspec */
120 #    define CMDLINE_EMPTY_TO_WILDCARD (1<<2)
121
122 /*
123  * Formatting
124  */
125
126 /* Format a dumpspec into a string, with shell-compatible quoting.
127  *
128  * Caller is responsible for freeing the string.
129  *
130  * @param dumpspec: the dumpspec to format
131  * @returns: newly allocated string, or NULL on error
132  */
133 char *
134 cmdline_format_dumpspec(
135     dumpspec_t *dumpspec);
136
137 /* Like cmdline_format_dumpspec, but with components supplied 
138  * individually.  Caller is responsible for freeing the 
139  * string.
140  *
141  * @param host: host name
142  * @param disk: disk name
143  * @param datestamp: datestamp
144  * @returns: newly allocated string, or NULL on error
145  */
146 char *
147 cmdline_format_dumpspec_components(
148     char *host,
149     char *disk,
150     char *datestamp,
151     char *level);
152
153 /*
154  * Searching
155  */
156
157 /* Find all holding files matching the dumpspec list.  If
158  * the dumpspec list contains a dumpspec with all blank
159  * entries, all holding files are returned.
160  *
161  * Free the resulting list with g_slist_free_full()
162  *
163  * @param dumpspec_list: a list of dumpspecs
164  * @returns: a list of holding disk filenames.
165  */
166 GSList *
167 cmdline_match_holding(
168     GSList *dumpspec_list);
169
170 #endif /* CMDLINE_H */
171