Пример #1
0
/*
 * get_line:
 *      Reads the next line up to '\n' or EOF.  Multiple spaces are
 *	compressed to one space; a space is inserted before a ','
 */
char *
get_line(void)
{
	size_t pos;
	int c, oy, ox;
	WINDOW *oscr;

	oscr = stdscr;
	stdscr = Msgwin;
	getyx(stdscr, oy, ox);
	refresh();
	/* loop reading in the string, and put it in a temporary buffer */
	for (pos = 0; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
			if (c == erasechar()) {	/* process erase character */
				if (pos > 0) {
					int i;

					pos--;
					for (i = strlen(unctrl(linebuf[pos])); i; i--)
						addch('\b');
				}
				continue;
			} else
				if (c == killchar()) {	/* process kill
							 * character */
					pos = 0;
					move(oy, ox);
					continue;
				} else
					if (pos == 0 && c == ' ')
						continue;
		if (pos >= LINESIZE - 1 || !(isprint(c) || c == ' '))
			putchar(CTRL('G'));
		else {
			if (islower(c))
				c = toupper(c);
			linebuf[pos++] = c;
			addstr(unctrl(c));
			Mpos++;
		}
	}
	linebuf[pos] = '\0';
	stdscr = oscr;
	return (linebuf);
}
Пример #2
0
/* s: chars allowed besides space or return */
void
xwaitforspace(char *s)
{
	int c;

	morc = 0;

	while((c = readchar()) != '\n') {
	    if(flags.cbreak) {
		if(c == ' ') break;
		if(s && strchr(s,c)) {
			morc = c;
			break;
		}
		hackbell();
	    }
	}
}
Пример #3
0
char *
regname(int regnam)
{
	static char buf[64];
	char *p;
	int c;

	p = buf;
	*p++ = regnam;
	while (isalnum(c = readchar())) {
		if (p >= buf+sizeof(buf)-1)
			error("register name too long");
		*p++ = c;
	}
	*p = 0;
	reread();
	return (buf);
}
static int findNonWs(FILETYPE f)
{
  int c, cont = 1;
  while (cont) {
    switch ((c = readchar(f))) {
    case 0:
      c = RETURN_CODE_EOF;
      break;
    case -1:
      c = RETURN_CODE_ERROR;
      break;
    default: ;
    }
    if (!isspace(c))
      break;
  }
  return c;
}
Пример #5
0
static int newton_init(int fd, unsigned long *id, unsigned long *extra)
{
	int i;
	unsigned char c;
	unsigned char response[35] = {
		0x16, 0x10, 0x02, 0x64, 0x5f, 0x69, 0x64, 0x00,
		0x00, 0x00, 0x0c, 0x6b, 0x79, 0x62, 0x64, 0x61,
		0x70, 0x70, 0x6c, 0x00, 0x00, 0x00, 0x01, 0x6e,
		0x6f, 0x66, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x10,
		0x03, 0xdd, 0xe7
	};

	for (i = 0; i < sizeof(response); i++)
		if (readchar(fd, &c, 400) || c != response[i])
			return -1;

	return 0;
}
Пример #6
0
/*
 * get_bool:
 *	Allow changing a boolean option and print it out
 */
