1 /* $Id: autosave.c,v 1.3 2001/03/02 08:49:04 amura Exp $ */
2 /*
3  Auto save support code programed by M.Suzuki
4  Ver	1.0.0	1997/01/07	Create
5 */
6 /*
7  * $Log: autosave.c,v $
8  * Revision 1.3  2001/03/02 08:49:04  amura
9  * now AUTOSAVE feature implemented almost all (except for WIN32
10  *
11  * Revision 1.2  2001/02/18 19:27:35  amura
12  * fix del_autosave_file()
13  *
14  * Revision 1.1  2001/02/18 17:07:23  amura
15  * append AUTOSAVE feature (but NOW not work)
16  *
17  */
18 
19 #include "config.h"
20 
21 #ifdef	AUTOSAVE
22 
23 #include "def.h"
24 #include <time.h>
25 
26 time_t autosave_interval = 10*60;/* Auto save interval time.(sec) */
27 int autosave_flag = FALSE;
28 int autosaved = FALSE;
29 
30 extern int getnum pro((char *prompt, int *num));
31 /* this is port dependent */
32 extern VOID autosave_name pro((char *buff, char *name, int buflen));
33 #ifdef	ITIMER
34 extern VOID itimer pro((VOID (*func)(void), time_t sec));
35 #else
36 static time_t check_time;
37 #endif
38 
39 VOID
autosave_check(flag)40 autosave_check(flag)
41 {
42     if (flag && autosave_interval!=0) {
43 	autosave_flag = TRUE;
44 	autosaved = FALSE;
45 #ifdef	ITIMER
46 	itimer(autosave_handler, autosave_interval);
47 #else
48 	check_time = time(NULL);
49 #endif
50     }
51     else {
52 	autosave_flag = FALSE;
53 #ifdef	ITIMER
54 	itimer(NULL, 0);
55 #endif
56     }
57 }
58 
59 VOID
autosave_handler()60 autosave_handler()
61 {
62     BUFFER *bp;
63     int currow;
64     int curcol;
65 #ifdef	ITIMER
66     itimer(NULL, 0);
67 #else	/* not ITIMER */
68     time_t now_time;
69     if (!autosave_flag || autosaved)
70 	return;
71     now_time = time(NULL);
72     if ((now_time - check_time) < autosave_interval)
73 	return;
74 #endif	/* ITIMER */
75 
76     autosaved = TRUE;
77     currow = ttrow;		/* save cursor position	*/
78     curcol = ttcol;
79     bp = bheadp;				/* For all buffers	*/
80     while (bp != NULL) {
81 	if( bp->b_bname[0] != '*' ){		/* Not internal buffer 	*/
82 	    if( bp->b_flag & BFACHG ){		/* modified ?	*/
83 		char fname[NFILEN];
84 		autosave_name(fname, bp->b_fname, NFILEN);
85 		writeout(bp, fname);
86 		bp->b_flag &= ~BFACHG;
87 	    }
88 	}
89 	bp = bp->b_bufp;
90     }	/* end of while	*/
91     ewprintf("autosave...done");
92     ttmove(currow,curcol);
93     ttflush();
94 }
95 
96 VOID
del_autosave_file(name)97 del_autosave_file(name)
98 char* name;
99 {
100     char fname[NFILEN];
101 
102     if (name!=NULL && name[0] != '*' ){	/* Not internal buffer ? */
103 	autosave_name(fname, name, NFILEN);
104 	unlink(fname);
105 	/* ewprintf("Unlink ... [%s]",fname); */
106     }
107 }
108 
109 VOID
clean_autosave_file()110 clean_autosave_file()
111 {
112     BUFFER *bp;
113     for (bp=bheadp; bp!=NULL; bp=bp->b_bufp)
114 	del_autosave_file(bp->b_fname);
115 }
116 
117 /*
118  * COMMAND: set-auto-save-interval
119  */
120 /*ARGSUSED*/
as_set_interval(f,n)121 as_set_interval(f, n)
122 {
123     if ((f & FFARG) == 0) {
124     	if (getnum("auto-save-interval", &n) == FALSE)
125 	    return (FALSE);
126     }
127     autosave_interval = n;
128     if (n == 0)
129 	autosave_flag = FALSE;
130     return (TRUE);
131 }
132 
133 #endif	/* AUTO_SAVE */
134