Example #1
0
static int
getinput(rl_t *rl)
{
int c=cliGetchar(rl->cliIdx);
int i;
int found=0;

    if(c>0) {

        trxdbg(3,0,0,"Checking out c=0x%02x[%c]\n", c, c);
        /* check the control characters */
        for(i=0;i<NCTRL;i++)
	        if(ctrls[i][1]==c) {
                    trxdbg(3,0,0,"Returning ctr code[%d]\n", i);
                    return ctrls[i][0];
                }

        /* check the keyboard sequences */
        rl->curseq[rl->seqidx++]=c;
        trxdbg(3,0,0,"c=%02x\n", c);
        for(i=0;i<NBIND;i++)
        {
        int j;

	        if(!*(seqs[i])) continue;
	        for(j=0;j<rl->seqidx;j++)
	        {
                        trxdbg(3,0,0,"%d- [0x%02x] vs [0x%02x]\n", j, (*seqs[i])[j], rl->curseq[j]);
		        if((*seqs[i])[j]==rl->curseq[j])
		        {
			        /* set found if we match the entire current input */
			        if(j==rl->seqidx-1) found=1;
			        if((*seqs[i])[j+1]=='\0') {
                                
                                    trxdbg(1,0,0,"Returning sequence code[%d]\n", i);
                                    rl->seqidx=0;
                                    return codes[i];
                                }
		        }
	        }
        }
        if(!found) {
                trxdbg(3,0,0,"returning c=0x%02x[%c]\n", c, c);
	        if(isprint(c)){
                    rl->seqidx=0;
		    return c;
                }
                buz(rl); 
                
                rl->seqidx=0;
        }
        else {
            return 0;
        }
    }
    trxdbg(3,0,0,"returning c=0x%02x\n", c, c);
    return c;
}
Example #2
0
//
// Main Routine
//
int main(void) {
#ifdef DEBUG
	CSerial ser;		// declare a UART object
	ser.enable();
	CDebug dbg(ser);	// Debug stream use the UART object
	dbg.start();
#endif

	//
	// Optional: Enable tickless technology
	//
#ifndef DEBUG
	CPowerSave::tickless(true);
#endif

	//
	// Your setup code here
	//
	CButton btn(BUTTON_PIN_0);

	CBuzzer buz(15);	// buzzer on P0.15
	buz.enable();

	CPin led(LED_PIN_0);
	led.output();

	CTimeout tmLED;

	//
    // Enter main loop.
	//
    while(1) {
    	//
    	// Your loop code here
    	//
    	switch(btn.isPressed()) {
    	case BTN_PRESSED:
    		buz.post(3);	// turn on buzzer x 3
    		break;
    	case BTN_RELEASED:
    		break;
    	case BTN_NOTHING:
    		break;
    	}

    	if ( tmLED.isExpired(500) ) {
    		tmLED.reset();
    		led.toggle();
    	}
    }
}
Example #3
0
int
main ()
{
#ifdef LOOP
  signal (SIGINT, &int_handler);
  // signal (SIGALRM, &alrm_handler);
  // alarm (300);
#endif
  bar(2);
  baz(3,(char*)"abc");
  buz(4);
#ifdef LOOP
  while (1) {nanosleep(&req, NULL);}
#endif
  return 0;
}
Example #4
0
void
rl_newChar(void *vrl)
{
rl_t *rl=(rl_t*)vrl;
int plen=strlen(rl->prompt);

    do {

		int i;

		switch((i=getinput(rl))) { 

		case UP_HISTORY: case DOWN_HISTORY:
		{
		int inc=(i==UP_HISTORY?1:-1);
		char *p;

			if((p=hist_getcmd(rl->hist, rl->histoff+inc)))
			{
				curleft(rl, rl->curpos-plen);
				strcpy(rl->buf+plen, p);
				rl->maxpos=strlen(rl->buf);
				showbuf(rl, 0);
				rl->curpos=rl->maxpos;
				rl->histoff+=inc;

			}else buz(rl);
		}
		break;
		case BACKSPACE: case DEL: case CURSOR_LEFT:
		{
			if(rl->curpos==plen) buz(rl);
			else
			{
				curleft(rl, 1);
				/* we need to reprint if backspace */
				if(i==BACKSPACE || i==DEL)
				{
					memmove(rl->buf+rl->curpos, rl->buf+rl->curpos+1, rl->maxl-rl->curpos-1);
					rl->maxpos--;
					showbuf(rl, 1);
				}
			}
		}
		break;
		case DELETE:
		{
			if(rl->curpos==rl->maxpos) buz(rl);
			else
			{
				memmove(rl->buf+rl->curpos, rl->buf+rl->curpos+1, rl->maxl-rl->curpos-1);
				rl->maxpos--;
				showbuf(rl, 1);
			}
		}
		break;
		case CURSOR_RIGHT:
		{
			if(rl->curpos==rl->maxpos) buz(rl);
			else { curright(rl, 1); }
		}
		break;
		case LINEDONE:
		{
                    char *p;
                    
			/* we're about to return, so set the cursor position */
			curright(rl, rl->maxpos-rl->curpos);
			cliPutStr(rl->cliIdx,"\r\n");
                        /* make any history substitutions */
		        if((p=hist_cmd(rl->hist, rl->buf+plen)))
		        {
			        /* hist_cmd() return a pointer to the actual history
			           entry, so make a copy to buf and return to user */
                                trxdbg(1,0,0,"rl command '%s' replaced by '%s'\n", rl->buf+plen, p);
			        strcpy(rl->buf+plen,p);
			        if(cliNewCmd(rl->buf+plen, rl->cliIdx)) {
                                    rl_resetState(rl);
                                    rlShowPrompt(rl, 0);
		                }
                                else rl_resetState(rl);
                        }
                        else {
                            trxdbg(1,0,0,"rl command '%s' NOT replaced by history!\n", rl->buf+plen);
                            rl_resetState(rl);
                            rlShowPrompt(rl, 0);
                        }
		}
                break;
		/* erase entire line . Currently not linked to any keys */
		case KILLLINE:
		{
			curleft(rl, rl->curpos-plen);
			rl->maxpos=plen;
			rl->buf[plen]='\0';
			showbuf(rl, 1);
		}
                break;
		/* erase the current word */
		case KILLWORD:
		{
			/* if we are at the start of the line , bip */
			if(rl->curpos==plen) buz(rl);
			else
			{
			int i=rl->curpos-1;

				/* if the cursor sits on a white character already 
				   find the first non white one */
				while(!isalnum(rl->buf[i])&&i>plen) i--;
				/* skip back untill beginning of line or white again */
				while(isalnum(rl->buf[i])&&i>plen) i--;
				if(i<rl->maxpos && !isalnum(rl->buf[i])) i++;
				/* move every backward */
				memmove(rl->buf+i, rl->buf+rl->curpos, rl->maxl-rl->curpos);
				curleft(rl, rl->curpos-i);
				rl->maxpos=strlen(rl->buf);
				showbuf(rl, 1);
			}
		}
		break;
		case KILLTOBOL:
		{
			memmove(rl->buf+plen, rl->buf+rl->curpos, rl->maxl-rl->curpos);
			curleft(rl, rl->curpos-plen);
			rl->maxpos=strlen(rl->buf);
			showbuf(rl, 1);
		}
		break;
		case KILLTOEOL:
		{
			rl->buf[rl->curpos]='\0';
			rl->maxpos=strlen(rl->buf);
			showbuf(rl, 1);
		}
		break;
		case GOTOBOL: { curleft(rl, rl->curpos-plen); } break;
		case GOTOEOL: { curright(rl, rl->maxpos-rl->curpos); } break;
		case CLRSCR: 
		{ 
			if(home) {

				int i=rl->curpos;

				cliPutStr(rl->cliIdx,home); 
				rl->curpos=0;
				showbuf(rl, 0);
				rl->curpos=rl->maxpos;
				curleft(rl, rl->maxpos-i);

			} else buz(rl); 

		} break;
				
		case REDRAW: { } break;  /* do nothing */
		case WORD_FORWARD:
			/* if we are at the start of the line , bip */
			if(rl->curpos==rl->maxpos) buz(rl);
			else
			{
			int i=rl->curpos;

				/* if the cursor sits on a white character already 
		   		find the first non white one */
				while(!isalnum(rl->buf[i])&&i<rl->maxpos) i++;
				/* scip back untill beginning of line or white again */
				while(isalnum(rl->buf[i])&&i<rl->maxpos) i++;
				curright(rl, i-rl->curpos);
			}
		break;

		case WORD_BACKWARD:
			/* if we are at the start of the line , bip */
			if(rl->curpos==plen) buz(rl);
			else
			{
			int i=rl->curpos;

				/* if the cursor sits on a white character already 
		   		   find the first non white one */
				if(i>plen) i--;
				while(!isalnum(rl->buf[i])&&i>plen) i--;
				/* scip back untill beginning of line or white again */
				while(isalnum(rl->buf[i])&&i>plen) i--;
				while(!isalnum(rl->buf[i])&&i>plen) i++;
				curleft(rl, rl->curpos-i);
			}
		break;

		case KILL_WORD_FORWARD:
			/* if we are at the start of the line , bip */
			if(rl->curpos==rl->maxpos) buz(rl);
			else
			{
			int i=rl->curpos;

				/* if the cursor sits on a white character already 
		   		find the first non white one */
				while(!isalnum(rl->buf[i])&&i<rl->maxpos) i++;
				/* scip back untill beginning of line or white again */
				while(isalnum(rl->buf[i])&&i<rl->maxpos) i++;
				while(!isalnum(rl->buf[i])&&i<rl->maxpos) i++;
				/* keep one space */
				if(i<rl->maxpos && isalnum(rl->buf[i])) i--;
				/* move every backward */
				memmove(rl->buf+rl->curpos, rl->buf+i, rl->maxl-i);
				rl->maxpos=strlen(rl->buf);
				showbuf(rl, 1);
			}
		break;

		default: 
		{
                        if (i == -1) {
                            closeCli(rl->cliIdx);
                        }
                        else if(i) {
			    if(rl->maxpos==rl->maxl) buz(rl);
			    else
			    {
				    memmove(rl->buf+rl->curpos+1, rl->buf+rl->curpos, rl->maxl-rl->curpos-1);
				    rl->buf[rl->curpos]=i;
				    rl->maxpos++;
				    showbuf(rl, 1);
				    curright(rl, 1);
			    }
                        }
		}
		break;
		}
	} while(0);
}
Example #5
0
int main()
{
  try 
  {
    bool CLUSTER_MODE = false;
    boost::shared_ptr<redis::client> shared_c;
    
    if(CLUSTER_MODE)
      shared_c = init_cluster_client();
    else
      shared_c = init_non_cluster_client();
    
    redis::client & c = *shared_c;
    
	//c.set("a", 1);

	string s = c.get("a");
	
	return 0;
    // Test on high number databases

    c.select(14);
    c.flushdb();
    ASSERT_EQUAL(c.dbsize(), (redis::client::int_type) 0);

    c.select(15);
    c.flushdb();
    ASSERT_EQUAL(c.dbsize(), (redis::client::int_type) 0);

    string foo("foo"), bar("bar"), baz("baz"), buz("buz"), goo("goo");

    test("auth");
    {
      // TODO ... needs a conf for redis-server
    }

    test("binary save values");
    {
      int repeations = 3;
      string bin;
      for(int i=0; i < repeations; i++)
      {
        for(int i1=0; i1 <= 255; i1++)
          bin += (char) i1;
      }
      c.set("binary", bin);
      string response = c.get("binary");
      ASSERT_EQUAL(response.size(), (size_t)repeations*256);
      ASSERT_EQUAL(response, bin);

      c.append("binary", bin);
      ASSERT_EQUAL(c.get("binary"), bin+bin);

      string second_half = c.substr("binary", bin.size(), -1);
      ASSERT_EQUAL(second_half, bin);
    }
    
    test("binary save keys");
    {
      string bin1 = "bin_";
      for(int i1=0; i1 <= 127; i1++)
        bin1 += (char) i1;
      
      ASSERT_EQUAL(c.exists(bin1), false);
      c.set(bin1, "hello world");
      ASSERT_EQUAL(c.exists(bin1), true);
      ASSERT_EQUAL(c.get(bin1), string("hello world"));

      string bin2 = "bin_";
      for(int i1=128; i1 <= 255; i1++)
        bin2 += (char) i1;
      
      ASSERT_EQUAL(c.exists(bin2), false);
      c.set(bin2, "hello world");
      ASSERT_EQUAL(c.exists(bin2), true);
      ASSERT_EQUAL(c.get(bin2), string("hello world"));

      redis::client::string_vector keys;
      redis::client::int_type count = c.keys("bin_*", keys);
      ASSERT_EQUAL(count, (redis::client::int_type) 2);
      ASSERT_EQUAL(keys.size(), (size_t) 2);
      if( keys[0] == bin1 )
        ASSERT_EQUAL(keys[1], bin2);
      else if( keys[0] == bin2 )
        ASSERT_EQUAL(keys[1], bin1);
      else
        // keys[0] must be bin1 or bin2 so we must fail here
        ASSERT_EQUAL(true, false);
    }
    
    redis::server_info info;
    
    test("info");
    {
      // doesn't throw? then, has valid numbers and known info-keys.
      c.info(info);
    }

    test("set, get");
    {
      c.set(foo, bar);
      ASSERT_EQUAL(c.get(foo), bar);
    }

    test("getset");
    {
      ASSERT_EQUAL(c.getset(foo, baz), bar);
      ASSERT_EQUAL(c.get(foo), baz);
    }

    test("mget");
    {
      string x_val("hello"), y_val("world");
      c.set("x", x_val);
      c.set("y", y_val);
      redis::client::string_vector keys;
      keys.push_back("x");
      keys.push_back("y");
      redis::client::string_vector vals;
      c.mget(keys, vals);
      ASSERT_EQUAL(vals.size(), size_t(2));
      ASSERT_EQUAL(vals[0], x_val);
      ASSERT_EQUAL(vals[1], y_val);
    }

    test("setnx");
    {
      ASSERT_EQUAL(c.setnx(foo, bar), false);
      ASSERT_EQUAL(c.setnx(buz, baz), true);
      ASSERT_EQUAL(c.get(buz), baz);
    }

    test("incr");
    {
      ASSERT_EQUAL(c.incr("goo"), 1L);test("nonexistent (0) -> 1");
      ASSERT_EQUAL(c.incr("goo"), 2L);test("1->2");
    }

    test("decr");
    {
      ASSERT_EQUAL(c.decr("goo"), 1L);test("2->1");
      ASSERT_EQUAL(c.decr("goo"), 0L);test("1->0");
    }

    test("incrby");
    {
      ASSERT_EQUAL(c.incrby("goo", 3L), 3L);test("0->3");
      ASSERT_EQUAL(c.incrby("goo", 2L), 5L);test("3->5");
    }

    test("exists");
    {
      ASSERT_EQUAL(c.exists("goo"), true);
    }

    test("del");
    {
      c.del("goo");
      ASSERT_EQUAL(c.exists("goo"), false);
    }

    test("type (basic)");
    {
      ASSERT_EQUAL(c.type(goo), redis::client::datatype_none);test("we deleted it");
      c.set(goo, "redis");
      ASSERT_EQUAL(c.type(goo), redis::client::datatype_string);
    }

    test("keys");
    {
      redis::client::string_vector keys;
//       ASSERT_EQUAL(c.keys("*oo", keys), 2L);
//       ASSERT_EQUAL(keys.size(), (size_t) 2);
//       ASSERT_EQUAL(keys[0], foo);
//       ASSERT_EQUAL(keys[1], goo);
    }

    test("randomkey");
    {
      ASSERT_GT(c.randomkey().size(), (size_t) 0);
    }

    test("rename");
    {
      ASSERT_EQUAL(c.exists("foo"), true);
      ASSERT_EQUAL(c.exists("doo"), false);
      c.rename("foo", "doo");
      ASSERT_EQUAL(c.exists("foo"), false);
      ASSERT_EQUAL(c.exists("doo"), true);
    }

    test("renamenx");
    {
      ASSERT_EQUAL(c.exists("doo"), true);
      ASSERT_EQUAL(c.exists("foo"), false);
      ASSERT_EQUAL(c.renamenx("doo", "foo"), true);
      ASSERT_EQUAL(c.exists("doo"), false);
      ASSERT_EQUAL(c.exists("foo"), true);
      ASSERT_EQUAL(c.renamenx("goo", "foo"), false);
      ASSERT_EQUAL(c.exists("foo"), true);
      ASSERT_EQUAL(c.exists("goo"), true);
    }

    test("dbsize");
    {
      ASSERT_GT(c.dbsize(), 0L);
    }

    test("expire");
    {
      c.expire("goo", 1);
#ifndef NDEBUG
      cerr << "please wait a few seconds.." << endl;
#endif
#ifdef _WIN32
      Sleep(2000);
#else
      sleep(2);
#endif
      ASSERT_EQUAL(c.exists("goo"), false);
    }
    
    test("move");
    {
      c.select(14);
      ASSERT_EQUAL(c.exists("ttt"), false);
      c.select(15);
      c.set("ttt", "uuu");
      c.move("ttt", 14);
      c.select(14);
      ASSERT_EQUAL(c.exists("ttt"), true);
      c.select(15);
      ASSERT_EQUAL(c.exists("ttt"), false);
    }
    
    test("move should fail since key exists already");
    {
      c.select(14);
      c.set("ttt", "xxx");
      c.select(15);
      c.set("ttt", "uuu");
      
      bool threw = false;
      
      try
      {
        c.move("ttt", 14);
      }
      catch (redis::protocol_error & e)
      {
        threw = true;
      }
      
      ASSERT_EQUAL(threw, true);
      
      c.select(14);
      ASSERT_EQUAL(c.exists("ttt"), true);
      c.select(15);
      ASSERT_EQUAL(c.exists("ttt"), true);
    }
    
    test("sort ascending");
    {
      c.sadd("sort1", "3");
      c.sadd("sort1", "2");
      c.sadd("sort1", "1");
      
      redis::client::string_vector sorted;
      ASSERT_EQUAL(c.sort("sort1", sorted), 3L);
      ASSERT_EQUAL(sorted.size(), (size_t) 3);
      ASSERT_EQUAL(sorted[0], string("1"));
      ASSERT_EQUAL(sorted[1], string("2"));
      ASSERT_EQUAL(sorted[2], string("3"));
    }
    
    test("sort descending");
    {
      redis::client::string_vector sorted;
      ASSERT_EQUAL(c.sort("sort1", sorted, redis::client::sort_order_descending), 3L);
      ASSERT_EQUAL(sorted.size(), (size_t) 3);
      ASSERT_EQUAL(sorted[0], string("3"));
      ASSERT_EQUAL(sorted[1], string("2"));
      ASSERT_EQUAL(sorted[2], string("1"));
    }
    
    test("sort with limit");
    {
      // TODO
    }
    
    test("sort lexicographically");
    {
      // TODO
    }
    
    test("sort with pattern and weights");
    {
      // TODO
    }
    
    test_lists(c);
    test_sets(c);
    test_zsets(c);
    test_hashes(c);

    test_distributed_strings(c);
    test_distributed_ints(c);
    //test_distributed_mutexes(c);
    
    test_generic(c);
    
    benchmark(c, 10000);

    test("save");
    {
      c.save();
    }

    test("bgsave");
    {
      c.bgsave();
    }

    test("lastsave");
    {
#ifdef _WIN32
      ASSERT_GT(c.lastsave(), (time_t)0L);
#else
      ASSERT_GT(c.lastsave(), 0L);
#endif
    }

    test("shutdown");
    {
// You can test this if you really want to ...
//      c.shutdown();
    }
  } 
  catch (redis::redis_error & e) 
  {
    cerr << "got exception: " << e.what() << endl << "FAIL" << endl;
    return 1;
  }

  cout << endl << "testing completed successfully" << endl;
  return 0;
}