>From a0ca52e731716d5339036df0a19f1ed23b598b73 Mon Sep 17 00:00:00 2001 From: Daniel Reichelt Date: Tue, 2 Apr 2013 10:39:11 +0200 Subject: [PATCH 2/2] print command type in eval.c and adjust Makefile --- bash/Makefile.in | 4 ++ bash/eval.c | 2 + bash/print_cmd_on_noexec.c | 126 ++++++++++++++++++++++++++++++++++++++++++++ bash/print_cmd_on_noexec.h | 30 ++++++++++ bash/testsh | 16 ++++++ 5 files changed, 178 insertions(+), 0 deletions(-) create mode 100644 bash/print_cmd_on_noexec.c create mode 100755 bash/print_cmd_on_noexec.h create mode 100755 bash/testsh diff --git a/bash/Makefile.in b/bash/Makefile.in index f01d49c..3a21b1a 100644 --- a/bash/Makefile.in +++ b/bash/Makefile.in @@ -418,6 +418,7 @@ CSOURCES = shell.c eval.c parse.y general.c make_cmd.c print_cmd.c y.tab.c \ input.c bashhist.c array.c arrayfunc.c assoc.c sig.c pathexp.c \ unwind_prot.c siglist.c bashline.c bracecomp.c error.c \ list.c stringlib.c locale.c findcmd.c redir.c \ + print_cmd_on_noexec.c \ pcomplete.c pcomplib.c syntax.c xmalloc.c HSOURCES = shell.h flags.h trap.h hashcmd.h hashlib.h jobs.h builtins.h \ @@ -427,6 +428,7 @@ HSOURCES = shell.h flags.h trap.h hashcmd.h hashlib.h jobs.h builtins.h \ subst.h externs.h siglist.h bashhist.h bashline.h bashtypes.h \ array.h arrayfunc.h sig.h mailcheck.h bashintl.h bashjmp.h \ execute_cmd.h parser.h pathexp.h pathnames.h pcomplete.h assoc.h \ + print_cmd_on_noexec.h \ $(BASHINCFILES) SOURCES = $(CSOURCES) $(HSOURCES) $(BUILTIN_DEFS) @@ -446,6 +448,7 @@ OBJECTS = shell.o eval.o y.tab.o general.o make_cmd.o print_cmd.o $(GLOBO) \ trap.o input.o unwind_prot.o pathexp.o sig.o test.o version.o \ alias.o array.o arrayfunc.o assoc.o braces.o bracecomp.o bashhist.o \ bashline.o $(SIGLIST_O) list.o stringlib.o locale.o findcmd.o redir.o \ + print_cmd_on_noexec.o \ pcomplete.o pcomplib.o syntax.o xmalloc.o $(SIGNAMES_O) # Where the source code of the shell builtins resides. @@ -1055,6 +1058,7 @@ variables.o: pcomplete.h ${BASHINCDIR}/chartypes.h variables.o: ${BASHINCDIR}/posixtime.h assoc.h version.o: conftypes.h patchlevel.h version.h xmalloc.o: config.h bashtypes.h ${BASHINCDIR}/ansi_stdlib.h error.h +print_cmd_on_noexec.o: bashtypes.h command.h config.h general.h print_cmd_on_noexec.h # job control diff --git a/bash/eval.c b/bash/eval.c index 9011e0b..9db35d5 100644 --- a/bash/eval.c +++ b/bash/eval.c @@ -138,6 +138,8 @@ reader_loop () { if (interactive_shell == 0 && read_but_dont_execute) { + if (read_and_print_but_dont_execute) + print_cmd_on_noexec(global_command); last_command_exit_value = EXECUTION_SUCCESS; dispose_command (global_command); global_command = (COMMAND *)NULL; diff --git a/bash/print_cmd_on_noexec.c b/bash/print_cmd_on_noexec.c new file mode 100644 index 0000000..5af0216 --- /dev/null +++ b/bash/print_cmd_on_noexec.c @@ -0,0 +1,126 @@ +#include "print_cmd_on_noexec.h" + + + + +/*TODO: instead of duplicating builtins, these should be moved from mkinternals.c + to an includable header +*/ +char *builtins[] = { + ":", ".", "alias", "alias", "bg", "break", "cd", "command", "continue", + "declare", "eval", "exec", "exit", "export", "export", "false", "fc", + "fg", "getopts", "jobs", "kill", "local", "newgrp", "pwd", "read", + "readonly", "readonly", "return", "set", "shift", "source", "times", + "trap", "true", "typeset", "umask", "unalias", "unset", "wait", + }; + +char *specials[] = { + ".", "eval", "nohup", + //TODO: how to handle parameters? + "bash", "exec", "invoke-rc.d", "ionice", "nice", "sh", + }; + +/*TODO: apparently not yet covered: + < <(foo) + case $(bar) in + a=$(( $(foo) + $(bar) )) + $(foo) +*/ +void print_cmd_on_noexec(COMMAND *cmd) { + int i; + WORD_LIST *wl = NULL; + + if (cmd != NULL) { + switch (cmd->type) { +#if defined (DPAREN_ARITHMETIC) + case cm_arith: + //TODO: implement? + printf("cm_arith\n"); + break; +#endif +#if defined (ARITH_FOR_COMMAND) + case cm_arith_for: + printf("cm_arith_for\n"); + print_cmd_on_noexec(cmd->value.ArithFor->action); + //TODO: implement further members? + break; +#endif + case cm_case: + //TODO: handle this how?! + printf("cm_case: %s\n", cmd->value.Case->word->word); + //TODO: print pattern_list? + break; +#if defined (COND_COMMAND) + case cm_cond: + //TODO: implement? + printf("cm_cond\n"); + break; +#endif + case cm_connection: + printf("cm_connection\n"); + print_cmd_on_noexec(cmd->value.Connection->first); + print_cmd_on_noexec(cmd->value.Connection->second); + break; + case cm_coproc: + printf("cm_coproc\n"); + print_cmd_on_noexec(cmd->value.Coproc->command); + break; + case cm_for: + printf("cm_for\n"); + print_cmd_on_noexec(cmd->value.For->action); + break; + case cm_function_def: + printf("cm_function_def: %s\n", cmd->value.Function_def->name->word); + print_cmd_on_noexec(cmd->value.Function_def->command); + break; + case cm_group: + printf("cm_group\n"); + print_cmd_on_noexec(cmd->value.Group->command); + break; + case cm_if: + printf("cm_if\n"); + print_cmd_on_noexec(cmd->value.If->test); + print_cmd_on_noexec(cmd->value.If->true_case); + print_cmd_on_noexec(cmd->value.If->false_case); + break; +#if defined (SELECT_COMMAND) + case cm_select: + printf("cm_select\n"); + print_cmd_on_noexec(cmd->value.Select->action); + break; +#endif + case cm_simple: + wl = cmd->value.Simple->words; + if (wl->word->flags && W_ASSIGNMENT) { + printf("cm_simple assignment: %s\n", wl->word->word); + } else { + for (i=0; specials[i]; i++) { + if (strcmp(wl->word->word, specials[i]) == 0) { + printf("cm_simple %s: %s\n", specials[i], wl->word->word); + return; + } + } + if (strcmp(wl->word->word, builtins[i]) == 0) { + //FIXME: filter bash internals or at least represent them differently + printf("cm_simple internal: %s\n", wl->word->word); + } else { + printf("cm_simple exec: %s\n", wl->word->word); + } + } + break; + case cm_subshell: + printf("cm_subshell\n"); + print_cmd_on_noexec(cmd->value.Subshell->command); + break; + case cm_until: + //TODO: implement? + printf("cm_until\n"); + break; + case cm_while: + printf("cm_while\n"); + print_cmd_on_noexec(cmd->value.While->test); + print_cmd_on_noexec(cmd->value.While->action); + break; + } + } +} diff --git a/bash/print_cmd_on_noexec.h b/bash/print_cmd_on_noexec.h new file mode 100755 index 0000000..cce77a3 --- /dev/null +++ b/bash/print_cmd_on_noexec.h @@ -0,0 +1,30 @@ +#ifndef _INSPECT_CMD_H +#define _INSPECT_CMD_H + + + + +#include +/* order matters :-( */ +#include "config.h" +#include "bashtypes.h" +#include "command.h" +#include "general.h" + + + + +/* provide some luxury for malloc */ +#define REALLOC(ptr, size) if(((ptr) = realloc(ptr, size)) == NULL) { perror("error: realloc() "); } +#define MIN(a, b) (a < b) ? a : b + + + + +/* prototypes */ +void inspect_cmd(COMMAND *cmd); + + + + +#endif diff --git a/bash/testsh b/bash/testsh new file mode 100755 index 0000000..07f26ca --- /dev/null +++ b/bash/testsh @@ -0,0 +1,16 @@ +#!/bin/bash +a() { + b +} +while c +do + d +done < <(e) +echo $(f) +$(g) +`h` + +case i in +j) k ;; +$(echo l)) m;; +esac -- 1.7.2.5