OS2021_Project1.Shell
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 line
3.4 KiB

3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
  1. #pragma once
  2. #define CMDLINE_MAX_SIZE 1024 /* max length of a single command line */
  3. #define ARGS_MAX_QUANTITY 128 /* max args on a command line */
  4. #define BUFFER_MAX_SIZE 64 /* max size of a buffer which contains parsed arguments */
  5. #define CMDLINE_DIV ' \t\r\n\a'
  6. #define CMDLINE_HISTORY_MAX_QUANTITY 256
  7. #define JOBS_MAX_QUANTITY 16
  8. #define PATH_MAX_SIZE 256
  9. #define LS_BUF_SIZE 1024
  10. #define UNDF 0 /* undefined */
  11. #define FG 1 /* running in foreground */
  12. #define BG 2 /* running in background */
  13. #define ST 3 /* stopped */
  14. /* job_t - The job struct */
  15. struct job_t
  16. {
  17. pid_t pid; /* job PID */
  18. int jid; /* job ID [1, 2, ...] */
  19. int state; /* UNDEF, BG, FG, or ST */
  20. char cmdline[CMDLINE_MAX_SIZE]; /* command line */
  21. };
  22. /* readline - Get the command line */
  23. char *readline();
  24. /* parseline - Evaluate the command line that the user has just typed in */
  25. int parseline(char *cmdline, char **args);
  26. /* execute - Execute the command line */
  27. int execute(char *cmdline, char **args);
  28. /* builtin functions - Handle built-in command */
  29. int built_in(char **args);
  30. int builtin_cd(char **args);
  31. int builtin_history(char **args);
  32. int builtin_jobs(char **args);
  33. int builtin_mytop(char **args);
  34. /* do_bgfg - Execute background/foregroung tasks */
  35. int do_bgfg(char **args);
  36. /* waitfg - Wait foreground jobs to finish */
  37. void waitfg(pid_t pid);
  38. /* initjobs - Initialize the job list */
  39. void initjobs(struct job_t *jobs);
  40. /* addjob - Add jobs to joblist */
  41. int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline);
  42. /* listjobs - Print the job list */
  43. void listjobs(struct job_t *jobs);
  44. /* deletejob - Delete a job whose PID=pid from the job list */
  45. int deletejob(struct job_t *jobs, pid_t pid);
  46. /* clearjob - Clear the entries in a job struct */
  47. void clearjob(struct job_t *job);
  48. /* pide2jid - Map process ID to job ID*/
  49. int pid2jid(pid_t pid);
  50. /* fgpid - Return PID of current foreground job, 0 if no such job */
  51. pid_t fgpid(struct job_t *jobs);
  52. /* getjobpid - Find a job (by PID) on the job list */
  53. struct job_t *getjobpid(struct job_t *jobs, pid_t pid);
  54. /* getjobjid - Find a job (by JID) on the job list */
  55. struct job_t *getjobjid(struct job_t *jobs, int jid);
  56. /* Signal - wrapper for the sigaction function */
  57. typedef void handler_t(int);
  58. handler_t *Signal(int signum, handler_t *handler);
  59. /*
  60. * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
  61. * a child job terminates (becomes a zombie), or stops because it
  62. * received a SIGSTOP or SIGTSTP signal. The handler reaps all
  63. * available zombie children, but doesn't wait for any other
  64. * currently running children to terminate.
  65. */
  66. void sigchld_handler(int signal);
  67. /*
  68. * sigint_handler - The kernel sends a SIGINT to the shell whenver the
  69. * user types ctrl-c at the keyboard. Catch it and send it along
  70. * to the foreground job.
  71. */
  72. void sigint_handler(int signal);
  73. /*
  74. * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever
  75. * the user types ctrl-z at the keyboard. Catch it and suspend the
  76. * foreground job by sending it a SIGTSTP.
  77. */
  78. void sigtstp_handler(int signal);
  79. /*
  80. * sigquit_handler - The driver program can gracefully terminate the
  81. * child shell by sending it a SIGQUIT signal.
  82. */
  83. void sigquit_handler(int signal);