int
get_bool(void *vp, WINDOW *win)
{
    bool *bp = (bool *) vp;
    int oy, ox;
    bool op_bad;

    op_bad = TRUE;
    getyx(win, oy, ox);
    waddstr(win, *bp ? "True" : "False");
    while (op_bad)	
    {
	wmove(win, oy, ox);
	wrefresh(win);
	switch (readchar())
	{
	    case 't':
	    case 'T':
		*bp = TRUE;
		op_bad = FALSE;
		break;
	    case 'f':
	    case 'F':
		*bp = FALSE;
		op_bad = FALSE;
		break;
	    case '\n':
	    case '\r':
		op_bad = FALSE;
		break;
	    case ESCAPE:
		return QUIT;
	    case '-':
		return MINUS;
	    default:
		wmove(win, oy, ox + 10);
		waddstr(win, "(T or F)");
	}
    }
    wmove(win, oy, ox);
    waddstr(win, *bp ? "True" : "False");
    waddch(win, '\n');
    return NORM;
}
Пример #7
0
int
get_bool(void *vp, WINDOW *win)
{
    int *bp = (int *) vp;
    int oy, ox;
    int op_bad;

    op_bad = TRUE;
    getyx(win, oy, ox);
    waddstr(win, *bp ? "True" : "False");
    while(op_bad)	
    {
	wmove(win, oy, ox);
	draw(win);
	switch (readchar(win))
	{
	    case 't':
	    case 'T':
		*bp = TRUE;
		op_bad = FALSE;
		break;
	    case 'f':
	    case 'F':
		*bp = FALSE;
		op_bad = FALSE;
		break;
	    case '\n':
	    case '\r':
		op_bad = FALSE;
		break;
	    case '\033':
	    case '\007':
		return QUIT;
	    case '-':
		return MINUS;
	    default:
		mvwaddstr(win, oy, ox + 10, "(T or F)");
	}
    }
    wmove(win, oy, ox);
    waddstr(win, *bp ? "True" : "False");
    waddch(win, '\n');
    return NORM;
}
Пример #8
0
int
getchar(void)
{
	char ch;
	int len;

	ch = readchar(); // read(STDIN_FILENO, &ch, 1);
	// if (len<=0) {
		// /* end of file or error */
		// return EOF;
	// }

	/*
	 * Cast through unsigned char, to prevent sign extension. This
	 * sends back values on the range 0-255, rather than -128 to 127,
	 * so EOF can be distinguished from legal input.
	 */
	return (int)(unsigned char)ch;
}
Пример #9
0
Файл: io.c Проект: ariavie/bcm
int sal_readchar(const char *prompt)
{
#ifdef INCLUDE_EDITLINE
    extern int readchar(const char *prompt);
#else
    char	buf[64];
#endif

#ifdef INCLUDE_EDITLINE
    return(readchar(prompt));
#else
    printk("%s", prompt);
    if (NULL == (sal_console_gets(buf, sizeof(buf)))) {
	return(EOF);
    } else {
	return(buf[0]);
    }
#endif
}
int block_continueread(FILETYPE f, int brace_count, char *data, int size,
		       char spacer)
{
  char c;
  int i;

  if (!data || !size)
    return RETURN_CODE_ERROR;

  data[0] = 0;

  if (brace_count == 0)
    return 0;

  /* scan block data until brace_count = 0 or space eaten up */
  i = 0;
  while (i < size - 1) {
    if ((c = readchar(f)) <= 0) {
      data[i] = 0;
      return RETURN_CODE_ERROR;
    }
 
    if (c == '{') brace_count++;
    if (c == '}') {
      if (--brace_count == 0) {
	/* read complete block, strip trailing whitespaces */
	while (i > 1 && isspace(data[i - 1]))
	  i--;
	data[i] = 0;
	return 0;
      }
    }
    if (c == spacer && brace_count == 1) {
      data[i] = 0;
      return brace_count;
    }
    data[i++] = c;
  }
  data[i] = 0;

  return brace_count;
}
Пример #11
0
static int generic_led(unsigned int l)
{
	char c;
	CSR_GPIO_OUT = l;
	printf("Is the LED on? (y/n/s)\n");
	while(1) {
		c = readchar();
		switch(c) {
			case 'y':
				CSR_GPIO_OUT = 0;
				return TEST_STATUS_PASSED;
			case 'n':
				CSR_GPIO_OUT = 0;
				return TEST_STATUS_FAILED;
			case 's':
				CSR_GPIO_OUT = 0;
				return TEST_STATUS_NOT_DONE;
		}
	}
}
Пример #12
0
int dump_init(int fd, long *id, long *extra)
{
	unsigned char c, o = 0;

	c = 0x80;

	if (write(fd, &c, 1) != 1)         /* Enable command */
                return -1;

	while (1)
		if (!readchar(fd, &c, 1)) {
			printf("%02x (%c) ", c, ((c > 32) && (c < 127)) ? c : 'x');
			o = 1;
		} else {
			if (o) {
				printf("\n");
				o = 0;
			}
		}
}
Пример #13
0
static void
expect (char *string)
{
  char *p = string;
  int c;
  int nl = 0;

  while (1)
    {
      c = readchar (timeout);

      if (echo)
	{
	  if (c == '\r' || c == '\n')
	    {
	      if (!nl)
		putchar_unfiltered ('\n');
	      nl = 1;
	    }
	  else
	    {
	      nl = 0;
	      putchar_unfiltered (c);
	    }
	  gdb_flush (gdb_stdout);
	}
      if (normal (c) == normal (*p++))
	{
	  if (*p == '\0')
	    return;
	}
      else
	{
	  p = string;

	  if (normal (c) == normal (string[0]))
	    p++;
	}
    }
}
Пример #14
0
void
inventory(struct linked_list *container, int type)
{
    int cnt;

    if (type == 0)
    {
        msg("What kind of item <%s> to inventory (* for all)?",  type_list);

        type = readchar();

        if (type == ESCAPE)
        {
            after = FALSE;
            msg("");
            return;
        }
    }

    /*
     * Get a list of items to print out. If the user selects '*', list
     * them all.
     */

    if (type == '*')
        type = 0;   /* no type passed ->use them all */

    mpos = 0;

    if ((cnt = count_bag(container, type, NULL)) == 0)
        msg("You don't have any %s.", name_type(type));
    else
    {
        apply_to_bag(container, type, NULL, baf_print_item, &type);
        end_line();
        msg("");
    }

    return;
}
Пример #15
0
int
main(void)
{
	int c;
	bool leading = true;

	while ((c = readchar()) != EOF) {
		if (isspace(c))
			/* Save whitespace. */
			savewhite(c, leading);
		else {
			/* Reprint whitespace and print regular character. */
			printwhite();
			writechar(c);
			leading = false;
		}
	}
	/* Terminate non-empty files with a newline. */
	if (!leading)
		writechar('\n');
	return (0);
}
Пример #16
0
static int test_user_abort()
{
	char c;

	puts("I: Press Q or ESC to abort boot");
	CSR_TIMER0_COUNTER = 0;
	CSR_TIMER0_COMPARE = 2*brd_desc->clk_frequency;
	CSR_TIMER0_CONTROL = TIMER_ENABLE;
	while(CSR_TIMER0_CONTROL & TIMER_ENABLE) {
		if(readchar_nonblock()) {
			c = readchar();
			if((c == 'Q')||(c == '\e')) {
				puts("I: Aborted boot on user request");
				vga_set_console(1);
				return 0;
			}
			if(c == 0x07) {
				vga_set_console(1);
				netboot();
				return 0;
			}
		}
	}
Пример #17
0
int main()
{
#ifdef EMULATION
	emu_init();
#endif
	irq_setmask(0);
	irq_enable(1);
	uart_async_init();
	banner();
	brd_init();
	cpustats_init();
	time_init();
	mem_init();
	vga_init();
	snd_init();
	pfpu_init();
	tmu_init();
	renderer_init();
	apipe_init();
	rpipe_init();
	slowout_init();
	hdlcd_init();
	ui_init();
	shell_init();
	
	while(1) {
		if(readchar_nonblock())
			shell_input(readchar());
		apipe_service();
		rpipe_service();
#ifdef EMULATION
		emu_service();
#endif
	}
	
	return 0;
}
void replay_menu()
{
    int gameCounter=get_game_counter();
    int control;
    int i;
    int option;

    if (gameCounter==0)
    {
        printf("There are no games available.\n");
        printf("Press any key to go back to main menu...");
        readchar();
    }
    else
    {
        do
        {
            printf("\nGames available to replay:\n\n");

            for (i=1; i<=gameCounter; i++)
            {
                print_game_information(i);
            }

            printf("\n(Choose an option and press enter).\nInsert 0 to return to main menu: ");

            control=scanf("%d",&option);
            clean_buffer_keyboard();

        }
        while (control ==0 || option<0 || option>gameCounter);
        if (option!=0)
        {
            loadLogs(option);
        }
    }
}
Пример #19
0
void
quit(int sig)
{
    int oy, ox;

    NOOP(sig);

    /*
     * Reset the signal in case we got here via an interrupt
     */
    if (!q_comm)
	mpos = 0;
    getyx(curscr, oy, ox);
    msg("really quit?");
    if (readchar() == 'y')
    {
	signal(SIGINT, leave);
	clear();
	mvprintw(LINES - 2, 0, "You quit with %d gold pieces", purse);
	move(LINES - 1, 0);
	refresh();
	score(purse, 1, 0);
	my_exit(0);
    }
    else
    {
	move(0, 0);
	clrtoeol();
	status();
	move(oy, ox);
	refresh();
	mpos = 0;
	count = 0;
	to_death = FALSE;
    }
}
Пример #20
0
/*
 * gethand:
 *	Which hand is the hero interested in?
 */
int
gethand()
{
    int c;

    for (;;)
    {
	if (terse)
	    msg("left or right ring? ");
	else
	    msg("left hand or right hand? ");
	if ((c = readchar()) == ESCAPE)
	    return -1;
	mpos = 0;
	if (c == 'l' || c == 'L')
	    return LEFT;
	else if (c == 'r' || c == 'R')
	    return RIGHT;
	if (terse)
	    msg("L or R");
	else
	    msg("please type L or R");
    }
}
Пример #21
0
int do_sysreq(int fd, char key, int sysreq_fd)
{
	int yn;

	if (key < 'a' || key > 'z')
		return sockprint(fd, "key out of range\r\n");

	if (sockprint(fd, "Send %c to sysreq? (y/n)\r\n", key) == -1)
		return -1;

	do
	{
		yn = readchar(fd);
	}
	while(yn != 'y' && yn != 'n' && yn != -1);

	if (yn == 'y')
	{
		if (WRITE(sysreq_fd, &key, 1) == -1)
			return sockerror(fd, "WRITE(sysreq_fd)");
	}

	return yn == -1 ? -1 : 0;
}
Пример #22
0
/*
 * buy_it:
 *	Buy the item on which the hero stands
 */
int buy_it()
{
  int wh;

  if (purse <= 0) {
    msg("You have no money.");
    return 0;
  }
  if (curprice < 0) { /* if not yet priced */
    wh = price_it();
    if (!wh) /* nothing to price */
      return 0;
    msg("Do you want to buy it? ");
    do {
      wh = readchar();
      if (isupper(wh))
        wh = tolower(wh);
      if (wh == ESCAPE || wh == 'n') {
        msg("");
        return 0;
      }
    }
    until(wh == 'y');
  }
 /*
  * The ability field is read-only
  */
 int
 get_abil(void *vp, WINDOW *win)
 {
	 int *abil = (int *) vp;
     register int oy, ox, ny, nx;
     register int op_bad;
 
     op_bad = TRUE;
     getyx(win, oy, ox);
     put_abil(abil, win);
     getyx(win, ny, nx);
     while(op_bad)	
     {
 	wmove(win, oy, ox);
 	draw(win);
 	switch (readchar(win))
 	{
 	    case '\n':
 	    case '\r':
 		op_bad = FALSE;
 		break;
 	    case '\033':
 	    case '\007':
 		return QUIT;
 	    case '-':
 		return MINUS;
 	    default:
 		mvwaddstr(win, ny, nx + 5, "(no change allowed)");
 	}
     }
     wmove(win, ny, nx + 5);
     wclrtoeol(win);
     wmove(win, ny, nx);
     waddch(win, '\n');
     return NORM;
 }
Пример #24
0
int main(int argc, char **argv)
{
	unsigned long devt;
	int ldisc;
        int type;
	long id, extra;
        int fd;
	char c;

        if (argc < 2 || argc > 4 || (argc == 4 && strcmp(argv[3], "--daemon")) || !strcmp("--help", argv[1])) {
                puts("");
                puts("Usage: inputttach <mode> <device>");
                puts("");
                puts("Modes:");
                puts("  --sunkbd        -skb   Sun Type 4 and Type 5 keyboards");
		puts("  --lkkbd         -lk    DEC LK201 / LK401 keyboards");
		puts("  --vsxxx-aa      -vs    DEC VSXXX-AA / VSXXX-GA mouse and VSXXX-AB tablet");
                puts("  --spaceorb      -orb   SpaceOrb 360 / SpaceBall Avenger");
		puts("  --spaceball     -sbl   SpaceBall 2003 / 3003 / 4000 FLX");
                puts("  --magellan      -mag   Magellan / SpaceMouse");
                puts("  --warrior       -war   WingMan Warrior");
		puts("  --stinger       -stng  Gravis Stinger");
		puts("  --mousesystems  -msc   3-button Mouse Systems mice");
		puts("  --sunmouse      -sun   3-button Sun mice");
		puts("  --microsoft     -bare  2-button Microsoft mice");
		puts("  --mshack        -ms    3-button mice in Microsoft mode");
		puts("  --mouseman      -mman  3-button Logitech and Genius mice");
		puts("  --intellimouse  -ms3   Microsoft IntelliMouse");
		puts("  --mmwheel       -mmw   Logitech mice with 4-5 buttons or wheel");
		puts("  --iforce        -ifor  I-Force joysticks and wheels");
                puts("  --h3600ts       -ipaq  Ipaq h3600 touchscreen");
		puts("  --stowawaykbd   -ipaqkbd  Stowaway keyboard");
		puts("  --ps2serkbd     -ps2ser PS/2 via serial keyboard");
		puts("  --twiddler      -twid   Handykey Twiddler chording keyboard");
		puts("  --twiddler-joy  -twidjoy  Handykey Twiddler used as a joystick");
		puts("");
                return 1;
        }

        for (type = 0; input_types[type].speed; type++) {
                if (!strncasecmp(argv[1], input_types[type].name, 16) ||
			!strncasecmp(argv[1], input_types[type].name2, 16))
                        break;
        }

	if (!input_types[type].speed) {
		fprintf(stderr, "inputattach: invalid mode\n");
		return 1;
	}

	if ((fd = open(argv[2], O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
		perror("inputattach");
		return 1;
	}

	setline(fd, input_types[type].flags, input_types[type].speed);

	if (input_types[type].flush)
		while (!readchar(fd, &c, 100));

	id = input_types[type].id;
	extra = input_types[type].extra;

	if (input_types[type].init && input_types[type].init(fd, &id, &extra)) {
		fprintf(stderr, "inputattach: device initialization failed\n");
		return 1;
	}

	ldisc = N_MOUSE;
	if(ioctl(fd, TIOCSETD, &ldisc)) {
		fprintf(stderr, "inputattach: can't set line discipline\n"); 
		return 1;
	}

	devt = input_types[type].type | (id << 8) | (extra << 16);

	if(ioctl(fd, SPIOCSTYPE, &devt)) {
		fprintf(stderr, "inputattach: can't set device type\n");
		return 1;
	}
	
	if (argc == 4 && !strcmp(argv[3],"--daemon"))
		daemon(0,0);

	read(fd, NULL, 0);

	ldisc = 0;
	ioctl(fd, TIOCSETD, &ldisc);
	close(fd);

	return 0;
}
Пример #25
0
afs_int32
ReadVolumeHeader(afs_int32 count)
{
    struct volumeHeader vh;
    int i, done;
    char tag, c;

/*  memset(&vh, 0, sizeof(vh)); */

    done = 0;
    while (!done) {
	tag = readchar();
	switch (tag) {
	case 'i':
	    vh.volumeId = ntohl(readvalue(4));
	    break;

	case 'v':
	    (void)ntohl(readvalue(4));	/* version stamp - ignore */
	    break;

	case 'n':
	    for (i = 0, c = 'a'; c != '\0'; i++) {
		vh.volumeName[i] = c = readchar();
	    }
	    vh.volumeName[i] = c;
	    break;

	case 's':
	    vh.inService = ntohl(readvalue(1));
	    break;

	case 'b':
	    vh.blessed = ntohl(readvalue(1));
	    break;

	case 'u':
	    vh.uniquifier = ntohl(readvalue(4));
	    break;

	case 't':
	    vh.volType = ntohl(readvalue(1));
	    break;

	case 'p':
	    vh.parentVol = ntohl(readvalue(4));
	    break;

	case 'c':
	    vh.cloneId = ntohl(readvalue(4));
	    break;

	case 'q':
	    vh.maxQuota = ntohl(readvalue(4));
	    break;

	case 'm':
	    vh.minQuota = ntohl(readvalue(4));
	    break;

	case 'd':
	    vh.diskUsed = ntohl(readvalue(4));
	    break;

	case 'f':
	    vh.fileCount = ntohl(readvalue(4));
	    break;

	case 'a':
	    vh.accountNumber = ntohl(readvalue(4));
	    break;

	case 'o':
	    vh.owner = ntohl(readvalue(4));
	    break;

	case 'C':
	    vh.creationDate = ntohl(readvalue(4));
	    break;

	case 'A':
	    vh.accessDate = ntohl(readvalue(4));
	    break;

	case 'U':
	    vh.updateDate = ntohl(readvalue(4));
	    break;

	case 'E':
	    vh.expirationDate = ntohl(readvalue(4));
	    break;

	case 'B':
	    vh.backupDate = ntohl(readvalue(4));
	    break;

	case 'O':
	    for (i = 0, c = 'a'; c != '\0'; i++) {
		vh.message[i] = c = readchar();
	    }
	    vh.volumeName[i] = c;
	    break;

	case 'W':
	    vh.weekCount = ntohl(readvalue(2));
	    for (i = 0; i < vh.weekCount; i++) {
		vh.weekUse[i] = ntohl(readvalue(4));
	    }
	    break;

	case 'M':
	    for (i = 0, c = 'a'; c != '\0'; i++) {
		vh.motd[i] = c = readchar();
	    }
	    break;

	case 'D':
	    vh.dayUseDate = ntohl(readvalue(4));
	    break;

	case 'Z':
	    vh.dayUse = ntohl(readvalue(4));
	    break;

	case 'V':
	    readvalue(4); /*volUpCounter*/
	    break;

	default:
	    done = 1;
	    break;
	}
    }

    return ((afs_int32) tag);
}
Пример #26
0
int main(){
#ifdef LOCAL
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif

	memset(table, 0, sizeof(table));
	memset(context, 0, sizeof(context));
	memset(contextcode, 0, sizeof(contextcode));

	// input table
	while(1){
		char character = readchar();
		if(character == '*')
			break;
		else if(isalpha(character)){
			scanf("%s", table[character - 'A']);
		} else {
			scanf("%s", table[character - '0' + 26]);
		}
	}
	// printtable();

	// input context
	while(1){
		scanf("%s", context[contextcount]);
		if(context[contextcount][0] == '*')
			break;
		contextcount ++;
	}
	// printcontext();

	// convert context to code
	for(int i = 0; i < contextcount; i ++){
		int location = 0;
		for(int j = 0; j < 10 && context[i][j] != '\0'; j++){
			int index = 0;
			if(isalpha(context[i][j]))
				index = context[i][j] - 'A';
			else
				index = context[i][j] - '0' + 26;
			for(int m = 0; m < 6 && (table[index][m] != '\0'); m++){
				contextcode[i][location] = table[index][m];
				location ++;
			}
		}
	}
	// printcontextcode();

	// initialize order
	for(int i = 0; i < contextcount; i ++){
		order[i] = i;
	}
	for(int i = contextcount; i < 100; i++){
		order[i] = -1;
	}

	sortcontext();
	// printfsortedcontextcode();

	// input words
	while(1){
		scanf("%s", word);
		if(word[0] == '*')
			break;

		int multiple = 0, question = 1;
		int start = 0, end = contextcount - 1, mid = 0;
		while(start <= end){
			mid = (start + end)/2;
			if(strcmp(contextcode[order[mid]], word) == 0){
				question = 0;
				while(mid - 1 >= 0 && strcmp(contextcode[order[mid-1]], word) == 0){
					multiple = 1;
					mid --;
				}
				printf("%s", context[order[mid]]);

				if(multiple){
					printf("!");
				} else if (mid + 1 < contextcount && strcmp(contextcode[order[mid+1]], word) == 0){
					printf("!");
				}
				printf("\n");
				break;
			} else if (strcmp(contextcode[order[mid]], word) < 0){
				start = mid + 1;		
			} else {
				end = mid - 1;
			}
		}
		if(question){
			// int wordlength = strlen(word);
			// char smallword[]
			// for(int i = 1; i + wordlength < 60 && i < wordlength - 1; i++){
			// 	for(int j = 0; j < wordlength - i; j++){

			// 	}
			// }
			if (strcmp(contextcode[order[mid]], word) < 0){
				start = mid;
				end = mid + 1;
			} else {
				start = mid - 1;
				end = mid;
			}

			int least = -1, extra = 100, lessindex, extraindex;
			for(int i = start; i >= 0; i--){
				least = getless(contextcode[order[i]], word, strlen(contextcode[order[i]]), strlen(word));
				if(least != -1){
					lessindex = i;
					break;
				}
			}
			for(int i = end; i < contextcount; i++){
				int extranum = getextra(contextcode[order[i]], word, strlen(contextcode[order[i]]), strlen(word));
				if(extranum == -1){
					break;
				} else if (extranum < extra){
					extraindex = i;
					extra = extranum;
				}
			}
			if(least != -1 && extra != 100){
				if(least < extra){
					printf("%s", context[order[lessindex]]);
				} else {
					printf("%s", context[order[extraindex]]);
				}
			} else if (least != -1){
				printf("%s", context[order[lessindex]]);
			} else if(extra != 100){
				printf("%s", context[order[extraindex]]);
			}
			printf("?\n");
		}
		
	}
	return 0;
}
Пример #27
0
//**************************************************************************************************
// main
//
int main(int argc, char** argv)
{
   char cc;
   char indat[256*1024];    // huge because I am a lazy man
   char *indatloc;
   int readcnt;
   int totallen;

   unsigned numcams;


   // process user cli
   gFlagUserCliValid=0;
   gFlagUserCliHelp=0;
   gFlagUseBluetooth=0;
   if(0 != parseargs(argc,argv))
   {
      printusage(argv[0]);
      exit(1);
   }
   if(0 == gFlagUserCliValid)
   {
      printusage(argv[0]);
      exit(1);
   }
   if(0 != gFlagUserCliHelp)
   {
      printhelp(argv[0]);
      exit(0);
   }

   if(0 == gFlagUseBluetooth)
   {
      // russ: this is a little inelegant
      gCamin = fopen("/dev/ttyACM0","r");
      if(0 == gCamin)
      {
         fprintf(stderr, "Could not open /dev/ttyACM0 for reading; trying /dev/ttyACM1\n");
         gCamin = fopen("/dev/ttyACM1", "r");
         if(0 == gCamin)
         {
            fprintf(stderr, "Could not open /dev/ttyACM1 for reading\n");
            return -1;
         }
         gCamout = fopen("/dev/ttyACM1","w");
         if(0 == gCamout)
         {
            fprintf(stderr, "Could not open /dev/ttyACM1 for writing\n");
            fclose(gCamin);
            return -1;
         }
      }
      else
      {
         gCamout = fopen("/dev/ttyACM0","w");
         if(0 == gCamout)
         {
            fprintf(stderr, "Could not open /dev/ttyACM0 for writing\n");
            fclose(gCamin);
            return -1;
         }
      }
   }
   else
   {
      // use bluetooth
      gCamin = fopen("/dev/rfcomm0", "r");
      if(0 == gCamin)
      {
         fprintf(stderr, "Could not open /dev/rfcomm0 for reading\n");
         return -1;
      }
      gCamout = fopen("/dev/rfcomm0","w");
      if(0 == gCamout)
      {
         fprintf(stderr, "Could not open /dev/rfcomm0 for writing\n");
         fclose(gCamin);
         return -1;
      }
   }


   // find out if the device has 1 or 2 cameras
   fputc((char)SYMBOL_SOF,gCamout);
   dbgPrintOp("tx: 0x%02X\n", (unsigned char)SYMBOL_SOF);
   fputc((char)OPCODE_REQ_NUM_CAMS,gCamout);
   dbgPrintOp("tx: 0x%02X\n", (unsigned char)OPCODE_REQ_NUM_CAMS);
   fflush(gCamout);

   readuntilchar(gCamin,SYMBOL_SOF);
   dbgPrintOp("tx: 0x%02X\n", (unsigned char)SYMBOL_SOF);
   indat[0] = readchar(gCamin);
   dbgPrintOp("tx: 0x%02X\n", (unsigned char)indat[0]);
   if(OPCODE_RESP_NUM_CAMS != (unsigned char)indat[0])
   {
      assert(OPCODE_RESP_NUM_CAMS == (unsigned char)indat[0]);
   }
   numcams = readchar(gCamin);
   dbgPrintOp("tx: 0x%02X\n", (unsigned char)numcams);
   assert((0 < numcams) && (MAX_CAMS >= numcams));

   // print numcams on stdout for pipe interface
   printf("%c",SYMBOL_SOF);
   printf("%c",OPCODE_RESP_NUM_CAMS);
   printf("%c",(unsigned char)numcams);
   fflush(stdout);


   while(1)
   {
      // TODO: should be a function
      fputc((char)SYMBOL_SOF,gCamout);
      fputc((char)OPCODE_SINGLE_FRAME,gCamout);
      fflush(gCamout);

      readuntilchar(gCamin,SYMBOL_SOF);
      dbgPrintOp("rx: 0x%02X\n", (unsigned char)SYMBOL_SOF);
      printf("%c",SYMBOL_SOF);
      indat[0] = readchar(gCamin);
      dbgPrintOp("rx: 0x%02X\n", (unsigned char)indat[0]);
      printf("%c",indat[0]);
      if(OPCODE_FRAME != (unsigned char)indat[0])
      {
         assert(OPCODE_FRAME == (unsigned char)indat[0]);
      }


      totallen=0;
      indatloc=indat;
      while(FRAME_LEN*numcams > totallen)
      {
         readcnt = fread(indatloc,1,(FRAME_LEN*numcams)-totallen,gCamin);
         totallen+=readcnt;
         indatloc+=readcnt;
      }
      *indatloc = '\0';


      printf("%s",indat);
      fflush(stdout);


      cc = getch();
      // look for ESC key
      if(ESC_KEY == cc)
      {
         break;
      }
   }


   // tell listening program that we're done here
   printf("%c",SYMBOL_SOF);
   printf("%c",SYMBOL_EXIT);
   fflush(stdout);


   fclose(gCamin);
   fclose(gCamout);
   return 0;
}
Пример #28
0
void
sell(struct thing *tp)
{
    struct linked_list  *item;
    int i, j, min_worth, nitems, chance, which_item, w;
    char goods;
    struct object   *obj;
    char    buffer[2 * LINELEN];
    char    dbuf[2 * LINELEN];

    struct
    {
        int which;
        int plus1, plus2;
        int count;
        int worth;
        int flags;
        char    *name;
    }
    selection[SELL_ITEMS];

    int effective_purse = ((player.t_ctype == C_PALADIN) ?
                   (9 * purse / 10) : purse);

    min_worth = -1;     /* hope item is never worth less than this */
    item = find_mons(tp->t_pos.y, tp->t_pos.x); /* Get pointer to monster */

    /* Select the items */

    nitems = rnd(6) + 5;

    switch (rnd(6))
    {
        /* Armor */
        case 0:
        case 1:
            goods = ARMOR;
            for (i = 0; i < nitems; i++)
            {
                chance = rnd(100);

                for (j = 0; j < maxarmors; j++)
                    if (chance < armors[j].a_prob)
                        break;

                if (j == maxarmors)
                {
                    debug("Picked a bad armor %d", chance);
                    j = 0;
                }

                selection[i].which = j;
                selection[i].count = 1;

                if (rnd(100) < 40)
                    selection[i].plus1 = rnd(5) + 1;
                else
                    selection[i].plus1 = 0;

                selection[i].name = armors[j].a_name;

                switch (luck)
                {
                    case 0: break;
                    case 1:
                        if (rnd(3) == 0)
                        {
                            selection[i].flags |=  ISCURSED;
                            selection[i].plus1 =  -1 - rnd(5);
                        }
                        break;

                    default:
                        if (rnd(luck))
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 =  -1 - rnd(5);
                        }
                        break;
                }

                /* Calculate price */

                w = armors[j].a_worth;
                w *= (1 + luck + (10 * selection[i].plus1));
                w = (w / 2) + (roll(6, w) / 6);
                selection[i].worth = max(w, 25);

                if (min_worth > selection[i].worth || i == 1)
                    min_worth = selection[i].worth;
            }
            break;

            /* Weapon */
        case 2:
        case 3:
            goods = WEAPON;
            for (i = 0; i < nitems; i++)
            {
                selection[i].which = rnd(maxweapons);
                selection[i].count = 1;

                if (rnd(100) < 35)
                {
                    selection[i].plus1 = rnd(3);
                    selection[i].plus2 = rnd(3);
                }
                else
                {
                    selection[i].plus1 = 0;
                    selection[i].plus2 = 0;
                }

                if (weaps[selection[i].which].w_flags & ISMANY)
                    selection[i].count = rnd(15) + 8;

                selection[i].name = weaps[selection[i].which].w_name;

                switch (luck)
                {
                    case 0: break;
                    case 1:
                        if (rnd(3) == 0)
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 =  -rnd(3);
                            selection[i].plus2 =  -rnd(3);
                        }
                        break;

                    default:
                        if (rnd(luck))
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 =  -rnd(3);
                            selection[i].plus2 =  -rnd(3);
                        }
                        break;
                }

                w = weaps[selection[i].which].w_worth * selection[i].count;
                w *= (1 + luck + (10 * selection[i].plus1 +
                          10 * selection[i].plus2));
                w = (w / 2) + (roll(6, w) / 6);
                selection[i].worth = max(w, 25);

                if (min_worth > selection[i].worth || i == 1)
                    min_worth = selection[i].worth;
            }
            break;

            /* Staff or wand */
        case 4:
            goods = STICK;

            for (i = 0; i < nitems; i++)
            {
                selection[i].which = pick_one(ws_magic, maxsticks);
                selection[i].plus1 = rnd(11) + 5;
                selection[i].count = 1;
                selection[i].name = ws_magic[selection[i].which].mi_name;

                switch (luck)
                {
                    case 0: break;
                    case 1:
                        if (rnd(3) == 0)
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 = 1;
                        }
                        break;

                    default:
                        if (rnd(luck))
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 = 1;
                        }
                }

                w = ws_magic[selection[i].which].mi_worth;
                w += (luck + 1) * 20 * selection[i].plus1;
                w = (w / 2) + (roll(6, w) / 6);
                selection[i].worth = max(w, 25);

                if (min_worth > selection[i].worth || i == 1)
                    min_worth = selection[i].worth;
            }
            break;

            /* Ring */

        case 5:
            goods = RING;
            for (i = 0; i < nitems; i++)
            {
                selection[i].which = pick_one(r_magic, maxrings);
                selection[i].plus1 = rnd(2) + 1;
                selection[i].count = 1;

                if (rnd(100) < r_magic[selection[i].which].mi_bless + 10)
                    selection[i].plus1 += rnd(2) + 1;

                selection[i].name = r_magic[selection[i].which].mi_name;

                switch (luck)
                {
                    case 0: break;
                    case 1:
                        if (rnd(3) == 0)
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 =  -1 - rnd(2);
                        }
                        break;

                    default:
                        if (rnd(luck))
                        {
                            selection[i].flags |= ISCURSED;
                            selection[i].plus1 =  -1 - rnd(2);
                        }
                }

                w = r_magic[selection[i].which].mi_worth;

                switch(selection[i].which)
                {
                    case R_DIGEST:
                        if (selection[i].plus1 > 2)
                            selection[i].plus1 = 2;
                        else if (selection[i].plus1 < 1)
                            selection[i].plus1 = 1;
                    /* fall thru here to other cases */
                    case R_ADDSTR:
                    case R_ADDDAM:
                    case R_PROTECT:
                    case R_ADDHIT:
                    case R_ADDINTEL:
                    case R_ADDWISDOM:
                        if (selection[i].plus1 > 0)
                            w += selection[i].plus1 * 50;
                }

                w *= (1 + luck);
                w = (w / 2) + (roll(6, w) / 6);
                selection[i].worth = max(w, 25);

                if (min_worth > selection[i].worth * selection[i].count)
                    min_worth = selection[i].worth;
            }
    }

    /* See if player can afford an item */

    if (min_worth > effective_purse)
    {
        msg("The %s eyes your small purse and departs.",
            monsters[nummonst].m_name);

        /* Get rid of the monster */

        killed(NULL, item, NOMESSAGE, NOPOINTS);

        return;
    }

    /* Display the goods */

    msg("The %s shows you his wares.", monsters[nummonst].m_name);
    wstandout(cw);
    mvwaddstr(cw, 0, mpos, morestr);
    wstandend(cw);
    wrefresh(cw);
    wait_for(' ');
    msg("");
    clearok(cw, TRUE);
    touchwin(cw);

    wclear(hw);
    touchwin(hw);

    for (i = 0; i < nitems; i++)
    {
        if (selection[i].worth > effective_purse)
            continue;

        wmove(hw, i + 2, 0);
        sprintf(dbuf, "[%c] ", ('a' + i));

        switch(goods)
        {
            case ARMOR:
                strcat(dbuf, "Some ");
                break;
            case WEAPON:
                if (selection[i].count == 1)
                    strcat(dbuf, "A ");
                else
                {
                    sprintf(buffer, "%2d ", selection[i].count);
                    strcat(dbuf, buffer);
                }
                break;

            case STICK:
                strcat(dbuf, "A ");
                strcat(dbuf, ws_type[selection[i].which]);
                strcat(dbuf, " of ");
                break;

            case RING:
                strcat(dbuf, "A ring of ");
                break;
        }

        strcat(dbuf, selection[i].name);

        if (selection[i].count > 1)
            strcat(dbuf, "s");

        sprintf(buffer, "%-50s Price:  %d", dbuf, selection[i].worth);
        waddstr(hw, buffer);
    }

    sprintf(buffer, "Purse:  %d", purse);
    mvwaddstr(hw, nitems + 3, 0, buffer);
    mvwaddstr(hw, 0, 0, "How about one of the following goods? ");
    wrefresh(hw);

    /* Get rid of the monster */

    killed(NULL, item, NOMESSAGE, NOPOINTS);

    which_item = (short) ((readchar() & 0177) - 'a');

    while (which_item < 0 || which_item >= nitems ||
        selection[which_item].worth > effective_purse)
    {
        if (which_item == (short) ESCAPE - (short) 'a')
            return;

        mvwaddstr(hw, 0, 0, "Please enter one of the listed items: ");
        wrefresh(hw);
        which_item = (short) ((readchar() & 0177) - 'a');
    }

    if (purse > selection[which_item].worth)
         purse -= selection[which_item].worth;
    else
         purse = 0L;

    item = spec_item(goods, selection[which_item].which,
          selection[which_item].plus1, selection[which_item].plus2);

    obj = OBJPTR(item);

    if (selection[which_item].count > 1)
    {
        obj->o_count = selection[which_item].count;
        obj->o_group = ++group;
    }

    /* If a stick or ring, let player know the type */

    switch (goods)
    {
        case STICK: know_items[TYP_STICK][selection[which_item].which] = TRUE;
                    break;
        case RING:  know_items[TYP_RING][selection[which_item].which] = TRUE;
                    break;
    }

    if (add_pack(item, MESSAGE) == FALSE)
    {
        obj->o_pos = hero;
        fall(&player, item, TRUE, FALSE);
    }
}
Пример #29
0
expr(int a)
{	/* term | term dyadic expr |  */
	int	rc;
	WORD	lhs;

	rdc();
	reread();
	rc=term(a);
	while (rc) {
		lhs = expv;
		switch ((int)readchar()) {

		case '+':
			term(a|1);
			expv += lhs;
			break;

		case '-':
			term(a|1);
			expv = lhs - expv;
			break;

		case '#':
			term(a|1);
			expv = round(lhs,expv);
			break;

		case '*':
			term(a|1);
			expv *= lhs;
			break;

		case '%':
			term(a|1);
			if(expv != 0)
				expv = lhs/expv;
			else{
				if(lhs)
					expv = 1;
				else
					expv = 0;
			}
			break;

		case '&':
			term(a|1);
			expv &= lhs;
			break;

		case '|':
			term(a|1);
			expv |= lhs;
			break;

		case ')':
			if ((a&2)==0)
				error("unexpected `)'");

		default:
			reread();
			return(rc);
		}
	}
	return(rc);
}
Пример #30
0
item(int a)
{	/* name [ . local ] | number | . | ^  | <register | 'x | | */
	char	*base;
	char	savc;
	uvlong e;
	Symbol s;
	char gsym[MAXSYM], lsym[MAXSYM];

	readchar();
	if (isfileref()) {
		readfname(gsym);
		rdc();			/* skip white space */
		if (lastc == ':') {	/* it better be */
			rdc();		/* skip white space */
			if (!getnum(readchar))
				error("bad number");
			if (expv == 0)
				expv = 1;	/* file begins at line 1 */
			expv = file2pc(gsym, expv);
			if (expv == -1)
				error("%r");
			return 1;
		}
		error("bad file location");
	} else if (symchar(0)) {	
		readsym(gsym);
		if (lastc=='.') {
			readchar();	/* ugh */
			if (lastc == '.') {
				lsym[0] = '.';
				readchar();
				readsym(lsym+1);
			} else if (symchar(0)) {
				readsym(lsym);
			} else
				lsym[0] = 0;
			if (localaddr(cormap, gsym, lsym, &e, rget) < 0)
				error("%r");
			expv = e;
		}
		else {
			if (lookup(0, gsym, &s) == 0)
				error("symbol not found");
			expv = s.value;
		}
		reread();
	} else if (getnum(readchar)) {
		;
	} else if (lastc=='.') {	
		readchar();
		if (!symchar(0) && lastc != '.') {
			expv = dot;
		} else {
			if (findsym(rget(cormap, mach->pc), CTEXT, &s) == 0)
				error("no current function");
			if (lastc == '.') {
				lsym[0] = '.';
				readchar();
				readsym(lsym+1);
			} else
				readsym(lsym);
			if (localaddr(cormap, s.name, lsym, &e, rget) < 0)
				error("%r");
			expv = e;
		}	
		reread();
	} else if (lastc=='"') {
		expv=ditto;
	} else if (lastc=='+') {
		expv=inkdot(dotinc);
	} else if (lastc=='^') {
		expv=inkdot(-dotinc);
	} else if (lastc=='<') {
		savc=rdc();
		base = regname(savc);
		expv = rget(cormap, base);
	}
	else if (lastc=='\'')
		expv = ascval();
	else if (a)
		error("address expected");
	else {	
		reread();
		return(0);
	}
	return(1);
}