def handle_exit_status(args): global terminate, in_pipes if terminate and args: return # path expansions exit_value, args = path_expansions(args) if exit_value: os.environ['?'] = str(exit_value) Shell.printf(args) terminate = True return '' # globbing args = multi_glob(args) # run pipes and redirections if '|' in args: run_redirections(args, in_pipes) output, exit_value = run_pipes(args) else: # redirections inp, out, args, exit_value = run_redirections(args, in_pipes) handle_quotes(args) command = args.pop(0) exit_value, output = run_command(command, args, inp=inp, out=out) in_pipes = False # exit status os.environ['?'] = str(exit_value) return output
def run_utility(command, args, inp=PIPE, out=PIPE): paths = os.environ['PATH'].split(':') for path in paths: realpath = path + '/' + command if os.path.exists(realpath): return run_execution(realpath, args, inp=inp, out=out) Shell.printf('intek-sh: %s: command not found' % command) return 127, ''
def builtins_unset(variables=[]): # implement unset exit_value = 0 for variable in variables: if not check_name(variable): exit_value = 1 Shell.printf('intek-sh: unset: `%s\': not a valid identifier\n' % variable) elif variable in os.environ: os.environ.pop(variable) return exit_value, ''
def handle_signal(sig, frame): global process, terminate Shell.printf("^C") terminate = False if process: try: os.kill(process.pid, sig) terminate = True except ProcessLookupError: pass os.environ['?'] = '130'
def builtins_exit(exit_code='0'): # implement exit Shell.save_history() Shell.printf('exit') exit_value = 0 if exit_code: if exit_code.isdigit(): exit_value = int(exit_code) else: Shell.printf('intek-sh: exit: ' + exit_code) curses.endwin() exit_program(exit_value)
def run_command(command, args=[], inp=PIPE, out=PIPE): global com_sub built_ins = ('cd', 'printenv', 'export', 'unset', 'exit', 'history', 'clear') if command in built_ins: exit_code, output = run_builtins(command, args) if not com_sub and output and not exit_code and not in_pipes: Shell.printf(output) return exit_code, output elif '/' in command: return run_execution(command, args, inp=inp, out=out) elif 'PATH' in os.environ: return run_utility(command, args, inp=inp, out=out) Shell.printf('intek-sh: %s: command not found' % command) return 127, ''
def main(): setup_terminal() while True: try: input_user = process_input() if check_syntax_shell(input_user): handle_logic_op(input_user) except IndexError: pass except EOFError: break except TypeError: # when terminate is True pass except ValueError: # outrange chr() pass except Exception as e: Shell.printf(str(e)) reset_terminal() builtins_exit()
def builtins_export(variables=[]): # implement export exit_value = 0 output = [] if variables: for variable in variables: if '=' in variable: name, value = variable.split('=', 1) else: # if variable stands alone, set its value as '' name = variable value = '' if check_name(name): os.environ[name] = value else: exit_value = 1 Shell.printf('intek-sh: export: `%s\': ' 'not a valid identifier\n' % variable) else: env = builtins_printenv()[1].split('\n') result = [] for line in env: result.append('declare -x ' + line.replace('=', '=\"', 1) + '\"') output = '\n'.join(result) return exit_value, '\n'.join(output)
def show_error(error): if error: Shell.printf(error)
def run_execution(command, args, inp=PIPE, out=PIPE): global process output = [] try: process = Popen([command] + args, stdin=inp, stdout=out, stderr=PIPE) if out == PIPE and inp == PIPE: line = process.stdout.readline().decode() while line: if not com_sub and not in_pipes: Shell.printf(line.strip()) output.append(line) line = process.stdout.readline().decode() elif out == PIPE: content = process.stdout.read().decode() if not com_sub and not in_pipes: Shell.printf(content) output.append(content) for line in process.stderr: if line.decode() not in ['', '\n']: Shell.printf(line.decode()) process.wait() exit_value = process.returncode except PermissionError: exit_value = 126 Shell.printf('intek-sh: %s: Permission denied' % command) except FileNotFoundError: exit_value = 127 Shell.printf('intek-sh: %s: command not found' % command) except Exception as e: exit_value = 1 Shell.printf(str(e)) return exit_value, ''.join(output)