File: buffer.c

package info (click to toggle)
beav 1%3A1.40-18
  • links: PTS
  • area: main
  • in suites: bullseye, buster, etch, etch-m68k, jessie, jessie-kfreebsd, lenny, sarge, squeeze, stretch, wheezy
  • size: 612 kB
  • ctags: 1,368
  • sloc: ansic: 11,289; makefile: 51
file content (988 lines) | stat: -rw-r--r-- 20,820 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/*
*       Buffer handling.
*/

#include    <string.h>
#include    <stdlib.h>
#include    "def.h"

bool onebuf ();
bool killablebufs ();
bool _yankbuffer ();
char next_buf ();
bool bclear ();
bool addline ();
char makelist ();
bool popblist ();
char listbuffers ();
char _killbuffer ();
bool _usebuffer ();

extern ROW_FMT text_fmt;
extern char MSG_use_b[];
extern char MSG_kill_b[];
extern char MSG_not_fnd[];
extern char MSG_no_del_m[];
extern char MSG_buf_disp[];
extern char MSG_main[];
extern char MSG_l_buf_h[];
extern char MSG_l_buf_h1[];
extern char MSG_no_chg[];
extern char MSG_yank_b[];
extern char MSG_no_buf[];
extern char MSG_no_s_yank[];
extern char MSG_buf_nam[];
extern char MSG_bad_l[];
extern char MSG_pick[];
extern char MSG_siz_chg[];
extern char MSG_no_siz_chg[];
extern char MSG_up_arrow[];
extern char MSG_null[];
extern char MSG_save_buf[];
extern char MSG_cnt_al_b[];
extern char MSG_ins_cnt[];

BUFFER sav_buf;
LINE sav_line_h;
/*
* Attach a buffer to a window. The
* values of dot and mark come from the buffer
* if the use count is 0. Otherwise, they come
* from some other window.
*
* plus hacks for prev/next buffer and use-buffer-split  (jam)
* functions (like in file.c)
*/
char
usebuffer ()
{

    char bufn[NBUFN];
    register char s;

    if ((s = ereply (MSG_use_b, bufn, NBUFN, 0)) != TRUE)
	return (s);
    return (_usebuffer (bufn));
}

/* use buffer, split window first
*/
char
use_buffer ()
{
    char bufn[NBUFN];
    register char s;

    if ((s = ereply (MSG_use_b, bufn, NBUFN, 0)) != TRUE)
	return (s);
    splitwind ();
    return (_usebuffer (bufn));
}

/* does all the work for changing to a new buffer for use-buffer,
* use-buffer-split and prev-buff & next-buff
*/
bool
_usebuffer (bufn)
    char *bufn;
{
    register BUFFER *bp;
    register WINDOW *wp;

    if (strcmp (MSG_kill_b, bufn) == 0)	/* hack! */
	bp = blistp;
    else if ((bp = bfind (bufn, TRUE)) == NULL)
	return (FALSE);

    /* if current buffer is special and new buffer is normal */
    /* set to hex byte mode */
    if ((curbp == blistp) && (R_TYPE (curwp) == TEXT))
    {
	dispsize1 ();
	hexmode ();
    }

    if (--curbp->b_nwnd == 0)
    {
	/* Last use.         */
	curbp->b_dotp = curwp->w_dotp;
	curbp->b_doto = curwp->w_doto;
	curbp->b_unit_offset = curwp->w_unit_offset;	/* pvr */
	curbp->b_markp = curwp->w_markp;
	curbp->b_marko = curwp->w_marko;
    }
    curbp = bp;			/* Switch.       */
    curwp->w_bufp = bp;
    curwp->w_linep = bp->b_linep;	/* For macros, ignored.     */
    curwp->w_loff = 0;		/* pvr */
    curwp->w_flag |= WFMODE | WFFORCE | WFHARD;
    /* Quite nasty.      */
    if (bp->b_nwnd++ == 0)
    {
	/* First use.        */
	curwp->w_dotp = bp->b_dotp;
	curwp->w_doto = bp->b_doto;
	curwp->w_unit_offset = 0;	/* pvr */
	curwp->w_markp = bp->b_markp;
	curwp->w_marko = bp->b_marko;
	wind_on_dot (curwp);
	/* if we are in the funny TEXT mode then goto standard HEX mode */
	if (R_TYPE (curwp) == TEXT)
	    hexmode ();
	return (TRUE);
    }
    wp = wheadp;		/* Look for old.     */
    while (wp != NULL)
    {
	if (wp != curwp && wp->w_bufp == bp)
	{
	    curwp->w_dotp = wp->w_dotp;
	    curwp->w_doto = wp->w_doto;
	    curwp->w_unit_offset = wp->w_unit_offset;	/* pvr */
	    curwp->w_markp = wp->w_markp;
	    curwp->w_marko = wp->w_marko;
	    break;
	}
	wp = wp->w_wndp;
    }
    wind_on_dot (curwp);
    /* if we are in the funny TEXT mode then goto standard HEX mode */
    if (R_TYPE (curwp) == TEXT)
	hexmode ();
    return (TRUE);
}

/*
* Dispose of a buffer, by name.
* Ask for the name. Look it up (don't get too
* upset if it isn't there at all!). Get quite upset
* if the buffer is being displayed. Clear the buffer (ask
* if the buffer has been changed). Then free the header
* line and the buffer header. Bound to "C-X K".
*/
char
killbuffer ()
{
    register char s;
    char bufn[NBUFN];

    if ((s = ereply (MSG_kill_b, bufn, NBUFN, 0)) != TRUE)
	return (s);
    if ((s = _killbuffer (bufn)))
	writ_echo (okmsg);	/* verbose-ness (jam) */
    return (s);
}

char
_killbuffer (bufn)
    char *bufn;
{
    register BUFFER *bp, *bp1, *bp2;
    register char s, x = 0;

    if (((bp = bfind (bufn, FALSE)) == NULL) ||
	!strcmp (bufn, MSG_save_buf))
    {
	writ_echo (MSG_not_fnd);
	return (FALSE);
    }


    if (killablebufs (bp))	/* can't kill '?' if no other buffers */
    {
	writ_echo (MSG_no_del_m);
	return (FALSE);
    }

    /* see if the buffer to be killed is in a window */
    bp1 = bp;
    if (curbp == blistp && onebuf (bp))	/* Hack ! */
    {
	next_buf ();
	onlywind ();
	update ();
    }

    if (bp->b_nwnd > 0)
    {
	if ((s = eyesno (MSG_buf_disp)) != TRUE)
	    return (s);

	/* make the current window the only window if it is to die */
	onlywind ();
	if (curbp == bp)
	{
	    next_buf ();
	    if (curbp == bp)
		x++;
	}
    }
    if ((s = bclear (bp)) != TRUE)	/* Blow text away.      */
    {
	if (bp1 == blistp)	/* special buffer */
	    curbp = bp1;
	else if (!x)
	    _usebuffer (bp1->b_bname);
	/* back to original buffer (jam) */
	return (s);
    }
    if (x)
    {
	_usebuffer (MSG_main);
	x++;
    }

    free ((char *) bp->b_linep);/* Release header line.         */
    bp1 = NULL;			/* Find the header.     */
    bp2 = bheadp;
    while (bp2 != bp)
    {
	bp1 = bp2;
	bp2 = bp2->b_bufp;
    }
    bp2 = bp2->b_bufp;		/* Next one in chain.   */
    if (bp1 == NULL)		/* Unlink it.           */
	bheadp = bp2;
    else
	bp1->b_bufp = bp2;
    free ((char *) bp);		/* Release buffer block         */
    if (x)
	update ();
    /* update buffer display */
    if ((blistp->b_nwnd != 0) &&
	(blistp->b_type == BTLIST))
	listbuffers ();
    return (TRUE);
}

/*
* Display the buffer list. This is done
* in two parts. The "makelist" routine figures out
* the text, and puts it in the buffer whoses header is
* pointed to by the external "blistp". The "popblist"
* then pops the data onto the screen. Bound to
* "C-X C-B".
*/
char
listbuffers ()
{
    register char s;

    if ((s = makelist ()) != TRUE)
	return (s);
    return (popblist ());
}

/*
* Display the save buffer contents.
* Bound to "Meta C-W".
*/
char
showsavebuf ()
{
    WINDOW *wp;

    if (sav_buf.b_nwnd == 0)
    {
	splitwind ();
	_usebuffer (MSG_save_buf);
    }
    else
    {
	wp = wheadp;		/* Look for old.     */
	while (wp != NULL)
	{
	    if (wp->w_bufp == &sav_buf)
	    {
		wp->w_flag |= WFMODE | WFFORCE | WFHARD;
		break;
	    }
	    wp = wp->w_wndp;
	}
    }
    return (TRUE);
}

/*
* Pop the special buffer whose
* buffer header is pointed to by the external
* variable "blistp" onto the screen. This is used
* by the "listbuffers" routine (above) and by
* some other packages. Returns a status.
*/
bool
popblist ()
{
    register WINDOW *wp;
    register BUFFER *bp;

    if (blistp->b_nwnd == 0)	/* Not on screen yet.    */
    {
	if ((wp = wpopup ()) == NULL)
	    return (FALSE);
	bp = wp->w_bufp;
	if (--bp->b_nwnd == 0)
	{
	    bp->b_dotp = wp->w_dotp;
	    bp->b_doto = wp->w_doto;
	    bp->b_unit_offset = wp->w_unit_offset;	/* pvr */
	    bp->b_markp = wp->w_markp;
	    bp->b_marko = wp->w_marko;
	}
	curwp = wp;
	curbp = blistp;
	wp->w_bufp = blistp;
	++blistp->b_nwnd;
    }
    wp = wheadp;
    while (wp != NULL)
    {
	if (wp->w_bufp == blistp)
	{
	    wp->w_linep = lforw (blistp->b_linep);
	    wp->w_loff = 0;
	    wp->w_dotp = lforw (blistp->b_linep);
	    wp->w_doto = 0;
	    wp->w_unit_offset = 0;
	    wp->w_markp = NULL;
	    wp->w_marko = 0;
	    wp->w_disp_shift = 0;
	    wp->w_intel_mode = FALSE;
	    wp->w_fmt_ptr = &text_fmt;
	    wp->w_flag |= WFMODE | WFHARD;
	}
	wp = wp->w_wndp;
    }
    return (TRUE);
}

/*
* This routine rebuilds the
* text in the special secret buffer
* that holds the buffer list. It is called
* by the list buffers command. Return TRUE
* if everything works. Return FALSE if there
* is an error (if there is no memory).
*/
char
makelist ()
{
    register char *cp1;
    register char *cp2;
    register int c;
    register BUFFER *bp;
    register A32 nbytes;
    register char s;
    char b[8 + 1];
    char line[128];

    blistp->b_flag &= ~BFCHG;	/* Blow away old.    */
    if ((s = bclear (blistp)) != TRUE)
	return (s);
    blistp->b_flag |= BFVIEW;
    blistp->b_type = BTLIST;
    strcpy (blistp->b_fname, MSG_up_arrow);
    if (addline (MSG_l_buf_h) == FALSE
	|| addline (MSG_l_buf_h1) == FALSE)
	return (FALSE);
    bp = bheadp;		/* For all buffers   */
    while (bp != NULL)
    {
	cp1 = &line[0];		/* Start at left edge    */
	if ((bp->b_flag & BFCHG) != 0)	/* "*" if changed    */
	    *cp1++ = '*';
	else if (bp->b_flag & BFVIEW)	/* jam */
	    *cp1++ = 'R';	/* readonly */
	else
	    *cp1++ = ' ';
	*cp1++ = ' ';		/* Gap. */
	if ((bp->b_flag & BFBAD) != 0)	/* "?" if maybe trashed  */
	    *cp1++ = '?';
	else
	    *cp1++ = ' ';
	*cp1++ = ' ';		/* Gap. */
	nbytes = bp->b_linep->l_bp->l_file_offset +
	    bp->b_linep->l_bp->l_used;
	sprintf (b, "%8lx", nbytes);	/* 8 digit buffer size.   */
	cp2 = &b[0];
	while ((c = *cp2++) != 0)
	    *cp1++ = c;
	*cp1++ = ' ';		/* Gap.          */
	cp2 = &bp->b_bname[0];	/* Buffer name       */
	while ((c = *cp2++) != 0)
	    *cp1++ = c;
	*cp1++ = ' ';		/* Gap.          */
	*cp1++ = ' ';		/* Gap.          */
	cp2 = &bp->b_fname[0];	/* File name         */
	if (*cp2 != 0)
	{
	    while (cp1 < &line[1 + 1 + 1 + 1 + 6 + 1 + NBUFN + 1])
		*cp1++ = ' ';
	    while ((c = *cp2++) != 0)
	    {
		if (cp1 < &line[128 - 1])
		    *cp1++ = c;
	    }
	}
	while (cp1 < &line[80])	/* Fill out line to col 80 */
	    *cp1++ = ' ';

	*cp1 = 0;		/* Add to the buffer.    */
	if (addline (line) == FALSE)
	    return (FALSE);
	bp = bp->b_bufp;
    }
    return (TRUE);		/* All done      */
}

/*
* The argument "text" points to
* a string. Append this line to the
* buffer list buffer.
* Return TRUE if it worked and
* FALSE if you ran out of room.
*/
bool
addline (text)
    char *text;
{
    register LINE *lp;
    register int i, allocsize;
    register int ntext;

    ntext = strlen (text);
    allocsize = 128;

    if ((lp = lalloc (allocsize)) == NULL)
	return (FALSE);

    for (i = 0; i < ntext; ++i)
	lputc (lp, i, text[i]);

    for (; i < allocsize; ++i)	/* fill out line with spaces */
	lputc (lp, i, ' ');

    blistp->b_linep->l_bp->l_fp = lp;	/* Hook onto the end  */
    lp->l_bp = blistp->b_linep->l_bp;
    blistp->b_linep->l_bp = lp;
    lp->l_fp = blistp->b_linep;
    lp->l_size = allocsize;	/* line size is limited to 80 chars */
    lp->l_used = allocsize;
    lp->l_file_offset = lp->l_bp->l_file_offset + lp->l_bp->l_used;
    if (blistp->b_dotp == blistp->b_linep)	/* If "." is at the end    */
	blistp->b_dotp = lp;	/* move it to new line   */
    return (TRUE);
}

/*
* Look through the list of
* buffers. Return TRUE if there
* are any changed buffers. Special buffers
* like the buffer list buffer don't count, as
* they are not in the list. Return FALSE if
* there are no changed buffers.
*/
bool
anycb ()
{
    register BUFFER *bp;

    bp = bheadp;
    while (bp != NULL)
    {

	if ((bp->b_flag & BFCHG) != 0)
	    return (TRUE);
	bp = bp->b_bufp;
    }
    return (FALSE);
}

/*
* Search for a buffer, by name.
* If not found, and the "cflag" is TRUE,
* create a buffer and put it in the list of
* all buffers. Return pointer to the BUFFER
* block for the buffer.
*/
BUFFER *
bfind (bname, cflag)
    register char *bname;
    int cflag;
{
    register BUFFER *bp;

    bp = bheadp;
    while (bp != NULL)
    {
	if (strcmp (bname, bp->b_bname) == 0)
	    return (bp);
	bp = bp->b_bufp;
    }
    if (cflag != FALSE && (bp = bcreate (bname)) != NULL)
    {
	bp->b_bufp = bheadp;
	bheadp = bp;
    }
    return (bp);
}

/*
* Create a buffer, by name.
* Return a pointer to the BUFFER header
* block, or NULL if the buffer cannot
* be created. The BUFFER is not put in the
* list of all buffers; this is called by
* "edinit" to create the buffer list
* buffer.
*/
BUFFER *
bcreate (bname)
    register char *bname;
{

    register BUFFER *bp;
    register LINE *lp;

    if ((bp = (BUFFER *) malloc (sizeof (BUFFER))) == NULL)
    {
	err_echo (MSG_cnt_al_b);
	return (NULL);
    }
    if ((lp = lalloc (0)) == NULL)
    {
	free ((char *) bp);
	return (NULL);
    }
    bp->b_bufp = NULL;
    bp->b_dotp = lp;
    bp->b_doto = 0;
    bp->b_unit_offset = 0;	/* unit offset   pvr */
    bp->b_markp = NULL;
    bp->b_marko = 0;
    bp->b_flag = 0;
    bp->b_nwnd = 0;
    bp->b_linep = lp;
    strcpy (bp->b_fname, MSG_null);
    strcpy (bp->b_bname, bname);
    lp->l_fp = lp;
    lp->l_bp = lp;
    lp->l_file_offset = 0;	/* pvr */
    lp->l_used = 0;		/* pvr */
    lp->l_size = 0;		/* size of zero indicates the header line  pvr
							                                    */
    return (bp);
}

/*
* This routine blows away all of the text
* in a buffer. If the buffer is marked as changed
* then we ask if it is ok to blow it away; this is
* to save the user the grief of losing text. The
* window chain is nearly always wrong if this gets
* called; the caller must arrange for the updates
* that are required. Return TRUE if everything
* looks good.
*/
bool
bclear (bp)
    register BUFFER *bp;
{
    register LINE *lp;
    register char s;

    if ((bp->b_flag & BFCHG) != 0	/* Changed.       */
	&& (s = eyesno (MSG_no_chg)) != TRUE)
	return (s);
    bp->b_flag &= ~BFCHG;	/* Not changed       */
    while ((lp = lforw (bp->b_linep)) != bp->b_linep)
	lfree (lp);
    bp->b_dotp = bp->b_linep;	/* Fix "."      */
    bp->b_doto = 0;
    bp->b_unit_offset = 0;	/* pvr */
    bp->b_markp = NULL;		/* Invalidate mark  */
    bp->b_marko = 0;
    return (TRUE);
}

/* flip to next buffer in the list, wrap
* to beginning if required (wrap around)
* (skips buffers saved  by save-region)
*/
char
next_buf ()
{
    register BUFFER *bp;

    bp = curbp;
    while (TRUE)
    {
	if (!(bp = bp->b_bufp))
	    bp = bheadp;
	if ((bp->b_type == BTSAVE) ||
	    (bp->b_type == BTLIST) ||
	    (bp->b_type == BTHELP))
	    continue;
	break;
    }
    _usebuffer (bp->b_bname);
    return (TRUE);
}

/* flip to prev buffer in the list, wrap
* to end if required (wrap around)
* (does NOT skip buffers saved by save-region)
*/
char
prev_buf ()
{
    register BUFFER *sp;

    if ((sp = curbp) == bheadp)	/* front of list */
    {
	for (; sp->b_bufp; sp = sp->b_bufp)
	    ;
    }
    else
	/* cycle around */
    {
	for (sp = bheadp; sp->b_bufp; sp = sp->b_bufp)
	{
	    if (sp->b_bufp == curbp)
		break;
	}
    }
    return (_usebuffer (sp->b_bname));
}

/* yank a buffer into current buffer
*/
char
yank_buffer ()
{
    char bufn[NBUFN];

    if (ereply (MSG_yank_b, bufn, NBUFN, 0) != TRUE)
	return (FALSE);
    return (_yankbuffer (bufn));
}

