Beispiel #1
0
SimpleReBuilder * simplifyRe(char ** complexRe, SimpleReBuilder * builder) {

    int len = strlen(*complexRe);
    simpleReBuilder(&builder, len);

    int i,j;
    for (i = 0, j = 0; i < len; i++, j++) {
        switch(DEREF(complexRe, i)) {
            
            case '\\':
                handle_escape(builder, complexRe, &len, &j, &i);
                break;

            case '.':
                builder->re[j] = ANY; //nak is ANY
                break;

            case '+':
                builder->re[j] = PLUS; //0x01 is +
                break;

            case '?':
                builder->re[j] = QUESTION; //0x02 is ?
                break;

            case '*':
                builder->re[j] = STAR; //0x03 is *
                break;

            case '|':
                builder->re[j] = ALTERNATE; //0x04 is |
                break;

            case '(':
                builder->re[j] = PAREN_OPEN; //0x05 is (
                break;

            case ')':
                builder->re[j] = PAREN_CLOSE; //0x06 is )
                break;

            case '[':
                handle_range(builder, *complexRe, len, &j, &i);
                break;

            default:
                builder->re[j] = DEREF(complexRe,i);
                break;
        }

    }
    builder->re[j] = '\0';

    return builder;

}
Beispiel #2
0
static void putc(char c) {
  if (in_escape) {
    in_escape = handle_escape(c);
    return;
  }

  int back = c_back;
  int fore = c_fore;

  switch (c) {
  case 0x08: /* Backspace */
    --cursor_x;
    if (cursor_x > 80) { /* Wrapped? */
      cursor_x = 0;
      if (cursor_y > 0)
        --cursor_y;
    }
    break;

  case '\t':
    cursor_x = (cursor_x+8) & ~7;
    break;

  case '\r':
    cursor_x = 0;
    break;

  case '\n':
    cursor_x = 0;
    ++cursor_y;
    break;

  case '\033':
    /* VT220 escape */
    in_escape = 1;
    return;

  default:
    /* Is the character printable? */
    if (c >= ' ') {
      video_memory[cursor_y*80 + cursor_x] = MAKE_CHAR(c, fore, back);
      ++cursor_x;
    }
    break;
  }

  if (cursor_x >= 80) {
    cursor_x -= 80;
    ++cursor_y;
  }
  scroll();
  update_cursor();
}
Beispiel #3
0
static int cook_serial(unsigned char ch)
{
	switch(ch) {
	case 8:
		return KEY_BACKSPACE;

	case 27:
		return handle_escape();

	default:
		return ch;
	}
}
Beispiel #4
0
void print(const char *string, char *args[])
{
    int arg_count = 0;
    for(int i = 0; i < strlen(string); i++) {
        if(string[i] == '%' && args)
            handle_formatter(string[++i], args[arg_count++]);
        else if(string[i] == '\\') {
            if(handle_escape(string[++i]) == -1)
                return;
        } else
            putchar(string[i]);
    }
}
int uart_incoming_data(short revents)
{
	int i;

	unsigned char buf[FIFO_SIZE];
	/* Check OOB first */

	int r;
	r = recv(clientsockfd,buf,sizeof(buf),MSG_OOB);
	if (r>0) {
		fprintf(stderr,"OOB data %d\n",r);
		handle_escape(buf[0]);
		return 0;
	}

	r = recv(clientsockfd,buf,sizeof(buf),0);



	fprintf(stderr,"UART read %d\n",r);

	if (r>0) {
		i=0;
		pthread_mutex_lock(&fifo_lock);
		while (r--) {
            /*
			if (uartescape) {
				uartescape=0;
				if (buf[i]!=0xff) {
					fprintf(stderr,"Escape sequence: 0x%02x\n",buf[i]);
                    handle_escape(buf[i]);
					i++;
					continue;
				}
			} else {
				if (buf[i]==0xff) {
					uartescape=1;
					i++;
					continue;
				}
			}
            */
			fifodata[highmark]=buf[i];
			i++;
			highmark++;
			if (highmark>=FIFO_SIZE)
				highmark=0;
			if (highmark==lowmark) {
				printf("UART: FIFO overrun\n");
				pthread_mutex_unlock(&fifo_lock);
				abort();
			}
		}
	} else {
		poll_remove(clientsockfd);
		printf("Disconnected\n");
		clientsockfd=-1;
	}
	pthread_mutex_unlock(&fifo_lock);
	return 0;
}
Beispiel #6
0
int main(int argc, char * argv[])
{
	char line[2048] = {'\0'};
	char string[1024] = {'\0'};
	char cmd[20] = {'\0'};

	int id;
	unsigned length;
	unsigned fail = 0;
	unsigned count = 0;
	unsigned size = 10;

	FILE * handle = fopen(argv[1], "r");
	int result;

	regex_dfa * dfa_root = NULL;
	regex_nfa * nfa_root = NULL;

	#ifdef alg_dfa
		#define root dfa_root
		#define DESTROY regex_destroy_dfa
		#define CONSTRUCT regex_construct_dfa
		#define MATCH regex_match_dfa
	#endif

	#ifdef alg_nfa
		#define root nfa_root
		#define DESTROY regex_destroy_nfa
		#define CONSTRUCT regex_construct_nfa
		#define MATCH regex_match_nfa
	#endif

	regex_pattern * patterns = malloc(sizeof(regex_pattern) * size);

	int debug = argc == 3 && strcmp(argv[2], "debug") == 0;

	while(fscanf(handle, "%20s %1024s %u %u", cmd, string, &length, &id) == 4)
	{
		handle_escape(string);

		if(cmd[0] == '#') continue;

		if(strcmp(cmd, "add") == 0)
		{
			if(count + 1 == size)
			{
				size += 10;
				patterns = realloc(patterns, sizeof(regex_pattern) * size);
			}

			patterns[count].input = malloc(length);
			memcpy(patterns[count].input, string, length);
			patterns[count].length = length;
			patterns[count].id = id;
			count++;

			sprintf(line, "added %s with id %d\n", string, id);
		}

		if(strcmp(cmd, "commit") == 0)
		{
			DESTROY(root);
			root = CONSTRUCT(patterns, count);

			fail = (root == NULL && id != -1) || (root != NULL && id == -1);
			free_patterns(patterns, &count);

			sprintf(line, "commit %s\n", fail ? "FAIL" : "PASS");
		}

		if(strcmp(cmd, "match") == 0)
		{
			result = MATCH(root, string, length);

			fail = result != id;
			sprintf(line, "matching againts %s with result %d (%d), %s\n", string, result, id, fail ? "FAIL" : "PASS");
		}

		if(debug) printf("%s", line);
		if(fail)
		{
			break;
		}
	}

	DESTROY(root);
	free(patterns);
	fclose(handle);

	return fail;
}