Repository navigation
Expand file tree
/
Copy pathsbstr.c
More file actions
executable file
·2195 lines (1977 loc) · 64.7 KB
/
Copy pathsbstr.c
File metadata and controls
executable file
·2195 lines (1977 loc) · 64.7 KB
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
989
990
991
992
993
994
995
996
997
998
999
1000
/* SB - Copyright 1982 by Ken Harrenstien, SRI International
* This software is quasi-public; it may be used freely with
* like software, but may NOT be sold or made part of licensed
* products without permission of the author. In all cases
* the source code and any modifications thereto must remain
* available to any user.
*
* This is part of the SB library package.
* Any software using the SB library must likewise be made
* quasi-public, with freely available sources.
*/
#if 0
Todo stuff:
New definitions:
sbbuffer - old sbstr. Abbrev & struct "sbbuff". Macro SBBUFF
(or SBBUF?)
sbstring - list of sds. Abbrev sbstr. Macro SBSTR.
Should *sbstr == *sdblk? Yeah.
sbfile - as before. Macro SBFILE. (or SBFIL?)
Try to get zero-length sdblks flushed on the fly,
rather than waiting for moby GC. Also, need to set
up compaction of SD freelist, as well as SM freelist.
Make SM freelist compact self-invoked by SBM_MGET?
Any need for phys disk ptrs other than for tempfile?
Can do sbm_forn through SDblks to find active sdfiles
so list isn''t needed for that.
Can sdback be flushed? (not needed for keeping list sorted,
or for searching it -- only used when linking
blocks in or out of list.) Perhaps use circular list?
If list only used for tmpfile, then to link in/out could
always start from sfptr1 of tmpfile? Sure, but slow?
Last SD on phys list could belong to no logical list,
and denote free space on tmpfile?
--------------------------
An "open" SBBUFFER will allow one to read, write, insert into,
and delete from a sbstring (a logical character string). "Dot" refers
to the current logical character position, which is where all
operations must happen; sb_fseek must be used to change this location.
There are several states that the I/O can be in:
!SBCUR ----CLOSED----
All other elements, including SBIOP, should also be 0.
Dot is 0.
SBCUR && !SBIOP ----OPEN/IDLE----
SBCUR points to a SD block (its SDMEM may or may not exist)
SBIOP==0 (otherwise it would be open/ready)
Dot is SBDOT + SBOFF.
R/Wleft must be 0.
SBCUR && SBIOP ----OPEN/READY----
SBCUR points to a SDBLK (SDMEM must exist!)
SBIOP exists.
Dot is SBDOT + offset into SMBLK. SBOFF is ignored!
SB_WRIT flag is se
8000
t if "smuse" must be updated.
The R/Wleft counts are set up:
1. Rleft 0, Wleft 0 -- Since SBIOP is set, must assume
counts are too.
So this means at end of text, no room left.
Otherwise would imply that setup needs doing.
2. Rleft N, Wleft 0 -- At beg or middle of text
3. Rleft 0, Wleft N -- At end of text
4. Rleft N, Wleft N -- Shouldn''t ever happen
Note that Rleft is always correct. Wleft is sometimes
set 0 in order to force a call to determine real state.
Note that SBIOP alone is a sufficient test for being OPEN/READY.
The important thing about updating the smblk is to ensure that the "smuse"
field is correct. This can only be changed by writing or deleting. We assume
that deletions always update immediately, thus to determine if an update
is necessary, see if SB_WRIT is set. If so, update smuse before doing
anything but more writing!!!!
The SDBLK must be marked "modified" whenever a write operation is
done. We try to do this only the first time, by keeping Wleft zero
until after the first write. This is also when SB_WRIT gets set.
However, if in overwrite mode, Wleft must be kept zero in order to
force the proper actions; SB_WRIT is also not turned on since smuse
will not change. Note that at EOF, overwrite becomes the same thing
as insert and is treated identically...
If a SBLK has an in-core copy but no disk copy, it can be
freely modified. Otherwise, modifications should preferably split
the block so as to retain "pure" blocks as long as possible. "Pure" blocks
can always have their in-core versions flushed immediately (unless for
compaction purposes they''ll need to be written out in the same GC pass).
Alternatively, mods can simply mark the disk copy "free" and go
ahead as if no such copy existed.
No additions or changes to a pure block are allowed, but
deletions from the end or beginning are always allowed. All other
changes must split or insert new blocks to accomplish the changes.
Locking:
SDBLKs are subject to unpredictable relocation, compaction,
and garbage collecting. There are three ways in which a SDBLK can
remain fixed:
1. The SDBLK has the SD_LOCK flag set. This flag is used whenever
a SBBUF''s SBCUR is pointing to this SDBLK.
2. The SDBLK has the SD_LCK2 flag set. This flag is used only
during execution of various internal routines and should
not be seen anywhere during execution of user code.
3. The SDBLK has no back-pointer (is first block in a sbstring).
Such SDBLKs cannot be relocated (since it is not known
what may be pointing to them) but unlike the other 2 cases
they are still subject to compaction with succeeding SDBLKs.
The SDBLK must be locked with SD_LOCK for as long as it is being
pointed to by SBCUR. The sole exception is when a SBBUF in the
OPEN/IDLE state is pointing to the first SDBLK of a sbstring; this
sdblk is guaranteed not to be moved, since sdblks without a
back-pointer are never moved. SD_LOCK is asserted as soon as the state
changes to OPEN/READY, of course. The internal routines take pains to
always move SD_LOCK as appropriate. Note that only one SD in a
sbstring can ever have SD_LOCK turned on. SD_LCK2 is an auxiliary flag
which may appear in more than one SDBLK, for use by low-level routines
for various temporary reasons; either will prevent the SDBLK from being
modified in any way by the storage compactor.
SEEKs are a problem because it''s unclear at seek time what will happen
next, so the excision of the smblk can''t be optimized. If the seek
happens to land in a sdblk with an existing smblk, there''s no problem;
but if it''s a sdblk alone, how to decide which part of it to read in???
If next action is:
write - split up sdblk and create new one. Read nothing in.
read - read in 512 bytes starting at disk blk boundary if possible
else read in 128 bytes starting with selected char
(include beg of sdblk if less than 64 chars away)
overwrite - as for read.
backread - like read but position at end of sdblk.
delete - split up sdblk, read nothing in.
We solve this through the OPEN/IDLE state, where SBIOP == 0 means SBOFF
points to logical offset from start of current sdblk, so that the seek
need not take any action. Only when a specific operation is requested
will the transition to OPEN/READY take place, at which time we''ll know
what the optimal excision strategy is. The routine SBX_READY performs
this function.
The physical links (SDFORW and SDBACK) are only valid when SDFILE is
set (likewise for SDLEN and SDADDR). In other words, mungs to a sdblk
must check SDFILE to see whether or not the phys links should be
altered. Normally they aren''t except during sdblk creation, deletion,
or swapout, no matter how much the sdblk gets shuffled around
logically. The disk physical list is kept sorted in order of starting
addresses. The text blocks indicated can overlap. When a GC is
necessary, the code must figure out how much space is actually free.
-------------- Old woolgathering, ignore rest of this page ---------------
Question: should 512-byte buffers be maintained, one for each SBFILE?
Or should the in-core text be hacked up to serve for buffering?
Question is where to point the READ/WRITE system calls. Currently,
they are pointed directly at the in-core text, and there are no
auxiliary buffers.
If use auxiliary buffers:
How to handle flushing, when changing location etc?
Could be clever about reading from large disk block, only
get part of it into buffer instead of splitting up in order to
read a "whole" block.
Problem: sbstrings can include pieces of several different files.
Hard to maintain just one buffer per FD without hacking
done on one sbstring screwing that on another.
If don''t use buffers:
Need to have a "chars-left" field in mem blocks, so know how
much more can be added. Will need heuristics for how much
extra space to allocate.
#endif /*COMMENT*/
/* Includes, initial definitions */
#include <stdio.h>
#include "sb.h"
#ifndef V6
#define V6 0
#endif
#if V6
#include <stat.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#if MINIX
#include <fcntl.h> /* For open() flags */
#else
#include <sys/file.h> /* For open() flags */
#endif /* MINIX */
#endif /*-V6*/
extern int errno;
extern char *strerror(); /* From ANSI <string.h> */
/* Allocation decls */
SBFILE sbv_tf; /* SBFILE for temp swapout file */
int (*sbv_debug)(); /* Error handler address */
/* SBX_READY argument flags (internal to SBSTR routines only)
* The following values should all be unique; the exact value
* doesn't matter as long as the right SKM flags are given.
*/
#define SK_READF 0 /* 0-skip fwd, align BOB */
#define SK_READB (0|SKM_0BACK|SKM_EOB) /* 0-skip bkwd, align EOB */
#define SK_WRITEF (0|SKM_EOB) /* 0-skip fwd, align EOB */
#define SK_DELF (4|SKM_0BACK) /* 0-skip bkwd, align BOB */
#define SK_DELB (4|SKM_EOB) /* 0-skip fwd, align EOB */
#define SKM_0BACK 01 /* Zero-skip direction: 0 = fwd, set = backwd
* Don't ever change this value! See SBX_NORM. */
#define SKM_EOB 02 /* Alignment: 0 = Beg-Of-Buf, set = End-Of-Buf */
/* Note on routine names:
* "SB_" User callable, deals with sbbufs (usually).
* "SBS_" User callable, deals with sbstrings only.
* "SBX_" Internal routine, not meant for external use.
* "SBM_" Routine handling mem alloc, usually user callable.
*/
/* SBBUF Opening, Closing, Mode setting */
/* SB_OPEN(sb,sd) - Sets up SBBUF given pointer to first SD of a sbstring.
* If SD == 0 then creates null sbstring.
* Any previous contents of SBBUF are totally ignored!!! If you
* want to save the stuff, use SB_UNSET.
* Sets I/O ptr to start of sbstring.
* Returns 0 if error, else the given SB.
*/
SBBUF *
sb_open(sbp,sdp)
SBBUF *sbp;
SBSTR *sdp;
{ register struct sdblk *sd;
register int cnt;
register WORD *clrp;
if(!sbp) return((SBBUF *)0);
if((sd = sdp) == 0)
{ sd = sbx_ndget(); /* Get a fresh node */
clrp = (WORD *) sd; /* Clear it all */
cnt = rnddiv(sizeof(struct sdblk));
do { *clrp++ = 0; } while(--cnt);
sd->sdflags = SD_NID; /* Except flags of course */
}
else if(sd->slback) /* Must be first thing in sbstring */
return((SBBUF *)0); /* Perhaps could normalize tho */
clrp = (WORD *) sbp; /* Clear sbbuffer stuff */
cnt = rnddiv(sizeof(SBBUF));
do { *clrp++ = 0; } while(--cnt);
sbp->sbcur = sd;
/* Note that SD_LOCK need not be set, because first SDBLK has no
* backptr. This is desirable to allow storage compactor maximum
* freedom in merging sdblks.
*/
/* sd->sdflags |= SD_LOCK; */ /* Lock this one */
return(sbp);
}
/* SB_CLOSE(sb) - Close a SBBUF.
* Returns pointer to start of sbstring (first SD).
* Returns 0 if error.
*/
SBSTR *
sb_close(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
register struct sdblk *sd;
if((sb = sbp) == 0) /* Verify pointer */
return((SBSTR *)0);
sb_rewind(sb); /* Do most of the work, including unlock */
sd = sb->sbcur; /* Save ptr to sbstring */
sb->sbcur = 0; /* Now reset the sbbuffer structure */
sb->sbflags = 0;
return(sd);
}
/* SB_SETOVW(sbp) - Set SBBUF Over-write mode for PUTC's.
* SB_CLROVW(sbp) - Clear ditto.
*/
sb_setovw(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
if(sb=sbp)
{ sb->sbflags |= SB_OVW;
sb->sbwleft = 0;
}
}
sb_clrovw(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
if(sb=sbp) sb->sbflags &= ~SB_OVW;
}
/* SBSTRING file system operations (see also sb_fsave) */
/* SB_FDUSE(fd) - Make a sbstring for given file.
* FD is an open file descriptor.
* Returns pointer to a SBSTR containing the given file, or 0 if error.
* The FD must not be closed until a SB_FDCLS is done to
* purge memory of any blocks pointing at the file.
* ** Maybe allocate sbfile structs with sbx_ndget, i.e. overlay on
* ** top of sdblk node?? Wd this screw verify, GC, etc? Maybe not if
* ** SD_LCK2 set...
*/
struct sbfile *sbv_ftab[SB_NFILES];
chroff
sbx_fdlen(fd)
int
10000
fd;
{
#if !V6
struct stat statb;
#else
struct statb statb;
chroff len;
struct {int hiwd ; int lowd;} foo;
#endif /*V6*/
if(fstat(fd,&statb) < 0) return((chroff)-1);
#if V6
len = statb.i_size1;
len.hiwd = statb.i_size0 & 0377;
return(len);
#else
return((chroff)statb.st_size);
#endif /*-V6*/
}
SBSTR *
sb_fduse(ifd)
int ifd;
{ register struct sdblk *sd;
register struct sbfile *sf;
register int fd;
chroff len;
if((fd = ifd) < 0 || SB_NFILES <= fd /* Check for absurd FD */
|| sbv_ftab[fd]) /* and slot already in use */
return((SBSTR *)0);
if((len = sbx_fdlen(fd)) < 0) return((SBSTR *)0);
sbv_ftab[fd]= sf = (struct sbfile *)sbx_malloc(sizeof(struct sbfile));
sf->sffd = fd;
sf->sfptr1 = sd = sbx_ndget();
sf->sflen = len;
sd->slforw = 0;
sd->slback = 0;
sd->sdforw = 0;
sd->sdback = 0;
sd->sdmem = 0;
sd->sdfile = sf;
sd->sdlen = len;
sd->sdaddr = 0;
return(sd);
}
/* SB_FDCLS(fd) - Close a file descriptor being used by sbstrings.
* If arg is -1, closes all FD's that are unused (a "sweep").
* For specific arg, returns 0 if couldn't close FD because still in use.
* Perhaps later version of routine could have option to copy
* still-used SD's to tempfile, and force the FD closed?
*/
sb_fdcls(ifd)
int ifd;
{ register int fd;
if((fd = ifd) >= 0)
{ if(fd >= SB_NFILES) return(0); /* Error of sorts */
return(sbx_fcls(sbv_ftab[fd]));
}
fd = SB_NFILES-1;
do {
sbx_fcls(sbv_ftab[fd]);
} while(--fd); /* Doesn't try FD 0 ! */
return(1);
}
sbx_fcls(sfp)
struct sbfile *sfp;
{ register struct sbfile *sf;
register int fd;
if((sf = sfp)==0 /* Ignore null args */
|| sf == &sbv_tf) /* and never close our tempfile! */
return(0);
fd = sf->sffd; /* Find sys file descriptor */
if(sbv_ftab[fd] != sf) /* Ensure consistency */
return(sbx_err(0,"SF table inconsistency"));
if(sf->sfptr1) /* Any phys list still exists? */
return(0); /* Yes, still in use, can't close */
close(fd); /* Maybe do this when list gone? */
sbv_ftab[fd] = 0; /* Remove from table */
free(sf); /* Remove sbfile struct from mem */
}
/* SB_FDINP(sb,fd) - Returns TRUE if specified fd is still in use
* by specified sbbuffer.
*/
sb_fdinp(sb, fd)
register SBBUF *sb;
int fd;
{ register struct sdblk *sd;
register struct sbfile *sf;
if((sf = sbv_ftab[fd]) == 0
|| (sd = sb->sbcur) == 0)
return(0);
sd = sbx_beg(sd); /* Move to beginning of sbstring */
for(; sd; sd = sd->slforw) /* Scan thru all blocks in string */
if(sd->sdfile == sf) /* If any of them match, */
return(1); /* Return tally-ho */
return(0);
}
/* SB_FSAVE(sb,fd) - Write entire SBBUF out to specified FD.
* Returns 0 if successful, else system call error number.
*/
sb_fsave(sb,fd) /* Write all of given sbbuf to given fd */
register SBBUF *sb;
int fd;
{
sbx_smdisc(sb);
return(sbx_aout(sbx_beg(sb->sbcur), 2, fd));
}
/* SBBUF Character Operations */
/* SB_GETC(sb) - Get next char from sbstring.
* Returns char at current location and advances I/O ptr.
* Returns EOF on error or end-of-string.
*/
int
sb_sgetc(sb)
register SBBUF *sb;
{
if(--(sb->sbrleft) >= 0)
return sb_uchartoint(*sb->sbiop++);
/* Must do hard stuff -- check ptrs, get next blk */
sb->sbrleft = 0; /* Reset cnt to zero */
if(sb->sbcur == 0 /* Make sure sbbuffer there */
|| (int)sbx_ready(sb,SK_READF,0,SB_BUFSIZ) <= 0) /* Normalize & gobble */
return(EOF);
return(sb_sgetc(sb)); /* Try again */
} /* Loop wd be faster, but PDL OV will catch infinite-loop bugs */
/* SB_PUTC(sb,ch) - Put char into sbstring.
* Inserts char at current location.
* Returns EOF on error, else the char value.
*/
int
sb_sputc(sb,ch)
register SBBUF *sb;
int ch;
{
register struct sdblk *sd;
if(--(sb->sbwleft) >= 0) return(*sb->sbiop++ = ch);
sb->sbwleft = 0; /* Reset cnt to avoid overflow */
if((sd = sb->sbcur) == 0) /* Verify string is there */
return(EOF); /* Could perhaps create it?? */
if(sb->sbflags&SB_OVW) /* If overwriting, handle std case */
{ if(sb->sbiop &&
--sb->sbrleft >= 0) /* Use this for real count */
{ sd->sdflags |= SD_MOD; /* Win, munging... */
return(*sb->sbiop++ = ch);
}
/* Overwriting and hit end of this block. */
if((int)sbx_ready(sb,SK_READF,0,SB_BUFSIZ) > 0) /* Re-normalize */
return(sb_sputc(sb,ch));
/* No blks left, fall through to insert stuff at end */
}
/* Do canonical setup with heavy artillery */
if((int)sbx_ready(sb,SK_WRITEF,SB_SLOP,SB_BUFSIZ) <= 0) /* Get room */
return(EOF); /* Should never happen, but... */
sb->sbflags |= SB_WRIT;
sb->sbcur->sdflags |= SD_MOD;
return(sb_sputc(sb,ch)); /* Try again */
} /* Loop wd be faster, but PDL OV will catch infinite-loop bugs */
/* SB_PEEKC(sb) - Peek at next char from sbstring.
* Returns char that sb_getc would next return, but without
* changing I/O ptr.
* Returns EOF on error or end-of-string.
*/
int
sb_speekc(sb)
register SBBUF *sb;
{
if (sb->sbrleft <= 0) /* See if OK to read */
{ if (sb_sgetc(sb) == EOF) /* No, try hard to get next */
return EOF; /* Failed, return EOF */
sb_backc(sb); /* Won, back up */
}
return sb_uchartoint(*sb->sbiop);
}
/* SB_RGETC(sb) - Get previous char from sbstring.
* Returns char prior to current location and backs up I/O ptr.
* Returns EOF on error or beginning-of-string.
*/
int
sb_rgetc(sb)
register SBBUF *sb;
{
register struct smblk *sm;
register struct sdblk *sd;
if((sd=sb->sbcur) && (sm = sd->sdmem)
&& sb->sbiop > sm->smaddr)
{ if(sb->sbflags&SB_WRIT)
{ sm->smuse = sb->sbiop - sm->smaddr;
sb->sbwleft = 0;
sb->sbflags &= ~SB_WRIT;
}
sb->sbrleft++;
return sb_uchartoint(*--sb->sbiop); /* Return char */
}
if((int)sbx_ready(sb,SK_READB,SB_BUFSIZ,0) <= 0)
return(EOF);
return(sb_rgetc(sb));
}
/* SB_RDELC(sb) - Delete backwards one char.
* Returns nothing.
*/
sb_rdelc(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
register struct sdblk *sd;
if(((sb=sbp)->sbflags&SB_WRIT) /* Handle simple case fast */
&& sb->sbiop > (sd = sb->sbcur)->sdmem->smaddr)
{ sb->sbwleft++;
sb->sbiop--;
sd->sdflags |= SD_MOD;
return;
}
else sb_deln(sb,(chroff) -1); /* Else punt... */
}
/* SB_DELC(sb) - Delete one char forward? */
/* SB_INSC(sb,ch) - Insert char? (instead of or in addition to PUTC) */
/* SBBUF string or N-char operations */
/* SB_DELN(sb,chroff) - delete N chars. Negative N means backwards.
* Differs from sb_killn in that it flushes the text forever,
* and doesn't return anything.
*/
sb_deln(sbp, num)
SBBUF *sbp;
chroff num;
{
register struct sdblk *sd;
if(sd = sb_killn(sbp,num))
sbs_del(sd); /* Punt */
}
/* SB_KILLN(sb,chroff) - delete N chars, saving. Negative N means backwards.
* Returns SD pointer to beginning of saved sbstring.
*/
struct sdblk *
sb_killn(sbp, num)
SBBUF *sbp;
chroff num;
{ register SBBUF *sb;
register struct sdblk *sd, *sd2;
struct sdblk *sdr, *sdx;
chroff savdot;
if((sd = sbx_xcis((sb=sbp),num,&sdr,&savdot)) == 0)
return((struct sdblk *)0);
sb->sbcur->sdflags &= ~SD_LOCK; /* Now can flush sbcur lock */
/* SD and SD2 now delimit bounds of stuff to excise.
* First do direction dependent fixups
*/
if(num >= 0) /* If deleting forward, */
sb->sbdot = savdot; /* must reset dot to initial loc */
/* SD and SD2 now in first/last order. Complete SBCUR fixup. */
sd2 = sdr; /* sdr has ptr to end of stuff */
if(sd2 = sd2->slforw) /* More stuff after killed list? */
{ sb->sbcur = sd2; /* Yes, point at it */
sb->sboff = 0; /* Dot already set right */
}
else if(sdx = sd->slback) /* See if any prior to killed list */
{ sb->sbcur = sdx; /* Yes, point at it */
sb->sboff = (sdx->sdmem ? /* Get len of prev blk */
sdx->sdmem->smuse : sdx->sdlen);
sb->sbdot -= sb->sboff;
}
else sb_open(sb,(SBSTR *)0); /* No stuff left! Create null sbstring */
/* Fix up logical links. Note SD2 points to succ of killed stuff */
if(sd->slback) /* If previous exists */
{ if(sd->slback->slforw = sd2) /* Point it to succ, and */
sd2->slback = sd->slback; /* thence to self */
sd->slback = 0; /* Now init killed list */
}
else if(sd2) sd2->slback = 0; /* No prev, clean rest */
(sd2 = sdr)->slforw = 0; /* Finish killed list */
sb->sbcur->sdflags |= SD_LOCK; /* Ensure current SD now locked */
sd->sdflags &= ~SD_LCK2; /* And unlock killed list */
sd2->sdflags &= ~SD_LCK2;
return(sd);
}
/* SB_CPYN(sbp,num) - Copy num characters, returns SD to sbstring.
* Like SB_KILLN but doesn't take chars out of original sbstring.
*/
SBSTR *
sb_cpyn(sbp,num)
SBBUF *sbp;
chroff num;
{ register SBBUF *sb;
register struct sdblk *sd, *sd2;
struct sdblk *sdr;
chroff savloc;
sb = sbp;
if((sd = sbx_xcis(sb,num,&sdr,&savloc)) == 0)
return((SBSTR *)0);
sd2 = sbx_scpy(sd,sdr);
sb_seek(sb,-num,1); /* Return to original loc */
return(sd2); /* Return val is ptr to head of copy.
* It needn't be locked, because GC will
* never move list heads!
*/
}
/* SB_SINS(sb,sd) - Insert sbstring at current location
*
*/
sb_sins(sbp,sdp)
SBBUF *sbp;
struct sdblk *sdp;
{ register SBBUF *sb;
register struct sdblk *sd, *sdx;
chroff inslen;
if((sb = sbp)==0
|| (sd = sdp) == 0)
return(0);
if(sd->slback) /* Perhaps normalize to beg? */
return(0);
if((sdx = (struct sdblk *)sbx_ready(sb,SK_DELB)) == 0) /* Get cur pos ready */
return(0);
inslen = sbs_len(sd); /* Save length of inserted stuff */
sd->slback = sdx; /* Fix up links */
if(sdx->slforw)
{ while(sd->slforw) /* Hunt for end of inserted sbstring */
sd = sd->slforw;
sd->slforw = sdx->slforw;
sd->slforw->slback = sd;
}
sdx->slforw = sdp;
sb->sboff += inslen; /* Set IO ptr to end of new stuff */
return(1);
}
/* SBSTRING routines - operate on "bare" sbstrings. */
/* SBS_CPY(sd) - Copies given sbstring, returns ptr to new sbstring.
*/
SBSTR *
sbs_cpy(sdp)
SBSTR *sdp;
{ return(sbx_scpy(sdp,(struct sdblk *)0));
}
/* SBS_DEL(sd) - Flush a sbstring.
*/
sbs_del(sdp)
SBSTR *sdp;
{ register struct sdblk *sd;
if(sd = sdp)
while(sd = sbx_ndel(sd));
}
/* SBS_APP(sd1,sd2) - Appends sbstring sd2 at end of sbstring sd1.
* Returns sd1 (pointer to new sbstring).
*/
SBSTR *
sbs_app(sdp,sdp2)
struct sdblk *sdp,*sdp2;
{ register struct sdblk *sd, *sdx;
if(sd = sdp)
{ while(sdx = sd->slforw)
sd = sdx;
if(sd->slforw = sdx = sdp2)
sdx->slback = sd;
}
return(sdp);
}
/* SBS_LEN(sd) - Find length of sbstring.
*/
chroff
sbs_len(sdp)
SBSTR *sdp;
{ register struct sdblk *sd;
register struct smblk *sm;
chroff len;
if((sd = sdp)==0) return((chroff)0);
len = 0;
for(; sd ; sd = sd->slforw)
{ if(sm = sd->sdmem)
len += (chroff)sm->smuse;
else len += sd->sdlen;
}
return(len);
}
/* SBBUF I/O pointer ("dot") routines */
/* SB_SEEK(sb,chroff,flag) - Like FSEEK. Changes I/O ptr value as
* indicated by "flag":
* 0 - offset from beg
* 1 - offset from current pos
* 2 - offset from EOF
* Returns -1 on errors.
* Seeking beyond beginning or end of sbbuf will leave pointer
* at the beginning or end respectively.
* Returns 0 unless error (then returns -1).
*/
sb_seek(sbp, coff, flg)
SBBUF *sbp;
chroff coff;
int flg;
{ register SBBUF *sb;
register struct smblk *sm;
register struct sdblk *sd;
SBMO moff;
sb = sbp;
if((sd = sb->sbcur) == 0) return(-1);
if(sb->sbiop == 0)
{ switch(flg)
{ case 0: if(coff == 0) /* Optimize common case */
return(sb_rewind(sb));
sb->sboff = coff - sb->sbdot; /* Abs */
break;
case 1: sb->sboff += coff; /* Rel */
break;
case 2: sb->sboff += sb_ztell(sb) + coff;
break;
default: return(-1);
}
sbx_norm(sb,0);
return(0);
}
if((sm = sd->sdmem) == 0)
return(sbx_err(-1,"SDMEM 0"));
moff = sb->sbiop - sm->smaddr; /* Get cur smblk offset */
if(sb->sbflags&SB_WRIT) /* Update since moving out */
{ sm->smuse = moff;
sb->sbflags &= ~SB_WRIT;
}
sb->sbwleft = 0; /* Always gets zapped */
switch(flg)
{ case 0: /* Offset from beginning */
coff -= sb->sbdot + (chroff)moff; /* Make rel */
case 1: /* Offset from current loc */
break;
case 2: /* Offset from end */
coff += sb_ztell(sb);
break;
default: return(-1);
}
/* COFF now has relative offset from current location */
if (-(chroff)moff <= coff && coff <= sb->sbrleft)
{ /* Win! Handle repos-within-smblk */
sb->sbiop += coff;
sb->sbrleft -= coff; /* Set r; wleft already 0 */
return(0);
}
/* Come here when moving to a different sdblk. */
sb->sbrleft = 0;
sb->sbiop = 0;
sb->sboff = coff + (chroff)moff;
sbx_norm(sb,0);
return(0);
}
/* SB_REWIND(sb) - Go to beginning of sbbuffer.
* Much faster than using sb_seek. Note that this leaves the sbbuffer
* in an open/idle state which is maximally easy to compact.
*/
sb_rewind(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
register struct sdblk *sd;
if((sb = sbp)==0) return;
sbx_smdisc(sb); /* Ensure I/O disconnected */
(sd = sb->sbcur)->sdflags &= ~SD_LOCK; /* Unlock current blk */
sd = sbx_beg(sd); /* Move to beg of sbstring */
/* Need not lock - see sb_open comments, also sb_close */
/* sd->sdflags |= SD_LOCK; */ /* Lock onto this one */
sb->sbcur = sd;
sb->sbdot = 0;
sb->sboff = 0;
}
/* SB_TELL(sb) - Get I/O ptr value for SBBUF.
* Returns -1 on errors.
*/
chroff
sb_tell(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
register struct smblk *sm;
register struct sdblk *sd;
if((sd = (sb=sbp)->sbcur) == 0)
return((chroff)-1);
if(sb->sbiop == 0)
return(sb->sbdot + sb->sboff);
if((sm = sd->sdmem) == 0)
return(sbx_err(0,"SDMEM 0"));
return(sb->sbdot + (unsigned)(sb->sbiop - sm->smaddr));
}
/* SB_ZTELL(sb) - Get I/O ptr relative to "Z" (EOF).
* Returns # chars from current location to EOF; 0 if any errors.
*/
chroff
sb_ztell(sbp)
SBBUF *sbp;
{ register SBBUF *sb;
register struct smblk *sm;
register struct sdblk *sd;
if((sd = (sb=sbp)->sbcur) == 0)
return((chroff)0);
if(sb->sbiop && (sm = sd->sdmem))
{ if(sb->sbflags&SB_WRIT) /* If actively writing, */
return(sbs_len(sd->slforw)); /* ignore this blk. */
/* Note that previous code makes it unnecessary
* to invoke sbx_smdisc. (otherwise wrong
* smuse would confuse sbs_len).
*/
return(sbs_len(sd) - (sb->sbiop - sm->smaddr));
}
else
return(sbs_len(sd) - sb->sboff);
}
/* Code past this point should insofar as possible be INTERNAL. */
/* SBX_READY(sb,type,cmin,cmax) - Set up SBBUF for reading or writing.
*
* If no current smblk:
* reading - set up for reading
* writing - set up for splitting?
* If current smblk:
* reading - if can read, OK. Else position at beg of next sdblk
* writing - if can write, OK. Else position at end of prev sdblk,
* or set up for splitting?
* Types:
* 0 - Read forward (BOB)
* 1 - Read backward (EOB)
* 3 - Write (insert forward) (EOB)
* 4 - Delete forward (return SD, force BOB-aligned)
* 5 - Delete backward (return SD, force EOB-aligned)
* Connected SD is always locked.
* Returns 0 if error, -1 if EOF-type error, 1 for success.
*
* For types 0,1:
* CMIN,CMAX represent max # chars to read in to left and right of
* I/O ptr (prev and post). Actual amount read in may be
* much less, but will never be zero.
* Successful return guarantees that SBIOP etc. are ready.
* For type 3:
* If new block is allocated, CMIN and CMAX represent min, max sizes
* of the block.
* Successful return guarantees that SBIOP etc. are ready, but
* NOTE that SB_WRIT and SD_MOD are not set! If not going to use
* for writing, be sure to clear sbwleft on return!
* For types 4,5:
* CMIN, CMAX are ignored.
* SBIOP is always cleared. SBOFF is guaranteed to be 0 for
* type 4, SMUSE for type 5.
* Return value is a SD ptr; 0 indicates error. -1 isn't used.
*/
struct sdblk *
sbx_ready(sbp,type,cmin,cmax)
SBBUF *sbp;
int type;
SBMO cmin,cmax;
{ register SBBUF *sb;
register struct sdblk *sd;
register struct smblk *sm;
int cnt, slop, rem;
SBMO moff;
if((sd = (sb=sbp)->sbcur) == 0)
return(0);
if(sb->sbiop) /* Canonicalize for given operation */
{ if((sm = sd->sdmem)==0)
return(0);
moff = sb->sbiop - sm->smaddr; /* Current block offset */
switch(type)
{
case SK_READF: /* Read Forward */
if(sb->sbrleft > 0) /* Already set up? */
return(1); /* Yup, fast return */
sbx_smdisc(sb); /* None left, disc to get next */
if((sd = sbx_next(sb)) == 0) /* Try to get next blk */
return(-1); /* At EOF */
break;
case SK_READB: /* Read Backward */
if(moff) /* Stuff there to read? */
{ if(sb->sbflags&SB_WRIT) /* Yup, turn writes off */
{ sm->smuse = moff;
sb->sbflags &= ~SB_WRIT;
}
sb->sbwleft = 0;
return(1);
}
sbx_smdisc(sb);
break;
case SK_WRITEF: /* Writing */
if(sb->sbrleft <= 0)
sb->sbwleft = sm->smlen - moff;
if(sb->sbwleft > 0)
return(1); /* OK to write now */
/* NOTE: flags not set!!! */
sbx_smdisc(sb);
break;
case SK_DELF: /* Delete forward - force BOB */
if(sb->sbrleft <= 0) /* At end of blk? */
{ sbx_smdisc(sb); /* Win, unhook */
return(sbx_next(sb)); /* Return next or 0 if EOF */
}
sbx_smdisc(sb); /* Not at end, but see if */
if(moff == 0) /* at beg of blk? */
return(sd); /* Fast win! */
break;
case SK_DELB: /* Delete backward - force EOB */
if(sb->sbrleft <= 0) /* Win if already EOB */
{ sbx_smdisc(sb);
return(sd);
}
sbx_smdisc(sb);
break;
default:
return(0);
}
}
/* Schnarf in the text, or whatever.
* SD points to current sdblk (must be SD_LOCKed)
* SBDOT must have correct value for this SD
* SBOFF has offset from there to put I/O ptr at.
*
* After normalization, SBOFF is guaranteed to point within
* the SD. Other guarantees apply to boundary cases, depending
* on the mode (type) bits.
*/
sd = sbx_norm(sb,type); /* Normalize I/O pos appropriately */
sm = sd->sdmem;