/** * 式 * * @param[in] calc calcinfo構造体 * @return 値 */ static dbl expression(calcinfo *calc) { dbl x = 0.0; /* 値 */ dbglog("start"); if (is_error(calc)) return EX_ERROR; x = term(calc); dbglog(calc->fmt, x); while (true) { if (calc->ch == '+') { readch(calc); x += term(calc); } else if (calc->ch == '-') { readch(calc); x -= term(calc); } else { break; } } dbglog(calc->fmt, x); return x; }
/** * 因子 * * @param[in] calc calcinfo構造体 * @return 値 */ static dbl factor(calcinfo *calc) { dbl x = 0.0; /* 値 */ dbglog("start"); if (is_error(calc)) return EX_ERROR; if (calc->ch != '(') return token(calc); readch(calc); x = expression(calc); if (calc->ch != ')') { /* シンタックスエラー */ set_errorcode(calc, E_SYNTAX); return EX_ERROR; } readch(calc); dbglog(calc->fmt, x); return x; }
static MNCL_DATA * word(MNCL_DATA_PARSE_CTX *ctx, int scan) { const char *s = ctx->s + ctx->i; if (*s == 'n' && ctx->i + 4 <= ctx->size && !strncmp(s, "null", 4)) { int i; MNCL_DATA *result; for (i = 0; i < 4; ++i) { readch(ctx); } if (scan) { return mncl_data_ok; } result = (MNCL_DATA *)malloc(sizeof(MNCL_DATA)); if (!result) { snprintf(error_str, 512, "%d:%d: Out of memory", ctx->line, ctx->col); return NULL; } result->tag = MNCL_DATA_NULL; return result; } else if (*s == 't' && ctx->i + 4 <= ctx->size && !strncmp(s, "true", 4)) { int i; MNCL_DATA *result; for (i = 0; i < 4; ++i) { readch(ctx); } if (scan) { return mncl_data_ok; } result = (MNCL_DATA *)malloc(sizeof(MNCL_DATA)); if (!result) { snprintf(error_str, 512, "%d:%d: Out of memory", ctx->line, ctx->col); return NULL; } result->tag = MNCL_DATA_BOOLEAN; result->value.boolean = 1; return result; } else if (*s == 'f' && ctx->i + 5 <= ctx->size && !strncmp(s, "false", 5)) { int i; MNCL_DATA *result; for (i = 0; i < 5; ++i) { readch(ctx); } if (scan) { return mncl_data_ok; } result = (MNCL_DATA *)malloc(sizeof(MNCL_DATA)); if (!result) { snprintf(error_str, 512, "%d:%d: Out of memory", ctx->line, ctx->col); return NULL; } result->tag = MNCL_DATA_BOOLEAN; result->value.boolean = 0; return result; } snprintf(error_str, 512, "%d:%d: Expected value", ctx->line, ctx->col); return NULL; }
static void skip_until_mark(void) { int ch; for (;;) { ch = readch(); switch (ch) { case 'M': return; case EOF: errx(1, "mark not found"); return; case 'l': case 'L': case 's': case 'S': case ':': case ';': case '<': case '>': case '=': (void)readreg(); if (readch() == 'e') (void)readreg(); else unreadch(); break; case '[': free(read_string(&bmachine.readstack[bmachine.readsp])); break; case '!': switch (ch = readch()) { case '<': case '>': case '=': (void)readreg(); if (readch() == 'e') (void)readreg(); else unreadch(); break; default: free(readline()); break; } break; default: break; } } }
/* The buffer dst must have been pre-sized by a call to * mncl_data_str_size. This function returns 0 on failure, >= 0 on * success. */ static int mncl_data_strcpy(char *dst, MNCL_DATA_PARSE_CTX *ctx) { int c = readch(ctx); /* Skip the leading quote */ while(1) { c = readch(ctx); if (c == '\"') { *dst++ = '\0'; return 1; } else if (c == '\\') { int ch; c = readch(ctx); switch(c) { case 'u': ch = hex_decode(ctx); if (ch < 0) { return 0; } else if (ch < 0x80) { *dst++ = ch; } else if (ch < 0x800) { *dst++ = (((ch >> 6) & 0xff) | 0xC0); *dst++ = (ch & 0x3f) | 0x80; } else { *dst++ = (((ch >> 12) & 0xff) | 0xe0); *dst++ = ((ch >> 6) & 0x3f) | 0x80; *dst++ = (ch & 0x3f) | 0x80; } break; case '\"': case '\\': case '/': *dst++ = c; break; case 'b': *dst++ = '\b'; break; case 'f': *dst++ = '\f'; break; case 'n': *dst++ = '\n'; break; case 'r': *dst++ = '\r'; break; case 't': *dst++ = '\t'; break; default: snprintf(error_str, 512, "%d:%d: Illegal string escape '%c'", ctx->line, ctx->col, c); return 0; }
int main() { char c,ext; c=readch(); // <---- getch() if(c!=27) printf("Caracter: %c - ASCII: %d\n\n",c,c); else { ext=readch(); printf("Caracteres: %d - %d\n\n",c,ext); ext=readch(); printf("Caracteres: %d - %d\n\n",c,ext); } }
CARD getcard() { int c, c1; for (;;) { while ((c = readch()) == '\n' || c == '\r' || c == ' ') continue; if (islower(c)) c = toupper(c); if (c == killchar() || c == erasechar()) return -1; addstr(unctrl(c)); clrtoeol(); switch (c) { case '1': case '2': case '3': case '4': case '5': case '6': c -= '0'; break; case '0': case 'P': case 'p': c = 0; break; default: beep(); addch('\b'); if (!isprint(c)) addch('\b'); c = -1; break; } refresh(); if (c >= 0) { while ((c1 = readch()) != '\r' && c1 != '\n' && c1 != ' ') if (c1 == killchar()) return -1; else if (c1 == erasechar()) { addch('\b'); clrtoeol(); refresh(); goto cont; } else beep(); return c; } cont: ; } }
// return value: 0:running; not 0:quit; static int on_keypress() { int ch = 0; static char keybuf[32] = {0}; static int cursor = 0; Win *win = get_win("RCMD"); if (NULL == win) { return 0; } ch = readch(); keybuf[cursor++] = ch; if (ch == '\n' || cursor >= (int)sizeof(keybuf) - 1) { cursor = 0; process_command(keybuf); memset(keybuf, 0, sizeof(keybuf)); werase(win->win); wclear(win->win); mvwaddch(win->win, 1, 1, '>'); wrefresh(win->win); } else { waddch(win->win, ch); wrefresh(win->win); } return ch != 'q'; }
int _getch() { init_keyboard(); kbhit(); char c = readch(); close_keyboard(); return c; }
// return value: 0:running; not 0:quit; static int wait_user_cmd(char *keybuf, size_t buflen) { int ch = 0; int cursor = 0; while(1) { ch = readch(); keybuf[cursor++] = ch; if (ch == '\n' || cursor >= buflen - 1) { wclear(win_input->win); wrefresh(win_input->win); return cursor; } else if (ch == KEY_BACKSPACE || ch == 127) { keybuf[cursor] = '\0'; cursor--; if (cursor < 0) { cursor = 0; } } if (NULL != win_input) { wclear(win_input->win); mvwaddstr(win_input->win, 1, 1, keybuf); wrefresh(win_input->win); } }; return cursor; }
/* * endgame: * Do what's necessary at the end of the game */ void endgame() { char ch; prman(); if (Errors >= MAXERRS) Errors = MAXERRS + 2; prword(); prdata(); move(MESGY, MESGX); if (Errors > MAXERRS) printw("Sorry, the word was \"%s\"\n", Word); else printw("You got it!\n"); for (;;) { mvaddstr(MESGY + 1, MESGX, "Another word? "); leaveok(stdscr, FALSE); refresh(); if ((ch = readch()) == 'n') die(0); else if (ch == 'y') break; mvaddstr(MESGY + 2, MESGX, "Please type 'y' or 'n'"); } leaveok(stdscr, TRUE); move(MESGY, MESGX); deleteln(); deleteln(); deleteln(); }
void eval(void) { int ch; for (;;) { ch = readch(); if (ch == EOF) { if (bmachine.readsp == 0) return; src_free(); bmachine.readsp--; continue; } #ifdef DEBUGGING fprintf(stderr, "# %c\n", ch); stack_print(stderr, &bmachine.stack, "* ", bmachine.obase); fprintf(stderr, "%zd =>\n", bmachine.readsp); #endif if (0 <= ch && ch < (signed)UCHAR_MAX) (*jump_table[ch])(); else warnx("internal error: opcode %d", ch); #ifdef DEBUGGING stack_print(stderr, &bmachine.stack, "* ", bmachine.obase); fprintf(stderr, "%zd ==\n", bmachine.readsp); #endif } }
static void eval_string(char *p) { int ch; if (bmachine.readsp > 0) { /* Check for tail call. Do not recurse in that case. */ ch = readch(); if (ch == EOF) { src_free(); src_setstring(&bmachine.readstack[bmachine.readsp], p); return; } else unreadch(); } if (bmachine.readsp == bmachine.readstack_sz - 1) { size_t newsz = bmachine.readstack_sz * 2; struct source *stack; stack = reallocarray(bmachine.readstack, newsz, sizeof(struct source)); if (stack == NULL) err(1, "recursion too deep"); bmachine.readstack_sz = newsz; bmachine.readstack = stack; } src_setstring(&bmachine.readstack[++bmachine.readsp], p); }
int main(void) { int XMax,YMax; Getborder(&XMax,&YMax); srand((unsigned int)time(0)); RP r={0,0}; RP *rp=&r; SNode *sp=NULL; Initheadtail(&sp); Randpoint(rp,sp,XMax,YMax); initscr(); initkeyboard(); //---------------------- int ch=0; while(ch!='q') { ch=0; if(kbhit()) ch=readch(); // printf("%c\n",ch); // read(0,&ch,1); switch(ch){ case 'w': sp->direction=N; break; case 's': sp->direction=S; break; case 'a': sp->direction=W; break; case 'd': sp->direction=E; break; } // printw("%d\n",sp->direction); switch(Checkhead(sp,rp)){ case -1: goto END; case 0: break; case 1: Eatpoint(&sp,rp,XMax,YMax); break; } Freshsanke(&sp); Convertborder(sp,XMax,YMax); Printscr(sp); Printpoint(r); refresh(); clear(); usleep(100000); } //---------------------- END:closekeyboard(); endwin(); Freenode(sp); return 0; }
/* * endgame: * Do what is necessary at the end of the game */ void endgame() { char ch; /* Print out the hangman */ prman(); /* Print out the current word */ prword(); /* Print out the current guesses */ prdata(); /* Move curses window cursor */ move(MESGY, MESGX); /* Tell the user how they did by printing formatted output in the curses window */ if (Errors >= MAXERRS) { printw("Sorry, the word was \"%s\"\n", Word); } else { printw("You got it!\n"); } for (;;) { /* Prompt for another word */ mvaddstr(MESGY + 1, MESGX, "Another word? "); leaveok(stdscr, FALSE); refresh(); if ((ch = readch()) == 'n') { /* If user says no, exit */ exit(0); } else { if (ch == 'y') { /* If user says yes, keep going */ break; // fixes bug 6 } /* If user says neither y or n, prompt user */ mvaddstr(MESGY + 2, MESGX, "Please type 'y' or 'n'"); } } /* Clean up the window */ leaveok(stdscr, TRUE); move(MESGY, MESGX); deleteln(); deleteln(); deleteln(); }
/* * Execute a colon-prefixed command. * Returns <0 if not a command that should cause * more of the file to be printed. */ int colon(char *filename, int cmd, int nlines) { int ch; if (cmd == 0) ch = readch(); else ch = cmd; lastcolon = ch; switch (ch) { case 'f': kill_line(); if (!no_intty) promptlen = printf("\"%s\" line %lld", fnames[fnum], (long long)Currline); else promptlen = printf("[Not a file] line %lld", (long long)Currline); fflush(stdout); return (-1); case 'n': if (nlines == 0) { if (fnum >= nfiles - 1) end_it(); nlines++; } putchar('\r'); erasep(0); skipf(nlines); return (0); case 'p': if (no_intty) { write(STDERR_FILENO, &bell, 1); return (-1); } putchar('\r'); erasep(0); if (nlines == 0) nlines++; skipf (-nlines); return (0); case '!': if (do_shell(filename) < 0) { kill_line(); prompt(filename); } return (-1); case 'q': case 'Q': end_it(); /*FALLTHROUGH*/ default: write(STDERR_FILENO, &bell, 1); return (-1); } }
/* Skip whitespace */ static void space (MNCL_DATA_PARSE_CTX *ctx) { while (1) { int c = peekch(ctx); if (!c || !isspace(c)) { break; } readch(ctx); } }
/** * 引数解析 * * @param[in] calc calcinfo構造体 * @param[out] x 値 * @param[out] ... 可変引数 * @return なし * @attention 最後の引数はNULLにすること. */ void parse_func_args(calcinfo *calc, dbl *x, ...) { dbl *val = NULL; /* 値 */ va_list ap; /* va_list */ dbglog("start"); if (is_error(calc)) return; if (calc->ch != '(') { set_errorcode(calc, E_SYNTAX); return; } readch(calc); *x = expression(calc); dbglog(calc->fmt, *x); va_start(ap, x); while ((val = va_arg(ap, dbl *)) != NULL) { if (calc->ch != ',') { set_errorcode(calc, E_SYNTAX); va_end(ap); return; } readch(calc); *val = expression(calc); dbglog(calc->fmt, *val); } va_end(ap); if (calc->ch != ')') { set_errorcode(calc, E_SYNTAX); return; } readch(calc); }
/** * 数または関数 * * @param[in] calc calcinfo構造体 * @return 値 */ static dbl token(calcinfo *calc) { dbl result = 0.0; /* 結果 */ int sign = '+'; /* 単項+- */ char func[MAX_FUNC_STRING + 1]; /* 関数文字列 */ int pos = 0; /* 配列位置 */ dbglog("start"); if (is_error(calc)) return EX_ERROR; /* 初期化 */ (void)memset(func, 0, sizeof(func)); if (calc->ch == '+' || calc->ch == '-') { /* 単項+- */ sign = calc->ch; readch(calc); } if (isdigit(calc->ch)) { /* 数値 */ result = number(calc); } else if (isalpha(calc->ch)) { /* 関数 */ while (isalpha(calc->ch) && calc->ch != '\0' && pos <= MAX_FUNC_STRING) { func[pos++] = calc->ch; readch(calc); } dbglog("func=%s", func); result = exec_func(calc, func); } else { /* エラー */ dbglog("ch=%c", calc->ch); set_errorcode(calc, E_SYNTAX); } dbglog(calc->fmt, result); return (sign == '+') ? result : -result; }
static int readreg(void) { int index, ch1, ch2; index = readch(); if (index == 0xff && bmachine.extended_regs) { ch1 = readch(); ch2 = readch(); if (ch1 == EOF || ch2 == EOF) { warnx("unexpected eof"); index = -1; } else index = (ch1 << 8) + ch2 + UCHAR_MAX + 1; } if (index < 0 || index >= bmachine.reg_array_size) { warnx("internal error: reg num = %d", index); index = -1; } return index; }
static int readreg(void) { int ch1, ch2, idx; idx = readch(); if (idx == 0xff && bmachine.extended_regs) { ch1 = readch(); ch2 = readch(); if (ch1 == EOF || ch2 == EOF) { warnx("unexpected eof"); idx = -1; } else idx = (ch1 << 8) + ch2 + UCHAR_MAX + 1; } if (idx < 0 || (unsigned)idx >= bmachine.reg_array_size) { warnx("internal error: reg num = %d", idx); idx = -1; } return (idx); }
/** * 文字列を数値に変換 * * @param[in] calc calcinfo構造体 * @return 値 */ static dbl number(calcinfo *calc) { dbl x = 0.0, y = 1.0; /* 値 */ dbglog("start"); x = calc->ch - '0'; while (readch(calc), isdigit(calc->ch)) /* 整数 */ x = (x * 10) + (calc->ch - '0'); dbglog(calc->fmt, x); if (calc->ch == '.') { /* 小数 */ while (readch(calc), isdigit(calc->ch)) x += (y /= 10) * (calc->ch - '0'); } dbglog(calc->fmt, x); check_validate(calc, x); return x; }
int bitmap_play(char * filename) { char ch; if (fb_init()) { printf ("Unable to init framebuffer device\n"); return 2; } fb_pixel * bmp_buffer; if ((bmp_buffer = bitmap_load(filename)) == NULL) { fb_uninit(); printf ("Error while reading bitmap\n"); return 1; } fb_clear_screen(screen); bitmap_render(bmp_buffer); init_keyboard(); ch=0; while (1) { if (!kbhit()) { ch = readch(); if (ch == KEY_ESC) break; if (ch == KEY_UP && position_y >= JUMP_SIZE) position_y-=JUMP_SIZE; if (ch == KEY_DOWN && fb_yres <= (bmp_info.bi_height-position_y-JUMP_SIZE)) position_y+=JUMP_SIZE; if (ch == KEY_LEFT && position_x >= JUMP_SIZE) position_x-=JUMP_SIZE; if (ch == KEY_RIGHT && fb_xres <= (bmp_info.bi_width-position_x-JUMP_SIZE)) position_x+=JUMP_SIZE; ch = 0; bitmap_render(bmp_buffer); } } close_keyboard(); fflush(stdin); fb_clear_screen(screen); bitmap_free(bmp_buffer); fb_uninit(); return 0; }
void *keyboard_thread(void *) { while (1) { key = readch(); if (key == 27) { pt.running = 0; } if (key == 32) { pt.PT_paused = !pt.PT_paused; } } return NULL; }
int main(int argc, char **argv) { struct sigaction handler; char num[100]; int n = 0; // Set Interrrupt Signal Handler memset(&handler, 0, sizeof(struct sigaction)); handler.sa_handler = SignalCatcher; sigfillset(&handler.sa_mask); sigaction(SIGINT, &handler, 0); sigaction(SIGTERM, &handler, 0); tcgetattr(0, &oldtio); init_keyboard(); for(;!m_bExitSignalPending;) { display_menu(); memset(num, 0, 100); n = readch(); printf("\r\n"); switch(n) { case '0' : m_bExitSignalPending = 1; break; case '1' : if(m_bFirmwareInstalled) break; printf("\r\n"); system("/usr/bin/tftp -g -r factoryInstall 192.168.10.20"); system("chmod +x /tmp/factoryInstall"); system("/tmp/factoryInstall factory"); printf("\r\n"); break; case '2' : if(!m_bFirmwareInstalled) break; system("/app/sw/hwtest"); break; } } close_keyboard(); return 0; }
static void compare(enum bcode_compare type) { int index, elseindex; struct number *a, *b; bool ok; struct value *v; elseindex = NO_ELSE; index = readreg(); if (readch() == 'e') elseindex = readreg(); else unreadch(); a = pop_number(); if (a == NULL) return; b = pop_number(); if (b == NULL) { push_number(a); return; } ok = compare_numbers(type, a, b); if (!ok && elseindex != NO_ELSE) index = elseindex; if (index >= 0 && (ok || (!ok && elseindex != NO_ELSE))) { v = stack_tos(&bmachine.reg[index]); if (v == NULL) warnx("register '%c' (0%o) is empty", index, index); else { switch(v->type) { case BCODE_NONE: warnx("register '%c' (0%o) is empty", index, index); break; case BCODE_NUMBER: warn("eval called with non-string argument"); break; case BCODE_STRING: eval_string(bstrdup(v->u.string)); break; } } } }
int main() { char ch; init_keyboard(); init_game(1); do { ch = readch(); printf("%c %d\n", ch, ch); } while (ch != 'q'); close_keyboard(); return 0; }
bool CBaseLexer::selectch() { if (isMacrosMode) { if (idxMacros >= strMacros.size()-1) { isMacrosMode = false; peek = prevPeek; return true; } peek = strMacros[++idxMacros]; return true; } return readch(); }
int main() { int ch = 0; init_keyboard(); while (ch != 'q') { printf("looping\n"); sleep(1); if (kbhit()) { ch = readch(); printf("You hit '%c'\n", ch); } } close_keyboard(); return 0; }
/** * 項 * * @param[in] calc calcinfo構造体 * @return 値 */ static dbl term(calcinfo *calc) { dbl x = 0.0, y = 0.0; /* 値 */ dbglog("start"); if (is_error(calc)) return EX_ERROR; x = factor(calc); dbglog(calc->fmt, x); while (true) { if (calc->ch == '*') { readch(calc); x *= factor(calc); } else if (calc->ch == '/') { readch(calc); y = factor(calc); if (y == 0) { /* ゼロ除算エラー */ set_errorcode(calc, E_DIVBYZERO); return EX_ERROR; } x /= y; } else if (calc->ch == '^') { readch(calc); y = factor(calc); x = get_pow(calc, x, y); } else { break; } } dbglog(calc->fmt, x); return x; }