Skip to content

Instantly share code, notes, and snippets.

@konsolebox
Created July 11, 2024 07:29
Show Gist options
  • Save konsolebox/d9fb2fadd2b8b13d96d0aa7ebea836d9 to your computer and use it in GitHub Desktop.
Save konsolebox/d9fb2fadd2b8b13d96d0aa7ebea836d9 to your computer and use it in GitHub Desktop.
> git diff
diff --git a/builtins/evalfile.c b/builtins/evalfile.c
index 17a568a0..4084a496 100644
--- a/builtins/evalfile.c
+++ b/builtins/evalfile.c
@@ -85,8 +85,9 @@ _evalfile (const char *filename, int flags)
size_t file_size;
sh_vmsg_func_t *errfunc;
#if defined (ARRAY_VARS)
- SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
- ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
+ char *real_filename, realbuf[PATH_MAX+1];
+ SHELL_VAR *funcname_v, *bash_source_v, *bash_source_real_v, *bash_lineno_v;
+ ARRAY *funcname_a, *bash_source_a, *bash_source_real_a, *bash_lineno_a;
struct func_array_state *fa;
# if defined (DEBUGGER)
SHELL_VAR *bash_argv_v, *bash_argc_v;
@@ -238,6 +239,7 @@ file_error_and_exit:
#if defined (ARRAY_VARS)
GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
+ GET_ARRAY_FROM_VAR ("BASH_SOURCE_REAL", bash_source_real_v, bash_source_real_a);
GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
# if defined (DEBUGGER)
GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v, bash_argv_a);
@@ -245,6 +247,8 @@ file_error_and_exit:
# endif
array_push (bash_source_a, (char *)filename);
+ real_filename = sh_realpath (filename, realbuf);
+ array_push (bash_source_real_a, real_filename != NULL ? real_filename : "");
t = itos (executing_line_number ());
array_push (bash_lineno_a, t);
free (t);
@@ -253,6 +257,8 @@ file_error_and_exit:
fa = (struct func_array_state *)xmalloc (sizeof (struct func_array_state));
fa->source_a = bash_source_a;
fa->source_v = bash_source_v;
+ fa->source_real_a = bash_source_real_a;
+ fa->source_real_v = bash_source_real_v;
fa->lineno_a = bash_lineno_a;
fa->lineno_v = bash_lineno_v;
fa->funcname_a = funcname_a;
diff --git a/command.h b/command.h
index 1c068148..171fcff1 100644
--- a/command.h
+++ b/command.h
@@ -349,6 +349,7 @@ typedef struct function_def {
WORD_DESC *name; /* The name of the function. */
COMMAND *command; /* The parsed execution tree. */
char *source_file; /* file in which function was defined, if any */
+ char *source_file_real; /* realpath version */
} FUNCTION_DEF;
/* A command that is `grouped' allows pipes and redirections to affect all
diff --git a/copy_cmd.c b/copy_cmd.c
index 6426c016..2aac6f37 100644
--- a/copy_cmd.c
+++ b/copy_cmd.c
@@ -323,6 +323,7 @@ copy_function_def_contents (FUNCTION_DEF *old, FUNCTION_DEF *new_def)
new_def->flags = old->flags;
new_def->line = old->line;
new_def->source_file = old->source_file ? savestring (old->source_file) : old->source_file;
+ new_def->source_file_real = old->source_file_real ? savestring (old->source_file_real) : 0;
return (new_def);
}
diff --git a/execute_cmd.c b/execute_cmd.c
index 8600de20..c5d78f9d 100644
--- a/execute_cmd.c
+++ b/execute_cmd.c
@@ -5111,6 +5111,7 @@ restore_funcarray_state (struct func_array_state *fa)
ARRAY *funcname_a;
array_pop (fa->source_a);
+ array_pop (fa->source_real_a);
array_pop (fa->lineno_a);
GET_ARRAY_FROM_VAR ("FUNCNAME", nfv, funcname_a);
@@ -5152,14 +5153,15 @@ execute_function (SHELL_VAR *var, WORD_LIST *words, int flags, struct fd_bitmap
COMMAND *tc, *fc, *save_current;
char *debug_trap, *error_trap, *return_trap;
#if defined (ARRAY_VARS)
- SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
+ SHELL_VAR *funcname_v, *bash_source_v, *bash_source_real_v, *bash_lineno_v;
ARRAY *funcname_a;
volatile ARRAY *bash_source_a;
+ volatile ARRAY *bash_source_real_a;
volatile ARRAY *bash_lineno_a;
struct func_array_state *fa;
#endif
FUNCTION_DEF *shell_fn;
- char *sfile, *t;
+ char *sfile, *sfile_real, *t;
sh_getopt_state_t *gs;
SHELL_VAR *gv;
@@ -5176,6 +5178,7 @@ execute_function (SHELL_VAR *var, WORD_LIST *words, int flags, struct fd_bitmap
#if defined (ARRAY_VARS)
GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
+ GET_ARRAY_FROM_VAR ("BASH_SOURCE_REAL", bash_source_real_v, bash_source_real_a);
GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
#endif
@@ -5276,9 +5279,10 @@ execute_function (SHELL_VAR *var, WORD_LIST *words, int flags, struct fd_bitmap
/* This is quite similar to the code in shell.c and elsewhere. */
shell_fn = find_function_def (this_shell_function->name);
sfile = shell_fn ? shell_fn->source_file : "";
+ sfile_real = shell_fn && shell_fn->source_file_real ? shell_fn->source_file_real : "";
array_push ((ARRAY *)funcname_a, this_shell_function->name);
-
array_push ((ARRAY *)bash_source_a, sfile);
+ array_push ((ARRAY *)bash_source_real_a, sfile_real);
lineno = GET_LINE_NUMBER ();
t = itos (lineno);
array_push ((ARRAY *)bash_lineno_a, t);
@@ -5289,6 +5293,8 @@ execute_function (SHELL_VAR *var, WORD_LIST *words, int flags, struct fd_bitmap
fa = (struct func_array_state *)xmalloc (sizeof (struct func_array_state));
fa->source_a = (ARRAY *)bash_source_a;
fa->source_v = bash_source_v;
+ fa->source_real_a = (ARRAY *)bash_source_real_a;
+ fa->source_real_v = bash_source_real_v;
fa->lineno_a = (ARRAY *)bash_lineno_a;
fa->lineno_v = bash_lineno_v;
fa->funcname_a = (ARRAY *)funcname_a;
diff --git a/execute_cmd.h b/execute_cmd.h
index 981e94a1..1940b5a7 100644
--- a/execute_cmd.h
+++ b/execute_cmd.h
@@ -30,6 +30,8 @@ struct func_array_state
SHELL_VAR *funcname_v;
ARRAY *source_a;
SHELL_VAR *source_v;
+ ARRAY *source_real_a;
+ SHELL_VAR *source_real_v;
ARRAY *lineno_a;
SHELL_VAR *lineno_v;
};
diff --git a/make_cmd.c b/make_cmd.c
index 3830a3b2..ad453d97 100644
--- a/make_cmd.c
+++ b/make_cmd.c
@@ -731,8 +731,8 @@ make_function_def (WORD_DESC *name, COMMAND *command, int lineno, int lstart)
{
FUNCTION_DEF *temp;
#if defined (ARRAY_VARS)
- SHELL_VAR *bash_source_v;
- ARRAY *bash_source_a;
+ SHELL_VAR *bash_source_v, *bash_source_real_v;
+ ARRAY *bash_source_a, *bash_source_real_a;
#endif
temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
@@ -743,11 +743,14 @@ make_function_def (WORD_DESC *name, COMMAND *command, int lineno, int lstart)
command->line = lstart;
/* Information used primarily for debugging and error messages. */
- temp->source_file = 0;
+ temp->source_file = temp->source_file_real = 0;
#if defined (ARRAY_VARS)
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
+ GET_ARRAY_FROM_VAR ("BASH_SOURCE_REAL", bash_source_real_v, bash_source_real_a);
if (bash_source_a && array_num_elements (bash_source_a) > 0)
temp->source_file = array_reference (bash_source_a, 0);
+ if (bash_source_real_a && array_num_elements (bash_source_real_a) > 0)
+ temp->source_file_real = array_reference (bash_source_real_a, 0);
#endif
/* Assume that shell functions without a source file before the shell is
initialized come from the environment. Otherwise default to "main"
@@ -769,6 +772,7 @@ make_function_def (WORD_DESC *name, COMMAND *command, int lineno, int lstart)
#endif
temp->source_file = temp->source_file ? savestring (temp->source_file) : 0;
+ temp->source_file_real = temp->source_file_real ? savestring (temp->source_file_real) : 0;
return (make_command (cm_function_def, (SIMPLE_COM *)temp));
}
diff --git a/shell.c b/shell.c
index 01fffac2..f9d7970c 100644
--- a/shell.c
+++ b/shell.c
@@ -1578,8 +1578,9 @@ open_shell_script (char *script_name)
int sample_len;
struct stat sb;
#if defined (ARRAY_VARS)
- SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
- ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
+ char *real_filename, realbuf[PATH_MAX+1];
+ SHELL_VAR *funcname_v, *bash_source_v, *bash_source_real_v, *bash_lineno_v;
+ ARRAY *funcname_a, *bash_source_a, *bash_source_real_a, *bash_lineno_a;
#endif
filename = savestring (script_name);
@@ -1636,9 +1637,12 @@ open_shell_script (char *script_name)
#if defined (ARRAY_VARS)
GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
+ GET_ARRAY_FROM_VAR ("BASH_SOURCE_REAL", bash_source_real_v, bash_source_real_a);
GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
array_push (bash_source_a, filename);
+ real_filename = sh_realpath (filename, realbuf);
+ array_push (bash_source_real_a, real_filename != NULL ? real_filename : "");
if (bash_lineno_a)
{
t = itos (executing_line_number ());
diff --git a/variables.c b/variables.c
index 203387f7..5770349b 100644
--- a/variables.c
+++ b/variables.c
@@ -1889,6 +1889,7 @@ initialize_dynamic_variables (void)
v = init_dynamic_array_var ("BASH_ARGV", get_bashargcv, null_array_assign, att_noassign|att_nounset);
# endif /* DEBUGGER */
v = init_dynamic_array_var ("BASH_SOURCE", get_self, null_array_assign, att_noassign|att_nounset);
+ v = init_dynamic_array_var ("BASH_SOURCE_REAL", get_self, null_array_assign, att_noassign|att_nounset);
v = init_dynamic_array_var ("BASH_LINENO", get_self, null_array_assign, att_noassign|att_nounset);
v = init_dynamic_assoc_var ("BASH_CMDS", get_hashcmd, assign_hashcmd, att_nofree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment