2 * ========================================================================
3 * Copyright 2006 University of Washington
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
13 * Program: Buffer management routines
18 * Some of the functions are internal,
19 * and some are actually attached to user
20 * keys. Like everyone else, they set hints
21 * for the display system.
25 int sgetline(char **, int *, char *, int);
29 * Look through the list of
30 * buffers. Return TRUE if there
31 * are any changed buffers. Buffers
32 * that hold magic internal stuff are
33 * not considered; who cares if the
34 * list of buffer names is hacked.
35 * Return FALSE if no buffers
45 if ((bp
->b_flag
&BFTEMP
)==0 && (bp
->b_flag
&BFCHG
)!=0)
53 * Find a buffer, by name. Return a pointer
54 * to the BUFFER structure associated with it. If
55 * the named buffer is found, but is a TEMP buffer (like
56 * the buffer list) conplain. If the buffer is not found
57 * and the "cflag" is TRUE, create it. The "bflag" is
58 * the settings for the flags in in buffer.
61 bfind(char *bname
, int cflag
, int bflag
)
64 register BUFFER
*sb
; /* buffer to insert after */
69 if (strcmp(bname
, bp
->b_bname
) == 0) {
70 if ((bp
->b_flag
&BFTEMP
) != 0) {
71 mlwrite_utf8("Cannot select builtin buffer", NULL
);
79 if ((bp
=(BUFFER
*)malloc(sizeof(BUFFER
))) == NULL
)
81 if ((lp
=lalloc(0)) == NULL
) {
85 /* find the place in the list to insert this buffer */
86 if (bheadp
== NULL
|| strcmp(bheadp
->b_bname
, bname
) > 0) {
87 /* insert at the beginning */
92 while (sb
->b_bufp
!= NULL
) {
93 if (strcmp(sb
->b_bufp
->b_bname
, bname
) > 0)
99 bp
->b_bufp
= sb
->b_bufp
;
103 /* and set up the other buffer fields */
113 strncpy(bp
->b_fname
, "", sizeof(bp
->b_fname
));
114 bp
->b_fname
[sizeof(bp
->b_fname
)-1] = '\0';
115 strncpy(bp
->b_bname
, bname
, sizeof(bp
->b_bname
));
116 bp
->b_bname
[sizeof(bp
->b_bname
)-1] = '\0';
124 * This routine blows away all of the text
125 * in a buffer. If the buffer is marked as changed
126 * then we ask if it is ok to blow it away; this is
127 * to save the user the grief of losing text. The
128 * window chain is nearly always wrong if this gets
129 * called; the caller must arrange for the updates
130 * that are required. Return TRUE if everything
137 register int s
= FALSE
;
140 if ((bp
->b_flag
&BFTEMP
) == 0 /* Not scratch buffer. */
141 && (bp
->b_flag
&BFCHG
) != 0){ /* Something changed */
142 emlwrite("buffer lines not freed.", NULL
);
147 if ((bp
->b_flag
&BFTEMP
) == 0 /* Not scratch buffer. */
148 && (bp
->b_flag
&BFCHG
) != 0 /* Something changed */
149 /* TRANSLATORS: A question asking whether to forget about
150 the changes and revert to the unchanged version. */
151 && (s
=mlyesno_utf8(_("Discard changes"), -1)) != TRUE
){
156 bp
->b_flag
&= ~BFCHG
; /* Not changed */
157 while ((lp
=lforw(bp
->b_linep
)) != bp
->b_linep
)
159 bp
->b_dotp
= bp
->b_linep
; /* Fix "." */
161 bp
->b_markp
= NULL
; /* Invalidate "mark" */
168 * packbuf - will pack up the main buffer in the buffer provided
169 * to be returned to the program that called pico.
170 * if need be, allocate memory for the new message.
171 * will also free the memory associated with the editor
172 * buffer, by calling zotedit.
177 int lcrlf
) /* EOLs are local or CRLF */
181 register int retval
= 0;
183 register char *eobuf
;
185 if(anycb() != FALSE
){
187 lp
= lforw(curbp
->b_linep
);
188 do{ /* how many chars? */
191 * add extra for new lines to be inserted later
196 while(lp
!= curbp
->b_linep
);
198 if(i
> *blen
){ /* new buffer ? */
200 * don't forget to add one for the null terminator!!!
202 if((bufp
= (char *)malloc((i
+1)*sizeof(char))) == NULL
){
203 zotedit(); /* bag it! */
214 eobuf
= bufp
+ *blen
;
215 lp
= lforw(curbp
->b_linep
); /* First line. */
217 for (i
= 0; i
< llength(lp
); i
++){ /* copy into buffer */
218 if((bufp
+1) < eobuf
){
219 *bufp
++ = (lp
->l_text
[i
].c
& 0xFF);
223 * the idea is to malloc enough space for the new
228 return(BUF_CHANGED
|COMP_FAILED
);
232 *bufp
++ = '\n'; /* EOLs use local convention */
235 *bufp
++ = 0x0D; /* EOLs use net standard */
240 while (lp
!= curbp
->b_linep
);
245 retval
= BUF_CHANGED
;
254 * readbuf - reads in a buffer.
265 char *sptr
; /* pointer into buffer string */
271 bp
->b_flag
&= ~(BFTEMP
|BFCHG
);
275 while((s
=sgetline(&sptr
,&nbytes
,line
,NLINE
)) == FIOSUC
|| s
== FIOLNG
){
277 if ((lp1
=lalloc(nbytes
)) == NULL
) {
278 s
= FIOERR
; /* Keep message on the */
279 break; /* display. */
281 lp2
= lback(curbp
->b_linep
);
283 lp1
->l_fp
= curbp
->b_linep
;
285 curbp
->b_linep
->l_bp
= lp1
;
286 for (i
=0; i
<nbytes
; ++i
){
292 for (wp
=wheadp
; wp
!=NULL
; wp
=wp
->w_wndp
) {
293 if (wp
->w_bufp
== curbp
) {
294 wheadp
->w_linep
= lforw(curbp
->b_linep
);
295 wheadp
->w_dotp
= lback(curbp
->b_linep
);
297 wheadp
->w_markp
= NULL
;
299 wheadp
->w_flag
|= WFHARD
;
303 strncpy(bp
->b_bname
, "main", sizeof(bp
->b_bname
));
304 bp
->b_bname
[sizeof(bp
->b_bname
)-1] = '\0';
305 strncpy(bp
->b_fname
, "", sizeof(bp
->b_fname
));
306 bp
->b_fname
[sizeof(bp
->b_fname
)-1] = '\0';
308 bp
->b_dotp
= bp
->b_linep
;
314 * sgetline - copy characters from ibuf to obuf, ending at the first
315 * newline. return with ibuf pointing to first char after
319 sgetline(char **ibuf
, int *nchars
, char *obuf
, int blen
)
322 register char *cbuf
= *ibuf
;
323 register char *bufp
= obuf
;
324 register int retval
= FIOSUC
;
333 len
= obuf
+ blen
- 1;
334 while (*cbuf
!= CR
&& *cbuf
!= LF
&& *cbuf
!= '\0'){
346 *bufp
= '\0'; /* end returned line */
347 *ibuf
= (*cbuf
== CR
) ? ++cbuf
: cbuf
;
348 *ibuf
= (*cbuf
== LF
) ? ++cbuf
: cbuf
;