📄 textsw_file.c
字号:
Textsw abstract; char *error_buf; Es_status status; int locx, locy;{ char *msg1, *msg2; char msg[200]; Textsw_view view = VIEW_ABS_TO_REP(abstract); Event event; switch (status) { case ES_BACKUP_FAILED: msg[0] = '\0'; msg1 = "Unable to Save Current File. "; msg2 = "Cannot back-up file:"; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_BACKUP_OUT_OF_SPACE: msg[0] = '\0'; msg1 = "Unable to Save Current File. "; msg2 = "No space for back-up file:"; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_CANNOT_OPEN_OUTPUT: msg[0] = '\0'; msg1 = "Unable to Save Current File. "; msg2 = "Cannot re-write file:"; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_CANNOT_GET_NAME: msg[0] = '\0'; msg1 = "Unable to Save Current File. "; msg2 = "INTERNAL ERROR: Forgot the name of the file."; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_UNKNOWN_ERROR: /* Fall through */ default: goto InternalError; }InternalError: msg[0] = '\0'; msg1 = "Unable to Save Current File. "; msg2 = "An INTERNAL ERROR has occurred."; strcat(msg, msg1); strcat(msg, msg2);PostError: (void) alert_prompt( (Frame)window_get(WINDOW_FROM_VIEW(abstract), WIN_OWNER), &event, ALERT_BUTTON_YES, "Continue", ALERT_TRIGGER, ACTION_STOP, ALERT_MESSAGE_STRINGS, msg1, msg2, error_buf, 0, 0); return(ES_UNKNOWN_ERROR);}/* ARGSUSED */static Es_statustextsw_save_internal(folio, error_buf, locx, locy) register Textsw_folio folio; char *error_buf; int locx, locy; /* Currently unused */{ extern Es_handle es_file_make_backup(); char original_name[MAXNAMLEN], *name; register char *msg; Es_handle backup, original = ES_NULL; int status; Es_status es_status; /* Save overwrites the contents of the original stream, which makes * the call to textsw_give_shelf_to_svc in textsw_destroy_esh * (reached via textsw_save_store_common calling textsw_replace_esh) * perform bad operations on the pieces that are the shelf. * To get around this problem, we first save the shelf explicitly. */ textsw_give_shelf_to_svc(folio); if (textsw_file_name(folio, &name) != 0) return(ES_CANNOT_GET_NAME); (void) strcpy(original_name, name); original = (Es_handle)LINT_CAST(es_get(folio->views->esh, ES_PS_ORIGINAL)); if (!original) { msg = "es_ps_original"; goto Return_Error_Status; } if ((backup = es_file_make_backup(original, "%s%%", &es_status)) == ES_NULL) { return(((es_status == ES_CHECK_ERRNO) && (errno == ENOSPC)) ? ES_BACKUP_OUT_OF_SPACE : ES_BACKUP_FAILED); } (void) es_set(folio->views->esh, ES_STATUS_PTR, &es_status, ES_PS_ORIGINAL, backup, 0); if (es_status != ES_SUCCESS) { int result; Event alert_event; result = alert_prompt( (Frame)window_get( WINDOW_FROM_VIEW( VIEW_FROM_FOLIO_OR_VIEW(folio)), WIN_OWNER), &alert_event, ALERT_MESSAGE_STRINGS, "Unable to Save Current File.", "Was the file edited with another editor?.", 0, ALERT_BUTTON_YES, "Continue", ALERT_TRIGGER, ACTION_STOP, 0); if (result == ALERT_FAILED) { (void) es_destroy(backup); status = (int)es_status; msg = "ps_replace_original"; goto Return_Error_Status; } goto Dont_Return_Error_Status; } switch (status = textsw_save_store_common(folio, original_name, RELOAD)) { case ES_SUCCESS: (void) es_destroy(original); textsw_notify(folio->first_view, TEXTSW_ACTION_LOADED_FILE, original_name, 0); return(ES_SUCCESS); case ES_CANNOT_OPEN_OUTPUT: if (errno == EACCES) goto Return_Error; msg = "es_file_create"; goto Return_Error_Status; default: msg = "textsw_save_store_common"; break; }Return_Error_Status: (void) sprintf(error_buf, " %s; status = 0x%x", msg, status);Dont_Return_Error_Status: status = ES_UNKNOWN_ERROR;Return_Error: if (original) (void) es_set(folio->views->esh, ES_STATUS_PTR, &es_status, ES_PS_ORIGINAL, original, 0); return(status);}extern unsignedtextsw_save(abstract, locx, locy) Textsw abstract; int locx, locy;{ char error_buf[MAXNAMLEN+100]; Es_status status; Textsw_view view = VIEW_ABS_TO_REP(abstract); error_buf[0] = '\0'; status = textsw_save_internal(FOLIO_FOR_VIEW(view), error_buf, locx, locy); if (status != ES_SUCCESS) status = textsw_process_save_error( abstract, error_buf, status, locx, locy); return((unsigned)status);}static Es_statustextsw_get_from_fd(view, fd, print_error_msg) register Textsw_view view; int fd; int print_error_msg;{ Textsw_folio folio = FOLIO_FOR_VIEW(view); int record; Es_index old_insert_pos, old_length; register long count; char buf[2096]; Es_status result = ES_SUCCESS; int status; textsw_flush_caches(view, TFC_PD_SEL); /* Changes length! */ textsw_input_before(view, &old_insert_pos, &old_length); for (;;) { count = read(fd, buf, sizeof(buf)-1); if (count == 0) break; if (count < 0) { return(ES_UNKNOWN_ERROR); } buf[count] = '\0'; status = ev_input_partial(FOLIO_FOR_VIEW(view)->views, buf, count); if (status) { if (print_error_msg) (void) textsw_esh_failed_msg(view, "Insertion failed - "); result = (Es_status)LINT_CAST(es_get(folio->views->esh, ES_STATUS)); break; } } record = (TXTSW_DO_AGAIN(folio) && ((folio->func_state & TXTSW_FUNC_AGAIN) == 0) ); (void) textsw_input_after(view, old_insert_pos, old_length, record); return(result);}pkg_private inttextsw_cd(textsw, locx, locy) Textsw_folio textsw; int locx, locy;{ char buf[MAXNAMLEN]; if (0 == textsw_get_selection_as_filename( textsw, buf, sizeof(buf), locx, locy)) { (void) textsw_change_directory(textsw, buf, FALSE, locx, locy); } return;}pkg_private Textsw_statustextsw_get_from_file(view, filename, print_error_msg) Textsw_view view; char *filename; int print_error_msg;{ Textsw_folio folio = FOLIO_FOR_VIEW(view); int fd; Es_status status; Textsw_status result = TEXTSW_STATUS_CANNOT_INSERT_FROM_FILE; int got_from_file = 0; char buf[MAXNAMLEN]; pkg_private int textsw_is_out_of_memory(); if (!TXTSW_IS_READ_ONLY(folio) && (strlen(filename) > 0)) { strcpy(buf, filename); if (textsw_expand_filename(folio, buf, sizeof(buf), -1, -1) == 0) { if ((fd = open(buf, 0)) >= 0) { textsw_take_down_caret(folio); textsw_checkpoint_undo(VIEW_REP_TO_ABS(view), (caddr_t)TEXTSW_INFINITY-1); status = textsw_get_from_fd(view, fd, print_error_msg); textsw_checkpoint_undo(VIEW_REP_TO_ABS(view), (caddr_t)TEXTSW_INFINITY-1); textsw_update_scrollbars(folio, TEXTSW_VIEW_NULL); (void) close(fd); if (status == ES_SUCCESS) result = TEXTSW_STATUS_OKAY; else if (textsw_is_out_of_memory(folio, status)) result = TEXTSW_STATUS_OUT_OF_MEMORY; textsw_invert_caret(folio); } } } return(result);} pkg_private inttextsw_file_stuff(view, locx, locy) Textsw_view view; int locx, locy;{ Textsw_folio folio = FOLIO_FOR_VIEW(view); int fd; char buf[MAXNAMLEN], msg[MAXNAMLEN+100], *sys_msg; char alert_msg1[MAXNAMLEN+100]; char *alert_msg2; Es_status status; int cannot_open = 0; Event event; if (0 == textsw_get_selection_as_filename( folio, buf, sizeof(buf), locx, locy)) { if ((fd = open(buf, 0)) < 0) { cannot_open = (fd == -1); goto InternalError; }; errno = 0; textsw_checkpoint_undo(VIEW_REP_TO_ABS(view), (caddr_t)TEXTSW_INFINITY-1); status = textsw_get_from_fd(view, fd, TRUE); textsw_checkpoint_undo(VIEW_REP_TO_ABS(view), (caddr_t)TEXTSW_INFINITY-1); textsw_update_scrollbars(folio, TEXTSW_VIEW_NULL); (void) close(fd); if (status != ES_SUCCESS && status != ES_SHORT_WRITE) goto InternalError; } return;InternalError: if (cannot_open) { char *full_pathname; full_pathname = textsw_full_pathname(buf); (void) sprintf(msg, "'%s': ", full_pathname); (void) sprintf(alert_msg1, "'%s'", full_pathname); alert_msg2 = " "; free(full_pathname); } else { (void) sprintf(msg, "%s", "Unable to Include File. An INTERNAL ERROR has occurred.: "); (void) sprintf(alert_msg1, "%s", "Unable to Include File."); alert_msg2 = "An INTERNAL ERROR has occurred."; } sys_msg = (errno > 0 && errno < sys_nerr) ? sys_errlist[errno] : NULL; (void) alert_prompt( (Frame)window_get(WINDOW_FROM_VIEW(view), WIN_OWNER), &event, ALERT_BUTTON_YES, "Continue", ALERT_TRIGGER, ACTION_STOP, ALERT_MESSAGE_STRINGS, (strlen(sys_msg)) ? sys_msg : alert_msg1, (strlen(sys_msg)) ? alert_msg1 : alert_msg2, (strlen(sys_msg)) ? alert_msg2 : 0, 0, 0);}pkg_private inttextsw_file_stuff_quietly(view, file, msg, locx, locy) Textsw_view view; char *file, *msg; int locx, locy;{ Textsw_folio folio = FOLIO_FOR_VIEW(view); int fd; char *sys_msg; Es_status status; int cannot_open = 0; if ((fd = open(file, 0)) < 0) { cannot_open = (fd == -1); goto InternalError; }; errno = 0; textsw_checkpoint_undo(VIEW_REP_TO_ABS(view), (caddr_t)TEXTSW_INFINITY-1); status = textsw_get_from_fd(view, fd, TRUE); textsw_checkpoint_undo(VIEW_REP_TO_ABS(view), (caddr_t)TEXTSW_INFINITY-1); textsw_update_scrollbars(folio, TEXTSW_VIEW_NULL); (void) close(fd); if (status != ES_SUCCESS && status != ES_SHORT_WRITE) goto InternalError; return;InternalError: if (cannot_open) {/* char *full_pathname; full_pathname = textsw_full_pathname(file); (void) sprintf(msg, "'%s': ", full_pathname); free(full_pathname);*/ } else (void) sprintf( msg, "%s", "Paste from file failed due to ERROR: "); sys_msg = (errno > 0 && errno < sys_nerr) ? sys_errlist[errno] : NULL; if (sys_msg) (void) strcat(msg, sys_msg);}pkg_private Es_statustextsw_store_init(textsw, filename) Textsw_folio textsw; char *filename;{ struct stat stat_buf; if (stat(filename, &stat_buf) == 0) { Es_handle original = (Es_handle)LINT_CAST( es_get(textsw->views->esh, ES_PS_ORIGINAL)); if AN_ERROR(original == ES_NULL) { return(ES_CANNOT_GET_NAME); } switch ((Es_enum)es_get(original, ES_TYPE)) { case ES_TYPE_FILE: if (es_file_copy_status(original, filename) != 0) return(ES_USE_SAVE); /* else fall through */ default: if ((stat_buf.st_size > 0) && (textsw->state & TXTSW_CONFIRM_OVERWRITE)) return(ES_CANNOT_OVERWRITE); break; } } else if (errno != ENOENT) { return(ES_CANNOT_OPEN_OUTPUT); } return(ES_SUCCESS);}/* ARGSUSED */pkg_private Es_statustextsw_process_store_error(textsw, filename, status, locx, locy) Textsw_folio textsw; char *filename; /* Currently unused */ Es_status status; int locx, locy;{ char *msg1, *msg2; char msg[200]; Event event; int result; switch (status) { case ES_SUCCESS: LINT_IGNORE(ASSUME(0)); return(ES_UNKNOWN_ERROR); case ES_CANNOT_GET_NAME: msg[0] = '\0'; msg1 = "Unable to Store as New File. "; msg2 = "INTERNAL ERROR: Forgot the name of the file."; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_CANNOT_OPEN_OUTPUT: msg[0] = '\0'; msg1 = "Unable to Store as New File. "; msg2 = "Problems accessing specified file."; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_CANNOT_OVERWRITE: result = alert_prompt( (Frame)window_get(VIEW_FROM_FOLIO_OR_VIEW(textsw), WIN_OWNER), &event, ALERT_BUTTON_YES, "Confirm", ALERT_BUTTON_NO, "Cancel", ALERT_MESSAGE_STRINGS, "Please confirm Store as New File:", filename, " ", "That file exists and has data in it.", 0, 0); if (result == ALERT_FAILED) { return(ES_UNKNOWN_ERROR); } else { return((result == ALERT_YES) ? ES_SUCCESS : ES_UNKNOWN_ERROR); } case ES_FLUSH_FAILED: case ES_FSYNC_FAILED: case ES_SHORT_WRITE: msg[0] = '\0'; msg1 = "Unable to Store as New File. "; msg2 = "File system full."; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_USE_SAVE: msg[0] = '\0'; msg1 = "Unable to Store as New File. "; msg2 = "Use Save Current File instead."; strcat(msg, msg1); strcat(msg, msg2); goto PostError; case ES_UNKNOWN_ERROR: /* Fall through */ default: goto InternalError; }InternalError: msg[0] = '\0'; msg1 = "Unable to Store as New File. ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -