예제 #1
0
int main(int argc, char *argv[])
{
   char *pcLine;
   DynArray_T oTokens;

   Lexer_init(argv[0]);

   printf("%% ");

   while ((pcLine = Lexer_readLine(stdin)) != NULL)
   {
      printf("%s\n", pcLine);
      fflush(stdout);
      oTokens = Lexer_lexLine(pcLine);
    
      if (oTokens != NULL)
      {

         Lexer_writeTokens(oTokens);
         Lexer_freeTokens(oTokens);
         DynArray_free(oTokens);
      }

      printf("%% ");/*------------------------------------\n");
     */ free(pcLine);
   }
   printf("\n");
   return 0;
}
예제 #2
0
int main(int argc, char *argv[])
{
   char *pcLine;
   DynArray_T oTokens;

   pcPgmName = argv[0];

   printf("%s \n", pcPgmName);
   printf("------------------------------------\n");

   while ((pcLine = readLine(stdin)) != NULL)
   {
      printf("Line: %s\n", pcLine);
      fflush(stdout);
      oTokens = lexLine(pcLine);
      if (oTokens != NULL)
      {
         writeTokens(oTokens);
         freeTokens(oTokens);
         DynArray_free(oTokens);
      }

      printf("------------------------------------\n");
      free(pcLine);
   }

   return 0;
}
예제 #3
0
void Command_free(Command_T oCommand) {
    int iLength;
    int i;
    char* pcArg;

    assert(oCommand != NULL);
    assert(oCommand->pcName != NULL);
    assert(oCommand->oArgs != NULL);

    /* free the command name */
    free(oCommand->pcName);

    /* free the stdin redirect if not null */
    if (oCommand->pcStdIn != NULL)
        free(oCommand->pcStdIn);

    /* free the stdout redirect if not null */
    if (oCommand->pcStdOut != NULL)
        free(oCommand->pcStdOut);

    iLength = DynArray_getLength(oCommand->oArgs);
    for (i = 0; i < iLength; i++)
    {
        pcArg = DynArray_get(oCommand->oArgs, i);
        free(pcArg);
    }
    DynArray_free(oCommand->oArgs);

}
예제 #4
0
파일: ish.c 프로젝트: amandashi/CodeSample
static void performCommand(char *acLine, DynArray_T oHistoryList, 
                           char *pcProgName)

/* Expand any !commandprefix in acLine. Insert acLine into 
   oHistoryList iff the expanding succeeds and acLine does not consist
   of entirely whitespace characters. Lexically and syntactically 
   analyze acLine. Execute acLine if no errors are found. It is a 
   checked runtime error for acLine, oHistory, or pcProgName to be
   NULL. */

{
   char *pcTemp;
   Command_T oCommand;
   DynArray_T oTokens;
   int iSuccessful;

   assert(acLine != NULL);
   assert(oHistoryList != NULL);
   assert(pcProgName != NULL);

   if(histHasCommandPrefix(acLine))
   {
      iSuccessful = histExpandLine(acLine, oHistoryList, pcProgName);
      if(iSuccessful)
         printf("%s\n", acLine);
      else
         return;
   }
   oTokens = DynArray_new(0);
   iSuccessful = lexLine(acLine, oTokens, pcProgName);
   if(DynArray_getLength(oTokens) > 0)
   {
      /* Allocate memory to store command in oHistoryList iff 
         command does not consist of entirely whitespace
         characters. */
      pcTemp = (char*)malloc(strlen(acLine) + 1);
      assert(pcTemp != NULL);
      strcpy(pcTemp, acLine);
      DynArray_add(oHistoryList, pcTemp);

      if(iSuccessful)
      {
         oCommand = Command_new();
         iSuccessful = parseToken(oTokens, oCommand, pcProgName);
         if(iSuccessful)
            execute(oCommand, oHistoryList, pcProgName);
         Command_free(oCommand, NULL);
      }
   }
   DynArray_map(oTokens, Token_free, NULL);
   DynArray_free(oTokens);
}
예제 #5
0
/**
 * test_DynArray_alloc
 * -----------------------------------------------------
 * Tests the functionality of DynArray_new() and
 * DynArray_free() functions.
 */
