Repository navigation
Expand file tree
/
Copy pathbashbuiltin_return.c
More file actions
70 lines (64 loc) · 2.19 KB
/
Copy pathbashbuiltin_return.c
File metadata and controls
70 lines (64 loc) · 2.19 KB
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
/* Author: Christoph Gille Licence: GNU */
#include "bash/loadables.h"
#include "cg_bashbuiltin.h"
#define DEBUG 0
#define WITH_ASSOCARRAY 0
int init_retval_builtin(WORD_LIST *list){
const bool is_retval=posparam_count>0 && !strcmp(dollar_vars[1],"-$");
if (DEBUG) fprintf(stderr,"This is init_retval_builtin. Has -$: %s \n",is_retval?"Yes":"No");
if (is_retval) shift_args(1);
bind_variable_value(make_local_variable("__return_var__",0),(is_retval?"1":"0"),0); // strdup ????
set_var_retval_invisible();
return EXECUTION_SUCCESS;
}
char *init_retval_doc[]={
"Initializes returning results.",
"Must be first statement in function body.",
"No arguments.",
"",
"Internally:",
" - Shifts away a leading -$ option",
" - sets shell variable __return_var__ to 0 or 1",
(char*)NULL
};
struct builtin init_retval_struct={"init_retval",init_retval_builtin,BUILTIN_ENABLED,init_retval_doc,"init_retval",0};
int set_retval_builtin(WORD_LIST *list){
if (DEBUG) fprintf(stderr,"This is set_retval_builtin\n");
bool is_stdout=true;
{
const SHELL_VAR *v=find_variable("__return_var__");
if (!v || !local_p(v)){
report_error(RED_ERROR"set_retval without previous init_retval\n");
EXIT_SHELL_RETURN();
}else{
if (v->value && *v->value=='1') is_stdout=false;
}
}
if (DEBUG) fprintf(stderr,"Variable __return_var__ is %d\n",is_stdout);
if (!is_stdout){
#if WITH_ASSOCARRAY
SHELL_VAR *var=convert_var_to_array(bind_global_variable(VARNAME_RETVAL,NULL,0));
if (list && list->word->word){
assign_array_var_from_word_list(var,list,0);
}else{
bind_array_element(var,0,"",0);
}
#else
char *value=list?list->word->word:"";
bind_global_variable(VARNAME_RETVAL,value?value:"",0);
#endif
}else{
for(;list;list=list->next){
const char *val=list->word->word;
puts(val?val:"");
}
}
return EXECUTION_SUCCESS;
}
char *set_retval_doc[]={
"Sets the return value of a function.",
"Argument: The string[s] to be returned.",
"Placed before each return statement and at the end of the function body",
(char*)NULL
};
struct builtin set_retval_struct={"set_retval",set_retval_builtin,BUILTIN_ENABLED,set_retval_doc,"set_retval",0};