venus: Remove dedebug
[openafs.git] / src / libuafs / ukernel_swig.i
1 /*
2  * Copyright 2010, Sine Nomine Associates.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * libuafs SWIG interface file
12  *
13  * This file specifies the libuafs interfaces for SWIG, which can then be
14  * used to easily create libuafs bindings to other languages such as Perl.
15  *
16  * For each language you want a binding for, there are two typemaps you
17  * must define for that language, since SWIG does not handle them natively.
18  * These are the 'in' typemap for READBUF and LENGTH, and the 'argout'
19  * typemap for READBUF. Search this file for 'SWIGPERL' to see existing ones
20  * for the Perl bindings.
21  */
22
23 %module "AFS::ukernel"
24
25 %{
26 #include <afsconfig.h>
27 #include <afs/param.h>
28
29 #include <afs/afsutil.h>
30 #include <afs/sysincludes.h>
31 #include <afs_usrops.h>
32 #include <afs/cmd.h>
33 #include <afs/afs_args.h>
34 #include <afs/com_err.h>
35 %}
36
37 %include "typemaps.i";
38
39 %apply long { off_t };
40
41 %rename (uafs_ParseArgs) swig_uafs_ParseArgs;
42 %inline %{
43 /* SWIG doesn't handle argv-like string arrays too well, so instead have
44  * a wrapper to convert a single string of all arguments into an argv-like
45  * array. Conveniently, libcmd can do this.
46  *
47  * We could instead do this with SWIG typemaps, but writing this little
48  * function instead is much more language-neutral. With typemaps, we'd have
49  * to write the converting code for each target language.
50  */
51 extern int
52 swig_uafs_ParseArgs(char *line)
53 {
54     char *argv[1024];
55     int argc;
56     int code;
57
58     code = cmd_ParseLine(line, argv, &argc, sizeof(argv)/sizeof(argv[0]));
59     if (code) {
60         afs_com_err("AFS::ukernel", code, "parsing line: '%s'", line);
61         return code;
62     }
63
64     code = uafs_ParseArgs(argc, argv);
65
66     cmd_FreeArgv(argv);
67
68     return code;
69 }
70 %}
71 extern int uafs_Setup(const char *mount);
72 extern int uafs_Run(void);
73
74 /*
75  * Define typemaps for binary read buffers. SWIG natively handles
76  * NUL-terminated strings, but uafs_read could have NULs in the middle of the
77  * string, so we need these typemaps to pay attention to the string length.
78  *
79  * (Reading in a binary buffer from e.g. uafs_write is already handled natively
80  * by SWIG. Fancy that.)
81  */
82 #if defined(SWIGPERL)
83 %typemap(in, numinputs=1) (char *READBUF, int LENGTH) {
84     if (!SvIOK($input)) {
85         SWIG_croak("expected an integer");
86     }
87     $2 = SvIV($input);
88     Newx($1, $2, char);
89 }
90 %typemap(argout, numinputs=1) char *READBUF {
91     /* some logic here copied from typemaps.i and/or SWIG itself, since I'm not
92      * a perl dev */
93
94     if (argvi >= items) {
95         EXTEND(sp, 1);
96     }
97
98     /* 'result' is the return value from the actual C function call; we assume
99      * it is an int that represents how many bytes of data were actually read into
100      * the buffer if nonnegative */
101     if (result < 0) {
102         $result = &PL_sv_undef;
103     } else {
104         $result = sv_2mortal(newSVpvn($1, result));
105     }
106
107     Safefree($1);
108     argvi++;
109 }
110 #else
111 # error No READBUF typemap defined for the given language
112 #endif
113
114 extern int uafs_mkdir(char *path, int mode);
115 extern int uafs_chdir(char *path);
116 extern int uafs_open(char *path, int flags, int mode=0);
117 extern int uafs_creat(char *path, int mode);
118 extern int uafs_write(int fd, char *STRING, int LENGTH);
119 extern int uafs_pwrite(int fd, char *STRING, int LENGTH, off_t offset);
120 extern int uafs_read(int fd, char *READBUF, int LENGTH);
121 extern int uafs_pread(int fd, char *READBUF, int LENGTH, off_t offset);
122 extern int uafs_fsync(int fd);
123 extern int uafs_close(int fd);
124
125 %{
126 #define STAT_TYPE long
127 #define STAT_ARGS        \
128     STAT_TYPE *adev,     \
129     STAT_TYPE *aino,     \
130     STAT_TYPE *amode,    \
131     STAT_TYPE *anlink,   \
132     STAT_TYPE *auid,     \
133     STAT_TYPE *agid,     \
134     STAT_TYPE *ardev,    \
135     STAT_TYPE *asize,    \
136     STAT_TYPE *aatime,   \
137     STAT_TYPE *amtime,   \
138     STAT_TYPE *actime,   \
139     STAT_TYPE *ablksize, \
140     STAT_TYPE *ablocks
141
142 #define STAT_COPYFROM(st) do {   \
143     *adev     = (st).st_dev;     \
144     *aino     = (st).st_ino;     \
145     *amode    = (st).st_mode;    \
146     *anlink   = (st).st_nlink;   \
147     *auid     = (st).st_uid;     \
148     *agid     = (st).st_gid;     \
149     *ardev    = (st).st_rdev;    \
150     *asize    = (st).st_size;    \
151     *aatime   = (st).st_atime;   \
152     *amtime   = (st).st_mtime;   \
153     *actime   = (st).st_ctime;   \
154     *ablksize = (st).st_blksize; \
155     *ablocks  = (st).st_blocks;  \
156 } while (0)
157
158 int
159 swig_uafs_stat(char *path, STAT_ARGS)
160 {
161     int code;
162     struct stat st;
163     code = uafs_stat(path, &st);
164     if (code == 0) {
165         STAT_COPYFROM(st);
166     }
167     return code;
168 }
169 int
170 swig_uafs_lstat(char *path, STAT_ARGS)
171 {
172     int code;
173     struct stat st;
174     code = uafs_lstat(path, &st);
175     if (code == 0) {
176         STAT_COPYFROM(st);
177     }
178     return code;
179 }
180 int
181 swig_uafs_fstat(int fd, STAT_ARGS)
182 {
183     int code;
184     struct stat st;
185     code = uafs_fstat(fd, &st);
186     if (code == 0) {
187         STAT_COPYFROM(st);
188     }
189     return code;
190 }
191
192 #undef STAT_TYPE
193 #undef STAT_ARGS
194 #undef STAT_COPYFROM
195 %}
196
197 %define STAT_TYPE
198 long
199 %enddef
200 %define STAT_OUT_ARGS
201 STAT_TYPE *OUTPUT,
202 STAT_TYPE *OUTPUT,
203 STAT_TYPE *OUTPUT,
204 STAT_TYPE *OUTPUT,
205 STAT_TYPE *OUTPUT,
206 STAT_TYPE *OUTPUT,
207 STAT_TYPE *OUTPUT,
208 STAT_TYPE *OUTPUT,
209 STAT_TYPE *OUTPUT,
210 STAT_TYPE *OUTPUT,
211 STAT_TYPE *OUTPUT,
212 STAT_TYPE *OUTPUT,
213 STAT_TYPE *OUTPUT
214 %enddef
215
216 %rename (uafs_stat) swig_uafs_stat;
217 %rename (uafs_lstat) swig_uafs_lstat;
218 %rename (uafs_fstat) swig_uafs_fstat;
219
220 extern int swig_uafs_stat(char *path, STAT_OUT_ARGS);
221 extern int swig_uafs_lstat(char *path, STAT_OUT_ARGS);
222 extern int swig_uafs_fstat(int fd, STAT_OUT_ARGS);
223
224 extern int uafs_truncate(char *path, int len);
225 extern int uafs_ftruncate(int fd, int len);
226 extern int uafs_lseek(int fd, int offset, int whence);
227 extern int uafs_chmod(char *path, int mode);
228 extern int uafs_fchmod(int fd, int mode);
229 extern int uafs_symlink(char *target, char *source);
230 extern int uafs_unlink(char *path);
231 extern int uafs_rmdir(char *path);
232 extern int uafs_readlink(char *path, char *READBUF, int LENGTH);
233 extern int uafs_link(char *existing, char *new);
234 extern int uafs_rename(char *old, char *new);
235
236 extern usr_DIR *uafs_opendir(char *path);
237
238 %rename (uafs_readdir) swig_uafs_readdir;
239 %{
240 /*
241  * Language-neutral wrapper for uafs_readdir. Since the language won't know
242  * what to do with a struct usr_dirent, we could either make a SWIG typemap, or
243  * define a wrapper with multiple return arguments. Making a typemap is
244  * language-specific, so we define a wrapper, and let the typemaps.i library
245  * worry about the language-specific parts of getting multiple return values.
246  */
247 char *
248 swig_uafs_readdir(usr_DIR *dirp, unsigned long *d_ino, unsigned long *d_off, unsigned short *d_reclen)
249 {
250     struct usr_dirent *dentry;
251
252     dentry = uafs_readdir(dirp);
253     if (!dentry) {
254         *d_ino = *d_off = *d_reclen = 0;
255         return NULL;
256     }
257
258     *d_ino = dentry->d_ino;
259     *d_off = dentry->d_off;
260     *d_reclen = dentry->d_reclen;
261
262     return strdup(dentry->d_name);
263 }
264 %}
265
266 extern char * swig_uafs_readdir(usr_DIR *dirp, unsigned long *OUTPUT, unsigned long *OUTPUT, unsigned short *OUTPUT);
267 extern int uafs_closedir(usr_DIR * dirp);
268 extern void uafs_SetRxPort(int);
269 extern void uafs_Shutdown(void);