/** * parse_stream - Parse a text stream for cheats. * @list: list to add cheats to * @stream: stream to read cheats from * @return: 0: success, -1: error */ int parse_stream(gamelist_t *list, FILE *stream) { parser_ctx_t ctx; char line[LINE_MAX + 1]; int nl = 1; if (list == NULL || stream == NULL) return -1; init_parser(&ctx); while (fgets(line, sizeof(line), stream) != NULL) { /* Scanner */ if (!is_empty_str(line)) { /* Screener */ term_str(line, is_cmt_str); trim_str(line); /* Parser */ if (strlen(line) > 0 && parse_line(line, nl, &ctx, list) < 0) return -1; } nl++; } return 0; }
void term_init(void) { struct winsize win; struct termios newtermios; tcgetattr(0, &termios); newtermios = termios; newtermios.c_lflag &= ~(ICANON | ISIG); newtermios.c_lflag &= ~ECHO; tcsetattr(0, TCSAFLUSH, &newtermios); if (getenv("LINES")) rows = atoi(getenv("LINES")); if (getenv("COLUMNS")) cols = atoi(getenv("COLUMNS")); if (!ioctl(0, TIOCGWINSZ, &win)) { cols = win.ws_col; rows = win.ws_row; } cols = cols ? cols : 80; rows = rows ? rows : 25; term_str("\33[m"); }
/** * parse_buf - Parse a text buffer for cheats. * @list: list to add cheats to * @buf: buffer holding text (must be NUL-terminated!) * @return: 0: success, -1: error */ int parse_buf(gamelist_t *list, const char *buf) { parser_ctx_t ctx; char line[LINE_MAX + 1]; int nl = 1; if (list == NULL || buf == NULL) return -1; init_parser(&ctx); while (*buf) { /* Scanner */ int len = chr_idx(buf, LF); if (len < 0) len = strlen(line); else if (len > LINE_MAX) len = LINE_MAX; if (!is_empty_substr(buf, len)) { strncpy(line, buf, len); line[len] = NUL; /* Screener */ term_str(line, is_cmt_str); trim_str(line); /* Parser */ if (strlen(line) > 0 && parse_line(line, nl, &ctx, list) < 0) return -1; } nl++; buf += len + 1; } return 0; }