Imported Upstream version 3.3.3
[debian/amanda] / perl / Amanda / Cmdline.swg
1 /*
2  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 %module "Amanda::Cmdline"
23 %include "amglue/amglue.swg"
24 %include "exception.i"
25 %include "amglue/dumpspecs.swg"
26
27 %include "Amanda/Cmdline.pod"
28
29 %{
30 #include <glib.h>
31 #include "amanda.h"
32 #include "cmdline.h"
33 #include "fileheader.h"
34 %}
35
36 /* Add a few methods to make this type act like a class */
37 typedef struct dumpspec_t {
38     %immutable;
39     char *host;
40     char *disk;
41     char *datestamp;
42     char *level;
43     char *write_timestamp;
44     %mutable;
45
46     %extend {
47         /* constructor */
48         dumpspec_t(char *host, char *disk, char *datestamp, char *level, char *write_timestamp) {
49             return dumpspec_new(host, disk, datestamp, level, write_timestamp);
50         }
51
52         ~dumpspec_t() {
53             dumpspec_free(self);
54         }
55
56         %newobject format;
57         char *format() {
58             return cmdline_format_dumpspec(self);
59         }
60     }
61 } dumpspec_t;
62
63 %rename(format_dumpspec_components) cmdline_format_dumpspec_components;
64 char *cmdline_format_dumpspec_components(char *host, char *disk, char *datestamp, char *level);
65
66 /* Typemap to convert a perl list of strings to the strv that 
67  * cmdline_parse_dumpspecs expects.
68  */
69 %typemap(in, numinputs=1) (int argc, char **argv) {
70     AV *av;
71     int i;
72
73     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
74         SWIG_exception(SWIG_TypeError, "Expected an arrayref");
75     }
76     av = (AV *)SvRV($input);
77
78     $1 = av_len(av)+1; /* av_len(av) is like $#av */
79     $2 = malloc(sizeof(char *) * $1);
80     for (i = 0; i < $1; i++) {
81         SV **elt = av_fetch(av, i, 0);
82         if (!elt || !SvPOK(*elt)) {
83             SWIG_exception(SWIG_TypeError, "Non-string in arrayref");
84         }
85         $2[i] = SvPV_nolen(*elt); /* TODO: handle unicode here */
86     }
87 }
88
89 /* Free the space allocated by the previous typemap */
90 %typemap(freearg) (int argc, char **argv) {
91     free($2);
92 }
93
94 amglue_add_flag_tag_fns(cmdline_parse_dumpspecs_flags);
95 amglue_add_constant(CMDLINE_PARSE_DATESTAMP, cmdline_parse_dumpspecs_flags);
96 amglue_add_constant(CMDLINE_PARSE_LEVEL, cmdline_parse_dumpspecs_flags);
97 amglue_add_constant(CMDLINE_EMPTY_TO_WILDCARD, cmdline_parse_dumpspecs_flags);
98 amglue_add_constant(CMDLINE_EXACT_MATCH, cmdline_parse_dumpspecs_flags);
99 amglue_copy_to_tag(cmdline_parse_dumpspecs_flags, constants);
100
101 %rename(parse_dumpspecs) cmdline_parse_dumpspecs;
102 amglue_dumpspec_list *cmdline_parse_dumpspecs(int argc, char **argv, int flags);
103
104 /* TODO:
105  * convert AV back to GSList as input, convert resulting GSList into an AV of strings
106  * on output
107  */
108 /* amglue_dumpspec_list * cmdline_match_holding(amglue_dumpspec_list *dumpspec_list); */
109
110 %inline %{
111 gboolean header_matches_dumpspecs(dumpfile_t *dumpfile, amglue_dumpspec_list *dumpspecs) {
112     char level_str[100];
113
114     /* ignore anything that's not a (split) dumpfile */
115     if(dumpfile->type != F_DUMPFILE && dumpfile->type != F_SPLIT_DUMPFILE)
116         return FALSE;
117
118     g_snprintf(level_str, sizeof(level_str), "%d", dumpfile->dumplevel);
119
120     while (dumpspecs) {
121         dumpspec_t *ds = (dumpspec_t *)dumpspecs->data;
122         dumpspecs = g_slist_next(dumpspecs);
123
124         if (ds->host && *ds->host
125             && !match_host(ds->host, dumpfile->name))
126             continue;
127
128         if (ds->disk && *ds->disk
129             && !match_disk(ds->disk, dumpfile->disk))
130             continue;
131
132         if (ds->datestamp && *ds->datestamp
133             && !match_datestamp(ds->datestamp, dumpfile->datestamp))
134             continue;
135
136         if (ds->level && *ds->level
137             && !match_level(ds->level, level_str))
138             continue;
139
140         /* passed all the checks, so it's a match */
141         return TRUE;
142     }
143
144     return FALSE;
145 }
146 %}
147
148 amglue_export_ok(header_matches_dumpspecs parse_dumpspecs format_dumpspec_components);