Advertisement
Guest User

skeleton code

a guest
Sep 8th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <readline/readline.h>
  4. #include <readline/history.h>
  5. #include "parse.h"
  6.  
  7. /*
  8. * Function declarations
  9. */
  10.  
  11. void PrintCommand(int, Command *);
  12. void PrintPgm(Pgm *);
  13. void stripwhite(char *);
  14.  
  15. /* When non-zero, this global means the user is done using this program. */
  16. int done = 0;
  17.  
  18. /*
  19. * Name: main
  20. *
  21. * Description: Gets the ball rolling...
  22. *
  23. */
  24. int main(void)
  25. {
  26. Command cmd;
  27. int n;
  28.  
  29. while (!done) {
  30.  
  31. char *line;
  32. line = readline("> ");
  33.  
  34. if (!line) {
  35. /* Encountered EOF at top level */
  36. done = 1;
  37. }
  38. else {
  39. /*
  40. * Remove leading and trailing whitespace from the line
  41. * Then, if there is anything left, add it to the history list
  42. * and execute it.
  43. */
  44. stripwhite(line);
  45.  
  46. if(*line) {
  47. add_history(line);
  48. /* execute it */
  49. n = parse(line, &cmd);
  50. PrintCommand(n, &cmd);
  51. }
  52. }
  53.  
  54. if(line) {
  55. free(line);
  56. }
  57. }
  58. return 0;
  59. }
  60.  
  61. /*
  62. * Name: PrintCommand
  63. *
  64. * Description: Prints a Command structure as returned by parse on stdout.
  65. *
  66. */
  67. void
  68. PrintCommand (int n, Command *cmd)
  69. {
  70. printf("Parse returned %d:\n", n);
  71. printf(" stdin : %s\n", cmd->rstdin ? cmd->rstdin : "<none>" );
  72. printf(" stdout: %s\n", cmd->rstdout ? cmd->rstdout : "<none>" );
  73. printf(" bg : %s\n", cmd->bakground ? "yes" : "no");
  74. PrintPgm(cmd->pgm);
  75. }
  76.  
  77. /*
  78. * Name: PrintPgm
  79. *
  80. * Description: Prints a list of Pgm:s
  81. *
  82. */
  83. void
  84. PrintPgm (Pgm *p)
  85. {
  86. if (p == NULL) {
  87. return;
  88. }
  89. else {
  90. char **pl = p->pgmlist;
  91.  
  92. /* The list is in reversed order so print
  93. * it reversed to get right
  94. */
  95. PrintPgm(p->next);
  96. printf(" [");
  97. while (*pl) {
  98. printf("%s ", *pl++);
  99. }
  100. printf("]\n");
  101. }
  102. }
  103.  
  104. /*
  105. * Name: stripwhite
  106. *
  107. * Description: Strip whitespace from the start and end of STRING.
  108. */
  109. void
  110. stripwhite (char *string)
  111. {
  112. register int i = 0;
  113.  
  114. while (isspace( string[i] )) {
  115. i++;
  116. }
  117.  
  118. if (i) {
  119. strcpy (string, string + i);
  120. }
  121.  
  122. i = strlen( string ) - 1;
  123. while (i> 0 && isspace (string[i])) {
  124. i--;
  125. }
  126.  
  127. string [++i] = '\0';
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement