995
|
1 /*
|
|
2 * The routines in this file
|
|
3 * handle the reading and writing of
|
|
4 * disk files. All of details about the
|
|
5 * reading and writing of the disk are
|
|
6 * in "fileio.c".
|
|
7 */
|
|
8 #include <stdio.h>
|
|
9 #include "ueed.h"
|
|
10
|
|
11 /*
|
|
12 * Read a file into the current
|
|
13 * buffer. This is really easy; all you do it
|
|
14 * find the name of the file, and call the standard
|
|
15 * "read a file into the current buffer" code.
|
|
16 * Bound to "C-X C-R".
|
|
17 */
|
|
18 fileread(f, n)
|
|
19 {
|
|
20 register int s;
|
|
21 char fname[NFILEN];
|
|
22
|
|
23 if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
|
|
24 return (s);
|
|
25 return (readin(fname));
|
|
26 }
|
|
27
|
|
28 #ifndef OS9
|
|
29 /*
|
|
30 * Select a file for editing.
|
|
31 * Look around to see if you can find the
|
|
32 * fine in another buffer; if you can find it
|
|
33 * just switch to the buffer. If you cannot find
|
|
34 * the file, create a new buffer, read in the
|
|
35 * text, and switch to the new buffer.
|
|
36 * Bound to C-X C-V.
|
|
37 */
|
|
38 filevisit(f, n)
|
|
39 {
|
|
40 register BUFFER *bp;
|
|
41 register WINDOW *wp;
|
|
42 register LINE *lp;
|
|
43 register int i;
|
|
44 register int s;
|
|
45 char bname[NBUFN];
|
|
46 char fname[NFILEN];
|
|
47
|
|
48 if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
|
|
49 return (s);
|
|
50 for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
|
|
51 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
|
|
52 if (--curbp->b_nwnd == 0) {
|
|
53 curbp->b_dotp = curwp->w_dotp;
|
|
54 curbp->b_doto = curwp->w_doto;
|
|
55 curbp->b_markp = curwp->w_markp;
|
|
56 curbp->b_marko = curwp->w_marko;
|
|
57 }
|
|
58 curbp = bp;
|
|
59 curwp->w_bufp = bp;
|
|
60 if (bp->b_nwnd++ == 0) {
|
|
61 curwp->w_dotp = bp->b_dotp;
|
|
62 curwp->w_doto = bp->b_doto;
|
|
63 curwp->w_markp = bp->b_markp;
|
|
64 curwp->w_marko = bp->b_marko;
|
|
65 } else {
|
|
66 wp = wheadp;
|
|
67 while (wp != NULL) {
|
|
68 if (wp!=curwp && wp->w_bufp==bp) {
|
|
69 curwp->w_dotp = wp->w_dotp;
|
|
70 curwp->w_doto = wp->w_doto;
|
|
71 curwp->w_markp = wp->w_markp;
|
|
72 curwp->w_marko = wp->w_marko;
|
|
73 break;
|
|
74 }
|
|
75 wp = wp->w_wndp;
|
|
76 }
|
|
77 }
|
|
78 lp = curwp->w_dotp;
|
|
79 i = curwp->w_ntrows/2;
|
|
80 while (i-- && lback(lp)!=curbp->b_linep)
|
|
81 lp = lback(lp);
|
|
82 curwp->w_linep = lp;
|
|
83 curwp->w_flag |= WFMODE|WFHARD;
|
|
84 mlwrite("[Old buffer]");
|
|
85 return (TRUE);
|
|
86 }
|
|
87 }
|
|
88 makename(bname, fname); /* New buffer name. */
|
|
89 while ((bp=bfind(bname, FALSE, 0)) != NULL) {
|
|
90 s = mlreply("Buffer name: ", bname, NBUFN);
|
|
91 if (s == ABORT) /* ^G to just quit */
|
|
92 return (s);
|
|
93 if (s == FALSE) { /* CR to clobber it */
|
|
94 makename(bname, fname);
|
|
95 break;
|
|
96 }
|
|
97 }
|
|
98 if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
|
|
99 mlwrite("Cannot create buffer");
|
|
100 return (FALSE);
|
|
101 }
|
|
102 if (--curbp->b_nwnd == 0) { /* Undisplay. */
|
|
103 curbp->b_dotp = curwp->w_dotp;
|
|
104 curbp->b_doto = curwp->w_doto;
|
|
105 curbp->b_markp = curwp->w_markp;
|
|
106 curbp->b_marko = curwp->w_marko;
|
|
107 }
|
|
108 curbp = bp; /* Switch to it. */
|
|
109 curwp->w_bufp = bp;
|
|
110 curbp->b_nwnd++;
|
|
111 return (readin(fname)); /* Read it in. */
|
|
112 }
|
|
113 #endif
|
|
114
|
|
115 /*
|
|
116 * Read file "fname" into the current
|
|
117 * buffer, blowing away any text found there. Called
|
|
118 * by both the read and visit commands. Return the final
|
|
119 * status of the read. Also called by the mainline,
|
|
120 * to read in a file specified on the command line as
|
|
121 * an argument.
|
|
122 */
|
|
123 readin(fname)
|
|
124 char fname[];
|
|
125 {
|
|
126 register LINE *lp1;
|
|
127 register LINE *lp2;
|
|
128 register int i;
|
|
129 register WINDOW *wp;
|
|
130 register BUFFER *bp;
|
|
131 register int s;
|
|
132 register int nbytes;
|
|
133 register int nline;
|
|
134 char line[NLINE];
|
|
135
|
|
136 bp = curbp; /* Cheap. */
|
|
137 if ((s=bclear(bp)) != TRUE) /* Might be old. */
|
|
138 return (s);
|
|
139 bp->b_flag &= ~(BFTEMP|BFCHG);
|
|
140 strcpy(bp->b_fname, fname);
|
|
141 if ((s=ffropen(fname)) == FIOERR) /* Hard file open. */
|
|
142 goto out;
|
|
143 if (s == FIOFNF) { /* File not found. */
|
|
144 mlwrite("[New file]");
|
|
145 goto out;
|
|
146 }
|
|
147 mlwrite("[Reading file]");
|
|
148 nline = 0;
|
|
149 while ((s=ffgetline(line, NLINE)) == FIOSUC) {
|
|
150 nbytes = strlen(line);
|
|
151 if ((lp1=lalloc(nbytes)) == NULL) {
|
|
152 s = FIOERR; /* Keep message on the */
|
|
153 break; /* display. */
|
|
154 }
|
|
155 lp2 = lback(curbp->b_linep);
|
|
156 lp2->l_fp = lp1;
|
|
157 lp1->l_fp = curbp->b_linep;
|
|
158 lp1->l_bp = lp2;
|
|
159 curbp->b_linep->l_bp = lp1;
|
|
160 for (i=0; i<nbytes; ++i)
|
|
161 lputc(lp1, i, line[i]);
|
|
162 ++nline;
|
|
163 }
|
|
164 ffclose(); /* Ignore errors. */
|
|
165 if (s == FIOEOF) { /* Don't zap message! */
|
|
166 if (nline == 1)
|
|
167 mlwrite("[Read 1 line]");
|
|
168 else
|
|
169 mlwrite("[Read %d lines]", nline);
|
|
170 }
|
|
171 out:
|
|
172 for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
|
|
173 if (wp->w_bufp == curbp) {
|
|
174 wp->w_linep = lforw(curbp->b_linep);
|
|
175 wp->w_dotp = lforw(curbp->b_linep);
|
|
176 wp->w_doto =
|
|
177 wp->w_markp =
|
|
178 wp->w_marko = 0;
|
|
179 wp->w_flag |= WFMODE|WFHARD;
|
|
180 }
|
|
181 }
|
|
182 if (s == FIOERR) /* False if error. */
|
|
183 return (FALSE);
|
|
184 return (TRUE);
|
|
185 }
|
|
186
|
|
187 /*
|
|
188 * Take a file name, and from it
|
|
189 * fabricate a buffer name. This routine knows
|
|
190 * about the syntax of file names on the target system.
|
|
191 * I suppose that this information could be put in
|
|
192 * a better place than a line of code.
|
|
193 */
|
|
194 makename(bname, fname)
|
|
195 char bname[];
|
|
196 char fname[];
|
|
197 {
|
|
198 register char *cp1;
|
|
199 register char *cp2;
|
|
200
|
|
201 cp1 = &fname[0];
|
|
202 while (*cp1 != 0)
|
|
203 ++cp1;
|
|
204
|
|
205 #ifdef AMIGA
|
|
206 while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
|
|
207 --cp1;
|
|
208 #endif
|
|
209 #ifdef VMS
|
|
210 while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
|
|
211 --cp1;
|
|
212 #endif
|
|
213 #ifdef CPM
|
|
214 while (cp1!=&fname[0] && cp1[-1]!=':')
|
|
215 --cp1;
|
|
216 #endif
|
|
217 #ifdef MSDOS
|
|
218 while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
|
|
219 --cp1;
|
|
220 #endif
|
|
221 #ifdef V7
|
|
222 while (cp1!=&fname[0] && cp1[-1]!='/')
|
|
223 --cp1;
|
|
224 #endif
|
|
225 #ifdef OSK
|
|
226 while (cp1!=&fname[0] && cp1[-1]!='/')
|
|
227 --cp1;
|
|
228 #endif
|
|
229 cp2 = &bname[0];
|
|
230 while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
|
|
231 *cp2++ = *cp1++;
|
|
232 *cp2 = 0;
|
|
233 }
|
|
234
|
|
235 /*
|
|
236 * Ask for a file name, and write the
|
|
237 * contents of the current buffer to that file.
|
|
238 * Update the remembered file name and clear the
|
|
239 * buffer changed flag. This handling of file names
|
|
240 * is different from the earlier versions, and
|
|
241 * is more compatable with Gosling EMACS than
|
|
242 * with ITS EMACS. Bound to "C-X C-W".
|
|
243 */
|
|
244 filewrite(f, n)
|
|
245 {
|
|
246 register WINDOW *wp;
|
|
247 register int s;
|
|
248 char fname[NFILEN];
|
|
249
|
|
250 if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
|
|
251 return (s);
|
|
252 if ((s=writeout(fname)) == TRUE) {
|
|
253 strcpy(curbp->b_fname, fname);
|
|
254 curbp->b_flag &= ~BFCHG;
|
|
255 wp = wheadp; /* Update mode lines. */
|
|
256 while (wp != NULL) {
|
|
257 if (wp->w_bufp == curbp)
|
|
258 wp->w_flag |= WFMODE;
|
|
259 wp = wp->w_wndp;
|
|
260 }
|
|
261 }
|
|
262 return (s);
|
|
263 }
|
|
264
|