1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/* Copyright 2016
* Kaz Kylheku <kaz@kylheku.com>
* Vancouver, Canada
* All rights reserved.
*
* Redistribution of this software in source and binary forms, with or without
* modification, is permitted provided that the following two conditions are met.
*
* Use of this software in any manner constitutes agreement with the disclaimer
* which follows the two conditions.
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DAMAGES, HOWEVER CAUSED,
* AND UNDER ANY THEORY OF LIABILITY, ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdarg.h>
#include <wchar.h>
#include <signal.h>
#include <stdlib.h>
#include <stddef.h>
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <ftw.h>
#include "config.h"
#include ALLOCA_H
#include "lib.h"
#include "gc.h"
#include "args.h"
#include "utf8.h"
#include "eval.h"
#include "signal.h"
#include "unwind.h"
#include "sysif.h"
#include "ftw.h"
static val s_callback;
static uw_frame_t *s_exit_point;
static int ftw_callback(const char *c_path, const struct stat *c_sb,
int c_type, struct FTW *fb)
{
int c_result = 1;
uw_frame_t cont_guard;
uw_push_guard(&cont_guard);
uw_simple_catch_begin;
sig_check_fast();
{
val path = string_utf8(c_path);
val type = num(c_type);
val sb = stat_to_struct(*c_sb);
val level = num(fb->level);
val base = num(fb->base);
val result;
args_decl(args, ARGS_MIN);
args_add5(args, path, type, sb, level, base);
result = generic_funcall(s_callback, args);
c_result = if3(integerp(result), c_num(result), 0);
}
uw_unwind {
s_exit_point = uw_curr_exit_point;
uw_curr_exit_point = 0; /* stops unwinding */
}
uw_catch_end;
uw_pop_frame(&cont_guard);
return c_result;
}
val ftw_wrap(val dirpath, val fn, val flags_in, val nopenfd_in)
{
if (s_callback) {
uw_throwf(error_s, lit("ftw: cannot be re-entered from "
"ftw callback"), nao);
} else {
int nopenfd = c_num(default_arg(nopenfd_in, num_fast(20)));
int flags = c_num(default_arg(flags_in, zero));
char *dirpath_u8 = utf8_dup_to(c_str(dirpath));
int res = (s_callback = fn,
nftw(dirpath_u8, ftw_callback, nopenfd, flags));
s_callback = nil;
free(dirpath_u8);
if (s_exit_point) {
uw_frame_t *ep = s_exit_point;
s_exit_point = 0;
uw_continue(ep);
}
switch (res) {
case 0:
return t;
case -1:
return nil;
default:
return num(res);
}
}
}
void ftw_init(void)
{
prot1(&s_callback);
/* ftw flags */
#ifdef FTW_PHYS
reg_varl(intern(lit("ftw-phys"), user_package), num_fast(FTW_PHYS));
#endif
#ifdef FTW_MOUNT
reg_varl(intern(lit("ftw-mount"), user_package), num_fast(FTW_MOUNT));
#endif
#ifdef FTW_CHDIR
reg_varl(intern(lit("ftw-chdir"), user_package), num_fast(FTW_CHDIR));
#endif
#ifdef FTW_DEPTH
reg_varl(intern(lit("ftw-depth"), user_package), num_fast(FTW_DEPTH));
#endif
#ifdef FTW_ACTIONRETVAL
reg_varl(intern(lit("ftw-actionretval"), user_package), num_fast(FTW_ACTIONRETVAL));
#endif
/* callback type codes */
#ifdef FTW_F
reg_varl(intern(lit("ftw-f"), user_package), num_fast(FTW_F));
#endif
#ifdef FTW_D
reg_varl(intern(lit("ftw-d"), user_package), num_fast(FTW_D));
#endif
#ifdef FTW_DNR
reg_varl(intern(lit("ftw-dnr"), user_package), num_fast(FTW_DNR));
#endif
#ifdef FTW_NS
reg_varl(intern(lit("ftw-ns"), user_package), num_fast(FTW_NS));
#endif
#ifdef FTW_SL
reg_varl(intern(lit("ftw-sl"), user_package), num_fast(FTW_SL));
#endif
#ifdef FTW_DP
reg_varl(intern(lit("ftw-dp"), user_package), num_fast(FTW_DP));
#endif
#ifdef FTW_SLN
reg_varl(intern(lit("ftw-sln"), user_package), num_fast(FTW_SLN));
#endif
/* callback return values */
#ifdef FTW_CONTINUE
reg_varl(intern(lit("ftw-continue"), user_package), num_fast(FTW_CONTINUE));
#endif
#ifdef FTW_STOP
reg_varl(intern(lit("ftw-stop"), user_package), num_fast(FTW_STOP));
#endif
#ifdef FTW_SKIP_SUBTREE
reg_varl(intern(lit("ftw-skip-subtree"), user_package), num_fast(FTW_SKIP_SUBTREE));
#endif
#ifdef FTW_SKIP_SIBLINGS
reg_varl(intern(lit("ftw-skip-siblings"), user_package), num_fast(FTW_SKIP_SIBLINGS));
#endif
reg_fun(intern(lit("ftw"), user_package), func_n4o(ftw_wrap, 2));
}
|