bool
_yankbuffer (bufn)
    char *bufn;
{
    register LINE *lp;
    register BUFFER *bp = curbp;
    register int s;
    A32 cnt;
    char buf[NFILEN], buf1[NFILEN];

    if ((bp = bfind (bufn, FALSE)) == NULL)
    {
	writ_echo (MSG_no_buf);
	return (FALSE);
    }
    if (strcmp (bp->b_bname, curbp->b_bname) == 0)
    {
	writ_echo (MSG_no_s_yank);
	return (FALSE);
    }
    cnt = 0;
    lp = lforw (bp->b_linep);
    while (TRUE)
    {
	cnt += lp->l_used;
	for (s = 0; s < lp->l_used; s++)
	    if (linsert (1, lp->l_text[s]) == FALSE)
		return (FALSE);

	if ((lp = lforw (lp)) == bp->b_linep)
	{
	    break;
	}

	if ((cnt & 0x7ff) == 0)
	{
	    sprintf (buf1, MSG_ins_cnt, R_POS_FMT (curwp));
	    sprintf (buf, buf1, cnt);
	    writ_echo (buf);
	    /* check if we should quit */
	    if (ttkeyready ())
	    {
		l_fix_up (lp->l_bp);
		wind_on_dot_all ();
		if (ttgetc () == CTL_G)	/* was it an abort key? */
		    return (FALSE);
	    }
	}
    }
    writ_echo (okmsg);
    return (TRUE);
}

bool
buffername ()
{

    register WINDOW *wp;
    register char *p;
    register char s;
    char bname[NBUFN + 1];

    if ((s = ereply (MSG_buf_nam, bname, NBUFN, 0)) == ABORT)
	return (s);
    for (p = bname; *p && *p != ' '; p++)
	;
    *p = 0;			/* no blanks */
    strcpy (curbp->b_bname, bname);
    wp = wheadp;		/* Update mode lines.   */
    while (wp != NULL)
    {
	if (wp->w_bufp == curbp)
	    wp->w_flag |= WFMODE;
	wp = wp->w_wndp;
    }
    if ((blistp->b_nwnd != 0) &&/* update buffer display */
	(blistp->b_type == BTLIST))
	listbuffers ();
    return (TRUE);
}

/* any killable buffers around ? (jam)
*/
bool
killablebufs (bp)
    register BUFFER *bp;
{
    if (strcmp (bp->b_bname, MSG_main) == 0)	/* doomed buffer is 'empty' */
	if (bheadp == bp)	/* and is only buffer in list */
	    if (bheadp->b_bufp == 0)	/* then there are no killable buffers */
		return (TRUE);
    return (FALSE);
}

/* only 1 buffer around ?
*/
bool
onebuf (bp)
    register BUFFER *bp;
{
    if (strcmp (bp->b_bname, bheadp->b_bname) == 0)
	if (bheadp->b_bufp == 0)
	    return (TRUE);
    return (FALSE);
}

/* funky new name; real yukky!!!! (jam)
*/
void
funky_name (bname, n)
    register char *bname;
    int n;
{
    char num[10];
    register int i;
    register char *p;

    for (i = 0; i < 10; i++)
	num[i] = ' ';
    for (p = bname; *p; p++)
	*p = 0;
    *bname++ = '#';
    sprintf (num, "%lx", (long) n + 1);
    for (p = num; *p; p++)
	if (*p != ' ')
	    *bname++ = *p;
    *bname = 0;
}

/* pick a buffer to goto/kill
*/
#define BUFFEROFFSET (13)	/* depends on makelist !! */

bool
pickone ()
{
    register int s, i, c;
    register LINE *lp;
    char name[NBUFN + 1];
    char buf[3];
    //WINDOW *wp;

    lp = curwp->w_dotp;		/* get the buffer name from the line */

    i = 0;
    if (!llength (lp))
    {
	writ_echo (MSG_bad_l);
	return (FALSE);
    }
    for (s = BUFFEROFFSET; (c = lgetc (lp, s)) != ' '; s++)
    {
	name[i++] = c;
	if (s >= llength (lp))
	    break;
    }
    name[i] = 0;
    if (!bfind (name, FALSE))
    {
	writ_echo (MSG_bad_l);
	return (FALSE);
    }
  loop:
    if ((s = ereply (MSG_pick, buf, 2, name)) != TRUE)
	return (FALSE);
    if (ISLOWER (buf[0]) != FALSE)
	buf[0] = TOUPPER (buf[0]);
    if (buf[0] == 'K')
	_killbuffer (name);
    else if (buf[0] == 'G')
	_usebuffer (name);
    else if (buf[0] == 'S')
    {
	_usebuffer (name);
	/* goto this buffer, but don't show the user */
	filesave ();
	_usebuffer (MSG_kill_b);
	/* jump back to this window - HACK ! */
	listbuffers ();		/* update the list */
    }
    else
	goto loop;
    writ_echo (MSG_null);
    return (TRUE);
}

