void dir_echo(int sockfd) { ssize_t n; char recv_buf[2048]; char send_buf[20480]; char dir[1024]; int ret; again: if((n = read(sockfd, recv_buf, 2048))> 0) { printf("Recved Request:\n"); write(STDOUT_FILENO, recv_buf, n); sscanf(recv_buf, "GET %s", dir); printf("client request for:%s\n", dir); ret = dir_or_file(dir); if(ret == 1) { read_dir(send_buf, dir); } else if(ret == 2) { read_file(send_buf, dir); } else { fill_error(send_buf, dir, ret); } write(sockfd, send_buf, strlen(send_buf)); close(sockfd); } else if(n < 0 && errno == EINTR) { printf("EINTR\n"); goto again; } else printf("Read request from client error\n"); }
int main(int argc, const char *argv[]) { int x_max,y_max; init_curses(); draw_bottom(); #ifdef debug fprintf(moon_log,"hello yaomoon\n"); #endif fill_state(); display_win(&state); fill_error(); display_win(&error); //wprintw(state.win,"%s %s\n","name","yaomoon"); //wrefresh(state.win); while(1); endwin(); return 0; }
/* * Parses a file with an existing JParser. */ gboolean j_parse_more(JParser * p, const gchar * path, GError ** error) { gboolean ret = FALSE; gchar *all = readall(path, error); if (all == NULL) { return ret; } guint i, len = strlen(all); JParserState state = J_STATE_NEW; guint line = 1; guint start = 0; gchar *dname = NULL; GList *groups = NULL; /* stack */ for (i = 0; i < len; i++) { gchar c = all[i]; switch (state) { case J_STATE_COMMENT: if (j_isnewline(c)) { state = J_STATE_NEW; line++; } break; case J_STATE_NEW: if (j_iscomment(c)) { state = J_STATE_COMMENT; } else if (j_isnewline(c)) { /* skip empty line */ line++; } else if (j_isalpha(c)) { state = J_STATE_DIRECTIVE_NAME; start = i; } else if (j_isgroup(c)) { state = J_STATE_GROUP; } else if (!j_isspace(c)) { fill_error(error, "directive name must start with " "ascii letter, at %s:%u", path, line); goto OUT; } break; case J_STATE_DIRECTIVE_NAME: if (j_iscomment(c) || j_isnewline(c)) { dname = g_strndup(all + start, i - start); if (groups == NULL && g_strcmp0(INCLUDE_CONF, dname) == 0) { fill_error(error, "missing file path, at %s:%u", path, line); goto OUT; } JDirective *d = j_directive_alloc_take(dname, NULL); dname = NULL; if (groups == NULL) { j_parser_append_directive(p, d); } else { j_group_append_directive((JGroup *) groups->data, d); } if (j_iscomment(c)) { state = J_STATE_COMMENT; } else { state = J_STATE_NEW; line++; } } else if (j_isspace(c)) { state = J_STATE_DIRECTIVE_NAME_END; dname = g_strndup(all + start, i - start); } else if (!j_isname(c)) { fill_error(error, "directive name must only contain letters," " digits or underline, at %s:%u", path, line); goto OUT; } break; case J_STATE_DIRECTIVE_NAME_END: if (j_iscomment(c) || j_isnewline(c)) { if (groups == NULL && g_strcmp0(INCLUDE_CONF, dname) == 0) { fill_error(error, "missing file path, at %s:%u", path, line); goto OUT; } JDirective *d = j_directive_alloc_take(dname, NULL); if (groups == NULL) { j_parser_append_directive(p, d); } else { j_group_append_directive((JGroup *) groups->data, d); } dname = NULL; if (j_iscomment(c)) { state = J_STATE_COMMENT; } else { state = J_STATE_NEW; line++; } } else if (!j_isspace(c)) { state = J_STATE_DIRECTIVE_VALUE; start = i; } break; case J_STATE_DIRECTIVE_VALUE: if (j_iscomment(c) || j_isnewline(c)) { if (groups == NULL && g_strcmp0(INCLUDE_CONF, dname) == 0) { gchar *path = g_strstrip(g_strndup(all + start, i - start)); gboolean more = j_parse_more(p, path, error); g_free(path); if (!more) { goto OUT; } g_free(dname); } else { JDirective *d = j_directive_alloc_take(dname, g_strndup(all + start, i - start)); if (groups == NULL) { j_parser_append_directive(p, d); } else { j_group_append_directive((JGroup *) groups->data, d); } } dname = NULL; if (j_iscomment(c)) { state = J_STATE_COMMENT; } else { state = J_STATE_NEW; line++; } } break; case J_STATE_GROUP: if (j_isgroupend(c)) { state = J_STATE_GROUP_END; } else if (j_isspace(c)) { state = J_STATE_GROUP_START; } else if (j_isalpha(c)) { state = J_STATE_GROUP_START_NAME; start = i; } else { fill_error(error, "unexpected character %c, at %s:%u", c, path, line); goto OUT; } break; case J_STATE_GROUP_START: if (j_isalpha(c)) { state = J_STATE_GROUP_START_NAME; start = i; } else if (!j_isspace(c)) { fill_error(error, "unexpected character %c, at %s:%u", c, path, line); goto OUT; } break; case J_STATE_GROUP_START_NAME: if (j_iscomment(c)) { fill_error(error, "unexpected character %c in group name, at %s:%u", c, path, line); goto OUT; } else if (j_isnewline(c)) { fill_error(error, "unexpected EOL at %s:%u", path, line); goto OUT; } else if (j_isclose(c)) { JGroup *g = j_group_alloc_take(g_strndup(all + start, i - start), NULL); if (groups == NULL) { g = j_parser_append_group(p, g); } else { g = j_group_append_group((JGroup *) groups->data, g); } groups = g_list_prepend(groups, g); state = J_STATE_NEW; } else if (j_isspace(c)) { state = J_STATE_GROUP_START_NAME_END; dname = g_strndup(all + start, i - start); } else if (!j_isname(c)) { fill_error(error, "unexpected character %c, at %s:%u", c, path, line); goto OUT; } break; case J_STATE_GROUP_START_NAME_END: if (j_iscomment(c)) { fill_error(error, "unexpected character %c, at %s:%u", c, path, line); goto OUT; } else if (j_isnewline(c)) { fill_error(error, "unexpected EOL at %s:%u", path, line); goto OUT; } else if (j_isclose(c)) { JGroup *g = j_group_alloc_take(dname, NULL); if (groups == NULL) { g = j_parser_append_group(p, g); } else { g = j_group_append_group((JGroup *) groups->data, g); } groups = g_list_prepend(groups, g); dname = NULL; state = J_STATE_NEW; } else if (!j_isspace(c)) { state = J_STATE_GROUP_START_VALUE; start = i; } break; case J_STATE_GROUP_START_VALUE: if (j_iscomment(c)) { fill_error(error, "unexpected character %c at %s:%u", c, path, line); goto OUT; } else if (j_isnewline(c)) { fill_error(error, "unexpected EOL at %s:%u", path, line); goto OUT; } else if (j_isclose(c)) { JGroup *g = j_group_alloc_take(dname, g_strndup(all + start, i - start)); if (groups == NULL) { g = j_parser_append_group(p, g); } else { g = j_group_append_group((JGroup *) groups->data, g); } groups = g_list_prepend(groups, g); dname = NULL; state = J_STATE_NEW; } break; case J_STATE_GROUP_END: if (j_isalpha(c)) { state = J_STATE_GROUP_END_NAME; start = i; } else if (!j_isspace(c)) { fill_error(error, "unexpected character %c at %s:%u", path, line); goto OUT; } break; case J_STATE_GROUP_END_NAME: if (j_isspace(c)) { dname = g_strndup(all + start, i - start); state = J_STATE_GROUP_END_NAME_END; } else if (j_isclose(c)) { dname = g_strndup(all + start, i - start); if (groups) { JGroup *g = (JGroup *) groups->data; if (g_strcmp0(dname, j_group_get_name(g))) { fill_error(error, "group name doesn't match, at %s, %u", path, line); goto OUT; } GList *next = g_list_next(groups); g_list_free1(groups); groups = next; g_free(dname); dname = NULL; } else { fill_error(error, "unexpected group end, at %s:%u", path, line); goto OUT; } state = J_STATE_NEW; } else if (!j_isname(c)) { fill_error(error, "unexpected character %c in group name, at %s:%u", c, path, line); goto OUT; } break; case J_STATE_GROUP_END_NAME_END: if (j_isclose(c)) { dname = g_strndup(all + start, i - start); if (groups) { JGroup *g = (JGroup *) groups->data; if (g_strcmp0(dname, j_group_get_name(g))) { fill_error(error, "group name doesn't match, at %s, %u", path, line); goto OUT; } GList *next = g_list_next(groups); g_list_free1(groups); groups = next; g_free(dname); dname = NULL; } else { fill_error(error, "unexpected group end, at %s:%u", path, line); goto OUT; } state = J_STATE_NEW; } else if (!j_isspace(c)) { fill_error(error, "unexpected character %c, at %s:%u", path, line); goto OUT; } break; } } if (state != J_STATE_NEW) { fill_error(error, "unexpected EOF at %s", path); goto OUT; } else if (groups != NULL) { fill_error(error, "missing group end at %s", path); goto OUT; } ret = TRUE; OUT: g_free(all); g_list_free(groups); g_free(dname); return ret; }