OS2021_Project1.Shell
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

121 рядки
3.9 KiB

3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
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 REDIRECT_FILENAME_MAX_SIZE 64 /* redirection filename */
  11. #define REDIRECT_ARG_MAX_SIZE 16 /* redirection argument */
  12. #define UNDF 0 /* undefined */
  13. #define FG 1 /* running in foreground */
  14. #define BG 2 /* running in background */
  15. #define ST 3 /* stopped */
  16. #define REDIRECT_NO 0 /* no redirection */
  17. #define REDIRECT_OUT 1 /* redirect output */
  18. #define REDIRECT_IN 2 /* redirect input */
  19. /* job_t - The job struct */
  20. struct job_t
  21. {
  22. pid_t pid; /* job PID */
  23. int jid; /* job ID [1, 2, ...] */
  24. int state; /* UNDEF, BG, FG, or ST */
  25. char cmdline[CMDLINE_MAX_SIZE]; /* command line */
  26. };
  27. /* readline - Get the command line */
  28. char *readline();
  29. /* parseline - Evaluate the command line that the user has just typed in */
  30. int parseline(char *cmdline, char **args);
  31. /* check_redirect - check if the command contains redirection */
  32. int check_redirect(char **args, char *redirect_filename, char **redirect_args);
  33. /* check_redirect - check if the command contains pipe */
  34. int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2);
  35. /* execute - Execute the command line */
  36. int execute(char *cmdline, char **args);
  37. /* builtin functions - Handle built-in command */
  38. int built_in(char **args);
  39. int builtin_cd(char **args);
  40. int builtin_history(char **args);
  41. int builtin_jobs(char **args);
  42. int builtin_mytop(char **args);
  43. /* do_bgfg - Execute background/foregroung tasks */
  44. int do_bgfg(char **args);
  45. /* waitfg - Wait foreground jobs to finish */
  46. void waitfg(pid_t pid);
  47. /* initjobs - Initialize the job list */
  48. void initjobs(struct job_t *jobs);
  49. /* addjob - Add jobs to joblist */
  50. int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline);
  51. /* listjobs - Print the job list */
  52. void listjobs(struct job_t *jobs);
  53. /* deletejob - Delete a job whose PID=pid from the job list */
  54. int deletejob(struct job_t *jobs, pid_t pid);
  55. /* clearjob - Clear the entries in a job struct */
  56. void clearjob(struct job_t *job);
  57. /* pide2jid - Map process ID to job ID*/
  58. int pid2jid(pid_t pid);
  59. /* fgpid - Return PID of current foreground job, 0 if no such job */
  60. pid_t fgpid(struct job_t *jobs);
  61. /* getjobpid - Find a job (by PID) on the job list */
  62. struct job_t *getjobpid(struct job_t *jobs, pid_t pid);
  63. /* getjobjid - Find a job (by JID) on the job list */
  64. struct job_t *getjobjid(struct job_t *jobs, int jid);
  65. /* maxjid - Returns largest allocated job ID */
  66. int maxjid(struct job_t *jobs);
  67. /* Signal - wrapper for the sigaction function */
  68. typedef void handler_t(int);
  69. handler_t *Signal(int signum, handler_t *handler);
  70. /*
  71. * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
  72. * a child job terminates (becomes a zombie), or stops because it
  73. * received a SIGSTOP or SIGTSTP signal. The handler reaps all
  74. * available zombie children, but doesn't wait for any other
  75. * currently running children to terminate.
  76. */
  77. void sigchld_handler(int signal);
  78. /*
  79. * sigint_handler - The kernel sends a SIGINT to the shell whenver the
  80. * user types ctrl-c at the keyboard. Catch it and send it along
  81. * to the foreground job.
  82. */
  83. void sigint_handler(int signal);
  84. /*
  85. * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever
  86. * the user types ctrl-z at the keyboard. Catch it and suspend the
  87. * foreground job by sending it a SIGTSTP.
  88. */
  89. void sigtstp_handler(int signal);
  90. /*
  91. * sigquit_handler - The driver program can gracefully terminate the
  92. * child shell by sending it a SIGQUIT signal.
  93. */
  94. void sigquit_handler(int signal);