Esempio n. 1
0
static void *remix_thread(void *arg) {
    struct thread_task_t *ttask = (struct thread_task_t *) arg;
    struct task_pool_t *pool = ttask->pool;
    wchar_t *word_task;

/*#ifdef DEBUG
        wprintf(L"Checking thread %i: started\n", ttask->thread_num);
#endif*/

    while (1) {
        pthread_mutex_lock(&(pool->monitor));
        if (pool->first_unprocessed >= pool->tasks_size) {
            pthread_mutex_unlock(&(pool->monitor));
            break;
        }
        word_task = pool->tasks[pool->first_unprocessed++];
        pthread_mutex_unlock(&(pool->monitor));
        
        if (is_valid_word(word_task)) {            
            pthread_mutex_lock(&(pool->monitor));
            pool->results[(*(pool->results_size))++] = word_task;
            pthread_mutex_unlock(&(pool->monitor));
/*#ifdef DEBUG
            wprintf(L"Checking thread %i: GOOD: %ls\n", ttask->thread_num, word_task);
#endif */
        } else {
/*#ifdef DEBUG
            wprintf(L"Checking thread %i: BAD: %ls\n", ttask->thread_num, word_task);
#endif */
            free(word_task);
        }
    }

#ifdef DEBUG
        wprintf(L"Checking thread %i: exited\n", ttask->thread_num);
#endif

    return NULL;
}
int
validate (token_t *token_array)
{
  int i = 0;
  int res = 1;
  int in_subshell = 0;
  int token_array_len = 0;
  while (!(token_array[i]->line_num == -1)) {
    token_array_len++;
    i++;
  }

  i = 0;
  while (i < token_array_len && !(token_array[i]->line_num == -1)) 
  {
    char *operand = token_array[i]->word;

    // if (!is_char_validated(operand)) syntax_error(token_array[i]->line_num);
    if (!is_valid_operator(operand) && !is_valid_word(operand)) syntax_error(token_array[i]->line_num);

    if (operand[0] == '(') 
    {
      in_subshell = 1;
      if (i + 1 >= token_array_len) {
        res = 0;
        syntax_error(token_array_len);
      } 
      else 
      {
        if (token_array[i + 1]->word[0] != ')') 
        {
          token_t *sub_token_array = checked_malloc(sizeof (token_t) * 128);
          tokenize (token_array[i+1]->word, sub_token_array, token_array[i+1]->line_num);

          // int j = 0;
          // printf("subshell\n" );
          // while (sub_token_array[j]->line_num != -1) {
          //   printf ("token %d\n  word:'%s' line_number:%d\n", j, sub_token_array[j]->word, sub_token_array[j]->line_num);
          //   j++;
          // }

          if (validate (sub_token_array))
          {
            i++;
          }
        }
      }
      i++;
      continue;
    } 

    if (operand[0] == ')')
    {
      if (i - 1 < 0) {
        res = 0;
        syntax_error(0);
      } 
      else 
      {
        if (!in_subshell) 
        {
          res = 0;
          // error_number = (i - 1 >= 0) ? token_array[i - 1]->line_num: token_array[i]->line_num;
          syntax_error(token_array[i]->line_num);
        }
        else
        {
          in_subshell = 0;
        }
      }
      i++;
      continue;
    }

    if (!strcmp(operand, ";") || !strcmp(operand, "||") || !strcmp(operand, "|") || !strcmp(operand, "&&")) 
    {
      if (i - 1 < 0) 
      {
        res = 0;
        syntax_error(token_array[i]->line_num);
      }
      // else 
      // {
      //  if 
      // }
      if (!strcmp(operand, ";")) {
        if (i < token_array_len - 1 && (!strcmp(token_array[i + 1]->word, "&&") 
          || !strcmp(token_array[i + 1]->word, "||") || !strcmp(token_array[i + 1]->word, "|") 
          || token_array[i + 1]->word[0] == '>' || token_array[i + 1]->word[0] == '<' || 
          !strcmp(token_array[i+1]->word, ";")))
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }

        if (i >= token_array_len - 1) 
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }

        if (i - 1 >= 0)
        {
          char *tmp = token_array[i - 1]->word;
          int j = strlen(tmp) - 1;
          while (j >= 0)
          {
            if (tmp[j] == ' ' || tmp[j] == '\t') {j--; continue;}
            if (tmp[j] == '>' || tmp[j] == '<')
            {
              res = 0;
              syntax_error(token_array[i - 1]->line_num);
            }
            j--;
          }
        }
      }
      else 
      {
        if (i < token_array_len - 1 && (!strcmp(token_array[i + 1]->word, "&&") 
          || !strcmp(token_array[i + 1]->word, "||") || !strcmp(token_array[i + 1]->word, "|") 
          ))
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        } 
        if (i >= token_array_len - 1) 
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }
      }
      
    }

    if (!strcmp(operand, "<") || !strcmp(operand, ">"))
    {
      if (i == token_array_len - 1 || i == 0) 
      {
        res = 0;
        syntax_error(token_array[i]->line_num);
      } 
      else 
      {
        if (!strcmp(token_array[i+1]->word, ">") || !strcmp(token_array[i+1]->word, "<"))
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }
      }
    }
    

    i++;  
  }

  return res;
}