int Athena_ParseEngineMessage(struct Athena_MessageList *to, const char *(*read_function)(void *arg, uint32_t len), void(*free_function)(void *arg, const char *z), void *arg){ /* end is the ending position in msg, as will be passed to TurboJSON */ const char *const head = read_function(arg, 4), *msg = NULL, *end = NULL; if(!head) return -1; if(memcmp(head, "HTTP", 4)==0 || memcmp(head, "http", 4)==0){ return -3; /* Not implemented yet. */ } else if(memcmp(head, "Athe", 4)==0){ const char *const head2 = read_function(arg, 10); char c; int x = 0; if(!head2 || memcmp(head2, "na Message", 10)!=0) return -8; /* Bad Athena header. */ if(free_function){ free_function(arg, head); free_function(arg, head2); } do{ /* Burn through the rest of the line. Normally this will be a single space, to pad the first line to 16 bytes. */ const char *ch = read_function(arg, 1); if(!ch || ++x > 33) return -8; c = ch[0]; if(free_function) free_function(arg, ch); }while(c!='\n'); } return msg!=end; }
// read a variable and push in onto the stack. this returns 1 if a "normal" // variable was read, or 0 if an end-table or end-function marker was read (in which case // nothing is pushed onto the stack). static int read_variable( Transport *tpt, lua_State *L ) { struct exception e; uint8_t type = transport_read_uint8_t( tpt ); switch( type ) { case RPC_NIL: lua_pushnil( L ); break; case RPC_BOOLEAN: lua_pushboolean( L, transport_read_uint8_t( tpt ) ); break; case RPC_NUMBER: lua_pushnumber( L, transport_read_number( tpt ) ); break; case RPC_STRING: { uint32_t len = transport_read_uint32_t( tpt ); char *s = ( char * )alloca( len + 1 ); transport_read_string( tpt, s, len ); s[ len ] = 0; lua_pushlstring( L, s, len ); break; } case RPC_TABLE: read_table( tpt, L ); break; case RPC_TABLE_END: return 0; case RPC_FUNCTION: read_function( tpt, L ); break; case RPC_FUNCTION_END: return 0; case RPC_REMOTE: read_index( tpt, L ); break; default: e.errnum = type; e.type = fatal; Throw( e ); } return 1; }
void get_function(void) { function = read_function(); function_symbol = search_symbol(function, &function_code, 0); if (function_symbol == NULL) { function_symbol = poolp; pushstr("ECL_NIL"); pushc('\0'); } function_c_name = translate_function(function); }
char * read_token(void) { int c; int left_paren = 0; char *p; p = poolp; c = readc(); while (isspace(c)) c = readc(); do { if (c == '(') { left_paren++; pushc(c); } else if (c == ')') { if (left_paren == 0) { break; } else { left_paren--; pushc(c); } } else if (isspace(c) && left_paren == 0) { do c = readc(); while (isspace(c)); break; } else if (c == '@') { c = readc(); if (c == '\'') { (void)read_symbol(0); poolp--; } else if (c == '[') { (void)read_symbol(1); poolp--; } else if (c == '@') { pushc(c); } else { char *name; unreadc(c); poolp = name = read_function(); (void)translate_function(poolp); } } else { pushc(c); } c = readc(); } while (1); unreadc(c); pushc('\0'); return(p); }
ssize_t gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, char * str, size_t len, int fd) { ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, int fd); char *pos, *nextpos; size_t rlen, nslen; int end_of_buffer; ssize_t ret; if (request == NULL || request->read_function == NULL) read_function = gftp_fd_read; else read_function = request->read_function; if (*rbuf == NULL) { *rbuf = g_malloc0 (sizeof (**rbuf)); (*rbuf)->max_bufsize = len; (*rbuf)->buffer = g_malloc0 ((gulong) ((*rbuf)->max_bufsize + 1)); if ((ret = read_function (request, (*rbuf)->buffer, (*rbuf)->max_bufsize, fd)) <= 0) { gftp_free_getline_buffer (rbuf); return (ret); } (*rbuf)->buffer[ret] = '\0'; (*rbuf)->cur_bufsize = ret; (*rbuf)->curpos = (*rbuf)->buffer; } ret = 0; while (1) { pos = strchr ((*rbuf)->curpos, '\n'); end_of_buffer = (*rbuf)->curpos == (*rbuf)->buffer && ((*rbuf)->max_bufsize == (*rbuf)->cur_bufsize || (*rbuf)->eof); if ((*rbuf)->cur_bufsize > 0 && (pos != NULL || end_of_buffer)) { if (pos != NULL) { nslen = pos - (*rbuf)->curpos + 1; nextpos = pos + 1; if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') pos--; *pos = '\0'; } else { nslen = (*rbuf)->cur_bufsize; nextpos = NULL; /* This is not an overflow since we allocated one extra byte to buffer above */ ((*rbuf)->buffer)[nslen] = '\0'; } strncpy (str, (*rbuf)->curpos, len); str[len - 1] = '\0'; (*rbuf)->cur_bufsize -= nslen; if (nextpos != NULL) (*rbuf)->curpos = nextpos; else (*rbuf)->cur_bufsize = 0; ret = nslen; break; } else { if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') { rlen = (*rbuf)->max_bufsize; pos = (*rbuf)->buffer; } else { memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; } (*rbuf)->curpos = (*rbuf)->buffer; if ((*rbuf)->eof) ret = 0; else { ret = read_function (request, pos, rlen, fd); if (ret < 0) { gftp_free_getline_buffer (rbuf); return (ret); } } if (ret == 0) { if ((*rbuf)->cur_bufsize == 0) { gftp_free_getline_buffer (rbuf); return (ret); } (*rbuf)->eof = 1; } (*rbuf)->cur_bufsize += ret; (*rbuf)->buffer[(*rbuf)->cur_bufsize] = '\0'; } } return (ret); }
void main_loop(void) { int c; int in_defun=0; char *p; lineno = 1; reset(); put_lineno(); LOOP: c = jump_to_at(); if (c == ')') { if (!in_defun) error("unmatched @) found"); in_defun = 0; putc('}',out); reset(); goto LOOP; } else if (c == '\'') { char *p; poolp = pool; p = read_symbol(0); pushc('\0'); fprintf(out,"%s",p); goto LOOP; } else if (c == '[') { char *p; poolp = pool; p = read_symbol(1); pushc('\0'); fprintf(out,"%s",p); goto LOOP; } else if (c != '(') { char *p; unreadc(c); poolp = pool; poolp = p = read_function(); fprintf(out,"%s",translate_function(poolp)); goto LOOP; } p = read_token(); if (strcmp(p, "defun") == 0) { if (in_defun) error("@) expected before new function definition"); in_defun = 1; get_function(); get_lambda_list(); put_fhead(); put_lineno(); c = jump_to_at(); put_declaration(); put_lineno(); } else if (strcmp(p, "return") == 0) { tab_save = tab; get_return(); put_return(); } else error_symbol(p); goto LOOP; }