📄 file.c
字号:
int update_type;
char *tokens;
register int rc = ERROR;
register WINDOW *win; /* put window pointer in a register */
int bottom_line;
win = window;
update_type = win == NULL ? GLOBAL : LOCAL;
if (update_type == LOCAL) {
if (!g_status.sas_defined || g_status.command == DefineGrep ||
g_status.command == DefineRegXGrep) {
/*
* prompt for the search pattern and the seize path.
* initialize all this stuff.
*/
if (g_status.command == DefineGrep)
g_status.sas_search_type = BOYER_MOORE;
else
g_status.sas_search_type = REG_EXPRESSION;
if (g_status.sas_search_type == BOYER_MOORE) {
*sas_bm.pattern = '\0';
if (get_name( win16a, win->bottom_line, (char *)sas_bm.pattern,
g_display.message_color ) == ERROR)
return( ERROR );
if (*sas_bm.pattern == '\0')
return( ERROR );
} else {
*sas_regx.pattern = '\0';
if (get_name( win16b, win->bottom_line, (char *)sas_regx.pattern,
g_display.message_color ) == ERROR)
return( ERROR );
if (*sas_regx.pattern == '\0')
return( ERROR );
else
strcpy( (char *)regx.pattern, (char *)sas_regx.pattern );
}
*g_status.sas_tokens = '\0';
if (get_name( win17, win->bottom_line, g_status.sas_tokens,
g_display.message_color ) == ERROR)
return( ERROR );
i = 0;
tokens = strtok( g_status.sas_tokens, SAS_DELIMITERS );
while (tokens != NULL) {
g_status.sas_arg_pointers[i++] = tokens;
tokens = strtok( NULL, SAS_DELIMITERS );
}
if (i == 0)
return( ERROR );
g_status.sas_arg_pointers[i] = NULL;
g_status.sas_argc = i;
g_status.sas_arg = 0;
g_status.sas_argv = g_status.sas_arg_pointers;
g_status.sas_found_first = FALSE;
if (g_status.command == DefineGrep) {
g_status.sas_defined = TRUE;
bm.search_defined = sas_bm.search_defined = OK;
build_boyer_array( );
} else {
i = build_nfa( );
if (i == OK) {
g_status.sas_defined = TRUE;
regx.search_defined = sas_regx.search_defined = OK;
} else
g_status.sas_defined = FALSE;
}
}
bottom_line = win->bottom_line;
} else
bottom_line = g_display.nlines;
if (g_status.sas_defined && g_status.sas_arg < g_status.sas_argc) {
if (win != NULL) {
entab_linebuff( );
un_copy_line( win->ll, win, TRUE );
}
/*
* while we haven't found a valid file, search thru the command
* line path.
* we may have an invalid file name when we finish matching all
* files according to a pattern. then, we need to go to the next
* command line argument if it exists.
*/
while (rc == ERROR && g_status.sas_arg < g_status.sas_argc) {
/*
* if we haven't starting searching for a file, check to see if
* the file is a valid file name. if no file is found, then let's
* see if we can find according to a search pattern.
*/
if (g_status.sas_found_first == FALSE) {
assert( strlen( g_status.sas_argv[g_status.sas_arg] ) < MAX_COLS );
strcpy( name, g_status.sas_argv[g_status.sas_arg] );
rc = get_fattr( name, &i );
/*
* a new or blank file generates a return code of 2.
* a pattern with wild cards generates a return code of 3.
*/
if (rc == OK || rc == 2) {
++g_status.sas_arg;
rc = OK;
/*
* if we get this far, we got an invalid path name.
* let's try to find a matching file name using pattern.
*/
} else if (rc != ERROR) {
rc = my_findfirst( &g_status.sas_dta, name, NORMAL | READ_ONLY |
HIDDEN | SYSTEM | ARCHIVE );
/*
* if we found a file using wildcard characters,
* set the g_status.sas_found_first flag to true so we can
* find the next matching file. we need to save the
* pathname stem so we know which directory we are working in.
*/
if (rc == OK) {
g_status.sas_found_first = TRUE;
i = strlen( name ) - 1;
while (i >= 0) {
if (name[i] == ':' || name[i] == '\\')
break;
--i;
}
name[++i] = '\0';
assert( strlen( name ) + strlen( g_status.sas_dta.name )
< MAX_COLS );
strcpy( g_status.sas_path, name );
strcpy( name, g_status.sas_path );
strcat( name, g_status.sas_dta.name );
} else {
++g_status.sas_arg;
if (win != NULL)
/*
* invalid path or file name
*/
error( WARNING, win->bottom_line, win8 );
}
} else if (rc == ERROR)
++g_status.sas_arg;
} else {
/*
* we already found one file with wild card characters,
* find the next matching file.
*/
rc = my_findnext( &g_status.sas_dta );
if (rc == OK) {
assert( strlen( g_status.sas_path ) +
strlen( g_status.sas_dta.name ) < MAX_COLS );
strcpy( name, g_status.sas_path );
strcat( name, g_status.sas_dta.name );
} else {
g_status.sas_found_first = FALSE;
++g_status.sas_arg;
}
}
/*
* if everything is everything so far, set up the file
* and window structures and bring the file into the editor.
*/
if (rc == OK) {
assert( strlen( win19 ) + strlen( name ) < MAX_COLS );
strcpy( searching, win19 );
strcat( searching, name );
save_screen_line( 0, bottom_line, line_buff );
set_prompt( searching, bottom_line );
file_mode = TEXT;
bin_length = 0;
assert( strlen( name ) <= MAX_COLS );
_splitpath( name, spdrive, spdir, spname, spext );
if (stricmp( spext, ".exe" ) == 0 || stricmp( spext, ".com" ) == 0){
file_mode = BINARY;
bin_length = g_status.file_chunk;
}
rc = attempt_edit_display( name, update_type, file_mode, bin_length );
if (rc == OK)
show_avail_mem( );
restore_screen_line( 0, bottom_line, line_buff );
if (rc == OK) {
win = g_status.current_window;
bin_offset_adjust( win, g_status.sas_rline );
find_adjust( win, g_status.sas_ll, g_status.sas_rline,
g_status.sas_rcol );
make_ruler( win );
show_ruler( win );
show_ruler_pointer( win );
show_window_header( win );
if (win->vertical)
show_vertical_separator( win );
win->file_info->dirty = LOCAL;
}
}
/*
* either there are no more matching files or we had an
* invalid file name, set rc to ERROR and let's look at the
* next file name or pattern on the command line.
*/
else
rc = ERROR;
}
}
if (rc == ERROR && g_status.sas_arg >= g_status.sas_argc && win != NULL)
/*
* no more files to load
*/
error( WARNING, win->bottom_line, win9 );
return( rc );
}
/*
* Name: attempt_edit_display
* Purpose: try to load then display a file
* Date: June 5, 1991
* Passed: fname: file name to load
* update_type: update current window or entire screen
* file_mode: BINARY or TEXT
* bin_len: if opened in BINARY mode, length of binary lines
* Notes: When we first start the editor, we need to update the entire
* screen. When we load in a new file, we only need to update
* the current window.
*/
int attempt_edit_display( char *fname, int update_type, int file_mode,
int bin_len )
{
register int rc;
WINDOW *win;
rc = edit_file( fname, file_mode, bin_len );
if (rc != ERROR) {
rc = initialize_window( );
if (rc != ERROR) {
win = g_status.current_window;
if (update_type == LOCAL) {
if (g_status.command != DefineGrep &&
g_status.command != DefineRegXGrep &&
g_status.command != RepeatGrep)
redraw_current_window( win );
show_file_count( g_status.file_count );
show_window_count( g_status.window_count );
show_avail_mem( );
} else if (update_type == GLOBAL)
redraw_screen( win );
if (win->file_info->new_file) {
g_status.command = AddLine;
insert_newline( win );
win->file_info->modified = FALSE;
}
}
}
return( rc );
}
/*
* Name: make_backup_fname
* Purpose: add .bak to file name
* Date: January 6, 1992
* Passed: file: information allowing access to the current file
*/
void make_backup_fname( file_infos *file )
{
char name[MAX_COLS]; /* new name for file */
char *p;
int i;
int len;
/*
* if this is a new file then don't create a backup - can't backup
* a nonexisting file.
*/
if (file->new_file)
file->backed_up = TRUE;
/*
* find the file name extension if it exists
*/
else {
assert( strlen( file->file_name ) < MAX_COLS );
strcpy( name, file->file_name );
len = strlen( name );
for (i=len,p=name+len; i>=0; i--) {
/*
* we found the '.' extension character. get out
*/
if (*p == '.')
break;
/*
* we found the drive or directory character. no extension so
* set the pointer to the end of file name string.
*/
else if (*p == '\\' || *p == ':') {
p = name + len;
break;
/*
* we're at the beginning of the string - no '.', drive, or directory
* char was found. set the pointer to the end of file name string.
*/
} else if (i == 0) {
p = name + len;
break;
}
--p;
}
assert( strlen( name ) < MAX_COLS );
strcpy( p, ".bak" );
strcpy( file->backup_fname, name );
}
}
/*
* Name: file_file
* Purpose: To file the current file to disk.
* Date: September 17, 1991
* Passed: window: pointer to current window
*/
int file_file( WINDOW *window )
{
if (save_file( window ) == OK)
finish( window );
return( OK );
}
/*
* Name: save_file
* Purpose: To save the current file to disk.
* Date: June 5, 1991
* Passed: window: pointer to current window
* Notes: If anything goes wrong, then the modified flag is set.
* If the file is saved successfully, then modified flag is
* cleared.
*/
int save_file( WINDOW *window )
{
char name[MAX_COLS]; /* name of file to be saved */
register file_infos *file;
int rc;
line_list_ptr temp_ll;
entab_linebuff( );
if (un_copy_line( window->ll, window, TRUE ) == ERROR)
return( ERROR );
file = window->file_info;
if (file->modified == FALSE)
return( OK );
/*
* set up file name
*/
assert( strlen( file->file_name ) < MAX_COLS );
strcpy( name, file->file_name );
/*
* see if there was a file name - if not, then make the user
* supply one.
*/
if (strlen( name ) == 0)
rc = save_as_file( window );
else {
/*
* save the file
*/
rc = write_to_disk( window, name );
if (rc != ERROR) {
file->modified = FALSE;
file->new_file = FALSE;
}
}
/*
* clear the dirty flags
*/
if (rc == OK) {
temp_ll = window->file_info->line_list;
for (; temp_ll->len != EOF; temp_ll=temp_ll->next)
temp_ll->dirty = FALSE;
window->file_info->dirty = GLOBAL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -