PROC scroll(bool down) { int i; if (count <= 0) count = dofscroll; strput(CURoff); if (down) { curr = min(bufmax-1, nextline(TRUE, curr, count)); i = min(bufmax-1, nextline(TRUE, pend, count)); if (i > pend) scrollforward(i); } else { curr = bseekeol(max(0,nextline(FALSE, curr, count))); i = bseekeol(max(0,nextline(FALSE, ptop, count))); if (i < ptop) if (canUPSCROLL) scrollback(i); else { ptop = i; setend(); redisplay(TRUE); } } strput(CURon); setpos(skipws(curr)); /* initialize new position - first nonwhite */ yp = setY(curr); mvcur(yp, xp); /* go there */ }
extern int yylex() { static bool dollar = FALSE; bool saw_meta = FALSE; int c; size_t i; /* The purpose of all these local assignments is to */ const char *meta; /* allow optimizing compilers like gcc to load these */ char *buf = realbuf; /* values into registers. On a sparc this is a */ YYSTYPE *y = &yylval; /* win, in code size *and* execution time */ if (errset) { errset = FALSE; return '\n'; } /* rc variable-names may contain only alnum, '*' and '_', so use dnw if we are scanning one. */ meta = (dollar ? dnw : nw); if (newline) { --lineno; /* slight space optimization; nextline() always increments lineno */ nextline(); newline = FALSE; } top: while ((c = gchar()) == ' ' || c == '\t') w = NW; if (c != '(') dollar = FALSE; if (c == EOF) return END; if (!meta[(unsigned char) c]) { /* it's a word or keyword. */ checkfreecaret; w = RW; i = 0; read: do { buf[i++] = c; if (c == '?' || c == '[' || c == '*') saw_meta = TRUE; if (i >= bufsize) buf = realbuf = erenew_arr(char, buf, bufsize *= 2); } while ((c = gchar()) != EOF && !meta[(unsigned char) c]); while (c == '\\') { if ((c = gchar()) == '\n') { nextline(); c = ' '; /* Pretend a space was read */ break; } else { bs: if (meta != dnw) { /* all words but varnames may have a bslash */ buf[i++] = '\\'; if (i >= bufsize) buf = realbuf = erenew_arr(char, buf, bufsize *= 2); if (!meta[(unsigned char) c]) goto read; } else { ugchar(c); c = '\\'; break; } }
static inline char * __seekline(char *base, int len) { char *head = nextline(base, len); char *end; if (head == NULL) return NULL; end = nextline(head, len - (head - base)); if (end == NULL) return NULL; return head; }
static int step_into_gie_block (ffio *G) { /**************************************************************************************** Make sure we're inside a <gie>-block. Return 1 on success, 0 otherwise. ****************************************************************************************/ /* Already inside */ if (G->level % 2) return 1; if (0==locate_tag (G, "<gie>")) return 0; while (0!=strncmp ("<gie>", G->next_args, 5)) { G->next_args[0] = 0; if (feof (G->f)) return 0; if (nullptr==fgets (G->next_args, (int) G->next_args_size - 1, G->f)) return 0; pj_chomp (G->next_args); G->next_lineno++; } G->level++; /* We're ready at the start - now step into the block */ return nextline (G); }
int echo_group(char* group) { const char* myhome = getenv("MY_HOME"); int homelen = strlen(myhome); int grouplen = strlen(group); char listpath [ homelen + grouplen + 7 ]; memset( listpath, 0, (homelen + grouplen + 6) * sizeof(char) ); strcpy( listpath, myhome ); strcat( listpath, "/"); strcat( listpath, group); strcat( listpath, "/.list"); FILE * fp; if ( fp = fopen(listpath, "r") ) { char line[1023]; while( nextline(line, 1023, fp) ) { line[1] = '\t'; puts(line); } } }
static void handle_set_parameter(rtsp_conn_info *conn, rtsp_message *req, rtsp_message *resp) { if (!req->contentlength) debug(1, "received empty SET_PARAMETER request\n"); char *cp = req->content; int cp_left = req->contentlength; char *next; while (cp_left && cp) { next = nextline(cp, cp_left); cp_left -= next-cp; if (!strncmp(cp, "volume: ", 8)) { float volume = atof(cp + 8); debug(1, "volume: %f\n", volume); player_volume(volume); } else { debug(1, "unrecognised parameter: >>%s<< (%d)\n", cp, strlen(cp)); } cp = next; } resp->respcode = 200; }
static int skip_to_next_tag (ffio *G) { /**************************************************************************************** Skip forward to the next command tag. Return 1 on success, 0 otherwise. ****************************************************************************************/ const char *c; if (0==step_into_gie_block (G)) return 0; c = at_tag (G); /* If not already there - get there */ while (!c) { if (0==nextline (G)) return 0; c = at_tag (G); } /* If we reached the end of a <gie> block, locate the next and retry */ if (0==strcmp (c, "</gie>")) { G->level++; if (feof (G->f)) return 0; if (0==step_into_gie_block (G)) return 0; G->args[0] = 0; return skip_to_next_tag (G); } G->lineno = G->next_lineno; return 1; }
void Buffer::update(int ch) { switch (ch) { case KEY_UP: prevline(); break; case KEY_DOWN: nextline(); break; case KEY_RIGHT: nextchar(); break; case KEY_LEFT: prevchar(); break; case KEY_HOME: begofline(); break; case KEY_END: endofline(); break; case 127: delchar(); break; case '\n': case '\r': newline(); break; default: addchar(ch); } }
void *sam_header_parse2(const char *headerText) { list_t *hlines = NULL; HeaderLine *hline; const char *text; char *buf = NULL; size_t nbuf = 0; int tovalidate = 0; if (!headerText) return 0; text = headerText; while ((text = nextline(&buf, &nbuf, text))) { hline = sam_header_line_parse(buf); if (hline && (!tovalidate || sam_header_line_validate(hline))) // With too many (~250,000) reference sequences the header parsing was too slow with list_append. hlines = list_append_to_end(hlines, hline); else { if (hline) sam_header_line_free(hline); sam_header_free(hlines); if (buf) free(buf); return NULL; } } if (buf) free(buf); return hlines; }
/* Reads in one line of source code and run it through the partial * preprocessor. The return value is zero if the file has reached the * end or if the file can't be read. */ static int readline(CparsePP *ppp, FILE *infile) { int size; int prev, ch; ch = fgetc(infile); if (ch == EOF) return 0; prev = EOF; for (size = 0 ; ch != EOF ; ++size) { if (ch == '\n' && prev != '\\') break; if (size + 1 == ppp->linealloc) { ppp->linealloc *= 2; ppp->line = reallocate(ppp->line, ppp->linealloc); } ppp->line[size] = ch; prev = ch; ch = fgetc(infile); } if (ferror(infile)) { error(errFileIO); return 0; } ppp->endline = ch != EOF; ppp->line[size] = '\0'; seq(ppp); nextline(ppp->cl, NULL); return 1; }
/* Reads in one line of source code and run it through the partial * preprocessor. The return value is zero if the file has reached the * end or if the file can't be read. */ static int readline_buf(CparsePP *ppp, const char *inbuf) { int size, i = 1; int prev, ch; ch = inbuf[0]; if (ch == '\0') return 0; prev = '\0'; for (size = 0 ; ch != '\0' ; ++size) { if (ch == '\n' && prev != '\\') break; if (size + 1 == ppp->linealloc) { ppp->linealloc *= 2; ppp->line = reallocate(ppp->line, ppp->linealloc); } ppp->line[size] = ch; prev = ch; ch = inbuf[i++]; } ppp->endline = ch != '\0'; ppp->line[size] = '\0'; seq(ppp); nextline(ppp->cl, NULL); return 1; }
/* print lines in this chunk, possibly recursing into getchunk */ int printchunk(int i, int chunklinelen, char *chunkname) { int j; int k; int linelen; char *getname; int getlen = 0; if (DEBUG==3) { printf("=== \\start{%s} ===\n",chunkname); } for (k=i+chunklinelen+1; ((linelen=nextline(k)) != -1); ) { if ((getlen=foundGetchunk(k,linelen)) > 0) { getname = getChunkname(k,getlen); getchunk(getname); free(getname); k=k+getlen+12l; } else { if ((linelen >= 11) && (foundEnd(k,chunkname) == 1)) { if (DEBUG==3) { printf("=== \\end{%s} ===\n",chunkname); } return(k+12); } else { if (DEBUG==2) { printf("======== printchunk else %d %d\n",k,linelen); } printline(k,linelen); k=k+linelen+1; } }} if (DEBUG==2) { printf("=================\\out{%s} %d\n",chunkname,k); } return(k); }
/* find a pdf object */ int pdf_find(char *pdf, int len, int obj, int rev) { int obj_beg, obj_cnt; int cur_rev, cur_pos; char *beg; int i; int pos = pdf_xref(pdf, len); if (pos < 0) return -1; /* the numbers after xref */ while (pos < len && sscanf(pdf + pos, "%d %d", &obj_beg, &obj_cnt) == 2) { for (i = 0; i < obj_cnt; i++) { if ((pos = nextline(pdf, len, pos)) < 0) return -1; if (sscanf(pdf + pos, "%d %d", &cur_pos, &cur_rev) != 2) return -1; if (obj_beg + i == obj && cur_rev == rev) { if (cur_pos < 0 || cur_pos >= len) return -1; if (!(beg = strstr(pdf + cur_pos, "obj"))) return -1; pos = beg - pdf + 3; pos += pdf_ws(pdf, len, pos); return pos; } } } return -1; }
/* inputInit - initialize input processing */ void inputInit(fd) { limit = cp = &buffer[MAXTOKEN + 1]; lineno = 0; file = 0; /* omit */ bsize = -1; infd = fd; nextline(); }
/* the position of the trailer */ int pdf_trailer(char *pdf, int len) { int pos = prevline(pdf, len, len); /* %%EOF */ while (!startswith(pdf + pos, "trailer")) if ((pos = prevline(pdf, len, pos)) < 0) return -1; return nextline(pdf, len, pos); /* skip trailer\n */ }
static int read_group() { char *p; int n, m; int grouptab_len; int commas; char **grmem; // Read user group file into memory group = read_file("/etc/group", &group_len); if (!group) return -1; // Calculate the number of entries in user group file n = linecount(group); // The total number of group members entries is at most the number of commas // plus the number of lines commas = 0; p = group; while (*p) if (*p++ == ',') commas++; // Allocate memory for user group table grouptab_len = n * sizeof(struct group) + (commas + n * 2) * sizeof(char *); grouptab = vmalloc(NULL, grouptab_len, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE, 'UDB'); if (!grouptab) return -1; grmem = (char **) (grouptab + n * sizeof(struct group)); // Build user group table entries p = group; n = 0; m = 0; while (*p) { char *next = nextline(p); grouptab[n].gr_name = p; p = skip(p, ':'); grouptab[n].gr_passwd = p; p = skip(p, ':'); grouptab[n].gr_gid = atoi(p); p = skip(p, ':'); grouptab[n].gr_mem = &grmem[m]; while (*p) { grmem[m++] = p; p = skip(p, ','); } grmem[m++] = NULL; p = next; n++; } // Protect group memory if (vmprotect(group, passwd_len, PAGE_READONLY) < 0) return -1; if (vmprotect(grouptab, grouptab_len, PAGE_READONLY) < 0) return -1; group_cnt = n; return 0; }
static char *seek_line(Text_file *gf, char *s, int line) /* Find character pointer to start of a specific line of buffer */ { while (--line >= 0) { s = nextline(gf, s); } return(s); }
static void handle_announce(rtsp_conn_info *conn, rtsp_message *req, rtsp_message *resp) { char *paesiv = NULL; char *prsaaeskey = NULL; char *pfmtp = NULL; char *cp = req->content; int cp_left = req->contentlength; char *next; while (cp_left && cp) { next = nextline(cp, cp_left); cp_left -= next-cp; if (!strncmp(cp, "a=fmtp:", 7)) pfmtp = cp+7; if (!strncmp(cp, "a=aesiv:", 8)) paesiv = cp+8; if (!strncmp(cp, "a=rsaaeskey:", 12)) prsaaeskey = cp+12; cp = next; } if (!paesiv || !prsaaeskey || !pfmtp) { warn("required params missing from announce"); return; } int len, keylen; uint8_t *aesiv = base64_dec(paesiv, &len); if (len != 16) { warn("client announced aeskey of %d bytes, wanted 16", len); free(aesiv); return; } memcpy(conn->stream.aesiv, aesiv, 16); free(aesiv); uint8_t *rsaaeskey = base64_dec(prsaaeskey, &len); uint8_t *aeskey = rsa_apply(rsaaeskey, len, &keylen, RSA_MODE_KEY); free(rsaaeskey); if (keylen != 16) { warn("client announced rsaaeskey of %d bytes, wanted 16", keylen); free(aeskey); return; } memcpy(conn->stream.aeskey, aeskey, 16); free(aeskey); int i; for (i=0; i<sizeof(conn->stream.fmtp)/sizeof(conn->stream.fmtp[0]); i++) conn->stream.fmtp[i] = atoi(strsep(&pfmtp, " \t")); resp->respcode = 200; }
/* the position of the last xref table */ static int pdf_xref(char *pdf, int len) { int pos = prevline(pdf, len, len); /* %%EOF */ if ((pos = prevline(pdf, len, pos)) < 0) return -1; /* read startxref offset */ if (sscanf(pdf + pos, "%d", &pos) != 1 || pos >= len || pos < 0) return -1; return nextline(pdf, len, pos); /* skip xref\n */ }
void inputInit() { limit = cp = &buffer[MAXLINE+1]; bsize = -1; lineno = 0; file = NULL; fillbuf(); if (cp >= limit) cp = limit; nextline(); }
void translate_from_english(FILE * wt) { rewind(wt); char buffer[9999]; char def[9999]; int pos = 0; int sense = 0; int cont = 1; while(cont) { cont = nextline(wt, buffer); // fprintf(stderr, "'%s'\n", buffer); if(!strcmp(buffer, "====Noun====")) { // fprintf(stderr, "; Noun.\n"); pos = NOUN; continue; } if(!strcmp(buffer, "====Verb====")) { // fprintf(stderr, "; Noun.\n"); pos = VERB; continue; } if(!strncmp(buffer, "{{trans-top|", 12)) { sense++; strcpy(def, buffer + 12); char * end = strstr(def, "}}"); end[0] = '\0'; // fprintf(stderr, "; sense number %d: %s\n", sense, def); englishlemma(pos, sense); continue; } if(!strncmp(buffer, "* Czech: ", 9)) { mfn_translation(sense, pos, buffer, "Czech", "cs", "czech"); continue; } if(!strncmp(buffer, "* Latin: ", 9)) { mfn_translation(sense, pos, buffer, "Latin", "la", "latin"); continue; } if(!strncmp(buffer, "* Ainu: ", 8)) { extract_ainu(sense, pos, buffer); continue; } // if(!strncmp(buffer, "*: Mandarin: ", 13)) { // untangle_chinese(sense, pos, buffer, "|cmn|"); // continue; // } } }
/* print the longest input line */ int main() { int len, max; char line[MAXLINE]; char longest[MAXLINE]; while ((len = nextline(line, MAXLINE)) > 0) { if (len > 80) { printf("%s", line); } } return EXIT_SUCCESS; }
static int locate_tag (ffio *G, const char *tag) { /**************************************************************************************** Find start-of-line tag (currently only used to search for for <gie>, but any tag valid). Returns 1 on success, 0 on failure. ****************************************************************************************/ size_t n = strlen (tag); while (0!=strncmp (tag, G->next_args, n)) if (0==nextline (G)) return 0; return 1; }
static int getHostname(char *result, int len) { FILE *fp; char *cp; fp=fopen("/etc/hostname","r"); if(fp == NULL) return -1; cp=nextline(fp); fclose(fp); if(len<strlen(cp)) return -1; sprintf(result, "%s\0", cp); return 1; }
//优化数据文件,其实就是使得每一行都一样长 int phraser_optimise(PHRASER * phraser) { char p0[64], p1[64]; const int max_length = 64; // 绝对够的,不够你找偶 char * ptr, *preserve,*ptr2; char * p; int type, preservesize; ptr = phraser->start_ptr; preservesize = 1024 * 1024; //预先申请 1 M 内存,不够了再说 ptr2 = preserve = mmap(0, preservesize, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (!preserve) return -1; //进入循环,一行一行的扫描 :) while ((ptr = nextline(ptr)) && ((ptr - phraser->start_ptr) < phraser->fsize)) { memcpy(ptr2, ptr, 64); //直接拷贝过去就可以了 nextline(ptr2)[-1] = 0; ptr2 += 64; } munmap(phraser->start_ptr,phraser->fsize); phraser->start_ptr = preserve ; phraser->fsize = preservesize; return 0; }
static int read_passwd() { char *p; int n; int passwdtab_len; // Read password file into memory passwd = read_file("/etc/passwd", &passwd_len); if (!passwd) return -1; // Calculate the number of entries in password file n = linecount(passwd); // Allocate memory for password table passwdtab_len = n * sizeof(struct passwd); passwdtab = vmalloc(NULL, passwdtab_len, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE, 'UDB'); if (!passwdtab) return -1; // Build password table entries p = passwd; n = 0; while (*p) { char *next = nextline(p); passwdtab[n].pw_name = p; p = skip(p, ':'); passwdtab[n].pw_passwd = p; p = skip(p, ':'); passwdtab[n].pw_uid = atoi(p); p = skip(p, ':'); passwdtab[n].pw_gid = atoi(p); p = skip(p, ':'); passwdtab[n].pw_gecos = p; p = skip(p, ':'); passwdtab[n].pw_dir = p; p = skip(p, ':'); passwdtab[n].pw_shell = p; p = next; n++; } // Protect password memory if (vmprotect(passwd, passwd_len, PAGE_READONLY) < 0) return -1; if (vmprotect(passwdtab, passwdtab_len, PAGE_READONLY) < 0) return -1; passwd_cnt = n; return 0; }
int main(int argc, char **argv) { FILE *fh; int slen, rlen, retval = 0; char *next, *sbuf, *rbuf; if (argc != 2) { fprintf(stderr, "Syntax: %s scriptfile\n", argv[0]); exit(1); } fh = fopen(argv[1], "r"); if (!fh) { perror("fopen"); exit(1); } if (init() < 0) exit(1); if (l3(RFID_PROTOCOL_TCL) < 0) exit(1); printf("Protocol T=CL\n"); /* we've established T=CL at this point */ while (next = nextline(fh)) { if (!(strlen(next) >= 2 && strncmp(next, "//", 2) == 0)) { if (make_command(next, &sbuf, &slen)) { rlen = 1024; rbuf = calloc(rlen, 1); retval = send_command(sbuf, slen, rbuf, rlen); free(sbuf); free(rbuf); } } free(next); if (retval < 0) break; } rfid_reader_close(rh); exit(0); }
static inline ssize_t __findline(int fd, int (*comp) (const char *, void *), void *param, size_t low, size_t high, int maxlen) { size_t s, t, m; ssize_t nb; char buf[maxlen * 2]; /* including one extra byte for '\0' */ char *line; int len = 0; if (low >= high) return -1; buf[0] = '\0'; for (s = low, t = high; s < t;) { m = (s + t) / 2; /* the last byte of buf is reserved */ nb = pread(fd, buf, sizeof(buf) - 1, m); if (nb <= 0) { t = m; continue; } /* one extra byte is included to get nextline work */ len = (int) min((ssize_t)(high - m), nb) + 1; line = __seekline(buf, len); if (line == NULL) { t = m; continue; } int cmp = comp(line, param); if (cmp == 0) return m + (line - buf); if (cmp < 0) s = m + 1; else t = m; } line = nextline(buf, len); if (line && comp(buf, param) == 0) return low; return -1; }
void input_init(int argc, char *argv[]) { static int inited; if (inited) return; inited = 1; main_init(argc, argv); limit = cp = &buffer[MAXLINE+1]; bsize = -1; lineno = 0; file = NULL; fillbuf(); if (cp >= limit) cp = limit; nextline(); }
/* print longest input line; specialized version */ int main() { int len; extern int max; extern char longest[]; max = 0; while ((len = nextline()) > 0) { if (len > max) { max = len; copy(); } } if (max > 0) { /* there was a line */ printf("%s", longest); } return EXIT_SUCCESS; }