static int recv_greeting(req_t *req) { /* Performs the initial handshake with the client * (SOMEDAY including authentication & encryption, if needed). * Returns 0 if the greeting is valid, or -1 on error. */ int n; char buf[MAX_SOCK_LINE]; Lex l; int done = 0; int tok; assert(req->sd >= 0); if ((n = read_line(req->sd, buf, sizeof(buf))) < 0) { log_msg(LOG_NOTICE, "Unable to read greeting from <%s:%d>: %s", req->fqdn, req->port, strerror(errno)); return(-1); } else if (n == 0) { log_msg(LOG_NOTICE, "Connection terminated by <%s:%d>", req->fqdn, req->port); return(-1); } DPRINTF((5, "Received greeting: %s", buf)); l = lex_create(buf, proto_strs); while (!done) { tok = lex_next(l); switch(tok) { case CONMAN_TOK_HELLO: parse_greeting(l, req); break; case LEX_EOF: case LEX_EOL: done = 1; break; default: break; } } lex_destroy(l); /* Validate greeting. */ if (!req->user) { req->user = create_string("unknown"); send_rsp(req, CONMAN_ERR_BAD_REQUEST, "Invalid greeting: no user specified"); return(-1); } /* Send response to greeting. */ return(send_rsp(req, CONMAN_ERR_NONE, NULL)); }
static int recv_req(req_t *req) { /* Receives the request from the client after the greeting has completed. * Returns 0 if the request is read OK, or -1 on error. */ int n; char buf[MAX_SOCK_LINE]; Lex l; int done = 0; int tok; assert(req->sd >= 0); if ((n = read_line(req->sd, buf, sizeof(buf))) < 0) { log_msg(LOG_NOTICE, "Unable to read request from <%s:%d>: %s", req->fqdn, req->port, strerror(errno)); return(-1); } else if (n == 0) { log_msg(LOG_NOTICE, "Connection terminated by <%s:%d>", req->fqdn, req->port); return(-1); } DPRINTF((5, "Received request: %s", buf)); l = lex_create(buf, proto_strs); while (!done) { tok = lex_next(l); switch(tok) { case CONMAN_TOK_CONNECT: req->command = CONMAN_CMD_CONNECT; parse_cmd_opts(l, req); break; case CONMAN_TOK_MONITOR: req->command = CONMAN_CMD_MONITOR; parse_cmd_opts(l, req); break; case CONMAN_TOK_QUERY: req->command = CONMAN_CMD_QUERY; parse_cmd_opts(l, req); break; case LEX_EOF: case LEX_EOL: done = 1; break; default: break; } } lex_destroy(l); return(0); }
void lex_parse_test(char *buf, char *toks[]) { Lex l; int tok; int newline = 1; if (!buf || !(l = lex_create(buf, toks))) return; while ((tok = lex_next(l)) != LEX_EOF) { assert(lex_prev(l) == tok); if (newline) { printf("%3d: ", lex_line(l)); newline = 0; } switch(tok) { case LEX_ERR: printf("ERR\n"); newline = 1; break; case LEX_EOL: printf("EOL\n"); newline = 1; break; case LEX_INT: printf("INT(%d) ", atoi(lex_text(l))); break; case LEX_STR: printf("STR(%s) ", lex_text(l)); break; default: if (tok < LEX_TOK_OFFSET) printf("CHR(%c) ", lex_text(l)[0]); else if (toks) printf("TOK(%d:%s) ", tok, toks[LEX_UNTOK(tok)]); else printf("\nINTERNAL ERROR: line=%d, tok=%d, str=\"%s\"\n", lex_line(l), lex_prev(l), lex_text(l)); break; } } lex_destroy(l); return; }
int main(int argc, char *argv[]) { lex_data_t d; lex_token_t t; if(argc < 2) { printf("Usage: %s source.code\n", argv[0]); exit(1); } lex_initialize(&d, argv[1]); while(lex_get_token(&d, &t) == 0) { switch(t.type) { case LEX_INTEGER: case LEX_DOUBLE: case LEX_LITERAL: case LEX_IDENTIFIER: case LEX_KW_INT: case LEX_KW_DOUBLE: case LEX_KW_STRING: case LEX_KW_AUTO: case LEX_KW_CIN: case LEX_KW_COUT: case LEX_KW_FOR: case LEX_KW_IF: case LEX_KW_ELSE: case LEX_KW_RETURN: printf("%d [%s, %s]\n", d.line + 1, ENUM_TO_STR(t.type), t.val); break; case LEX_EOF: printf("%d [%s, ]\n", d.line + 1, ENUM_TO_STR(t.type)); exit(0); break; default: printf("%d [%s, ]\n", d.line + 1, ENUM_TO_STR(t.type)); } } lex_destroy(&d); }