/** * Append specific string to destination string. */ void string_append(string_t* pstr_dest, const string_t* cpstr_src) { string_append_cstr(pstr_dest, string_c_str(cpstr_src)); }
/** * Find string in string in a backward direction. */ size_t string_rfind(const string_t* cpstr_string, const string_t* cpstr_find, size_t t_pos) { return string_rfind_cstr(cpstr_string, string_c_str(cpstr_find), t_pos); }
/** * Find string for last character that is not matches any character of specific string. */ size_t string_find_last_not_of(const string_t* cpstr_string, const string_t* cpstr_find, size_t t_pos) { return string_find_last_not_of_cstr(cpstr_string, string_c_str(cpstr_find), t_pos); }
/** * Compare two string. */ int string_compare(const string_t* cpstr_first, const string_t* cpstr_second) { return string_compare_cstr(cpstr_first, string_c_str(cpstr_second)); }
/** * Compare the first sub string and the second string. */ int string_compare_substring_string( const string_t* cpstr_first, size_t t_firstpos, size_t t_firstlen, const string_t* cpstr_second) { return string_compare_substring_cstr(cpstr_first, t_firstpos, t_firstlen, string_c_str(cpstr_second)); }
// EOF --> rcode == -2 // errors --> rcode == -1 BOOL xyzsh_readline_interface_onetime(int* rcode, char* cmdline, int cursor_pos, char* source_name, char** argv, int argc, fXyzshJobDone xyzsh_job_done_) { /// start interactive shell /// xyzsh_job_done = xyzsh_job_done_; /// edit line /// char* buf = ALLOC run_editline(cmdline, cursor_pos); /// run /// if(buf == NULL) { *rcode = -2; /// wait background job xyzsh_wait_background_job(); } else if(*buf) { stack_start_stack(); sObject* block = BLOCK_NEW_STACK(); int sline = 1; if(parse(buf, "xyzsh", &sline, block, NULL)) { xyzsh_set_signal(); sObject* fun = FUN_NEW_STACK(NULL); sObject* stackframe = UOBJECT_NEW_GC(8, gXyzshObject, "_stackframe", FALSE); vector_add(gStackFrames, stackframe); //uobject_init(stackframe); SFUN(fun).mLocalObjects = stackframe; sObject* argv2 = VECTOR_NEW_GC(16, FALSE); int i; for(i=0; i<argc; i++) { vector_add(argv2, STRING_NEW_GC(argv[i], FALSE)); } uobject_put(SFUN(fun).mLocalObjects, "ARGV", argv2); if(!run(block, gStdin, gStdout, rcode, gCurrentObject, fun)) { xyzsh_restore_signal_default(); if(*rcode == RCODE_BREAK) { fprintf(stderr, "invalid break. Not in a loop\n"); } else if(*rcode & RCODE_RETURN) { fprintf(stderr, "invalid return. Not in a function\n"); } else if(*rcode == RCODE_EXIT) { } else { fprintf(stderr, "run time error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); } if(*rcode != 0) { fprintf(stderr, "return code is %d\n", *rcode); } (void)vector_pop_back(gStackFrames); stack_end_stack(); /// wait background job xyzsh_wait_background_job(); FREE(buf); return FALSE; } (void)vector_pop_back(gStackFrames); } else { xyzsh_restore_signal_default(); fprintf(stderr, "parser error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); if(*rcode != 0) { fprintf(stderr, "return code is %d\n", *rcode); } stack_end_stack(); /// wait background job xyzsh_wait_background_job(); FREE(buf); return FALSE; } if(*rcode != 0) { fprintf(stderr, "return code is %d\n", *rcode); } stack_end_stack(); /// wait background job xyzsh_wait_background_job(); FREE(buf); } return TRUE; }
void xyzsh_readline_interface(char* cmdline, int cursor_point, char** argv, int argc, BOOL exit_in_spite_ofjob_exist, BOOL welcome_msg) { /// start interactive shell /// xyzsh_job_done = main_xyzsh_job_done; if(welcome_msg) { char* version = getenv("XYZSH_VERSION"); printf("-+- Welcome to xyzsh %s -+-\n", version); printf("run \"help\" command to see usage\n"); } char* buf; BOOL first = TRUE; while(1) { /// prompt /// if(first) { buf = ALLOC run_editline(cmdline, cursor_point); first = FALSE; } else { buf = ALLOC run_editline(NULL, -1); } /// run /// if(buf == NULL) { if(exit_in_spite_ofjob_exist) { break; } else { if(vector_count(gJobs) > 0) { fprintf(stderr,"\njobs exist\n"); } else { break; } } } else if(*buf) { stack_start_stack(); int rcode = 0; sObject* block = BLOCK_NEW_STACK(); int sline = 1; if(parse(buf, "xyzsh", &sline, block, NULL)) { xyzsh_set_signal(); sObject* fun = FUN_NEW_STACK(NULL); sObject* stackframe = UOBJECT_NEW_GC(8, gXyzshObject, "_stackframe", FALSE); vector_add(gStackFrames, stackframe); //uobject_init(stackframe); SFUN(fun).mLocalObjects = stackframe; sObject* argv2 = VECTOR_NEW_GC(16, FALSE); int i; for(i=0; i<argc; i++) { vector_add(argv2, STRING_NEW_GC(argv[i], FALSE)); } uobject_put(SFUN(fun).mLocalObjects, "ARGV", argv2); if(!run(block, gStdin, gStdout, &rcode, gCurrentObject, fun)) { if(rcode == RCODE_BREAK) { fprintf(stderr, "invalid break. Not in a loop\n"); } else if(rcode & RCODE_RETURN) { fprintf(stderr, "invalid return. Not in a function\n"); } else if(rcode == RCODE_EXIT) { (void)vector_pop_back(gStackFrames); stack_end_stack(); FREE(buf); break; } else { fprintf(stderr, "run time error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); } } (void)vector_pop_back(gStackFrames); } else { fprintf(stderr, "parser error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); } if(rcode != 0) { fprintf(stderr, "return code is %d\n", rcode); } stack_end_stack(); } /// wait background job xyzsh_wait_background_job(); FREE(buf); } }
void xyzsh_init(enum eAppType app_type, BOOL no_runtime_script) { setenv("XYZSH_VERSION", "1.5.8", 1); setenv("XYZSH_DOCDIR", DOCDIR, 1); setenv("XYZSH_DATAROOTDIR", DOCDIR, 1); setenv("XYZSH_EXT_PATH", EXTDIR, 1); setenv("XYZSH_SYSCONFDIR", SYSCONFDIR, 1); char* home = getenv("HOME"); if(home) { char home_library[PATH_MAX]; snprintf(home_library, PATH_MAX, "%s/.xyzsh/lib/", home); char* ld_library_path = getenv("LD_LIBRARY_PATH"); if(ld_library_path) { char ld_library_path2[512]; snprintf(ld_library_path2, 512, "%s:%s:%s", ld_library_path, EXTDIR, home_library); setenv("LD_LIBRARY_PATH", ld_library_path2, 1); } else { char ld_library_path2[512]; snprintf(ld_library_path2, 512, "%s:%s", EXTDIR, home_library); setenv("LD_LIBRARY_PATH", ld_library_path2, 1); } } else { char* ld_library_path = getenv("LD_LIBRARY_PATH"); if(ld_library_path) { char ld_library_path2[512]; snprintf(ld_library_path2, 512, "%s:%s", ld_library_path, EXTDIR); setenv("LD_LIBRARY_PATH", ld_library_path2, 1); } else { char ld_library_path2[512]; snprintf(ld_library_path2, 512, "%s", EXTDIR); setenv("LD_LIBRARY_PATH", ld_library_path2, 1); } } setlocale(LC_ALL, ""); stack_init(1);; stack_start_stack(); gErrMsg = STRING_NEW_STACK(""); gXyzshSigInt = FALSE; gXyzshSigUser = FALSE; gXyzshSigTstp = FALSE; gXyzshSigCont = FALSE; xyzsh_set_signal_other = NULL; gc_init(1); run_init(app_type); load_init(); xyzsh_editline_init(); gDirStack = VECTOR_NEW_GC(10, FALSE); uobject_put(gXyzshObject, "_dir_stack", gDirStack); char* term_env = getenv("TERM"); if(term_env != NULL && strcmp(term_env, "") != 0) { mcurses_init(); } if(!xyzsh_rehash("init", 0)) { fprintf(stderr, "run time error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); exit(1); } if(!no_runtime_script) { xyzsh_read_rc(); } else { xyzsh_read_rc_mini(); } }
static ALLOC char* run_editline(char* text, int cursor_pos) { stack_start_stack(); char* buf; char* prompt; if(gPrompt) { xyzsh_set_signal(); sObject* fun = FUN_NEW_STACK(NULL); sObject* stackframe = UOBJECT_NEW_GC(8, gXyzshObject, "_stackframe", FALSE); vector_add(gStackFrames, stackframe); //uobject_init(stackframe); SFUN(fun).mLocalObjects = stackframe; sObject* nextout = FD_NEW_STACK(); int rcode; if(!run(gPrompt, gStdin, nextout, &rcode, gCurrentObject, fun)) { if(rcode == RCODE_BREAK) { fprintf(stderr, "invalid break. Not in a loop\n"); } else if(rcode & RCODE_RETURN) { fprintf(stderr, "invalid return. Not in a function\n"); } else if(rcode == RCODE_EXIT) { fprintf(stderr, "invalid exit. In the prompt\n"); } else { fprintf(stderr, "run time error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); } } (void)vector_pop_back(gStackFrames); prompt = SFD(nextout).mBuf; } else { prompt = " > "; } char* rprompt; if(gRPrompt) { sObject* fun = FUN_NEW_STACK(NULL); sObject* stackframe = UOBJECT_NEW_GC(8, gXyzshObject, "_stackframe", FALSE); vector_add(gStackFrames, stackframe); //uobject_init(stackframe); SFUN(fun).mLocalObjects = stackframe; sObject* nextout2 = FD_NEW_STACK(); int rcode; if(!run(gRPrompt, gStdin, nextout2, &rcode, gCurrentObject, fun)) { if(rcode == RCODE_BREAK) { fprintf(stderr, "invalid break. Not in a loop\n"); } else if(rcode & RCODE_RETURN) { fprintf(stderr, "invalid return. Not in a function\n"); } else if(rcode == RCODE_EXIT) { fprintf(stderr, "invalid exit. In the prompt\n"); } else { fprintf(stderr, "run time error\n"); fprintf(stderr, "%s", string_c_str(gErrMsg)); } } (void)vector_pop_back(gStackFrames); rprompt = SFD(nextout2).mBuf; } else { rprompt = NULL; } mreset_tty(); buf = ALLOC editline(prompt, rprompt, text, cursor_pos); stack_end_stack(); return ALLOC buf; }
int input_box(char* msg, char* result, int result_size, char* def_input, int def_cursor) { gInputBoxMsg = msg; int result2 = 0; gInputBoxCursor = def_cursor; string_put(gInputBoxInput, def_input); gView = input_box_view; while(1) { xclear(); view(); //input_box_view(); refresh(); /// input /// int meta; int key = xgetch(&meta); if(key == 10 || key == 13) { result2 = 0; break; } else if(key == 6 || key == KEY_RIGHT) { input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 1); } else if(key == 2 || key == KEY_LEFT) { input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -1); } else if(key == 8 || key == KEY_BACKSPACE) { // CTRL-H if(gInputBoxCursor > 0) { char* str2 = string_c_str(gInputBoxInput); int utfpos = str_pointer2kanjipos(gKanjiCode, str2, str2 + gInputBoxCursor); char* before_point = str_kanjipos2pointer(gKanjiCode, str2, utfpos-1); int new_cursor = before_point-str2; string_erase(gInputBoxInput, before_point - str2, (str2 + gInputBoxCursor) - before_point); gInputBoxCursor = new_cursor; } } else if(key == 4 || key == KEY_DC) { // CTRL-D DELETE char* str2 = string_c_str(gInputBoxInput); if(string_length(gInputBoxInput) > 0) { if(gInputBoxCursor < string_length(gInputBoxInput)) { int utfpos = str_pointer2kanjipos(gKanjiCode, str2, str2 + gInputBoxCursor); char* next_point = str_kanjipos2pointer(gKanjiCode, str2, utfpos+1); string_erase(gInputBoxInput, gInputBoxCursor, next_point - (str2 + gInputBoxCursor)); } } } else if(key == 1 || key == KEY_HOME) { // CTRL-A input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -999); } else if(key == 5 || key == KEY_END) { // CTRL-E input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 999); } else if(key == 11) { // CTRL-K string_erase(gInputBoxInput, gInputBoxCursor, string_length(gInputBoxInput)-gInputBoxCursor); } else if(key == 21) { // CTRL-U string_put(gInputBoxInput, ""); gInputBoxCursor = 0; } else if(key == 23) { // CTRL-W if(gInputBoxCursor > 0) { const char* s = string_c_str(gInputBoxInput); int pos = gInputBoxCursor-1; if(s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"') { while(pos>=0 && (s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"')) { pos--; } } while(pos>=0 && s[pos]!=' ' && s[pos]!='/' && s[pos]!='\'' && s[pos]!='"') { pos--; } string_erase(gInputBoxInput, pos+1, gInputBoxCursor-pos-1); gInputBoxCursor = pos+1; } } else if(meta==1 && key == 'd') { // Meta-d const char* s = string_c_str(gInputBoxInput); if(s[gInputBoxCursor] != 0) { int pos = gInputBoxCursor; pos++; while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) { pos++; } while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') { pos++; } string_erase(gInputBoxInput, gInputBoxCursor, pos-gInputBoxCursor); } } else if(meta==1 && key == 'b') { // META-b if(gInputBoxCursor > 0) { const char* s = string_c_str(gInputBoxInput); int pos = gInputBoxCursor; pos--; while(pos>=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) { pos--; } while(pos>=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') { pos--; } gInputBoxCursor = pos+1; } } else if(meta==1 && key == 'f') { // META-f const char* s = string_c_str(gInputBoxInput); if(s[gInputBoxCursor] != 0) { int pos = gInputBoxCursor; pos++; while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) { pos++; } while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') { pos++; } gInputBoxCursor = pos; } } else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape result2 = 1; break; } else if(key == 12) { // CTRL-L xclear_immediately(); } else { if(meta == 0 && !(key >= 0 && key <= 27)) { char tmp[128]; snprintf(tmp, 128, "%c", key); string_insert(gInputBoxInput, gInputBoxCursor, tmp); gInputBoxCursor++; } } } gView = NULL; int maxx = mgetmaxx(); int maxy = mgetmaxy(); xstrncpy(result, string_c_str(gInputBoxInput), result_size); mmove_immediately(maxy -2, 0); #if defined(__CYGWIN__) xclear_immediately(); // 画面の再描写 view(); refresh(); #endif return result2; }
int main(int argc, char* argv[]) { string_t* pstr_str1a = create_string(); string_t* pstr_str1b = create_string(); string_t* pstr_str1c = create_string(); string_t* pstr_str2a = create_string(); string_t* pstr_str3a = create_string(); string_t* pstr_str4a = create_string(); size_t t_pos = 0; if(pstr_str1a == NULL || pstr_str1b == NULL || pstr_str1c == NULL || pstr_str2a == NULL || pstr_str3a == NULL || pstr_str4a == NULL) { return -1; } /* Searching a string for a substring as specified by a string */ string_init_cstr(pstr_str1a, "12-ab-12-ab"); string_init_cstr(pstr_str1b, "b-a"); string_init_cstr(pstr_str1c, "12"); printf("The original string str1a is \"%s\".\n", string_c_str(pstr_str1a)); t_pos = string_find_last_not_of(pstr_str1a, pstr_str1b, 5); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'b-a' in str1a before the 5th position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'b-a' were not found in the string str1a.\n"); } t_pos = string_find_last_not_of(pstr_str1a, pstr_str1c, NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of '12' in str1a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring '12' were not found in the string str1a.\n"); } string_destroy(pstr_str1a); string_destroy(pstr_str1b); string_destroy(pstr_str1c); /* Searching a string for a substring as specified by a c-string */ string_init_cstr(pstr_str2a, "BBB-111"); printf("The original string str2a is \"%s\".\n", string_c_str(pstr_str2a)); t_pos = string_find_last_not_of_cstr(pstr_str2a, "B1", 6); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'B1' in str2a before the 6th position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'B1' were not found in the string str2a.\n"); } t_pos = string_find_last_not_of_cstr(pstr_str2a, "B-1", NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'B-1' in str2a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'B-1' were not found in the string str2a.\n"); } string_destroy(pstr_str2a); /* Searching a string for a substring as specified by part of a c-string */ string_init_cstr(pstr_str3a, "444-555-GGG"); printf("The original string str3a is \"%s\".\n", string_c_str(pstr_str3a)); t_pos = string_find_last_not_of_cstr(pstr_str3a, "45G", NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of '45G' in str3a is %u.\n", t_pos); } else { printf("Elements other than those in the substring '45G' were not found in the string str3a.\n"); } t_pos = string_find_last_not_of_subcstr(pstr_str3a, "45G", 6, t_pos - 1); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of '45G' in str3a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring '45G' were not found in the string str3a.\n"); } string_destroy(pstr_str3a); /* Searching a string for a substring as specified by a single character */ string_init_cstr(pstr_str4a, "dddd-1dd4-abdd"); printf("The original string str4a is \"%s\".\n", string_c_str(pstr_str4a)); t_pos = string_find_last_not_of_char(pstr_str4a, 'd', 7); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'd' in str4a is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'd' were not found in the string str4a.\n"); } t_pos = string_find_last_not_of_char(pstr_str4a, 'x', NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'x' in str4a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'x' were not found in the string str4a.\n"); } string_destroy(pstr_str4a); return 0; }
status_t handle_list_command(user_session_t *session, string_t *args, size_t len) { /* Possible codes: 125: Data connection already open; transfer starting 150: File status okay; about to open data connection 226: Closing data connection; success 250: File action okay, completed 425: Can't open data connection 426: Connection closed 451: Aborted; local error 450: Requested file action not taken 500: Syntax error; command unrecognized 501: Syntax error in params 502: Not implemented 421: Service connection closing 530: Not logged in */ status_t error; if (!session->logged_in) { error = send_530(session); goto exit0; } if (session->data_sock < 0) { error = send_425(session); goto exit0; } string_t listing; string_initialize(&listing); string_t tmp; string_initialize(&tmp); DIR *directory; if (len < 2) { directory = opendir(session->directory); if (directory == NULL) { error = send_451(session); goto exit1; } struct dirent *entry; while ((entry = readdir(directory))) { string_concatenate_char_array(&listing, entry->d_name); char_vector_push_back(&listing, '\n'); } closedir(directory); } else { string_assign_from_char_array(&tmp, session->directory); char_vector_push_back(&tmp, '/'); string_concatenate(&tmp, args + 1); if (access(string_c_str(&tmp), F_OK) < 0) { error = send_501(session); goto exit1; } directory = opendir(string_c_str(&tmp)); if (directory == NULL) { if (errno != ENOTDIR) { error = send_451(session); goto exit1; } //Already know that the file exists because of the call to access, //so if the file is not a directory, assuming that it's a regular //file, so just list it. This could also be checked using the stat //function. string_concatenate(&listing, args + 1); char_vector_push_back(&listing, '\n'); } else { struct dirent *entry; while ((entry = readdir(directory))) { string_concatenate_char_array(&listing, entry->d_name); char_vector_push_back(&listing, '\n'); } closedir(directory); } } error = send_125(session); if (error) { send_451(session); goto exit1; } error = send_data_string(session->data_sock, &listing, session->server->log); if (error) { send_451(session); } else { error = send_226(session); } exit1: string_uninitialize(&tmp); string_uninitialize(&listing); close(session->data_sock); session->data_sock = -1; exit0: return error; }
status_t handle_retr_command(user_session_t *session, string_t *args, size_t len) { /** * Possible codes: * 125: transfer_starting * 150: about to open data connection * 226: Completed successfully; closing * 250: Requested file action completed * 425: Can't open data connection * 426: Connection closed * 451: Action aborted; local error * 450: File unavailable (busy) * 550: File unavailable (doesn't exit) * 500, 501, 421 */ status_t error; if (!session->logged_in) { error = send_530(session); goto exit0; } if (session->data_sock < 0) { error = send_425(session); goto exit0; } if (len < 2) { error = send_501(session); goto exit1; } string_t path; string_initialize(&path); string_assign_from_char_array(&path, session->directory); char_vector_push_back(&path, '/'); string_concatenate(&path, args + 1); int fd = open(string_c_str(&path), O_RDONLY, 0); if (fd < 0) { error = send_550(session); goto exit2; } string_t file_string; string_initialize(&file_string); ssize_t chars_read; char buff[512]; while ((chars_read = read(fd, buff, sizeof buff)) > 0) { string_concatenate_char_array_with_size(&file_string, buff, sizeof buff); } if (chars_read < 0) { error = send_451(session); goto exit3; } error = send_125(session); if (error) { //let the first error supercede any that might occur here, //so don't save to error send_451(session); goto exit3; } error = send_data_string(session->data_sock, &file_string, session->server->log); if (error) { send_451(session); } else { error = send_226(session); } exit3: string_uninitialize(&file_string); exit2: string_uninitialize(&path); exit1: close(session->data_sock); session->data_sock = -1; exit0: return error; }
void *client_handler(void *void_args) { user_session_t *session = (user_session_t *) void_args; status_t error; //Finish session initialization session->directory = realpath(".", NULL); if (session->directory == NULL) { error = REALPATH_ERROR; goto exit0; } session->data_sock = -1; //End session initialization error = send_response(session->command_sock, SERVICE_READY, "Ready. Please send USER.", session->server->log, 0); if (error) { goto exit1; } string_t command; string_initialize(&command); uint8_t done = 0; do { char_vector_clear(&command); error = read_single_line(session->command_sock, &command); if (!error) { error = write_received_message_to_log(session->server->log, &command); if (!error) { //Remove the CRLF from the command char_vector_pop_back(&command); char_vector_pop_back(&command); //Split the string up by spaces size_t len; string_t *split = string_split_skip_consecutive(&command, ' ', &len, 1); char *c_str = string_c_str(split + 0); //Determine which command has been sent if (bool_strcmp(c_str, "USER")) { error = handle_user_command(session, split, len); } else if (bool_strcmp(c_str, "PASS")) { error = handle_pass_command(session, split, len); } else if (bool_strcmp(c_str, "CWD")) { error = handle_cwd_command(session, split, len); } else if (bool_strcmp(c_str, "CDUP")) { error = handle_cdup_command(session, split, len); } else if (bool_strcmp(c_str, "QUIT")) { done = 1; error = handle_quit_command(session, split, len); } else if (bool_strcmp(c_str, "PASV")) { error = handle_pasv_command(session, split, len); } else if (bool_strcmp(c_str, "EPSV")) { error = handle_epsv_command(session, split, len); } else if (bool_strcmp(c_str, "PORT")) { error = handle_port_command(session, split, len); } else if (bool_strcmp(c_str, "EPRT")) { error = handle_eprt_command(session, split, len); } else if (bool_strcmp(c_str, "RETR")) { error = handle_retr_command(session, split, len); } else if (bool_strcmp(c_str, "PWD")) { error = handle_pwd_command(session, split, len); } else if (bool_strcmp(c_str, "LIST")) { error = handle_list_command(session, split, len); } else if (bool_strcmp(c_str, "HELP")) { error = handle_help_command(session, split, len); } else { error = handle_unrecognized_command(session, split, len); } size_t i; for (i = 0; i < len; i++) { string_uninitialize(split + i); } free(split); } } if (error) { char message[] = "Error encountered while processing: "; string_t error_message; string_initialize(&error_message); char *error_string = get_error_message(error); string_assign_from_char_array(&error_message, error_string); prepend_and_write_to_log(session->server->log, &error_message, message, sizeof message); string_uninitialize(&error_message); } } while (!error && !done); char quitting_message[] = "Client quitting.\n"; exit2: string_uninitialize(&command); exit1: free(session->directory); exit0: write_log(session->server->log, quitting_message, sizeof quitting_message); printf("%s", quitting_message); free(session); pthread_exit(NULL); }
/** * Quick sort or heap sort for specify range. */ void _algo_intro_sort_if( random_access_iterator_t it_first, random_access_iterator_t it_last, bfun_t bfun_op, size_t t_depth, void* pv_value) { iterator_t it_pivot; iterator_t it_begin; iterator_t it_end; iterator_t it_prev; bool_t b_result = false; assert(_iterator_valid_range(it_first, it_last, _RANDOM_ACCESS_ITERATOR)); assert(pv_value != NULL); assert(bfun_op != NULL); if (iterator_distance(it_first, it_last) < 2) { return; } if (t_depth == 0){ /* do heap sort */ algo_partial_sort_if(it_first, it_last, it_last, bfun_op); return; } t_depth--; it_pivot = iterator_advance(it_first, iterator_distance(it_first, it_last) / 2); it_prev = iterator_prev(it_last); it_pivot = _algo_median_of_three_if(it_first, it_pivot, it_prev, bfun_op); /* the pv_value must be string_t type when the container type is char* */ if (strncmp(_iterator_get_typebasename(it_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) { string_assign_cstr((string_t*)pv_value, (char*)iterator_get_pointer(it_pivot)); it_begin = it_first; it_end = it_last; for (;;) { /* move begin */ (*bfun_op)(iterator_get_pointer(it_begin), string_c_str((string_t*)pv_value), &b_result); while (b_result) { it_begin = iterator_next(it_begin); (*bfun_op)(iterator_get_pointer(it_begin), string_c_str((string_t*)pv_value), &b_result); } /* move end */ it_end = iterator_prev(it_end); (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_end), &b_result); while (b_result) { it_end = iterator_prev(it_end); (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_end), &b_result); } /* across */ if (!iterator_less(it_begin, it_end)) { it_pivot = it_begin; break; } else { algo_iter_swap(it_begin, it_end); it_begin = iterator_next(it_begin); } } } else { iterator_get_value(it_pivot, pv_value); it_begin = it_first; it_end = it_last; for (;;) { /* move begin */ (*bfun_op)(iterator_get_pointer(it_begin), pv_value, &b_result); while (b_result) { it_begin = iterator_next(it_begin); (*bfun_op)(iterator_get_pointer(it_begin), pv_value, &b_result); } /* move end */ it_end = iterator_prev(it_end); (*bfun_op)(pv_value, iterator_get_pointer(it_end), &b_result); while (b_result) { it_end = iterator_prev(it_end); (*bfun_op)(pv_value, iterator_get_pointer(it_end), &b_result); } /* across */ if (!iterator_less(it_begin, it_end)) { it_pivot = it_begin; break; } else { algo_iter_swap(it_begin, it_end); it_begin = iterator_next(it_begin); } } } /* sort [it_first, it_pivot) */ _algo_intro_sort_if(it_first, it_pivot, bfun_op, t_depth, pv_value); /* sort [it_pivot, it_last) */ _algo_intro_sort_if(it_pivot, it_last, bfun_op, t_depth, pv_value); }
/** * Insertion sort for specify range. */ void _algo_insertion_sort_if(random_access_iterator_t it_first, random_access_iterator_t it_last, bfun_t bfun_op, void* pv_value) { iterator_t it_bound; iterator_t it_next; iterator_t it_tmp; iterator_t it_prev; bool_t b_result = false; assert(_iterator_valid_range(it_first, it_last, _RANDOM_ACCESS_ITERATOR)); assert(pv_value != NULL); assert(bfun_op != NULL); if (iterator_equal(it_first, it_last)) { return; } if (strncmp(_iterator_get_typebasename(it_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) { for (it_bound = iterator_next(it_first); !iterator_equal(it_bound, it_last); it_bound = iterator_next(it_bound)) { string_assign_cstr((string_t*)pv_value, (char*)iterator_get_pointer(it_bound)); (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_first), &b_result); if (b_result) { /* pv_value < *it_first */ it_next = iterator_next(it_bound); algo_copy_backward(it_first, it_bound, it_next); iterator_set_value(it_first, string_c_str((string_t*)pv_value)); } else { it_tmp = it_bound; it_prev = iterator_prev(it_tmp); (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_prev), &b_result); while (b_result) { /* pv_value < *it_prev */ iterator_set_value(it_tmp, iterator_get_pointer(it_prev)); it_tmp = it_prev; it_prev = iterator_prev(it_prev); (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_prev), &b_result); } iterator_set_value(it_tmp, string_c_str((string_t*)pv_value)); } } } else { for (it_bound = iterator_next(it_first); !iterator_equal(it_bound, it_last); it_bound = iterator_next(it_bound)) { iterator_get_value(it_bound, pv_value); (*bfun_op)(pv_value, iterator_get_pointer(it_first), &b_result); if (b_result) { /* pv_value < *it_first */ it_next = iterator_next(it_bound); algo_copy_backward(it_first, it_bound, it_next); iterator_set_value(it_first, pv_value); } else { it_tmp = it_bound; it_prev = iterator_prev(it_tmp); (*bfun_op)(pv_value, iterator_get_pointer(it_prev), &b_result); while (b_result) { /* pv_value < *it_prev */ iterator_set_value(it_tmp, iterator_get_pointer(it_prev)); it_tmp = it_prev; it_prev = iterator_prev(it_prev); (*bfun_op)(pv_value, iterator_get_pointer(it_prev), &b_result); } iterator_set_value(it_tmp, pv_value); } } } }