Imported Upstream version 2.6.0
[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., 505 N Mathlida Ave, Suite 120
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %module "Amanda::Cmdline"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24
25 %{
26 #include <glib.h>
27 #include "cmdline.h"
28 %}
29
30 %perlcode %{
31 =head1 NAME
32
33 Amanda::Cmdline - utilities for handling command lines
34
35 =head1 SYNOPSIS
36
37   use Amanda::Cmdline;
38
39   my $spec = Amanda::Cmdline::dumpspec_t->new($host, $disk, $datestamp, $level);
40   print "host: $spec->{'host'}; disk: $spec->{'disk'}\n";
41
42   my @specs = Amanda::Cmdline::parse_dumpspecs(["host", "disk", "date"],
43                             $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP);
44   
45 =head1 API STATUS
46
47 Will change.
48
49 =head1 Amanda::Cmdline::dumpspec_t Objects
50
51 =head2 Instance Variables
52
53 =over
54
55 =item C<$host>
56
57 =item C<$disk>
58
59 =item C<$datestamp>
60
61 =item C<$level>
62
63 =back
64
65 =head2 Methods
66
67 =over
68
69 =item C<format()>
70
71 Format the dumpspec as a string.
72
73 =back
74
75 =head1 Functions
76
77 =over
78
79 =item C<format_dumpspec_components($host, $disk, $datestamp, $level)>
80
81 This function returns a string representing the formatted form of the given dumpspec.  This formatting
82 is the same as performed by C<format_dumpspec_components>, but does not need a C<dumpspec_t>.
83
84 =item C<parse_dumpspecs(@cmdline, $flags)> 
85
86 This function parses C<@cmdline> into a list of C<dumpspec_t> objects,
87 according to C<$flags>, which is a logical combination of zero or
88 more of C<$CMDLINE_PARSE_DATESTAMP> to recognize datestamps and
89 C<$CMDLINE_PARSE_LEVEL> to recognize levels.
90
91 =back
92
93 =head1 SEE ALSO
94
95 L<Amanda::Config> handles C<-o> options itself, through C<config_overwrites>.
96
97 =cut
98 %}
99
100 /* Add a few methods to make this type act like a class */
101 typedef struct dumpspec_t {
102     %immutable;
103     char *host;
104     char *disk;
105     char *datestamp;
106     char *level;
107     %mutable;
108
109     %extend {
110         /* constructor */
111         dumpspec_t(char *host, char *disk, char *datestamp, char *level) {
112             return dumpspec_new(host, disk, datestamp, level);
113         }
114
115         ~dumpspec_t() {
116             dumpspec_free(self);
117         }
118
119         %newobject format;
120         char *format() {
121             return cmdline_format_dumpspec(self);
122         }
123     }
124 } dumpspec_t;
125
126 %rename(format_dumpspec_components) cmdline_format_dumpspec_components;
127 char *cmdline_format_dumpspec_components(char *host, char *disk, char *datestamp, char *level);
128
129 /* Typemap to convert the GSList that cmdline_parse_dumpspecs will
130  * return into an array of dumpspec_t's.  This uses some SWIG trickery
131  * to manage to convert the GSList itself to an array, while leaving the
132  * dumpspecs as C objects.
133  */
134 %typemap(out) GSList * {
135     if ($1) {
136         GSList *iter;
137         EXTEND(SP, g_slist_length($1)); /* make room for return values */
138
139         iter = $1;
140         while (iter) {
141             /* Let SWIG take ownership of the object; we'll free the GSList momentarily */
142             $result = SWIG_NewPointerObj(iter->data, $descriptor(dumpspec_t*), SWIG_OWNER | SWIG_SHADOW);
143             argvi++;
144             iter = iter->next;
145         }
146
147         /* Now free the GSList, but *not* its contents (which are now "owned" by SWIG) */
148         g_slist_free($1);
149     }
150 }
151
152 /* Typemap to convert a perl list of strings to the strv that 
153  * cmdline_parse_dumpspecs expects.
154  */
155 %typemap(in, numinputs=1) (int argc, char **argv) {
156     AV *av;
157     int i;
158
159     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
160         SWIG_exception(SWIG_TypeError, "Expected an arrayref");
161     }
162     av = (AV *)SvRV($input);
163
164     $1 = av_len(av)+1; /* av_len(av) is like $#av */
165     $2 = malloc(sizeof(char *) * $1);
166     for (i = 0; i < $1; i++) {
167         SV **elt = av_fetch(av, i, 0);
168         if (!elt || !SvPOK(*elt)) {
169             SWIG_exception(SWIG_TypeError, "Non-string in arrayref");
170         }
171         $2[i] = SvPV_nolen(*elt); /* TODO: handle unicode here */
172     }
173 }
174
175 /* Free the space allocated by the previous typemap */
176 %typemap(freearg) (int argc, char **argv) {
177     free($2);
178 }
179
180 amglue_add_flag_tag_fns(cmdline_parse_dumpspecs_flags);
181 amglue_add_constant(CMDLINE_PARSE_DATESTAMP, cmdline_parse_dumpspecs_flags);
182 amglue_add_constant(CMDLINE_PARSE_LEVEL, cmdline_parse_dumpspecs_flags);
183 amglue_add_constant(CMDLINE_EMPTY_TO_WILDCARD, cmdline_parse_dumpspecs_flags);
184
185 %rename(parse_dumpspecs) cmdline_parse_dumpspecs;
186 GSList *cmdline_parse_dumpspecs(int argc, char **argv, int flags);
187
188 /* TODO:
189  * convert AV back to GSList as input, convert resulting GSList into an AV of strings
190  * on output
191  */
192 /* GSList * cmdline_match_holding(GSList *dumpspec_list); */
193