/* * DoAutoSave - try to do autosave of current file */ void DoAutoSave( void ) { char path[FILENAME_MAX]; char path2[FILENAME_MAX]; char tmp[FILENAME_MAX]; bool quiet; FILE *f; vi_rc rc; status_type lastst; if( EditVars.AutoSaveInterval == 0 ) { return; } if( clock() < NextAutoSave ) { return; } if( CurrentFile == NULL ) { return; } if( CurrentFile->is_stdio || CurrentFile->viewonly || !CurrentFile->need_autosave ) { SetNextAutoSaveTime(); return; } MakeTmpPath( path, "" ); if( !CurrentFile->been_autosaved ) { getTmpName( path, CurrentFile->as_name ); } strcat( path, CurrentFile->as_name ); quiet = EditFlags.Quiet; EditFlags.Quiet = TRUE; lastst = UpdateCurrentStatus( CSTATUS_AUTOSAVE ); rc = SaveFile( path, -1, -1, TRUE ); EditFlags.Quiet = quiet; UpdateCurrentStatus( lastst ); if( rc != ERR_NO_ERR ) { SetNextAutoSaveTime(); return; } /* * update history file */ CurrentFile->need_autosave = FALSE; if( !CurrentFile->been_autosaved ) { GetCurrentFilePath( path2 ); CurrentFile->been_autosaved = TRUE; MakeTmpPath( tmp, checkFileName ); f = fopen( tmp, "a" ); if( f != NULL ) { MyFprintf( f, "%s %s\n", path, path2 ); fclose( f ); } } SetNextAutoSaveTime(); } /* DoAutoSave */
/* * TmpFileOpen - open a tmp file */ vi_rc TmpFileOpen( char *inname, int *_handle ) { char file[FILENAME_MAX]; tmpnam( inname ); MakeTmpPath( file, inname ); return( FileOpen( file, FALSE, O_TRUNC | O_RDWR | O_BINARY | O_CREAT, PMODE_RW, _handle ) ); } /* TmpFileOpen */
/* * TmpFileClose - close and delete a tmp file */ void TmpFileClose( int handle, char *name ) { char file[FILENAME_MAX]; if( handle < 0 ) { return; } close( handle ); MakeTmpPath( file, name ); remove( file ); } /* TmpFileClose */
/* * AutoSaveFini - clean up auto-saved files */ void AutoSaveFini( void ) { char path[FILENAME_MAX]; info *cinfo; if( EditVars.AutoSaveInterval == 0 ) { return; } if( !noEraseFileList ) { close( lockFileHandle ); MakeTmpPath( path, checkFileName ); remove( path ); MakeTmpPath( path, lockFileName ); remove( path ); } for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next ) { if( cinfo->CurrentFile->been_autosaved ) { MakeTmpPath( path, cinfo->CurrentFile->as_name ); remove( path ); } } } /* AutoSaveFini */
/* * RemoveFromAutoSaveList - take a file that we are quitting out of the list */ void RemoveFromAutoSaveList( void ) { FILE *f, *f2; char as_path[FILENAME_MAX]; char as2_path[FILENAME_MAX]; char path[FILENAME_MAX]; char path2[FILENAME_MAX]; char data[FILENAME_MAX]; // bool found; int i; if( EditVars.AutoSaveInterval == 0 ) { return; } if( CurrentFile == NULL ) { return; } if( !CurrentFile->been_autosaved ) { return; } MakeTmpPath( as_path, checkFileName ); MakeTmpPath( as2_path, checkFileTmpName ); GetCurrentFilePath( path ); // found = FALSE; f = fopen( as_path, "r" ); if( f == NULL ) { return; } f2 = fopen( as2_path, "w" ); if( f2 == NULL ) { fclose( f ); return; } while( fgets( path2, FILENAME_MAX, f ) != NULL ) { for( i = strlen( path2 ); i && isWSorCtrlZ( path2[i - 1] ); --i ) { path2[i - 1] = '\0'; } NextWord1( path2, data ); RemoveLeadingSpaces( path2 ); if( !strcmp( path, path2 ) ) { MakeTmpPath( path2, CurrentFile->as_name ); if( !strcmp( data, path2 ) ) { // found = TRUE; remove( path2 ); while( fgets( data, FILENAME_MAX, f ) != NULL ) { for( i = strlen( data ); i && isWSorCtrlZ( data[i - 1] ); --i ) { data[i - 1] = '\0'; } MyFprintf( f2, "%s\n", data ); } break; } } MyFprintf( f2, "%s %s\n", data, path2 ); } fclose( f ); fclose( f2 ); remove( as_path ); rename( as2_path, as_path ); } /* RemoveFromAutoSaveList */
/* * AutoSaveInit - initialize for auto-save */ void AutoSaveInit( void ) { char path[FILENAME_MAX]; char path2[FILENAME_MAX]; char as_path[FILENAME_MAX]; char asl_path[FILENAME_MAX]; int len; int cnt; FILE *f; int pid; int ch; int handle; int off; // int old_len; int i; /* * initialize tmpname */ #ifdef __UNIX__ strcpy( currTmpName,"aaaaaaaaaaaa.tmp" ); #else strcpy( currTmpName,"aaaaaaaa.tmp" ); #endif pid = getpid(); itoa( pid, path, 36 ); len = strlen( path ); memcpy( &currTmpName[TMP_FNAME_LEN - len], path, len ); #ifdef __QNX__ { int len2, len3; int nid, uid; nid = getnid(); itoa( nid, path, 36 ); len2 = strlen( path ); memcpy( &currTmpName[TMP_FNAME_LEN - len - len2], path, len2 ); uid = getuid(); itoa( uid, path, 36 ); len3 = strlen( path ); memcpy( &currTmpName[TMP_FNAME_LEN - len - len2 - len3], path, len3 ); memcpy( &checkFileName[EXTRA_EXT_OFF], path, len3 ); memcpy( &checkFileTmpName[EXTRA_EXT_OFF], path, len3 ); memcpy( &lockFileName[EXTRA_EXT_OFF], path, len3 ); } #endif /* * check if we need to recover lost files */ if( EditFlags.RecoverLostFiles ) { cnt = 0; MakeTmpPath( as_path, checkFileName ); MakeTmpPath( asl_path, lockFileName ); off = strlen( as_path ) - 5; for( ch = START_CHAR; ch <= END_CHAR; ch++ ) { as_path[off] = ch; asl_path[off] = ch; handle = sopen3( as_path, O_RDONLY | O_TEXT, SH_DENYRW ); if( handle < 0 ) { continue; } f = fdopen( handle, "r" ); if( f != NULL ) { while( fgets( path2, FILENAME_MAX, f ) != NULL ) { for( i = strlen( path2 ); i && isWSorCtrlZ( path2[i - 1] ); --i ) { path2[i - 1] = '\0'; } NextWord1( path2, path ); RemoveLeadingSpaces( path2 ); NewFile( path, FALSE ); AddString2( &(CurrentFile->name), path2 ); SetFileWindowTitle( CurrentWindow, CurrentInfo, TRUE ); FileSPVAR(); CurrentFile->modified = TRUE; CurrentFile->check_readonly = TRUE; FTSRunCmds( path2 ); cnt++; } fclose( f ); remove( as_path ); } else { close( handle ); } remove( asl_path ); } if( cnt == 0 ) { MyPrintf( "No files recovered!\n" ); ExitEditor( -1 ); } Message1( "%d files recovered", cnt ); } if( EditVars.AutoSaveInterval == 0 ) { return; } // old_len = strlen( lockFileName ); MakeTmpPath( path, lockFileName ); len = strlen( path ) - strlen( lockFileName ); off = len + CHAR_OFF; for( ch = START_CHAR; ch <= END_CHAR; ch++ ) { path[off] = ch; lockFileHandle = sopen4( path, O_CREAT | O_TRUNC | O_RDWR | O_TEXT, SH_DENYRW, PMODE_RW ); if( lockFileHandle > 0 ) { break; } } if( lockFileHandle < 0 ) { // MyPrintf( "Too many editors running!\n" ); MyPrintf( "Error opening temp file - '%s'\n", strerror( errno ) ); ExitEditor( -1 ); } lockFileName[CHAR_OFF] = ch; checkFileName[CHAR_OFF] = ch; checkFileTmpName[CHAR_OFF] = ch; } /* AutoSaveInit */
/* * LostFileCheck - check if there are any lost files out there */ bool LostFileCheck( void ) { char path[FILENAME_MAX]; vi_key key; char ch; int off; int handle; MakeTmpPath( path, lockFileName ); off = strlen( path ) - 5; for( ch = START_CHAR; ch <= END_CHAR; ch++ ) { path[off] = ch; handle = sopen3( path, O_RDONLY | O_TEXT, SH_DENYRW ); if( handle > 0 ) { MakeTmpPath( path, checkFileName ); path[off] = ch; if( access( path, F_OK ) == -1 ) { MakeTmpPath( path, lockFileName ); path[off] = ch; close( handle ); handle = -1; remove( path ); } else { break; } } } if( handle > 0 ) { close( handle ); if( !EditFlags.RecoverLostFiles ) { if( !EditFlags.IgnoreLostFiles ) { #ifdef __WIN__ CloseStartupDialog(); key = GetAutosaveResponse(); handleKey( key ); ShowStartupDialog(); return( TRUE ); #else SetPosToMessageLine(); MyPrintf( "Files have been lost since your last session, do you wish to:\n" ); MyPrintf( "\ti)gnore\n\tr)ecover\n\tq)uit\n" ); for( ;; ) { key = GetKeyboard(); if( handleKey( key ) ) { return( TRUE ); } } #endif } else { remove( path ); return( FALSE ); } } } else { if( EditFlags.RecoverLostFiles ) { EditFlags.RecoverLostFiles = FALSE; EditFlags.NoInitialFileLoad = FALSE; } } return( FALSE ); } /* LostFileCheck */
/* * DoGenericFilter - filter some crap */ vi_rc DoGenericFilter( linenum s, linenum e, const char *cmd ) { fcb *cfcb, *tfcb; line *cline; vi_rc rc; char filtin[_MAX_PATH], filtout[_MAX_PATH]; fcb_list fcblist; int fh; rc = ModificationTest(); if( rc != ERR_NO_ERR ) { return( rc ); } /* * filter on a line */ rc = GetCopyOfLineRange( s, e, &fcblist ); if( rc != ERR_NO_ERR ) { return( rc ); } /* * get file */ MakeTmpPath( filtin, FILTER_FILE_NAME ); fh = mkstemp( filtin ); if( fh == -1 ) return( ERR_FILE_OPEN ); /* * now, dump this crap to a tmp file */ for( cfcb = fcblist.head; cfcb != NULL; cfcb = tfcb ) { FetchFcb( cfcb ); for( cline = cfcb->lines.head; cline != NULL; cline = cline->next ) { write( fh, cline->data, strlen( cline->data ) ); #if defined( __UNIX__ ) write( fh, "\n", 1 ); #else write( fh, "\r\n", 2 ); #endif } tfcb = cfcb->next; FcbFree( cfcb ); } close( fh ); MakeTmpPath( filtout, FILTER_FILE_NAME ); fh = mkstemp( filtout ); if( fh == -1 ) { remove( filtin ); return( ERR_FILE_OPEN ); } close( fh ); /* * shell out to the given command */ ExecCmd( filtin, filtout, cmd ); StartUndoGroup( UndoStack ); rc = DeleteLineRange( s, e, 0 ); if( rc == ERR_NO_ERR ) { ReadAFile( s - 1, filtout ); Message1( "%l lines filtered through %s", e - s + 1, cmd ); } EndUndoGroup( UndoStack ); /* * cleanup */ if( rc == ERR_NO_ERR ) { rc = DO_NOT_CLEAR_MESSAGE_WINDOW; } remove( filtin ); remove( filtout ); return( rc ); } /* DoGenericFilter */