static void read_props(void) { uint32_t len; uint32_t key = ~0; char *val = NULL; char *t; while ((t = buffer_read_line()) && strcmp(t, "PROPS-END")) { if (!strncmp(t, "K ", 2)) { len = atoi(&t[2]); key = pool_intern(buffer_read_string(len)); buffer_read_line(); } else if (!strncmp(t, "V ", 2)) { len = atoi(&t[2]); val = buffer_read_string(len); if (key == keys.svn_log) { /* Value length excludes terminating nul. */ rev_ctx.log = log_copy(len + 1, val); } else if (key == keys.svn_author) { rev_ctx.author = pool_intern(val); } else if (key == keys.svn_date) { if (parse_date_basic(val, &rev_ctx.timestamp, NULL)) fprintf(stderr, "Invalid timestamp: %s\n", val); } else if (key == keys.svn_executable) { node_ctx.type = REPO_MODE_EXE; } else if (key == keys.svn_special) { node_ctx.type = REPO_MODE_LNK; } key = ~0; buffer_read_line(); } } }
void process_server_info(struct sockaddr_in *sockaddr, struct buffer_t *buffer) { struct queried_server_t *cserver = queried_servers; struct found_server_t new_server_info; uint16_t servers; struct sockaddr_in s; time_t t; int proto_ver; while(cserver) { if(cserver->ip == sockaddr->sin_addr.s_addr && cserver->port == sockaddr->sin_port) { proto_ver = buffer_read_uint8(buffer); if(proto_ver != EM_PROTO_VER) { if(proto_ver > EM_PROTO_VER) num_servers_new_proto++; break; } new_server_info.ip = sockaddr->sin_addr.s_addr; new_server_info.port = sockaddr->sin_port; new_server_info.ping = get_wall_time() - cserver->stamp; new_server_info.host_name = buffer_read_string(buffer); new_server_info.map_name = buffer_read_string(buffer); new_server_info.num_players = buffer_read_uint8(buffer); new_server_info.max_players = buffer_read_uint8(buffer); new_server_info.authenticating = buffer_read_uint8(buffer); add_new_found_server(&new_server_info); add_new_server(&rumoured_servers, sockaddr, time(NULL)); servers = buffer_read_uint16(buffer); while(servers--) { s.sin_addr.s_addr = buffer_read_uint32(buffer); s.sin_port = buffer_read_uint16(buffer); t = buffer_read_uint32(buffer); if(!find_queried_server(s.sin_addr.s_addr, s.sin_port)) { if(add_new_server(&unqueried_servers, &s, t)) num_servers_unqueried++; } } break; } LL_NEXT(cserver); } }
/* * Send execute messae. "conn" should at the point right after the message * kind was read. */ void process_execute(char *buf, PGconn *conn) { int len; char *portal; int maxrows; char *bufp; SKIP_TABS(buf); len = sizeof(int); portal = buffer_read_string(buf, &bufp); buf = bufp; len += strlen(portal) + 1; SKIP_TABS(buf); fprintf(stderr, "FE=> Execute(portal=\"%s\")\n", portal); SKIP_TABS(buf); maxrows = buffer_read_int(buf, &bufp); len += sizeof(int); send_char('E', conn); send_int(len, conn); send_string(portal, conn); send_int(maxrows, conn); }
/* * Send describe messae. "conn" should at the point right after the message kind * was read. */ void process_describe(char *buf, PGconn *conn) { char kind; int len; char *stmt; char *bufp; SKIP_TABS(buf); len = sizeof(int); kind = buffer_read_char(buf, &bufp); buf = bufp; len += 1; SKIP_TABS(buf); stmt = buffer_read_string(buf, &bufp); buf = bufp; len += strlen(stmt) + 1; SKIP_TABS(buf); if (kind == 'S') { fprintf(stderr, "FE=> Describe(stmt=\"%s\")\n", stmt); } else if (kind == 'P') { fprintf(stderr, "FE=> Describe(portal=\"%s\")\n", stmt); } else { fprintf(stderr, "Close: unknown kind:%c\n", kind); exit(1); } send_char('D', conn); send_int(len, conn); send_char(kind, conn); send_string(stmt, conn); free(stmt); }
int main(int argc, char *argv[]) { char *s; if (argc != 1) usage("test-line-buffer < input.txt"); if (buffer_init(NULL)) die_errno("open error"); while ((s = buffer_read_line())) { s = buffer_read_string(strtouint32(s)); fputs(s, stdout); fputc('\n', stdout); buffer_skip_bytes(1); if (!(s = buffer_read_line())) break; buffer_copy_bytes(strtouint32(s) + 1); } if (buffer_deinit()) die("input error"); if (ferror(stdout)) die("output error"); buffer_reset(); return 0; }
/* * Send parse messae. "conn" should at the point right after the message kind * was read. */ void process_parse(char *buf, PGconn *conn) { char *query; int len; char *stmt; short noids; int oids[MAXENTRIES]; int i; char *bufp; SKIP_TABS(buf); len = sizeof(int); stmt = buffer_read_string(buf, &bufp); buf = bufp; len += strlen(stmt) + 1; SKIP_TABS(buf); query = buffer_read_string(buf, &bufp); buf = bufp; len += strlen(query) + 1; SKIP_TABS(buf); fprintf(stderr, "FE=> Parse(stmt=\"%s\", query=\"%s\")", stmt, query); noids = buffer_read_int(buf, &bufp); buf = bufp; if (noids > MAXENTRIES) { fprintf(stderr, "Too many oid params for parse message (%d)\n", noids); exit(1); } len += sizeof(short) + noids * sizeof(int); if (noids > 0) { fprintf(stderr, ", oids={"); for (i = 0; i < noids; i++) { oids[i] = buffer_read_int(buf, &bufp); fprintf(stderr, "%d", oids[i]); if ((i + 1) != noids) fprintf(stderr, ","); buf = bufp; } } fprintf(stderr, "\n"); send_char('P', conn); send_int(len, conn); send_string(stmt, conn); free(stmt); send_string(query, conn); free(query); send_int16(noids, conn); if (noids > 0) { for (i = 0; i < noids; i++) { send_int(oids[i], conn); } } }
/* * Send bind message. "conn" should be at the point right after the message kind * was read. */ void process_bind(char *buf, PGconn *conn) { int len; char *stmt; char *portal; short nparams; short ncodes; short codes[MAXENTRIES]; int paramlens[MAXENTRIES]; char *paramvals[MAXENTRIES]; short nresult_formatcodes; short result_formatcodes[MAXENTRIES]; int i; char *bufp; SKIP_TABS(buf); len = sizeof(int); portal = buffer_read_string(buf, &bufp); buf = bufp; len += strlen(portal) + 1; SKIP_TABS(buf); stmt = buffer_read_string(buf, &bufp); buf = bufp; len += strlen(stmt) + 1; fprintf(stderr, "FE=> Bind(stmt=\"%s\", portal=\"%s\")", stmt, portal); SKIP_TABS(buf); ncodes = buffer_read_int(buf, &bufp); len += sizeof(short) + sizeof(short) * ncodes; buf = bufp; SKIP_TABS(buf); if (ncodes > MAXENTRIES) { fprintf(stderr, "Too many codes for bind message (%d)\n", ncodes); exit(1); } if (ncodes > 0) { for (i = 0; i < ncodes; i++) { codes[i] = buffer_read_int(buf, &bufp); buf = bufp; SKIP_TABS(buf); } } nparams = buffer_read_int(buf, &bufp); len += sizeof(short) + sizeof(short) * nparams; buf = bufp; SKIP_TABS(buf); if (nparams > MAXENTRIES) { fprintf(stderr, "Too many params for bind message (%d)\n", nparams); exit(1); } for (i = 0; i < nparams; i++) { paramlens[i] = buffer_read_int(buf, &bufp); len += sizeof(int); if (paramlens[i] > 0) { buf = bufp; paramvals[i] = buffer_read_string(buf, &bufp); buf = bufp; SKIP_TABS(buf); len += paramlens[i]; } } SKIP_TABS(buf); nresult_formatcodes = buffer_read_int(buf, &bufp); buf = bufp; len += sizeof(short) + sizeof(short) * nresult_formatcodes; SKIP_TABS(buf); if (nresult_formatcodes >= 2) { for (i = 0; i < nresult_formatcodes; i++) { result_formatcodes[i] = buffer_read_int(buf, &bufp); buf = bufp; SKIP_TABS(buf); } } fprintf(stderr, "\n"); send_char('B', conn); send_int(len, conn); send_string(portal, conn); send_string(stmt, conn); send_int16(ncodes, conn); for (i = 0; i < ncodes; i++) { send_int16(codes[i], conn); } send_int16(nparams, conn); for (i = 0; i < nparams; i++) { if (paramlens[i] != -1) { if (ncodes == 0 || codes[i] == 0) { send_string(paramvals[i], conn); } else { send_int(atoi(paramvals[i]), conn); } } } send_int16(nresult_formatcodes, conn); for (i = 0; i < nresult_formatcodes; i++) { send_int16(result_formatcodes[i], conn); } }