int tty_setty(int fd, ttydata_t *td) { #ifdef POSIX RETRY(xtcsetattr(fd, TCSADRAIN, &td->d_t)); #else # ifdef TERMIO RETRY(ioctl(fd, TCSETAW, (ioctl_t) &td->d_t)); # else # ifdef TIOCSETN RETRY(ioctl(fd, TIOCSETN, (ioctl_t) &td->d_t)); # endif /* TIOCSETN */ # ifdef TIOCGETC RETRY(ioctl(fd, TIOCSETC, (ioctl_t) &td->d_tc)); # endif /* TIOCGETC */ # ifdef TIOCGPAGE RETRY(ioctl(fd, TIOCSPAGE, (ioctl_t) &td->d_pc)); # endif /* TIOCGPAGE */ # ifdef TIOCLGET RETRY(ioctl(fd, TIOCLSET, (ioctl_t) &td->d_lb)); # endif /* TIOCLGET */ # endif /* TERMIO */ #endif /* POSIX */ #ifdef TIOCGLTC RETRY(ioctl(fd, TIOCSLTC, (ioctl_t) &td->d_ltc)); #endif /* TIOCGLTC */ return 0; }
int tty_getty(int fd, ttydata_t *td) { #ifdef POSIX RETRY(tcgetattr(fd, &td->d_t)); #else /* TERMIO || GSTTY */ # ifdef TERMIO RETRY(ioctl(fd, TCGETA, (ioctl_t) &td->d_t)); # else /* GSTTY */ # ifdef TIOCGETP RETRY(ioctl(fd, TIOCGETP, (ioctl_t) &td->d_t)); # endif /* TIOCGETP */ # ifdef TIOCGETC RETRY(ioctl(fd, TIOCGETC, (ioctl_t) &td->d_tc)); # endif /* TIOCGETC */ # ifdef TIOCGPAGE RETRY(ioctl(fd, TIOCGPAGE, (ioctl_t) &td->d_pc)); # endif /* TIOCGPAGE */ # ifdef TIOCLGET RETRY(ioctl(fd, TIOCLGET, (ioctl_t) &td->d_lb)); # endif /* TIOCLGET */ # endif /* TERMIO */ #endif /* POSIX */ #ifdef TIOCGLTC RETRY(ioctl(fd, TIOCGLTC, (ioctl_t) &td->d_ltc)); #endif /* TIOCGLTC */ return 0; }
static WRImgFormat identFile(const char *path) { FILE *file; unsigned char buffer[32]; size_t nread; assert(path != NULL); RETRY( file = fopen(path, "rb") ) if (file == NULL) { RErrorCode = RERR_OPEN; return IM_ERROR; } RETRY( nread = fread(buffer, 1, sizeof(buffer), file) ) if (nread < sizeof(buffer) || ferror(file)) { RETRY( fclose(file) ) RErrorCode = RERR_READ; return IM_ERROR; } RETRY( fclose(file) ) /* check for XPM */ if (strncmp((char *)buffer, "/* XPM */", 9) == 0) return IM_XPM; /* check for TIFF */ if ((buffer[0] == 'I' && buffer[1] == 'I' && buffer[2] == '*' && buffer[3] == 0) || (buffer[0] == 'M' && buffer[1] == 'M' && buffer[2] == 0 && buffer[3] == '*')) return IM_TIFF; #ifdef USE_PNG /* check for PNG */ if (!png_sig_cmp(buffer, 0, 8)) return IM_PNG; #endif /* check for raw PPM or PGM */ if (buffer[0] == 'P' && (buffer[1] == '5' || buffer[1] == '6')) return IM_PPM; /* check for JPEG */ if (buffer[0] == 0xff && buffer[1] == 0xd8) return IM_JPEG; /* check for GIF */ if (buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F') return IM_GIF; return IM_UNKNOWN; }
// // Read and unfold one semantic line from the source file, stripping off any coments. // int Parser::ReadLine() { #define RETRY() do { \ state = STAT_0; \ goto retry; \ } while(0) #define ENTER_COMMENT() do { prev_state = state; state = STAT_SLASH; } while(0) #define EXIT_COMMENT() do { state = prev_state; prev_state = STAT_INVALID; } while(0) CC_STRING& line = pline.parsed; pline.from.clear(); pline.parsed.clear(); pline.to.clear(); pline.pp_id = SSID_INVALID; pline.content = -1; enum { STAT_INVALID = -1, STAT_INIT = 0, STAT_SPACE1, STAT_SHARP, STAT_SPACE2, STAT_WORD, STAT_0, STAT_SLASH, STAT_LC, STAT_BC, STAT_ASTERISK, STAT_FOLD, STAT_SQ, STAT_DQ, STAT_SQ_ESC, STAT_DQ_ESC, } state, prev_state = STAT_INVALID; int c; int foldcnt = 0; File *file = GetCurrentFile().ifile; CC_STRING directive; bool eof = false; state = STAT_INIT; while(!eof) { c = file->ReadChar(); if(c == EOF) { if(line.isnull()) return 0; c = '\n'; eof = true; } else pline.from += c; if( c == '\r' ) /* Ignore carriage characters */ continue; if(c == '\n') file->line++; retry: switch(state) { case STAT_INIT: switch(c) { case ' ': case '\t': state = STAT_SPACE1; line = c; continue; case '#': state = STAT_SHARP; line = c; continue; } state = STAT_0; case STAT_0: switch(c) { case '/': ENTER_COMMENT(); break; case '\\': state = STAT_FOLD; break; case '"': state = STAT_DQ; break; case '\'': state = STAT_SQ; break; default: if(rtc && c == rtc->as_lc_char && pline.pp_id == SSID_INVALID) state = STAT_LC; break; } break; case STAT_SLASH: switch(c) { case '/': state = STAT_LC; mark_comment_start(); line.remove_last(); break; case '*': state = STAT_BC; mark_comment_start(); line.remove_last(); break; case '"': state = STAT_DQ; break; case '\'': state = STAT_SQ; break; default: EXIT_COMMENT(); } break; case STAT_LC: /* line comment */ if(c == '\n') state = STAT_0; break; case STAT_BC: /* block comments */ if(c == '*') state = STAT_ASTERISK; if(c == '\n') foldcnt++; break; case STAT_ASTERISK: /* asterisk */ if(c == '/') { EXIT_COMMENT(); continue; } else if(c != '*') state = STAT_BC; break; case STAT_FOLD: /* folding the line */ if(c == '\t' || c == ' ') ; else { if(c == '\n') foldcnt++; state = STAT_0; continue; } break; case STAT_DQ: switch(c) { case '\\': state = STAT_DQ_ESC; break; case '"': state = STAT_0; break; } break; case STAT_SQ: switch(c) { case '\\': state = STAT_SQ_ESC; break; case '\'': state = STAT_0; break; } break; case STAT_DQ_ESC: state = STAT_DQ; break; case STAT_SQ_ESC: state = STAT_SQ; break; case STAT_SPACE1: switch(c) { case '#': state = STAT_SHARP; break; case ' ': case '\t': break; case '/': ENTER_COMMENT(); break; default: RETRY(); } break; case STAT_SHARP: if(isblank(c)) { state = STAT_SPACE2; break; } /* fall through */ case STAT_SPACE2: if(isalpha(c) || isdigit(c) || c == '_') { state = STAT_WORD; directive = '#'; directive += c; } else if(c == '/') ENTER_COMMENT(); else if(!isblank(c)) RETRY(); break; case STAT_WORD: if(isalpha(c) || isdigit(c) || c == '_') directive += c; else { for(size_t i = 0; i < num_preprocessors; i++) if(preprocessors[i] == directive) { pline.pp_id = intab->syLut.Put(preprocessors[i]); pline.content = line.size(); state = STAT_0; break; } RETRY(); } break; case STAT_INVALID: errmsg = "Invalid state"; return -1; } if(state != STAT_LC && state != STAT_BC && state != STAT_FOLD && state != STAT_ASTERISK) line += c; if ((state == STAT_INIT || state == STAT_0) && c == '\n') break; } if(state != STAT_INIT && state != STAT_0) { errmsg = "Syntax error"; return -1; } file->offset = -foldcnt; return 1; }