コード例 #1
0
/*
 * mvgetn_wstr --
 *  Get a string (of maximum n) characters from stdscr starting at (y, x).
 */
int
mvgetn_wstr(int y, int x, wchar_t *wstr, int n)
{
#ifndef HAVE_WCHAR
	return ERR;
#else
	return mvwgetn_wstr(stdscr, y, x, wstr, n);
#endif /* HAVE_WCHAR */
}
コード例 #2
0
ファイル: enigma.c プロジェクト: Hafting/enigma
/* change the highlighted code wheel */
void next_wheel(machine *m, ui_info *ui) {
	if (ui->chosen_wheel < 0) return;
	wheelslot *sl = &m->slot[ui->chosen_wheel];
	if (sl->type == T_WHEEL) {
		/* Change an ordinary wheel or reflector, to another allowed wheel */
		do {
			sl->w = sl->w->next_in_set;
		} while(!sl->w->allow_slot[ui->chosen_wheel]);
		draw_wheel(m, ui, ui->chosen_wheel);
	} else {
		/* change plugboard- / crossbar-settings, or rewire a wheel */
		wint_t s[m->alphabet_len * 2];
		echo();
		char *err = ""; /* Short msg if they screw up */
		for (bool done = false; !done; ) {
			wclear(ui->w_pop);
			if (sl->type == T_PAIRSWAP) {
				wprintw(ui->w_pop, "%sGive plugboard swaps in the format AX CF ...\nor just enter for the identity mapping\n", err);
				mvwgetn_wstr(ui->w_pop, 2, 0, s, (m->alphabet_len / 2) * 3);
				identity_map(m, sl->w);
				wchar_t *l1=s, *l2;
				done = true; /* optimistic */
				do { /* Each iteration parses one stecker pair */
					while (*l1 == L' ') ++l1;
					if (*l1) { /*  if we didn't hit \0 */
						l2 = l1 + 1;
						int i1 = lookup(*l1, m->alphabet);
						int i2 = lookup(*l2, m->alphabet);
						if (i1 == -1 || i2 == -1) {
							*l1 = 0;
							err = "Letter not in machine alphabet. ";
							done = false;
						} else {
							/* Got a pair, and it is valid! Set up the encoding & decoding */						
							sl->w->encode[i1] = i2;
							sl->w->encode[i2] = i1;
							sl->w->decode[i1] = i2;
							sl->w->decode[i2] = i1;
							l1 = l2 + 1;
						}
					}
				} while (*l1);
			} else {
				wprintw(ui->w_pop, "Type the new mapping like XYZABCDE...\nor just enter for the identity mapping\n");
				mvwgetn_wstr(ui->w_pop, 2, 0, s, m->alphabet_len);
				identity_map(m, sl->w);
				done = true; /* Unless we get a bad string */
				int i = 0;
				wchar_t *l = s;
				while (*l && i < m->alphabet_len && done) {
					int c = lookup(*l, m->alphabet);
					if (c == -1) {
						err = "Letter not in machine alphabet. ";
						done = false;
					} else {
						sl->w->encode[i] = c;
						sl->w->decode[c] = i;
					}
					++i;
					++l;
				}
				/* More sanity checking */
				if (done) {
					if (*l) {
						err = "Too many characters. ";
						done = false;
					} else if (i < m->alphabet_len) {
						err = "Too few characters. ";
						done = false;
					}
				}

			}
		}
		noecho();
		redrawwin(ui->w_code);
		wnoutrefresh(ui->w_code);	

	}
	wnoutrefresh(ui->w_wheels);
	step_cleanup(m);
}
コード例 #3
0
ファイル: test_get_wstr.c プロジェクト: ysleu/RTL8685
static int
test_get_wstr(int level, char **argv, WINDOW *strwin)
{
    WINDOW *txtbox = 0;
    WINDOW *txtwin = 0;
    FILE *fp;
    int ch;
    int rc;
    int txt_x = 0, txt_y = 0;
    int base_y;
    int flavor = 0;
    int limit = getmaxx(strwin) - 5;
    int actual;
    wint_t buffer[MAX_COLS];

    if (argv[level] == 0) {
	beep();
	return FALSE;
    }

    if (level > 1) {
	txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
	box(txtbox, 0, 0);
	wnoutrefresh(txtbox);

	txtwin = derwin(txtbox,
			getmaxy(txtbox) - 2,
			getmaxx(txtbox) - 2,
			1, 1);
	base_y = 0;
    } else {
	txtwin = stdscr;
	base_y = BASE_Y;
    }

    keypad(txtwin, TRUE);	/* enable keyboard mapping */
    (void) cbreak();		/* take input chars one at a time, no wait for \n */
    (void) noecho();		/* don't echo input */

    txt_y = base_y;
    txt_x = 0;
    wmove(txtwin, txt_y, txt_x);

    if ((fp = fopen(argv[level], "r")) != 0) {
	while ((ch = fgetc(fp)) != EOF) {
	    if (waddch(txtwin, UChar(ch)) != OK) {
		break;
	    }
	}
	fclose(fp);
    } else {
	wprintw(txtwin, "Cannot open:\n%s", argv[1]);
    }

    wmove(txtwin, txt_y, txt_x);
    actual = ShowFlavor(strwin, txtwin, flavor, limit);
    while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
	switch (ch) {
	case KEY_DOWN:
	case 'j':
	    if (txt_y < getmaxy(txtwin) - 1) {
		MovePrompt(txtwin, actual, ++txt_y, txt_x);
	    } else {
		beep();
	    }
	    break;
	case KEY_UP:
	case 'k':
	    if (txt_y > base_y) {
		MovePrompt(txtwin, actual, --txt_y, txt_x);
	    } else {
		beep();
	    }
	    break;
	case KEY_LEFT:
	case 'h':
	    if (txt_x > 0) {
		MovePrompt(txtwin, actual, txt_y, --txt_x);
	    } else {
		beep();
	    }
	    break;
	case KEY_RIGHT:
	case 'l':
	    if (txt_x < getmaxx(txtwin) - 1) {
		MovePrompt(txtwin, actual, txt_y, ++txt_x);
	    } else {
		beep();
	    }
	    break;

	case 'w':
	    test_get_wstr(level + 1, argv, strwin);
	    if (txtbox != 0) {
		touchwin(txtbox);
		wnoutrefresh(txtbox);
	    } else {
		touchwin(txtwin);
		wnoutrefresh(txtwin);
	    }
	    break;

	case '-':
	    if (limit > 0) {
		actual = ShowFlavor(strwin, txtwin, flavor, --limit);
		MovePrompt(txtwin, actual, txt_y, txt_x);
	    } else {
		beep();
	    }
	    break;

	case '+':
	    actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
	    MovePrompt(txtwin, actual, txt_y, txt_x);
	    break;

	case '<':
	    if (flavor > 0) {
		actual = ShowFlavor(strwin, txtwin, --flavor, limit);
		MovePrompt(txtwin, actual, txt_y, txt_x);
	    } else {
		beep();
	    }
	    break;

	case '>':
	    if (flavor + 1 < eMaxFlavor) {
		actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
		MovePrompt(txtwin, actual, txt_y, txt_x);
	    } else {
		beep();
	    }
	    break;

	case ':':
	    actual = ShowFlavor(strwin, txtwin, flavor, limit);
	    *buffer = '\0';
	    rc = ERR;
	    echo();
	    (void) wattrset(txtwin, A_REVERSE);
	    switch (flavor) {
	    case eGetStr:
		if (txtwin != stdscr) {
		    wmove(txtwin, txt_y, txt_x);
		    rc = wget_wstr(txtwin, buffer);
		} else {
		    move(txt_y, txt_x);
		    rc = get_wstr(buffer);
		}
		break;
	    case eGetNStr:
		if (txtwin != stdscr) {
		    wmove(txtwin, txt_y, txt_x);
		    rc = wgetn_wstr(txtwin, buffer, limit);
		} else {
		    move(txt_y, txt_x);
		    rc = getn_wstr(buffer, limit);
		}
		break;
	    case eMvGetStr:
		if (txtwin != stdscr) {
		    rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer);
		} else {
		    rc = mvget_wstr(txt_y, txt_x, buffer);
		}
		break;
	    case eMvGetNStr:
		if (txtwin != stdscr) {
		    rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit);
		} else {
		    rc = mvgetn_wstr(txt_y, txt_x, buffer, limit);
		}
		break;
	    case eMaxFlavor:
		break;
	    }
	    noecho();
	    (void) wattrset(txtwin, A_NORMAL);
	    wprintw(strwin, "%d", rc);
	    (void) waddwstr(strwin, (wchar_t *) buffer);
	    wnoutrefresh(strwin);
	    break;
	default:
	    beep();
	    break;
	}
	doupdate();
    }
    if (level > 1) {
	delwin(txtwin);
	delwin(txtbox);
    }
    return TRUE;
}