Imported Upstream version 3.2.0
[debian/amanda] / perl / Amanda / Cmdline.swg
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010 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 94085, 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 %include "Amanda/Cmdline.pod"
27
28 %{
29 #include <glib.h>
30 #include "amanda.h"
31 #include "cmdline.h"
32 #include "fileheader.h"
33 %}
34
35 /* Add a few methods to make this type act like a class */
36 typedef struct dumpspec_t {
37     %immutable;
38     char *host;
39     char *disk;
40     char *datestamp;
41     char *level;
42     char *write_timestamp;
43     %mutable;
44
45     %extend {
46         /* constructor */
47         dumpspec_t(char *host, char *disk, char *datestamp, char *level, char *write_timestamp) {
48             return dumpspec_new(host, disk, datestamp, level, write_timestamp);
49         }
50
51         ~dumpspec_t() {
52             dumpspec_free(self);
53         }
54
55         %newobject format;
56         char *format() {
57             return cmdline_format_dumpspec(self);
58         }
59     }
60 } dumpspec_t;
61
62 %rename(format_dumpspec_components) cmdline_format_dumpspec_components;
63 char *cmdline_format_dumpspec_components(char *host, char *disk, char *datestamp, char *level);
64
65 /* Typemap to convert a perl list of strings to the strv that 
66  * cmdline_parse_dumpspecs expects.
67  */
68 %typemap(in, numinputs=1) (int argc, char **argv) {
69     AV *av;
70     int i;
71
72     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
73         SWIG_exception(SWIG_TypeError, "Expected an arrayref");
74     }
75     av = (AV *)SvRV($input);
76
77     $1 = av_len(av)+1; /* av_len(av) is like $#av */
78     $2 = malloc(sizeof(char *) * $1);
79     for (i = 0; i < $1; i++) {
80         SV **elt = av_fetch(av, i, 0);
81         if (!elt || !SvPOK(*elt)) {
82             SWIG_exception(SWIG_TypeError, "Non-string in arrayref");
83         }
84         $2[i] = SvPV_nolen(*elt); /* TODO: handle unicode here */
85     }
86 }
87
88 /* Free the space allocated by the previous typemap */
89 %typemap(freearg) (int argc, char **argv) {
90     free($2);
91 }
92
93 amglue_add_flag_tag_fns(cmdline_parse_dumpspecs_flags);
94 amglue_add_constant(CMDLINE_PARSE_DATESTAMP, cmdline_parse_dumpspecs_flags);
95 amglue_add_constant(CMDLINE_PARSE_LEVEL, cmdline_parse_dumpspecs_flags);
96 amglue_add_constant(CMDLINE_EMPTY_TO_WILDCARD, cmdline_parse_dumpspecs_flags);
97 amglue_copy_to_tag(cmdline_parse_dumpspecs_flags, constants);
98
99 %rename(parse_dumpspecs) cmdline_parse_dumpspecs;
100 amglue_dumpspec_list *cmdline_parse_dumpspecs(int argc, char **argv, int flags);
101
102 /* TODO:
103  * convert AV back to GSList as input, convert resulting GSList into an AV of strings
104  * on output
105  */
106 /* amglue_dumpspec_list * cmdline_match_holding(amglue_dumpspec_list *dumpspec_list); */
107
108 %inline %{
109 gboolean header_matches_dumpspecs(dumpfile_t *dumpfile, amglue_dumpspec_list *dumpspecs) {
110     char level_str[100];
111
112     /* ignore anything that's not a (split) dumpfile */
113     if(dumpfile->type != F_DUMPFILE && dumpfile->type != F_SPLIT_DUMPFILE)
114         return FALSE;
115
116     g_snprintf(level_str, sizeof(level_str), "%d", dumpfile->dumplevel);
117
118     while (dumpspecs) {
119         dumpspec_t *ds = (dumpspec_t *)dumpspecs->data;
120         dumpspecs = g_slist_next(dumpspecs);
121
122         if (ds->host && *ds->host
123             && !match_host(ds->host, dumpfile->name))
124             continue;
125
126         if (ds->disk && *ds->disk
127             && !match_disk(ds->disk, dumpfile->disk))
128             continue;
129
130         if (ds->datestamp && *ds->datestamp
131             && !match_datestamp(ds->datestamp, dumpfile->datestamp))
132             continue;
133
134         if (ds->level && *ds->level
135             && !match_level(ds->level, level_str))
136             continue;
137
138         /* passed all the checks, so it's a match */
139         return TRUE;
140     }
141
142     return FALSE;
143 }
144 %}
145
146 amglue_export_ok(header_matches_dumpspecs parse_dumpspecs format_dumpspec_components);