Skip to content

Commit 9992b8e

Browse files
Apply new indentation, brace, and whitespace style
1 parent bab69f2 commit 9992b8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+32141
-30822
lines changed

autoload.cpp

+93-58
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ The classes responsible for autoloading functions and completions.
1717
/* The time before we'll recheck an autoloaded file */
1818
static const int kAutoloadStalenessInterval = 15;
1919

20-
file_access_attempt_t access_file(const wcstring &path, int mode) {
20+
file_access_attempt_t access_file(const wcstring &path, int mode)
21+
{
2122
//printf("Touch %ls\n", path.c_str());
2223
file_access_attempt_t result = {0};
2324
struct stat statbuf;
24-
if (wstat(path, &statbuf)) {
25+
if (wstat(path, &statbuf))
26+
{
2527
result.error = errno;
26-
} else {
28+
}
29+
else
30+
{
2731
result.mod_time = statbuf.st_mtime;
28-
if (waccess(path, mode)) {
32+
if (waccess(path, mode))
33+
{
2934
result.error = errno;
30-
} else {
35+
}
36+
else
37+
{
3138
result.accessible = true;
3239
}
3340
}
@@ -39,21 +46,23 @@ file_access_attempt_t access_file(const wcstring &path, int mode) {
3946
}
4047

4148
autoload_t::autoload_t(const wcstring &env_var_name_var, const builtin_script_t * const scripts, size_t script_count) :
42-
lock(),
43-
env_var_name(env_var_name_var),
44-
builtin_scripts(scripts),
45-
builtin_script_count(script_count),
46-
last_path(),
47-
is_loading_set()
49+
lock(),
50+
env_var_name(env_var_name_var),
51+
builtin_scripts(scripts),
52+
builtin_script_count(script_count),
53+
last_path(),
54+
is_loading_set()
4855
{
4956
pthread_mutex_init(&lock, NULL);
5057
}
5158

52-
autoload_t::~autoload_t() {
59+
autoload_t::~autoload_t()
60+
{
5361
pthread_mutex_destroy(&lock);
5462
}
5563

56-
void autoload_t::node_was_evicted(autoload_function_t *node) {
64+
void autoload_t::node_was_evicted(autoload_function_t *node)
65+
{
5766
// This should only ever happen on the main thread
5867
ASSERT_IS_MAIN_THREAD();
5968

@@ -63,27 +72,27 @@ void autoload_t::node_was_evicted(autoload_function_t *node) {
6372
delete node;
6473
}
6574

66-
int autoload_t::unload( const wcstring &cmd )
75+
int autoload_t::unload(const wcstring &cmd)
6776
{
6877
return this->evict_node(cmd);
6978
}
7079

71-
int autoload_t::load( const wcstring &cmd, bool reload )
80+
int autoload_t::load(const wcstring &cmd, bool reload)
7281
{
73-
int res;
74-
CHECK_BLOCK( 0 );
82+
int res;
83+
CHECK_BLOCK(0);
7584
ASSERT_IS_MAIN_THREAD();
7685

77-
env_var_t path_var = env_get_string( env_var_name );
86+
env_var_t path_var = env_get_string(env_var_name);
7887

7988
/*
8089
Do we know where to look?
8190
*/
82-
if( path_var.empty() )
91+
if (path_var.empty())
8392
return 0;
8493

8594
/* Check if the lookup path has changed. If so, drop all loaded files. path_var may only be inspected on the main thread. */
86-
if( path_var != this->last_path )
95+
if (path_var != this->last_path)
8796
{
8897
this->last_path = path_var;
8998
scoped_lock locker(lock);
@@ -93,10 +102,10 @@ int autoload_t::load( const wcstring &cmd, bool reload )
93102
/** Warn and fail on infinite recursion. It's OK to do this because this function is only called on the main thread. */
94103
if (this->is_loading(cmd))
95104
{
96-
debug( 0,
97-
_( L"Could not autoload item '%ls', it is already being autoloaded. "
98-
L"This is a circular dependency in the autoloading scripts, please remove it."),
99-
cmd.c_str() );
105+
debug(0,
106+
_(L"Could not autoload item '%ls', it is already being autoloaded. "
107+
L"This is a circular dependency in the autoloading scripts, please remove it."),
108+
cmd.c_str());
100109
return 1;
101110
}
102111

@@ -105,48 +114,50 @@ int autoload_t::load( const wcstring &cmd, bool reload )
105114

106115
/* Get the list of paths from which we will try to load */
107116
std::vector<wcstring> path_list;
108-
tokenize_variable_array( path_var, path_list );
117+
tokenize_variable_array(path_var, path_list);
109118

110-
/* Try loading it */
111-
res = this->locate_file_and_maybe_load_it( cmd, true, reload, path_list );
119+
/* Try loading it */
120+
res = this->locate_file_and_maybe_load_it(cmd, true, reload, path_list);
112121

113122
/* Clean up */
114123
bool erased = !! is_loading_set.erase(cmd);
115124
assert(erased);
116125

117-
return res;
126+
return res;
118127
}
119128

120-
bool autoload_t::can_load( const wcstring &cmd, const env_vars_snapshot_t &vars )
129+
bool autoload_t::can_load(const wcstring &cmd, const env_vars_snapshot_t &vars)
121130
{
122131
const env_var_t path_var = vars.get(env_var_name);
123132
if (path_var.missing_or_empty())
124133
return false;
125134

126135
std::vector<wcstring> path_list;
127-
tokenize_variable_array( path_var, path_list );
128-
return this->locate_file_and_maybe_load_it( cmd, false, false, path_list );
136+
tokenize_variable_array(path_var, path_list);
137+
return this->locate_file_and_maybe_load_it(cmd, false, false, path_list);
129138
}
130139

131140
static bool script_name_precedes_script_name(const builtin_script_t &script1, const builtin_script_t &script2)
132141
{
133142
return wcscmp(script1.name, script2.name) < 0;
134143
}
135144

136-
void autoload_t::unload_all(void) {
145+
void autoload_t::unload_all(void)
146+
{
137147
scoped_lock locker(lock);
138148
this->evict_all_nodes();
139149
}
140150

141151
/** Check whether the given command is loaded. */
142-
bool autoload_t::has_tried_loading( const wcstring &cmd )
152+
bool autoload_t::has_tried_loading(const wcstring &cmd)
143153
{
144154
scoped_lock locker(lock);
145155
autoload_function_t * func = this->get_node(cmd);
146156
return func != NULL;
147157
}
148158

149-
static bool is_stale(const autoload_function_t *func) {
159+
static bool is_stale(const autoload_function_t *func)
160+
{
150161
/** Return whether this function is stale. Internalized functions can never be stale. */
151162
return ! func->is_internalized && time(NULL) - func->access.last_checked > kAutoloadStalenessInterval;
152163
}
@@ -155,11 +166,15 @@ autoload_function_t *autoload_t::get_autoloaded_function_with_creation(const wcs
155166
{
156167
ASSERT_IS_LOCKED(lock);
157168
autoload_function_t *func = this->get_node(cmd);
158-
if (! func) {
169+
if (! func)
170+
{
159171
func = new autoload_function_t(cmd);
160-
if (allow_eviction) {
172+
if (allow_eviction)
173+
{
161174
this->add_node(func);
162-
} else {
175+
}
176+
else
177+
{
163178
this->add_node_without_eviction(func);
164179
}
165180
}
@@ -178,11 +193,11 @@ autoload_function_t *autoload_t::get_autoloaded_function_with_creation(const wcs
178193
179194
Result: if really_load is true, returns whether the function was loaded. Otherwise returns whether the function existed.
180195
*/
181-
bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really_load, bool reload, const wcstring_list_t &path_list )
196+
bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_load, bool reload, const wcstring_list_t &path_list)
182197
{
183198
/* Note that we are NOT locked in this function! */
184-
size_t i;
185-
bool reloaded = 0;
199+
size_t i;
200+
bool reloaded = 0;
186201

187202
/* Try using a cached function. If we really want the function to be loaded, require that it be really loaded. If we're not reloading, allow stale functions. */
188203
{
@@ -196,22 +211,30 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
196211

197212
/* Determine if we can use this cached function */
198213
bool use_cached;
199-
if (! func) {
214+
if (! func)
215+
{
200216
/* Can't use a function that doesn't exist */
201217
use_cached = false;
202-
} else if (really_load && ! func->is_placeholder && ! func->is_loaded) {
218+
}
219+
else if (really_load && ! func->is_placeholder && ! func->is_loaded)
220+
{
203221
/* Can't use an unloaded function */
204222
use_cached = false;
205-
} else if ( ! allow_stale_functions && is_stale(func)) {
223+
}
224+
else if (! allow_stale_functions && is_stale(func))
225+
{
206226
/* Can't use a stale function */
207227
use_cached = false;
208-
} else {
228+
}
229+
else
230+
{
209231
/* I guess we can use it */
210232
use_cached = true;
211233
}
212234

213235
/* If we can use this function, return whether we were able to access it */
214-
if (use_cached) {
236+
if (use_cached)
237+
{
215238
return func->is_internalized || func->access.accessible;
216239
}
217240
}
@@ -235,7 +258,8 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
235258
matching_builtin_script = found;
236259
}
237260
}
238-
if (matching_builtin_script) {
261+
if (matching_builtin_script)
262+
{
239263
has_script_source = true;
240264
script_source = str2wcstring(matching_builtin_script->def);
241265

@@ -253,13 +277,14 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
253277
if (! has_script_source)
254278
{
255279
/* Iterate over path searching for suitable completion files */
256-
for( i=0; i<path_list.size(); i++ )
280+
for (i=0; i<path_list.size(); i++)
257281
{
258282
wcstring next = path_list.at(i);
259283
wcstring path = next + L"/" + cmd + L".fish";
260284

261285
const file_access_attempt_t access = access_file(path, R_OK);
262-
if (access.accessible) {
286+
if (access.accessible)
287+
{
263288
/* Found it! */
264289
found_file = true;
265290

@@ -269,15 +294,17 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
269294

270295
/* Generate the source if we need to load it */
271296
bool need_to_load_function = really_load && (func == NULL || func->access.mod_time != access.mod_time || ! func->is_loaded);
272-
if (need_to_load_function) {
297+
if (need_to_load_function)
298+
{
273299

274300
/* Generate the script source */
275301
wcstring esc = escape_string(path, 1);
276302
script_source = L". " + esc;
277303
has_script_source = true;
278304

279305
/* Remove any loaded command because we are going to reload it. Note that this will deadlock if command_removed calls back into us. */
280-
if (func && func->is_loaded) {
306+
if (func && func->is_loaded)
307+
{
281308
command_removed(cmd);
282309
func->is_placeholder = false;
283310
}
@@ -287,7 +314,8 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
287314
}
288315

289316
/* Create the function if we haven't yet. This does not load it. Do not trigger eviction unless we are actually loading, because we don't want to evict off of the main thread. */
290-
if (! func) {
317+
if (! func)
318+
{
291319
func = get_autoloaded_function_with_creation(cmd, really_load);
292320
}
293321

@@ -306,17 +334,21 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
306334
Later we only research if the current time is at least five seconds later.
307335
This way, the files won't be searched over and over again.
308336
*/
309-
if( ! found_file && ! has_script_source )
337+
if (! found_file && ! has_script_source)
310338
{
311339
scoped_lock locker(lock);
312340
/* Generate a placeholder */
313341
autoload_function_t *func = this->get_node(cmd);
314-
if (! func) {
342+
if (! func)
343+
{
315344
func = new autoload_function_t(cmd);
316345
func->is_placeholder = true;
317-
if (really_load) {
346+
if (really_load)
347+
{
318348
this->add_node(func);
319-
} else {
349+
}
350+
else
351+
{
320352
this->add_node_without_eviction(func);
321353
}
322354
}
@@ -327,7 +359,7 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
327359
/* If we have a script, either built-in or a file source, then run it */
328360
if (really_load && has_script_source)
329361
{
330-
if( exec_subshell( script_source) == -1 )
362+
if (exec_subshell(script_source) == -1)
331363
{
332364
/*
333365
Do nothing on failiure
@@ -336,9 +368,12 @@ bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really
336368

337369
}
338370

339-
if (really_load) {
371+
if (really_load)
372+
{
340373
return reloaded;
341-
} else {
374+
}
375+
else
376+
{
342377
return found_file || has_script_source;
343378
}
344379
}

0 commit comments

Comments
 (0)