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