Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. /* mshell - a job manager */
  2.  
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11.  
  12. #include "jobs.h"
  13.  
  14. #define BOLD "\033[00;01m"
  15. #define NORM "\033[00;00m"
  16.  
  17. void do_help()
  18. {
  19. printf("available commands are:\n");
  20. printf(" exit - cause the shell to exit\n");
  21. printf(BOLD "\t exit\n" NORM);
  22. printf(" jobs - display status of jobs in the current session\n");
  23. printf(BOLD "\t jobs\n" NORM);
  24. printf
  25. (" fg - run a job identified by its pid or job id in the foreground\n");
  26. printf(BOLD "\t fg " NORM "pid" BOLD "|" NORM "jobid \n");
  27. printf
  28. (" bg - run a job identified by its pid or job id in the background\n");
  29. printf(BOLD "\t bg " NORM "pid" BOLD "|" NORM "jobid \n");
  30. printf(" stop - stop a job identified by its pid or job id\n");
  31. printf(BOLD "\t stop " NORM "pid" BOLD "|" NORM "jobid \n");
  32. printf(" kill - kill a job identified by its pid or job id\n");
  33. printf(BOLD "\t kill " NORM "pid" BOLD "|" NORM "jobid \n");
  34. printf(" help - print this message\n");
  35. printf(BOLD "\t help\n" NORM);
  36. printf("\n");
  37. printf
  38. ("ctrl-z and ctrl-c can be used to send a SIGTSTP and a SIGINT, respectively\n\n");
  39. }
  40.  
  41. /* treat_argv - Determine pid or jobid and return the associated job structure */
  42. struct job_t *treat_argv(char **argv)
  43. {
  44. struct job_t *jobp = NULL;
  45.  
  46. /* Ignore command if no argument */
  47. if (argv[1] == NULL) {
  48. printf("%s command requires PID or %%jobid argument\n",
  49. argv[0]);
  50. return NULL;
  51. }
  52.  
  53. /* Parse the required PID or %JID arg */
  54. if (isdigit((int)argv[1][0])) {
  55. pid_t pid = atoi(argv[1]);
  56. if (!(jobp = jobs_getjobpid(pid))) {
  57. printf("(%d): No such process\n", (int)pid);
  58. return NULL;
  59. }
  60. } else if (argv[1][0] == '%') {
  61. int jid = atoi(&argv[1][1]);
  62. if (!(jobp = jobs_getjobjid(jid))) {
  63. printf("%s: No such job\n", argv[1]);
  64. return NULL;
  65. }
  66. } else {
  67. printf("%s: argument must be a PID or %%jobid\n", argv[0]);
  68. return NULL;
  69. }
  70.  
  71. return jobp;
  72. }
  73.  
  74. /* do_bg - Execute the builtin bg command */
  75. void do_bg(char **argv)
  76. {
  77. struct job_t *job;
  78.  
  79. if (verbose) {
  80. printf("do_bg: entering");
  81. }
  82.  
  83. job = treat_argv(argv);
  84.  
  85. if (job == NULL) {
  86. unix_error("do_bg: No such job");
  87. }
  88.  
  89. if (kill(job->jb_pid, SIGCONT) == -1) {
  90. unix_error("do_bg: kill");
  91. }
  92.  
  93. job->jb_state = BG;
  94.  
  95. if (verbose) {
  96. printf("do_bg: exiting");
  97. }
  98.  
  99. return;
  100. }
  101.  
  102. /* waitfg - Block until process pid is no longer the foreground process */
  103. void waitfg(pid_t pid)
  104. {
  105. sigset_t set, oset;
  106.  
  107. if (verbose) {
  108. printf("waitfg: entering\n");
  109. }
  110.  
  111. sigemptyset(&set);
  112. sigaddset(&set, SIGCHLD);
  113. sigprocmask(SIG_BLOCK, &set, &oset);
  114.  
  115. while (pid == jobs_fgpid()) {
  116. sigsuspend(&oset);
  117. }
  118.  
  119. sigprocmask(SIG_SETMASK, &oset, NULL);
  120.  
  121. if (verbose) {
  122. printf("waitfg: exting\n");
  123. }
  124.  
  125. return;
  126. }
  127.  
  128. /* do_fg - Execute the builtin fg command */
  129. void do_fg(char **argv)
  130. {
  131. struct job_t *job;
  132.  
  133. if (verbose) {
  134. printf("do_fg: entering");
  135. }
  136.  
  137. job = treat_argv(argv);
  138.  
  139. if (job == NULL) {
  140. unix_error("do_fg: No such job");
  141. }
  142.  
  143. if (kill(job->jb_pid, SIGCONT) == -1) {
  144. unix_error("do_fg: kill");
  145. }
  146.  
  147. job->jb_state = FG;
  148.  
  149. waitfg(job->jb_pid);
  150.  
  151. if (verbose) {
  152. printf("do_fg: exiting");
  153. }
  154.  
  155. return;
  156. }
  157.  
  158. /* do_stop - Execute the builtin stop command */
  159. void do_stop(char **argv)
  160. {
  161. struct job_t *job;
  162.  
  163. if (verbose) {
  164. printf("do_stop: entering\n");
  165. }
  166.  
  167. job = treat_argv(argv);
  168.  
  169. if (job == NULL) {
  170. unix_error("do_stop: No such job");
  171. }
  172.  
  173. if (kill(job->jb_pid, SIGSTOP) == -1) {
  174. unix_error("do_stop: kill");
  175. }
  176.  
  177. job->jb_state = ST;
  178.  
  179. if (verbose) {
  180. printf("do_stop: exiting\n");
  181. }
  182.  
  183. return;
  184. }
  185.  
  186. /* do_kill - Execute the builtin kill command */
  187. void do_kill(char **argv)
  188. {
  189. struct job_t *job;
  190.  
  191. if (verbose) {
  192. printf("do_kill: entering\n");
  193. }
  194.  
  195. job = treat_argv(argv);
  196.  
  197. if (job == NULL) {
  198. unix_error("do_kill: No such job\n");
  199. }
  200.  
  201. if (kill(job->jb_pid, SIGKILL) == -1) {
  202. unix_error("do_kill: kill");
  203. }
  204.  
  205. if (verbose) {
  206. printf("do_kill: exiting\n");
  207. }
  208.  
  209. return;
  210. }
  211.  
  212. /* do_exit - Execute the builtin exit command */
  213. void do_exit()
  214. {
  215. if (verbose) {
  216. printf("do_exit: entering\n");
  217. }
  218.  
  219. if (jobs_getstoppedjob() == 0) {
  220. /* kill mshell */
  221. if (kill(getpid(), SIGKILL) == -1) {
  222. unix_error("do_exit: kill");
  223. }
  224. } else {
  225. printf("There are stopped jobs.\n");
  226. }
  227.  
  228. if (verbose) {
  229. printf("do_exit: exiting\n");
  230. }
  231.  
  232. return;
  233. }
  234.  
  235. /* do_jobs - Execute the builtin fg command */
  236. void do_jobs()
  237. {
  238. if (verbose) {
  239. printf("do_jobs: entering\n");
  240. }
  241.  
  242. jobs_listjobs();
  243.  
  244. if (verbose) {
  245. printf("do_jobs: exiting\n");
  246. }
  247.  
  248. return;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement