Command_T Command_new(char *pcName, DynArray_T oArgs, 
                      char *pcStdIn, char *pcStdOut) {
    int i;
    int argsLength; /* length of oArgs array */
    char* pcArg;
    size_t pcArgLength; /* track how much memory to give pcArg */
    Command_T oCommand;

    /* a command must have a name */
    assert(pcName != NULL);
    assert(oArgs != NULL);

    oCommand = (Command_T) malloc(sizeof(struct Command));
    if (oCommand == NULL) {
        fprintf(stderr, 
        "%s: Can't allocate memory for command\n", 
        pcPgmName); 
        exit(EXIT_FAILURE);
    }

    /* allocate memory for and set the command name */
    oCommand->pcName = (char*)malloc(strlen(pcName) + 1);
    if (oCommand->pcName == NULL) {
        fprintf(stderr, 
        "%s: Can't allocate memory for command\n", 
        pcPgmName); 
        exit(EXIT_FAILURE);
    }
    strcpy(oCommand->pcName, pcName);
    
    /* allocate memory for and set the command arguments*/
    argsLength = DynArray_getLength(oArgs);
    oCommand->oArgs = DynArray_new(0);
    for (i = 0; i < argsLength; i++) {
        pcArgLength = strlen((char*) DynArray_get(oArgs, i));
        pcArg = (char*) malloc(pcArgLength + 1);
        strcpy(pcArg, DynArray_get(oArgs, i));
        if (pcArg == NULL) {
            fprintf(stderr, 
            "%s: Can't allocate memory for command args\n", 
            pcPgmName); 
            exit(EXIT_FAILURE);
        }
        DynArray_add(oCommand->oArgs, pcArg);
    }

    /* allocate memory for and set the command stdin redirect */
    if (pcStdIn == NULL) {
        oCommand->pcStdIn = NULL;
    }
    else {
        oCommand->pcStdIn = (char*)malloc(strlen(pcStdIn) + 1);
        if (oCommand->pcStdIn == NULL) {
            fprintf(stderr, 
            "%s: Can't allocate memory for command\n", 
            pcPgmName); 
            exit(EXIT_FAILURE);
        }
        strcpy(oCommand->pcStdIn, pcStdIn);
    }   

    /* allocate memory for and set the command stdout redirect*/
    if (pcStdOut == NULL) {
        oCommand->pcStdOut = NULL;
    }
    else {
        oCommand->pcStdOut = (char*)malloc(strlen(pcStdOut) + 1);
        if (oCommand->pcStdOut == NULL) {
            fprintf(stderr, 
            "%s: Can't allocate memory for command\n", 
            pcPgmName); 
            exit(EXIT_FAILURE);
        }
        strcpy(oCommand->pcStdOut, pcStdOut);
    }
    return oCommand;

}
int main(int argc, char *argv[])
{
   char *pcLine;
   DynArray_T oTokens;
   DynArray_T oArgs;
   int iArgLength;
   int i;
   Command_T oCommand;

   pcPgmName = argv[0];

   /* Write to stdout a prompt consisting of a percent sign 
      and a space. */
   fprintf(stdout, "%% ");

   /* Read a line (that is, an array of characters) from stdin. */
   while ((pcLine = Token_readLine(stdin)) != NULL)
   {
      /* Write that line (array of characters) to stdout, and 
         flush the stdout buffer. */
      fprintf(stdout, "%s\n", pcLine);
      fflush(stdout);

      /* Pass the line (array of characters) to your lexical 
         analyzer to create a DynArray object containing tokens. */
      oTokens = Lex_analyze(pcLine);

      oCommand = Syn_analyze(oTokens);
      if (oCommand != NULL) {
         if (Command_getName(oCommand) != NULL)
            fprintf(stdout, "Command name: %s\n", 
               Command_getName(oCommand));
         if (Command_getArgs(oCommand) != NULL) {
            oArgs = Command_getArgs(oCommand);
            iArgLength = DynArray_getLength(oArgs);
            for (i = 0; i < iArgLength; i++) {
               fprintf(stdout, "Command arg: %s\n",
                  (char *)DynArray_get(oArgs, i));
            }
         }
         if (Command_getStdIn(oCommand) != NULL) {
            fprintf(stdout, "Command stdin: %s\n", 
               Command_getStdIn(oCommand));
         }
         if (Command_getStdOut(oCommand) != NULL) {
            fprintf(stdout, "Command stdout: %s\n", 
               Command_getStdOut(oCommand));
         }
         Command_free(oCommand);
      }   

      if (oTokens != NULL) {
         Token_freeTokens(oTokens);
         DynArray_free(oTokens);
      }  
      
      fprintf(stdout, "%% ");
      free(pcLine);
   }
   fprintf(stdout, "\n");

   return 0;
}
Example #3
0
int main(int argc, char *argv[])

/* Read lines from the .ishrc file residing in the HOME directory
   until EOF is reached. Write each line that is read to
   stdout and execute each line. Read a line from stdin and
   execute it. Repeat until EOF. Return 0. */

{
   char acLine[MAX_LINE_SIZE];
   char *pcTemp;
   DynArray_T oHistoryList; 
   FILE *psFile;
   int i;
   void (*pfRet)(int);

   pfRet = signal(SIGINT, SIG_IGN);
   if(pfRet == SIG_ERR) {perror(argv[0]); exit(EXIT_FAILURE); }

   oHistoryList = DynArray_new(0);

   pcTemp = getIshrc();
   psFile = fopen(pcTemp, "r");
   free(pcTemp);

   while (psFile != NULL && 
          fgets(acLine, MAX_LINE_SIZE, psFile) != NULL)
   {
      /* Remove '\n' if acLine ends with '\n'. This is done so 
         the commands in the history list do not end with '\n',
         which is necessary to properly expand !commandprefix. */
      if(acLine[strlen(acLine)-1] == '\n')
         acLine[strlen(acLine)-1] = '\0';
     
      printf("%% %s\n", acLine);
      /* Explicitly flush the stdout buffer so we can test ish
         properly by redirecting the output to a file. */
      fflush(stdout);
      
      performCommand(acLine, oHistoryList, argv[0]);
   }
   printf("%% ");
   fflush(stdout);
   while (fgets(acLine, MAX_LINE_SIZE, stdin) != NULL)
   {
      /* Remove '\n' if acLine ends with '\n'. This is done so 
         the commands in the history list do not end with '\n',
         which is necessary to properly expand !commandprefix. */
      if(acLine[strlen(acLine)-1] == '\n')
         acLine[strlen(acLine)-1] = '\0';
      
      performCommand(acLine, oHistoryList, argv[0]);

      printf("%% ");
      fflush(stdout);
   }
   printf("\n");
   fflush(stdout);

   for(i = 0; i < DynArray_getLength(oHistoryList); i++)
      free(DynArray_get(oHistoryList, i));
   DynArray_free(oHistoryList);
   return 0;
}