bool if2k_lua_interpreter::if_parse_cached_url( const char *url, const char *cachefile, bool ignore_remote_if_local_exists ) { jdk_remote_buf remote_lua_code( cachefile, url, settings.get("further_proxy"), 1024*1024, ignore_remote_if_local_exists ); if( remote_lua_code.check_and_grab() ) { jdk_synchronized(mutex); int oldtop = lua_gettop(get_l()); bool r = parse_buf( remote_lua_code.get_buf() )==0; lua_settop(get_l(),oldtop); return r; } return false; }
void *collector(void *args) { Collector_args_s *a = args; /* * 1 ms -> 7% overhead * 10 ms -> 3% overhead */ struct timespec sleep = { 0, 10 * ONE_MILLION }; u8 buf[BUF_SIZE]; int cpu = a->cpu_id; int trace_pipe; int rc; int i; ignore_pid(gettid()); trace_pipe = open_raw(cpu); for (i = 0;; i++) { rc = read(trace_pipe, buf, sizeof(buf)); if (rc == -1) { close(trace_pipe); cleanup(0); } rc = parse_buf(buf); if (rc < SMALL_READ) { ++Slept; nanosleep(&sleep, NULL); } } return NULL; }
void if2k_lua_interpreter::update() { jdk_debug_block( "if2k_lua_interpreter::update()" ); { jdk_synchronized( mutex ); jdk_lua_setglobal( get_l(), "settings", settings ); } jdk_log_debug2( "if2k_lua_interpreter settings set" ); { jdk_synchronized( mutex); parse_builtin( "if2k_lua_init.luac" ); int oldtop = lua_gettop( get_l()); jdk_lua_call( get_l(), "if_run" ); lua_settop( get_l(),oldtop); } jdk_log_debug2( "if2k_lua_interpreter if_run() complete" ); jdk_str<128> lua_builtin_init = settings.get("lua_builtin_init"); if( !lua_builtin_init.is_clear() ) { jdk_log_debug1( "if2k_lua_interpreter parsing builtin: %s", lua_builtin_init.c_str() ); jdk_synchronized( mutex); parse_builtin( lua_builtin_init.c_str() ); } jdk_remote_buf remote_lua_code( settings, "lua_init", 1024*1024, false ); if( remote_lua_code.check_and_grab() ) { jdk_synchronized( mutex); int oldtop = lua_gettop( get_l()); parse_buf( remote_lua_code.get_buf() ); lua_settop( get_l(),oldtop); jdk_log_debug1( "if2k_lua_interpreter remote buf parsed" ); } jdk_str<4096> lua_command = settings.get("lua_command"); if( !lua_command.is_clear() ) { jdk_synchronized( mutex); jdk_log_debug2( "if2k_lua_interpreter parsing lua_command: %s", lua_builtin_init.c_str() ); int oldtop = lua_gettop( get_l()); parse_string( lua_command.c_str() ); lua_settop( get_l(),oldtop); } }
bool if2k_lua_interpreter::parse_builtin( const char *fname ) { jdk_bindir *bin = jdk_bindir_find( "if2k_lua_files", fname ); if( bin ) { jdk_log_debug2( "found builtin %s", fname ); jdk_synchronized(mutex); int oldtop = lua_gettop(get_l()); parse_buf( bin->data, bin->length ); lua_settop(get_l(),oldtop); return true; } else { jdk_log( JDK_LOG_ERROR, "can't find builtin script %s", fname); return false; } }
void *pthreads(void *arg)// 线程读函数 { int newfd = *((int *)arg); char buf[BUF_SIZE] = {0}; int length = 0; send_info send, *p; p = &send; while(1) { length = read(newfd ,&send ,sizeof(send));//读取字节的长度 if(length <= 0)//读取服务器失败或者服务器为空 { printf("一个客户端退出!\n"); break; } parse_buf(p, newfd);//解析客户端传过来的字符串数据结构中的type 成员来调用函数。 memset(&send ,0 ,sizeof(send)); } client_exit(p, newfd); }
int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAX_LINE]; if (argc == 2 || argc == 3) { host = argv[1]; port = (argc == 2) ? DEFAULT_PORT : atoi(argv[2]); } else { fprintf(stderr, "usage: %s <host>\n", argv[0]); exit(0); } clientfd = Open_clientfd(host, port); printf("Bank client v.%d connected to %s on port %d\n", VERSION, host, port); msg_t *request = new_msg(); msg_t *response = new_msg(); printf("> "); fflush(stdout); while (Fgets(buf, MAX_LINE, stdin) != NULL) { if (parse_buf(buf, request)) { Rio_writen(clientfd, (void *) request, sizeof(msg_t)); // Send bits to server Rio_readn(clientfd, (void *) response, sizeof(msg_t)); // Get bits back from server print_response(response); } clear_msg(request); clear_msg(response); printf("> "); fflush(stdout); } Close(clientfd); free(request); free(response); exit(0); }
bool if2k_lua_interpreter::if_parse_url( const char *url ) { jdk_dynbuf response; jdk_http_response_header response_header; if( jdk_http_get( url, &response, 1024*512, &response_header, settings.get("further_proxy").c_str(), false )==200 ) { jdk_synchronized(mutex); int oldtop = lua_gettop(get_l()); bool r = parse_buf( response )==0; lua_settop(get_l(),oldtop); return r; } return false; }
int open_file( WX **pwx, int *pvw, int *pvh ) { WX *wx; char file[14], *ppath; int i, ret; DTA mydta, *savedta; long readamt; char *readbuf; if( (wx = calloc( 1L, sizeof(WX) )) == NULL ) { return (int)ENSMEM; } wx->name[0] = (char)Dgetdrv() + 'A'; wx->name[1] = ':'; Dgetpath( &(wx->name[2]), 0 ); strcat( wx->name, "\\*.*" ); file[0] = 0; ret = fsel_input( wx->name, file, &i ); if( ret != 1 ) { goto err1; } else if( !i ) { ret = (int)ERROR; goto err1; } if( (ppath = strrchr( wx->name, '\\' )) == NULL ) ppath = wx->name; else ++ppath; strcpy( ppath, file ); savedta = Fgetdta(); Fsetdta( &mydta ); Fsfirst( wx->name, 1 ); Fsetdta( savedta ); if( (readbuf = (char *)Malloc(mydta.d_length)) <= NULL ) { ret = (int)ENSMEM; goto err1; } if( (ret = (int)Fopen(wx->name, 0)) < 0 ) goto err; graf_mouse( BUSYBEE, NULL ); readamt = Fread( ret, mydta.d_length, readbuf ); graf_mouse( ARROW, NULL ); Fclose( ret ); if( readamt != mydta.d_length ) { ret = (int)readamt; err: Mfree( readbuf ); err1: free( wx ); return ret; } wx->tabs = 4 * form_alert( 1, "[2][ What size tabs? ][ 4 | 8 ]" ); graf_mouse( BUSYBEE, NULL ); ret = parse_buf( readbuf, readamt, wx->tabs, &wx->line0, pvw, pvh ); graf_mouse( ARROW, NULL ); Mfree( readbuf ); if( ret != E_OK ) { free_tlines( wx->curline ); goto err1; } *pwx = wx; return ret; }
EXTERN void parse(pTHX_ PSTATE* p_state, SV* chunk, SV* self) { char *s, *beg, *end; U32 utf8 = 0; STRLEN len; if (!chunk) { /* eof */ char empty[1]; if (p_state->buf && SvOK(p_state->buf)) { /* flush it */ s = SvPV(p_state->buf, len); end = s + len; utf8 = SvUTF8(p_state->buf); assert(len); while (s < end) { if (p_state->literal_mode) { if (strEQ(p_state->literal_mode, "plaintext") && !p_state->closing_plaintext) break; p_state->pending_end_tag = p_state->literal_mode; p_state->literal_mode = 0; s = parse_buf(aTHX_ p_state, s, end, utf8, self); continue; } if (!p_state->strict_comment && !p_state->no_dash_dash_comment_end && *s == '<') { p_state->no_dash_dash_comment_end = 1; s = parse_buf(aTHX_ p_state, s, end, utf8, self); continue; } if (!p_state->strict_comment && *s == '<') { /* some kind of unterminated markup. Report rest as as comment */ token_pos_t token; token.beg = s + 1; token.end = end; report_event(p_state, E_COMMENT, s, end, utf8, &token, 1, self); s = end; } break; } if (s < end) { /* report rest as text */ report_event(p_state, E_TEXT, s, end, utf8, 0, 0, self); } SvREFCNT_dec(p_state->buf); p_state->buf = 0; } if (p_state->pend_text && SvOK(p_state->pend_text)) flush_pending_text(p_state, self); if (p_state->ignoring_element) { /* document not balanced */ SvREFCNT_dec(p_state->ignoring_element); p_state->ignoring_element = 0; } report_event(p_state, E_END_DOCUMENT, empty, empty, 0, 0, 0, self); /* reset state */ p_state->offset = 0; if (p_state->line) p_state->line = 1; p_state->column = 0; p_state->literal_mode = 0; p_state->is_cdata = 0; return; } #ifdef UNICODE_HTML_PARSER if (p_state->utf8_mode) sv_utf8_downgrade(chunk, 0); #endif if (p_state->buf && SvOK(p_state->buf)) { sv_catsv(p_state->buf, chunk); beg = SvPV(p_state->buf, len); utf8 = SvUTF8(p_state->buf); } else { beg = SvPV(chunk, len); utf8 = SvUTF8(chunk); if (p_state->offset == 0) { report_event(p_state, E_START_DOCUMENT, beg, beg, 0, 0, 0, self); /* Print warnings if we find unexpected Unicode BOM forms */ #ifdef UNICODE_HTML_PARSER if (DOWARN && p_state->argspec_entity_decode && !p_state->utf8_mode && ( (!utf8 && len >= 3 && strnEQ(beg, "\xEF\xBB\xBF", 3)) || (utf8 && len >= 6 && strnEQ(beg, "\xC3\xAF\xC2\xBB\xC2\xBF", 6)) || (!utf8 && probably_utf8_chunk(aTHX_ beg, len)) ) ) { warn("Parsing of undecoded UTF-8 will give garbage when decoding entities"); } if (DOWARN && utf8 && len >= 2 && strnEQ(beg, "\xFF\xFE", 2)) { warn("Parsing string decoded with wrong endianess"); } #endif if (DOWARN) { if (!utf8 && len >= 4 && (strnEQ(beg, "\x00\x00\xFE\xFF", 4) || strnEQ(beg, "\xFE\xFF\x00\x00", 4)) ) { warn("Parsing of undecoded UTF-32"); } else if (!utf8 && len >= 2 && (strnEQ(beg, "\xFE\xFF", 2) || strnEQ(beg, "\xFF\xFE", 2)) ) { warn("Parsing of undecoded UTF-16"); } } } } if (!len) return; /* nothing to do */ end = beg + len; s = parse_buf(aTHX_ p_state, beg, end, utf8, self); if (s == end || p_state->eof) { if (p_state->buf) { SvOK_off(p_state->buf); } } else { /* need to keep rest in buffer */ if (p_state->buf) { /* chop off some chars at the beginning */ if (SvOK(p_state->buf)) { sv_chop(p_state->buf, s); } else { sv_setpvn(p_state->buf, s, end - s); if (utf8) SvUTF8_on(p_state->buf); else SvUTF8_off(p_state->buf); } } else { p_state->buf = newSVpv(s, end - s); if (utf8) SvUTF8_on(p_state->buf); } } return; }
/* * 读取文件块,保留完整行, */ static void read_logfile(char *filename) { long bytesread; char buf[BUFSIZE]; int sizeLeftover=0; int bLoopCompleted = 0; long pos = 0; FILE *handle; if (!(handle = fopen(filename,"rb"))) { log_debug("can't open file: %s", filename); exit(1); } log_debug("open file: %s", filename); do { // Read next block from file and save into buf, right after the // "left over" buffer bytesread = fread(buf+sizeLeftover, 1, sizeof(buf)-1-sizeLeftover, handle); if (bytesread<1) { // Turn on 'loop completed' flag so that we know to exit at the bottom // Still need to process any block we currently have in the // leftover buffer bLoopCompleted = 1; bytesread = 0; } // Add NULL terminator at the end of our buffer buf[bytesread+sizeLeftover] = 0; // Process data - Replace with your function // // Function should return the position in the file or -1 if failed // // We are also passing bLoopCompleted to let ProcessData know whether this is // the last record (in which case - if no end-of-record separator, // use eof and process anyway) pos = parse_buf(buf, bytesread+sizeLeftover, bLoopCompleted); // If error occured, bail if (pos<1) { bLoopCompleted = 1; pos = 0; } // Set Left over buffer size to // // * The remaining unprocessed buffer that was not processed // by ProcessData (because it couldn't find end-of-line) // // For protection if the remaining unprocessed buffer is too big // to leave sufficient room for a new line (MAXLINELENGTH), cap it // at maximumsize - MAXLINELENGTH sizeLeftover = min(bytesread+sizeLeftover-pos, sizeof(buf)-MAXLINELENGTH); // Extra protection - should never happen but you can never be too safe if (sizeLeftover<1) sizeLeftover=0; // If we have a leftover unprocessed buffer, move it to the beginning of // read buffer so that when reading the next block, it will connect to the // current leftover and together complete a full readable line if (pos!=0 && sizeLeftover!=0) memmove(buf, buf+pos, sizeLeftover); } while(!bLoopCompleted); // Close file fclose(handle); }