Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import parse
  2. import sys
  3. import os
  4.  
  5. EXIT_SUCCESS = 0
  6. EXIT_FAILURE = 1
  7.  
  8. # Internal change-directory command.
  9. def shell_cd (dir):
  10. os.chdir(dir)
  11.  
  12. # Internal exit/quit command.
  13. def shell_exit(code):
  14. sys.exit(code)
  15.  
  16. # Execute simple command
  17. def run_simple_command (simple_commands_list, father_command):
  18. cmd = simple_commands_list[0].word
  19. if cmd == 'exit' or cmd == 'quit':
  20. shell_exit(EXIT_SUCCESS)
  21. args = []
  22. for i in range(0, len(simple_commands_list)):
  23. args.append(simple_commands_list[i].word)
  24. if cmd == 'cd':
  25. shell_cd(args[1])
  26. return EXIT_SUCCESS
  27. try:
  28. pid = os.fork()
  29. if pid == -1:
  30. # error forking
  31. return EXIT_FAILURE
  32. elif pid == 0:
  33. # child process
  34. st = os.execvp("/bin/" + cmd, args)
  35. #st = os.execvp(command, args)
  36. # only if exec failed
  37. # os._exit(st)
  38. else:
  39. os.waitpid(pid, 0)
  40. #only parent process gets here
  41. # status = os.waitpid(pid, 0)
  42. # if os.WIFEXITED(status):
  43. # print("Child {} terminated normally, with code {}".format(pid, os.WEXITSTATUS(status)))
  44. # return status
  45. return EXIT_SUCCESS
  46. except Exception as e:
  47. print("Error: {}".format (e))
  48.  
  49.  
  50.  
  51. # TODO - extract command and arguments; ATTENTION! not all commands are simple_commands
  52.  
  53. # TODO - extract redirects from father_command ???
  54.  
  55. # TODO - fork parent process
  56. # TODO - execute child
  57. # TODO - wait for child
  58. # TODO - return exit status
  59.  
  60. # Process two commands in parallel, by creating two children.
  61. def do_in_parallel (cmd1, cmd2, father_command):
  62. # TODO - execute cmd1 and cmd2 simultaneously
  63. pass
  64.  
  65. def do_on_pipe (cmd1, cmd2, father_command):
  66. # TODO - redirect the output of cmd1 to the input of cmd2
  67. pass
  68.  
  69. #Parse and execute a command.
  70. def parse_command (command):
  71. if command.op == parse.OP_NONE:
  72. # TODO - run a simple command
  73. return run_simple_command (command.commands, command)
  74. elif command.op == parse.OP_SEQUENTIAL:
  75. # TODO - execute the commands one after the other
  76. pass
  77. elif command.op == parse.OP_PARALLEL:
  78. # TODO - execute the commands simultaneously
  79. pass
  80. elif command.op == parse.OP_CONDITIONAL_NZERO:
  81. # TODO - execute the second command only if the first one returns non zero
  82. pass
  83. elif command.op == parse.OP_CONDITIONAL_ZERO:
  84. # TODO - execute the second command only if the first one returns zero
  85. pass
  86. elif command.op == parse.OP_PIPE:
  87. # TODO - redirect the output of the first command to the input of the second
  88. return 0 # TODO - replace with actual exit code of command
  89.  
  90. fileName = sys.argv[1]
  91. fileHandle = open(fileName, "r")
  92. for line in fileHandle:
  93. command = parse.parse(line)
  94. #parse.dump(command) # TODO - delete the line before submitting the homework
  95. parse_command(command)
  96. fileHandle.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement