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.

128 rivejä
2.0 KiB

3 vuotta sitten
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "fun.h"
  5. void eval(char *cmdline)
  6. {
  7. char *argv[MAXLINE];
  8. char buf[MAXLINE];
  9. int isBg;
  10. int pid;
  11. strcpy(buf,cmdline);
  12. isBg = parseline(cmdline,argv);
  13. if(argv[0] == NULL)
  14. {
  15. return;
  16. }
  17. if(!builtin_cmd(argv))
  18. {
  19. /*
  20. if((pid=fork())==0)
  21. {
  22. setpgid(0,0);
  23. if(execvp(argv[0],argv)<0)
  24. {
  25. printf("%s:Command not found.\n",argv[0]);
  26. exit(0);
  27. }
  28. }
  29. if(!isBg)
  30. {
  31. waitfg(pid);
  32. }
  33. else
  34. {
  35. printf("[%d] (%d) %s",pid2jid(pid),pid,cmdline);
  36. }
  37. */
  38. }
  39. return;
  40. }
  41. int parseline(const char *cmdline, char **argv)
  42. {
  43. static char array[MAXLINE];
  44. char *buf = array;
  45. char *delim;
  46. int argc;
  47. int bg;
  48. strcpy(buf, cmdline);
  49. buf[strlen(buf)-1] = ' ';
  50. while (*buf && (*buf == ' '))
  51. buf++;
  52. argc = 0;
  53. if (*buf == '\'')
  54. {
  55. buf++;
  56. delim = strchr(buf, '\'');
  57. }
  58. else
  59. {
  60. delim = strchr(buf, ' ');
  61. }
  62. while (delim)
  63. {
  64. argv[argc++] = buf;
  65. *delim = '\0';
  66. buf = delim + 1;
  67. while (*buf && (*buf == ' '))
  68. {
  69. buf++;
  70. }
  71. if (*buf == '\'')
  72. {
  73. buf++;
  74. delim = strchr(buf, '\'');
  75. }
  76. else
  77. {
  78. delim = strchr(buf, ' ');
  79. }
  80. }
  81. argv[argc] = NULL;
  82. if (argc == 0)
  83. return 1;
  84. if ((bg = (*argv[argc-1] == '&')) != 0)
  85. {
  86. argv[--argc] = NULL;
  87. }
  88. return bg;
  89. }
  90. int builtin_cmd(char **argv)
  91. {
  92. if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit"))
  93. {
  94. exit(0);
  95. }
  96. if(!strcmp(argv[0],"history"))
  97. {
  98. print_his(argv[1]);
  99. return 1;
  100. }
  101. if(!strcmp(argv[0],"jobs"))
  102. {
  103. return 1;
  104. }
  105. if(!strcmp(argv[0],"bg")||!strcmp(argv[0],"fg"))
  106. {
  107. return 1;
  108. }
  109. if(!strcmp(argv[0], "&"))
  110. {
  111. return 1;
  112. }
  113. return 0;
  114. }