9b0c6e99875a5c068f74b5ccdf8968b7420882c1
[debian/amanda] / perl / Amanda / Cmdline.swg
1 /*
2  * Copyright (c) Zmanda, Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation.
7  *
8  * This library 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 Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 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
21 %module "Amanda::Cmdline"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24 %include "amglue/dumpspecs.swg"
25
26 %{
27 #include <glib.h>
28 #include "cmdline.h"
29 %}
30
31 %perlcode %{
32 =head1 NAME
33
34 Amanda::Cmdline - utilities for handling command lines
35
36 =head1 SYNOPSIS
37
38   use Amanda::Cmdline;
39
40   my $spec = Amanda::Cmdline::dumpspec_t->new($host, $disk, $datestamp, $level);
41   print "host: $spec->{'host'}; disk: $spec->{'disk'}\n";
42
43   my @specs = Amanda::Cmdline::parse_dumpspecs(["host", "disk", "date"],
44                             $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP);
45
46 =head1 API STATUS
47
48 Will change.
49
50 =head1 Amanda::Cmdline::dumpspec_t Objects
51
52 =head2 Instance Variables
53
54 =over
55
56 =item C<$host>
57
58 =item C<$disk>
59
60 =item C<$datestamp>
61
62 =item C<$level>
63
64 =back
65
66 =head2 Methods
67
68 =over
69
70 =item C<format()>
71
72 Format the dumpspec as a string.
73
74 =back
75
76 =head1 Functions
77
78 =over
79
80 =item C<format_dumpspec_components($host, $disk, $datestamp, $level)>
81
82 This function returns a string representing the formatted form of the given dumpspec.  This formatting
83 is the same as performed by C<format_dumpspec_components>, but does not need a C<dumpspec_t>.
84
85 =item C<parse_dumpspecs(@cmdline, $flags)> 
86
87 This function parses C<@cmdline> into a list of C<dumpspec_t> objects,
88 according to C<$flags>, which is a logical combination of zero or
89 more of C<$CMDLINE_PARSE_DATESTAMP> to recognize datestamps and
90 C<$CMDLINE_PARSE_LEVEL> to recognize levels.
91
92 =back
93
94 =head1 SEE ALSO
95
96 L<Amanda::Config> handles C<-o> options itself, through C<config_overwrites>.
97
98 =cut
99 %}
100
101 /* Add a few methods to make this type act like a class */
102 typedef struct dumpspec_t {
103     %immutable;
104     char *host;
105     char *disk;
106     char *datestamp;
107     char *level;
108     %mutable;
109
110     %extend {
111         /* constructor */
112         dumpspec_t(char *host, char *disk, char *datestamp, char *level) {
113             return dumpspec_new(host, disk, datestamp, level);
114         }
115
116         ~dumpspec_t() {
117             dumpspec_free(self);
118         }
119
120         %newobject format;
121         char *format() {
122             return cmdline_format_dumpspec(self);
123         }
124     }
125 } dumpspec_t;
126
127 %rename(format_dumpspec_components) cmdline_format_dumpspec_components;
128 char *cmdline_format_dumpspec_components(char *host, char *disk, char *datestamp, char *level);
129
130 /* Typemap to convert a perl list of strings to the strv that 
131  * cmdline_parse_dumpspecs expects.
132  */
133 %typemap(in, numinputs=1) (int argc, char **argv) {
134     AV *av;
135     int i;
136
137     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
138         SWIG_exception(SWIG_TypeError, "Expected an arrayref");
139     }
140     av = (AV *)SvRV($input);
141
142     $1 = av_len(av)+1; /* av_len(av) is like $#av */
143     $2 = malloc(sizeof(char *) * $1);
144     for (i = 0; i < $1; i++) {
145         SV **elt = av_fetch(av, i, 0);
146         if (!elt || !SvPOK(*elt)) {
147             SWIG_exception(SWIG_TypeError, "Non-string in arrayref");
148         }
149         $2[i] = SvPV_nolen(*elt); /* TODO: handle unicode here */
150     }
151 }
152
153 /* Free the space allocated by the previous typemap */
154 %typemap(freearg) (int argc, char **argv) {
155     free($2);
156 }
157
158 amglue_add_flag_tag_fns(cmdline_parse_dumpspecs_flags);
159 amglue_add_constant(CMDLINE_PARSE_DATESTAMP, cmdline_parse_dumpspecs_flags);
160 amglue_add_constant(CMDLINE_PARSE_LEVEL, cmdline_parse_dumpspecs_flags);
161 amglue_add_constant(CMDLINE_EMPTY_TO_WILDCARD, cmdline_parse_dumpspecs_flags);
162
163 %rename(parse_dumpspecs) cmdline_parse_dumpspecs;
164 amglue_dumpspec_list *cmdline_parse_dumpspecs(int argc, char **argv, int flags);
165
166 /* TODO:
167  * convert AV back to GSList as input, convert resulting GSList into an AV of strings
168  * on output
169  */
170 /* amglue_dumpspec_list * cmdline_match_holding(amglue_dumpspec_list *dumpspec_list); */
171