static int test_DynArray_alloc()
{
    printf("Testing DynArray_new(), DynArray_free(): ");
    int result = 1;
    DynArray_t* da = DynArray_new(10);
    
    result &= (da != NULL);
    result &= (da->length == 10);
    result &= (da->contents != NULL);

    DynArray_free(da);

    printf("%d\n", result);
    return result;
}
예제 #6
0
static DynArray_T lexLine(const char *pcLine)
{
   /* lexLine() uses a DFA approach.  It "reads" its characters from
      pcLine. The DFA has these three states: */
   enum LexState {STATE_START, STATE_IN_NUMBER, STATE_IN_WORD};

   /* The current state of the DFA. */
   enum LexState eState = STATE_START;

   /* An index into pcLine. */
   int iLineIndex = 0;

   /* Pointer to a buffer in which the characters comprising each
      token are accumulated. */
   char *pcBuffer;

   /* An index into the buffer. */
   int iBufferIndex = 0;

   char c;
   struct Token *psToken;
   DynArray_T oTokens;
   int iSuccessful;

   assert(pcLine != NULL);

   /* Create an empty token DynArray object. */
   oTokens = DynArray_new(0);
   if (oTokens == NULL)
      {perror(pcPgmName); exit(EXIT_FAILURE);}

   /* Allocate memory for a buffer that is large enough to store the
      largest token that might appear within pcLine. */
   pcBuffer = (char*)malloc(strlen(pcLine) + 1);
   if (pcBuffer == NULL)
      {perror(pcPgmName); exit(EXIT_FAILURE);}

   for (;;)
   {
      /* "Read" the next character from pcLine. */
      c = pcLine[iLineIndex++];

      switch (eState)
      {
         /* Handle the START state. */
         case STATE_START:
            if (c == '\0')
            {
               free(pcBuffer);
               return oTokens;
            }
            else if (isdigit(c))
            {
               pcBuffer[iBufferIndex++] = c;
               eState = STATE_IN_NUMBER;
            }
            else if (isalpha(c))
            {
               pcBuffer[iBufferIndex++] = c;
               eState = STATE_IN_WORD;
            }
            else if (isspace(c))
               eState = STATE_START;
            else
            {
               fprintf(stderr, "Invalid line\n");
               free(pcBuffer);
               freeTokens(oTokens);
               DynArray_free(oTokens);
               return NULL;
            }
            break;

         /* Handle the IN_NUMBER state. */
         case STATE_IN_NUMBER:
            if (c == '\0')
            {
               /* Create a NUMBER token. */
               pcBuffer[iBufferIndex] = '\0';
               psToken = newToken(TOKEN_NUMBER, pcBuffer);
               iSuccessful = DynArray_add(oTokens, psToken);
               if (! iSuccessful)
                  {perror(pcPgmName); exit(EXIT_FAILURE);}
               iBufferIndex = 0;
               free(pcBuffer);
               return oTokens;
            }
            else if (isdigit(c))
            {
               pcBuffer[iBufferIndex++] = c;
               eState = STATE_IN_NUMBER;
            }
            else if (isspace(c))
            {
               /* Create a NUMBER token. */
               pcBuffer[iBufferIndex] = '\0';
               psToken = newToken(TOKEN_NUMBER, pcBuffer);
               iSuccessful = DynArray_add(oTokens, psToken);
               if (! iSuccessful)
                  {perror(pcPgmName); exit(EXIT_FAILURE);}
               iBufferIndex = 0;
               eState = STATE_START;
            }
            else
            {
               fprintf(stderr, "Invalid line\n");
               free(pcBuffer);
               freeTokens(oTokens);
               DynArray_free(oTokens);
               return NULL;
            }
            break;

         /* Handle the IN_WORD state. */
         case STATE_IN_WORD:
            if (c == '\0')
            {
               /* Create a WORD token. */
               pcBuffer[iBufferIndex] = '\0';
               psToken = newToken(TOKEN_WORD, pcBuffer);
               iSuccessful = DynArray_add(oTokens, psToken);
               if (! iSuccessful)
                  {perror(pcPgmName); exit(EXIT_FAILURE);}
               iBufferIndex = 0;
               free(pcBuffer);
               return oTokens;
            }
            else if (isalpha(c))
            {
               pcBuffer[iBufferIndex++] = c;
               eState = STATE_IN_WORD;
            }
            else if (isspace(c))
            {
               /* Create a WORD token. */
               pcBuffer[iBufferIndex] = '\0';
               psToken = newToken(TOKEN_WORD, pcBuffer);
               iSuccessful = DynArray_add(oTokens, psToken);
               if (! iSuccessful)
                  {perror(pcPgmName); exit(EXIT_FAILURE);}
               iBufferIndex = 0;

               eState = STATE_START;
            }
            else
            {
               fprintf(stderr, "Invalid line\n");
               free(pcBuffer);
               freeTokens(oTokens);
               DynArray_free(oTokens);
               return NULL;
            }
            break;

         default:
            assert(0);
      }
   }
}
예제 #7
0
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;
}
예제 #8
0
파일: ish.c 프로젝트: amandashi/CodeSample
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;
}