/*
*   Toggle the buffer size lock bit.
*/
char
bufsizlock ()
{
    if (curbp->b_flag & BFSLOCK)
    {
	curbp->b_flag &= ~BFSLOCK;
	writ_echo (MSG_siz_chg);
    }
    else
    {
	if (insert_mode)
	    insert_toggle ();
	curbp->b_flag |= BFSLOCK;
	writ_echo (MSG_no_siz_chg);
    }
    return (TRUE);
}

/*
 *   Append the given line to the end of the given buffer.
 */
void
b_append_l (buf_p, lp)
    BUFFER *buf_p;
    LINE *lp;
{
    LINE *h_lp;

    h_lp = buf_p->b_linep;

    lp->l_fp = h_lp;
    lp->l_bp = h_lp->l_bp;
    lp->l_bp->l_fp = lp;
    h_lp->l_bp = lp;
    lp->l_file_offset = lp->l_bp->l_file_offset + lp->l_bp->l_used;
}

/*
 *   Append the given line to the end of the given buffer.
 */
bool
b_append_c (buf_p, ch)
    BUFFER *buf_p;
    D8 ch;
{
    LINE *lp;

    lp = buf_p->b_linep->l_bp;	/* get last line */
    /* do I need to get a new line? */
    if (lp->l_size <= lp->l_used)
    {
	if ((lp = lalloc (KBLOCK)) == NULL)
	    return (FALSE);

	lp->l_fp = buf_p->b_linep;
	lp->l_bp = buf_p->b_linep->l_bp;
	lp->l_bp->l_fp = lp;
	buf_p->b_linep->l_bp = lp;
	lp->l_file_offset = lp->l_bp->l_file_offset + lp->l_bp->l_used;
    }
    lp->l_text[lp->l_used++] = ch;

    return (TRUE);
}

/*
 * Initialize the save buffer.
 */
void
save_buf_init ()
{
    register BUFFER *bp;

    sav_line_h.l_fp = &sav_line_h;
    sav_line_h.l_bp = &sav_line_h;
    sav_line_h.l_file_offset = 0;
    sav_line_h.l_used = 0;
    sav_line_h.l_size = 0;

    sav_buf.b_type = BTSAVE;
    sav_buf.b_bufp = NULL;
    sav_buf.b_dotp = &sav_line_h;
    sav_buf.b_doto = 0;
    sav_buf.b_unit_offset = 0;
    sav_buf.b_markp = NULL;
    sav_buf.b_marko = 0;
    sav_buf.b_linep = &sav_line_h;
    sav_buf.b_nwnd = 0;
    sav_buf.b_flag = BFVIEW;
    sav_buf.b_begin_addr = 0;
    sav_buf.b_file_size = 0;
    sav_buf.b_fname[0] = 0;
    strcpy (sav_buf.b_bname, MSG_save_buf);

    /* put on end of chain */
    bp = bheadp;
    while ((bp->b_bufp) != NULL)
	bp = bp->b_bufp;

    bp->b_bufp = &sav_buf;

}

/*
 * Set the save buffer dot pointer to the begining.
 */
void
save_buf_home ()
{
    sav_buf.b_dotp = sav_buf.b_linep->l_fp;
    sav_buf.b_doto = 0;
    sav_buf.b_flag = BFVIEW;
}

D16
get_save_char ()
{
    D8 ch;

    /* are we past the end of the buffer */
    if (sav_buf.b_dotp == sav_buf.b_linep)
	return (-1);
    ch = sav_buf.b_dotp->l_text[sav_buf.b_doto++];
    if (sav_buf.b_doto >= sav_buf.b_dotp->l_used)
    {
	sav_buf.b_doto = 0;
	sav_buf.b_dotp = sav_buf.b_dotp->l_fp;
    }
    return ((D16) ch);
}