Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <readline/readline.h>
- #include <readline/history.h>
- #include "parse.h"
- /*
- * Function declarations
- */
- void PrintCommand(int, Command *);
- void PrintPgm(Pgm *);
- void stripwhite(char *);
- /* When non-zero, this global means the user is done using this program. */
- int done = 0;
- /*
- * Name: main
- *
- * Description: Gets the ball rolling...
- *
- */
- int main(void)
- {
- Command cmd;
- int n;
- while (!done) {
- char *line;
- line = readline("> ");
- if (!line) {
- /* Encountered EOF at top level */
- done = 1;
- }
- else {
- /*
- * Remove leading and trailing whitespace from the line
- * Then, if there is anything left, add it to the history list
- * and execute it.
- */
- stripwhite(line);
- if(*line) {
- add_history(line);
- /* execute it */
- n = parse(line, &cmd);
- PrintCommand(n, &cmd);
- }
- }
- if(line) {
- free(line);
- }
- }
- return 0;
- }
- /*
- * Name: PrintCommand
- *
- * Description: Prints a Command structure as returned by parse on stdout.
- *
- */
- void
- PrintCommand (int n, Command *cmd)
- {
- printf("Parse returned %d:\n", n);
- printf(" stdin : %s\n", cmd->rstdin ? cmd->rstdin : "<none>" );
- printf(" stdout: %s\n", cmd->rstdout ? cmd->rstdout : "<none>" );
- printf(" bg : %s\n", cmd->bakground ? "yes" : "no");
- PrintPgm(cmd->pgm);
- }
- /*
- * Name: PrintPgm
- *
- * Description: Prints a list of Pgm:s
- *
- */
- void
- PrintPgm (Pgm *p)
- {
- if (p == NULL) {
- return;
- }
- else {
- char **pl = p->pgmlist;
- /* The list is in reversed order so print
- * it reversed to get right
- */
- PrintPgm(p->next);
- printf(" [");
- while (*pl) {
- printf("%s ", *pl++);
- }
- printf("]\n");
- }
- }
- /*
- * Name: stripwhite
- *
- * Description: Strip whitespace from the start and end of STRING.
- */
- void
- stripwhite (char *string)
- {
- register int i = 0;
- while (isspace( string[i] )) {
- i++;
- }
- if (i) {
- strcpy (string, string + i);
- }
- i = strlen( string ) - 1;
- while (i> 0 && isspace (string[i])) {
- i--;
- }
- string [++i] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement