Ejemplo n.º 1
0
uptr_t eval(uptr_t *env, uptr_t form) {
  if (IS_INT(form) || IS_NIL(form))
    return form;

  if (IS_SYM(form))
    return get(*env, form);

  if (IS_CONS(form)) {
    uptr_t *form_p = refer(form),
      *fn_p = refer(eval(env, CAR(*form_p))),
      rval;

    if (IS_SYM(*fn_p)) {
      rval = exec_special(env, *form_p);
    } else if (IS_CONS(*fn_p) && SVAL(CAR(*fn_p)) == S_FN) {
      rval = _fn(env, *fn_p, eval_list(env, CDR(*form_p)));
    } else {
      printf_P(PSTR("ERROR: "));
      print_form(CAR(*form_p));
      printf_P(PSTR(" cannot be in function position.\n"));

      rval = NIL;
    }

    release(2); // form_p, fn_p
    return rval;
  }

  return NIL;
}
Ejemplo n.º 2
0
int main() {

  init_env(); // Poorly named. Has nothing to do with env alist.
  init_mem();

  uptr_t *env = refer(NIL);
  init_syms(env);

  uptr_t *form_p = refer(NIL);
  while(1) {
    print_env(env);
    print_mem();

    printf_P(PSTR("> "));
    *form_p = read_form(stdin);
    while(getc(stdin) != '\r');
    print_form(eval(env, *form_p));
    printf_P(PSTR("\n"));

    //    print_mem();
    __GC__();
  }

  release(2); // Just a formality really...

  return 0;
}
Ejemplo n.º 3
0
void* eval(void** env, void* expr) {
  void *op, *arg, *args;

  switch(type(expr)) {
  case CONS:
    if (!CAR(expr) && !CDR(expr)) {
      return expr;
    } else {
      op = get(*env, CAR(expr));
      args = CDR(expr);
      while((args)) {
        arg = CAR(args);
        printf("arg: ");
        print_form(arg);
        printf("\n");
        args = CDR(args) ;
      }
      switch(type(op)) {
      case SPECIAL:
        return(expr);
        /* return SPECIAL(op)->fn(*env, list); */
      default:
        return expr;
      }
    }
  case SYMBOL:
    return get(*env, expr);
  default:
    return expr;
  }
}
Ejemplo n.º 4
0
int main(int argc, char *argv[]) {

#ifdef ARDUINO
  ARDUINO_INIT_IO(9600);
#endif

  /* populate env with special forms */
  void *env = empty();
  Special Car = { SPECIAL, "car", &car };
  Special Cdr = { SPECIAL, "cdr", &cdr };
  Special Quote = { SPECIAL, "quote", &quote };
  Special Eq = { SPECIAL, "eq", &eq };
  Special Eval = { SPECIAL, "eval", &eval };
  assoc(&env, sym("car"), (void*)&Car);
  assoc(&env, sym("cdr"), (void*)&Cdr);
  assoc(&env, sym("quote"), (void*)&Quote);
  assoc(&env, sym("eq"), (void*)&Eq);
  assoc(&env, sym("eval"), (void*)&Eval);

  print_form(cons(sym("eq"), cons(integer(1), integer(2))));
  printf("\n");
  print_form(eval(&env, cons(sym("eq"), cons(integer(2), integer(3)))));
  printf("\n");

  printf("\n");
  print_form(eval(&env, cons(sym("eq"), cons(integer(1), cons(integer(2), integer(3))))));
  printf("\n");

  while(1) {
    printf("=> ");
    print_form(read_form(stdin));
    printf("\n");
  }

  return 0;
}
Ejemplo n.º 5
0
void RestServer::print_html(Stream &_client) {
	if ((request_options & JSON_FORMAT) == 0) {
		print_flash_string(PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"), _client);

		print_flash_string(PSTR("<b>Current time: "), _client);
		_client.print( (millis()/1000) );
		print_flash_string(PSTR("s.<br /><hr />Current state:</b><br />\r\n"), _client);

	    for(int i = 0; i < int(resources_count); i++) {
			if (resources[i].get || resources[i].post) {

				_client.print(resources[i].service->getName());
				print_flash_string(PSTR(": "), _client);
				_client.print(resources[i].service->getValue());
				print_flash_string(PSTR("<br />\r\n"), _client);
	        }
	    }
		print_form(_client);
		print_flash_string(PSTR("\r\n\r\n\r\n"), _client); 
	}
}
Ejemplo n.º 6
0
static void test__get_form_from_url( const char *param )
{
	CURL *curl;
	int  ctr;
	char login_url[ 2048 ] = "file://";

	const gchar *action = "https://accounts.google.com/ServiceLoginAuth";
	const gchar *const input_names[ ] = { "Email", "Passwd", NULL };
	form_field_t *form;

	/* Create a file URL. */
	getcwd( &login_url[ strlen( "file://" ) ], sizeof( login_url ) );
	strcat( login_url, "/../../data/google-login.html" );

	curl = curl_easy_init( );
	form = get_form_from_url( curl, login_url, action, input_names );
	curl_easy_cleanup( curl );

	ctr = 1;
	print_form( form, &ctr );
}
Ejemplo n.º 7
0
    int main(int argc, char *argv[])
    {



        const struct sniff_ethernet *ethernet; /* The ethernet header */
        const struct sniff_ip *ip; /* The IP header */
        const struct sniff_tcp *tcp; /* The TCP header */
        const char *payload; /* Packet payload */

        u_int size_ip;
        u_int size_tcp;

        char *dev; /* name of the device to use */
        char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
        pcap_t *handle;		/* Session handle */
        struct bpf_program fp;		/* The compiled filter expression */
        char filter_exp[] = "";/* "port 80";	/* The filter expression */
        bpf_u_int32 mask;		/* The netmask of our sniffing device */
        bpf_u_int32 net;		/* The IP of our sniffing device */
        struct pcap_pkthdr *header;	/* The header that pcap gives us */
        const u_char *packet;		/* The actual packet */

        dev = pcap_lookupdev(errbuf);
        printf("\n\n    Start capture packet \n\n");
            /* error checking */
            if(dev == NULL)
            {
                printf("%s\n",errbuf);
                exit(1);
            }
            printf("Device = %s\n",dev);


        handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
        if (handle == NULL) {
            fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
            return(2);
        }if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
            fprintf(stderr, "Can't get netmask for device %s\n", dev);
            net = 0;
            mask = 0;
        }
        if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
            fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
            return(2);
        }
        if (pcap_setfilter(handle, &fp) == -1) {
            fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
            return(2);
        }
        /* Grab a packet */
        int res;
        int num=0;
        while((res=pcap_next_ex(handle, &header,&packet))>=0)
        {


            print_form(packet, num);
            num++;
            if (res==0) {

                continue;
            }
        }

        /* And close the session */
        pcap_close(handle);
        return(0);
        //end here
    }
Ejemplo n.º 8
0
uptr_t exec_special(uptr_t *env, uptr_t form) {
  uptr_t fn = CAR(form);
  uptr_t args = CDR(form);

  switch(SVAL(fn)) {
  case S_LET:
    return let(env, args);

  case S_FN:
    return form;

  case S_LOOP:
    return loop(env, args);

  case S_DO: {
    uptr_t *body_p = refer(args), rval = NIL;

    while (*body_p) {
      rval = eval(env, CAR(*body_p));
      *body_p = CDR(*body_p);
    }
    release(1); // body_p
    return rval;
  }

  case S_RECUR: {
    uptr_t rval, *fn_p = refer(fn);
    rval = build_cons(*fn_p, eval_list(env, args));
    release(1); // fn_p
    return rval;
  }

  case S_QUOTE:
    return CAR(args);

  case S_CAR:
    return CAR(eval(env, CAR(args)));

  case S_CDR:
    return CDR(eval(env, CAR(args)));

  case S_AND: {
    if (IS_NIL(args)) return PS_TRUE;
    uptr_t *rem_args = refer(args),
      rval = NIL;
    while ((rval = eval(env, CAR(*rem_args))) && (*rem_args = CDR(*rem_args)));
    release(1);
    return rval;
  }

  case S_OR: {
    if (IS_NIL(args)) return NIL;
    uptr_t *rem_args = refer(args),
      rval = NIL;
    while (!(rval = eval(env, CAR(*rem_args))) && (*rem_args = CDR(*rem_args)));
    release(1);
    return rval;
  }

  case S_NOT: {
    if (IS_NIL(args)) return NIL;
    uptr_t rval = eval(env, CAR(args));
    return rval ? NIL : PS_TRUE;
  }

  case S_IF: {
    uptr_t rval = NIL, *clauses = refer(args);

    if (eval(env, CAR(*clauses)) && CDR(*clauses))
      rval = eval(env, CADR(*clauses));
    else if (CDDR(*clauses))
      rval = eval(env, CADDR(*clauses));

    release(1); // clauses
    return rval;
  }

  case S_WHEN: {
    uptr_t rval = NIL, *cond_p = refer(CAR(args)), *body_p = refer(CDR(args));

    if (eval(env, *cond_p))
      while(*body_p) {
        rval = eval(env, CAR(*body_p));
        *body_p = CDR(*body_p);
      }

    release(2); // cond_p, body_p
    return rval;
  }

  case S_CONS: {
    uptr_t rval = NIL, *args_p = refer(args);
    rval = build_cons(eval(env, CAR(*args_p)), eval(env, CADR(*args_p)));
    release(1); // args_p
    return rval;
  }

  case S_PRINT:
    print_form(eval(env, CAR(args)));
    printf_P(PSTR("\n"));
    return NIL;

  case S_DEF: {
    uptr_t *args_p = refer(args),
      *binding = refer(eval(env, CADR(args)));
    assoc(env, CAR(*args_p), *binding);
    release(2); // args_p, binding
    return *binding; // Yeah, it's been "released", but the pointer is still valid.
  }

  case S_EVAL:
    return eval(env, eval(env, CAR(args)));

#define _COMPR(rval) {                                                  \
      if (IS_NIL(args)) return NIL;                                     \
                                                                        \
      uptr_t *args_p = refer(args);                                     \
      while(CDR(*args_p) && (eval(env, CAR(*args_p)) _COMP_OPR eval(env, CADR(*args_p)))) \
        *args_p = CDR(*args_p);                                         \
                                                                        \
      if (IS_NIL(CDR(*args_p)))                                         \
        rval = eval(env, CAR(*args_p));                                 \
      release(1);                                                       \
    }

#define _COMP_OPR ==
  case S_EQL: {
    uptr_t rval = NIL;
    _COMPR(rval);
    return rval;
  }

  case S_NEQL: {
    uptr_t rval = NIL;
    _COMPR(rval);
    return rval ? NIL : PS_TRUE;
  }
#undef _COMP_OPR

#define _COMP_OPR <
  case S_LT: {
    uptr_t rval = NIL;
    _COMPR(rval);
    return rval;
  }
#undef _COMP_OPR

#define _COMP_OPR <=
  case S_LTE: {
    uptr_t rval = NIL;
    _COMPR(rval);
    return rval;
  }
#undef _COMP_OPR

#define _COMP_OPR >
  case S_GT: {
    uptr_t rval = NIL;
    _COMPR(rval);
    return rval;
  }
#undef _COMP_OPR

#define _COMP_OPR >=
  case S_GTE: {
    uptr_t rval = NIL;
    _COMPR(rval);
    return rval;
  }
#undef _COMP_OPR

#define _ARITH(coll) {                                          \
      uptr_t *rem_args = refer(args);                           \
      coll = TO_INT(eval(env, CAR(*rem_args)));                 \
      *rem_args = CDR(*rem_args);                               \
      while (*rem_args) {                                       \
        coll _ARITH_OPR TO_INT(eval(env, CAR(*rem_args)));      \
        *rem_args = CDR(*rem_args);                             \
      }                                                         \
      release(1);                                               \
    }

#define _ARITH_OPR +=
  case S_PLUS: {
    if (! args) return INTERN_INT(0);
    if (! CDR(args)) return eval(env, CAR(args));
    int rval;
    _ARITH(rval);
    return INTERN_INT(rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR -=
  case S_MINUS: {
    if (! args) return NIL;
    if (! CDR(args)) return INTERN_INT(0 - TO_INT(eval(env, CAR(args))));
    int rval;
    _ARITH(rval);
    return INTERN_INT(rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR *=
  case S_MULT: {
    if (! args) return INTERN_INT(1);
    if (! CDR(args)) return eval(env, CAR(args));
    int rval;
    _ARITH(rval);
    return INTERN_INT(rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR /=
  case S_DIV: {
    if (! args) return NIL;
    if (! CDR(args)) return INTERN_INT(eval(env, CAR(args)) == INTERN_INT(1) ? 1 : 0);
    int rval;
    _ARITH(rval);
    return INTERN_INT(rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR &=
  case S_BAND: {
    if (! args) return NIL;
    if (! CDR(args)) return eval(env, CAR(args));
    uint8_t rval;
    _ARITH(rval);
    return INTERN_INT((int)rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR |=
  case S_BOR: {
    if (! args) return NIL;
    if (! CDR(args)) return eval(env, CAR(args));
    uint8_t rval;
    _ARITH(rval);
    return INTERN_INT((int)rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR ^=
  case S_BXOR: {
    if (! args) return NIL;
    if (! CDR(args)) return eval(env, CAR(args));
    uint8_t rval;
    _ARITH(rval);
    return INTERN_INT((int)rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR <<=
  case S_BSL: {
    if (! args) return NIL;
    if (! CDR(args)) return eval(env, CAR(args));
    uint8_t rval;
    _ARITH(rval);
    return INTERN_INT((int)rval);
  }
#undef _ARITH_OPR

#define _ARITH_OPR >>=
  case S_BSR: {
    if (! args) return NIL;
    if (! CDR(args)) return eval(env, CAR(args));
    uint8_t rval;
    _ARITH(rval);
    return INTERN_INT((int)rval);
  }
#undef _ARITH_OPR

  case S_SREG: {
    uptr_t *args_p = refer(args),
      reg = eval(env, CAR(*args_p));
    if (IS_REG(reg))
      *BYTE_PTR(reg) = eval(env, CADR(*args_p));
    else {
      printf_P(PSTR("Invalid register: "));
      print_form(reg);
      printf_P(PSTR("\n"));
    }
    release(1); // args_p
    return NIL;
  }

  case S_SLP:
    _delay_ms(TO_INT(eval(env, CAR(args))));
    return NIL;

  default:
    printf_P(PSTR("ERROR: "));
    print_form(fn);
    printf_P(PSTR(" is not a function.\n"));
    return NIL;
  }
}
Ejemplo n.º 9
0
static void test__modify_form_inputs( const char *param )
{
	FILE               *filehd;
	char               *html_buf;
	const gchar *const input_names[ ] = { NULL, NULL };
	form_field_t       *form;
	int                ctr = 1;

	const input_field_t input_to_change1 = { .name = "dsh", .value = "test1" };
	const input_field_t input_to_change2 = { .name = "new", .value = "test2" };

	html_buf = malloc( 75000 );
	filehd = fopen( "../../data/google-login.html", "r" );
	if( ! filehd )
	{
		printf( "Error: cannot find test data\n" );
		exit( EXIT_FAILURE );
	}
	if( fread( html_buf, 75000, 1, filehd ) )
	{
		printf( "Error: cannot read test data\n" );
		exit( EXIT_FAILURE );
	}
	form = find_form( html_buf, "https://accounts.google.com/", input_names );
	if( ! form )
	{
		printf( "Form not found\n" );
		exit( 1 );
	}

	modify_form_inputs( (gpointer) &input_to_change1, (gpointer) form );
	modify_form_inputs( (gpointer) &input_to_change2, (gpointer) form );
	print_form( form, &ctr );
}



static void test__modify_form( const char *param )
{
	FILE               *filehd;
	char               *html_buf;
	const gchar *const input_names[ ] = { NULL, NULL };
	form_field_t       *form;
	int                ctr = 1;

	input_field_t input_to_change1  = { .name = "dsh", .value = "test1" };
	input_field_t input_to_change2  = { .name = "new", .value = "test2" };
	GSList        *inputs_to_modify = NULL;

	html_buf = malloc( 75000 );
	filehd = fopen( "../../data/google-login.html", "r" );
	if( ! filehd )
	{
		printf( "Error: cannot find test data\n" );
		exit( EXIT_FAILURE );
	}
	if( fread( html_buf, 75000, 1, filehd ) )
	{
		printf( "Error: cannot read test data\n" );
		exit( EXIT_FAILURE );
	}
	form = find_form( html_buf, "https://accounts.google.com/", input_names );
	if( ! form )
	{
		printf( "Form not found\n" );
		exit( 1 );
	}

	inputs_to_modify = g_slist_append( inputs_to_modify, &input_to_change1 );
	inputs_to_modify = g_slist_append( inputs_to_modify, &input_to_change2 );
	modify_form( form, inputs_to_modify );

	print_form( form, &ctr );
}



static void test__post_form( const char *param )
{
	FILE               *filehd;
	char               *html_buf;
	const gchar *const input_names[ ] = { NULL, NULL };
	form_field_t       *form;
	char               action_url[ 2048 ] = "file://";
	CURL               *curl;
	char               *html;

	html_buf = malloc( 75000 );
	filehd = fopen( "../../data/google-login.html", "r" );
	if( ! filehd )
	{
		printf( "Error: cannot find test data\n" );
		exit( EXIT_FAILURE );
	}
	if( fread( html_buf, 75000, 1, filehd ) )
	{
		printf( "Error: cannot read test data\n" );
		exit( EXIT_FAILURE );
	}
	form = find_form( html_buf, "https://accounts.google.com/", input_names );
	if( ! form )
	{
		printf( "Form not found\n" );
		exit( 1 );
	}

	/* Create a file URL action. */
	g_free( form->action );
	getcwd( &action_url[ strlen( "file://" ) ], sizeof( action_url ) );
	strcat( action_url, "/../../data/post.response.html" );
	form->action = action_url;

	curl = curl_easy_init( );
	html = post_form( curl, form, NULL );
	curl_easy_cleanup( curl );
	printf( "%s\n", html );
}



static void test__login_to_gmail( const char *param )
{
	FILE *filehd;
	char username[ 100 ];
	char password[ 100 ];
	CURL *curl;

	global_config.ipv4_only = TRUE;

	/* Skip test if no internet access is available. */
	if( test_check_internet( ) == FALSE )
	{
		exit( 77 );
	}

	/* Skip the test if the password file is not available. To enable the
	   test, enter your own email and password in the "password" file in the
	   test/data directory; use test/data/password.template as a template. */
	filehd = fopen( "../../data/password", "r" );
	if( ! filehd )
	{
		exit( 77 );
	}
	fgets( username, sizeof( username ), filehd );
	fgets( password, sizeof( username ), filehd );

	curl = curl_easy_init( );
	curl_easy_setopt( curl, CURLOPT_COOKIESESSION, 1 );
	curl_easy_setopt( curl, CURLOPT_COOKIEFILE, "cookies.txt" );
	(void)login_to_gmail( curl, username, password );
	curl_easy_cleanup( curl );
}
Ejemplo n.º 10
0
static void test__search_input_by_name( const char *param )
{
	gchar         *input_names[ ] = { "name1", "name2", "name3", NULL };
	input_field_t input1          = { .name = "name2", .value = "value2" };
	input_field_t input2          = { .name = "name3", .value = "value3" };
	input_field_t input4          = { .name = "name4", .value = "value4" };
	int found;

	found = search_input_by_name( &input1, input_names );
	printf( "1: %d\n", found );
	found = search_input_by_name( &input2, input_names );
	printf( "2: %d\n", found );
	found = search_input_by_name( &input4, input_names );
	printf( "3: %d\n", found );
}



static void test__search_form_by_action_and_inputs( const char *param )
{
	input_field_t input1 = { "in1", "ivalue1" };
	input_field_t input2 = { "in2", "ivalue2" };
	input_field_t input3 = { "in3", "ivalue3" };
	GSList        *input;

	form_field_t form1 = { "form1", "value1", "action1", NULL };

	const gchar *search1[ ] = { "in1", NULL };
	const gchar *search2[ ] = { "in4", NULL };
	const gchar *search3[ ] = { "in2", NULL };
	struct form_search_t search_form1 = { "action1", search1 };
	struct form_search_t search_form2 = { "act", search2 };
	struct form_search_t search_form3 = { "action4", search3 };

	int found;

	input = g_slist_append( NULL, &input1 );
	input = g_slist_append( input, &input2 );
	input = g_slist_append( input, &input3 );

	form1.input_fields = input;
	found = search_form_by_action_and_inputs( &form1, &search_form1 );
	printf( "1: %d\n", found );
	found = search_form_by_action_and_inputs( &form1, &search_form2 );
	printf( "2: %d\n", found );
	found = search_form_by_action_and_inputs( &form1, &search_form3 );
	printf( "3: %d\n", found );
}



static void test__find_form( const char *param )
{
	FILE *filehd;
	char *html_buf;
	int    ctr;
	form_field_t *form1;
	form_field_t *form2;
	const char *input_names[ ] = { "bgresponse", "submit_access", NULL };

	html_buf = malloc( 75000 );

	filehd = fopen( "../../data/oauth2-confirm.html", "r" );
	if( ! filehd )
	{
		printf( "Error: cannot find test data\n" );
		exit( EXIT_FAILURE );
	}
	if( fread( html_buf, 75000, 1, filehd ) )
	{
		printf( "Error: cannot read test data\n" );
		exit( EXIT_FAILURE );
	}

	ctr = 1;
	form1 = find_form( html_buf, "https://accounts.google.com/", input_names );
	print_form( form1, &ctr );
	form2 = find_form( html_buf, "https://accounts.google.com/o/oauth2/nonexistent", input_names );
	if( form2 != NULL )
	{
		printf( "2: found\n" );
	}
	else
	{
		printf( "2: not found\n" );
	}
}



static void test__receive_curl_response( const char *param )
{
	struct curl_write_buffer_t write_buffer = { NULL, 0 };
	const char                 *testdata = "01234567891011121314151617181920";
	size_t                     processed;

	processed = receive_curl_response( testdata, 5, 3, &write_buffer );
	printf( "1: %2d \"%s\"\n", (int) processed, write_buffer.data );

	processed = receive_curl_response( &testdata[ 15 ], 10, 1, &write_buffer );
	printf( "2: %2d \"%s\"\n", (int) processed, write_buffer.data );

	processed = receive_curl_response( &testdata[ 25 ], 1, 3, &write_buffer );
	printf( "3: %2d \"%s\"\n", (int) processed, write_buffer.data );
}



static void test__read_url( const char *param )
{
	CURL  *curl;
	gchar *html;

	/* Skip test if no internet access is available. */
	if( test_check_internet( ) == FALSE )
	{
		exit( 77 );
	}

	curl = curl_easy_init( );
	html = read_url( curl, "http://www.microsoft.com" );
	curl_easy_cleanup( curl );
	if( html != NULL )
	{
		printf( "%s\n", html );
		g_free( html );
	}
}