-
Notifications
You must be signed in to change notification settings - Fork 1
/
tail.s
1473 lines (1290 loc) · 27.9 KB
/
tail.s
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
* tail - output tail of input
*
* Itagaki Fumihiko 07-Feb-93 Create.
* 1.0
* Itagaki Fumihiko 19-Feb-93 標準入力が切り替えられていても端末から^Cや^Sなどが効くようにした
* Itagaki Fumihiko 19-Feb-93 シークできないデバイスからの -<N>l が正常に動作しないバグを修正
* Itagaki Fumihiko 22-Feb-93 +<N> の結果が 1 ずれているバグ(仕様のバグ)を修正
* Itagaki Fumihiko 24-Feb-93 シーク可能かどうかの判定方法を変更
* 1.1
* Itagaki Fumihiko 08-Jan-94 RAW CHAR デバイスを入力すると無条件ループに入るバグを修正
* Itagaki Fumihiko 09-Jan-94 Brush up.
* Itagaki Fumihiko 29-Aug-94 Brush up.
* Itagaki Fumihiko 10-Oct-94 countの形式を拡張.
* 1.2
* Itagaki Fumihiko 10-Oct-94 +count1-count2 や -count1-count2 の count2 が最初のファイルにしか
* 効かないバグを修正
* 1.3
* Itagaki Fumihiko 03-Jun-95 出力がcookedモードのキャラクタ・デバイスでない場合や -Bオプション
* が指定されている場合に, +count1-count2 や -count1-count2 の形式で
* count2 が行単位であってもバイト単位として処理されてしまう不具合を
* 修正.
* 1.4
*
* Usage: tail [ -qvBCZ ] { [ {+-}<#>[ckl][-<#>[ckl]] | -[<#>]r ] [ -- ] [ <ファイル> ] } ...
.include doscall.h
.include chrcode.h
.xref DecodeHUPAIR
.xref issjis
.xref isdigit
.xref atou
.xref strlen
.xref strfor1
.xref strip_excessive_slashes
IMPLEMENT_FOLLOW equ 0
STACKSIZE equ 2048
OUTBUF_SIZE equ 8192
READSIZE equ 8192
DEFAULT_COUNT equ 10
CTRLD equ $04
CTRLZ equ $1A
FLAG_q equ 0 * -q
FLAG_v equ 1 * -v
FLAG_B equ 2 * -B
FLAG_C equ 3 * -C
FLAG_Z equ 4 * -Z
FLAG_from_top equ 5
FLAG_byte_unit equ 6
FLAG_follow equ 7
FLAG_reverse equ 8
FLAG_head equ 9
FLAG_output_byte_unit equ 10
FLAG_add_newline equ 11
FLAG_can_seek equ 12
FLAG_eof equ 13
.text
start:
bra.s start1
dc.b '#HUPAIR',0
start1:
lea bss_top(pc),a6
lea stack_bottom(a6),a7 * A7 := スタックの底
lea $10(a0),a0 * A0 : PDBアドレス
move.l a7,d0
sub.l a0,d0
move.l d0,-(a7)
move.l a0,-(a7)
DOS _SETBLOCK
addq.l #8,a7
*
move.l #-1,stdin(a6)
*
* 引数並び格納エリアを確保する
*
lea 1(a2),a0 * A0 := コマンドラインの文字列の先頭アドレス
bsr strlen * D0.L := コマンドラインの文字列の長さ
addq.l #1,d0
bsr malloc
bmi insufficient_memory
movea.l d0,a1 * A1 := 引数並び格納エリアの先頭アドレス
*
* 引数をデコードし,解釈する
*
bsr DecodeHUPAIR * 引数をデコードする
movea.l a1,a0 * A0 : 引数ポインタ
move.l d0,d7 * D7.L : 引数カウンタ
moveq #0,d5 * D5.B : フラグ
move.l #DEFAULT_COUNT,count(a6)
decode_opt_loop1:
tst.l d7
beq decode_opt_done
cmpi.b #'-',(a0)
bne decode_opt_done
move.b 1(a0),d0
beq decode_opt_done
bsr isdigit
beq decode_opt_done
cmp.b #'r',d0
bne decode_opt_loop1_1
tst.b 2(a0)
beq decode_opt_done
decode_opt_loop1_1:
subq.l #1,d7
addq.l #1,a0
move.b (a0)+,d0
cmp.b #'-',d0
bne decode_opt_loop2
tst.b (a0)+
beq decode_opt_done
subq.l #1,a0
decode_opt_loop2:
cmp.b #'q',d0
beq option_q_found
cmp.b #'v',d0
beq option_v_found
cmp.b #'B',d0
beq option_B_found
cmp.b #'C',d0
beq option_C_found
moveq #FLAG_Z,d1
cmp.b #'Z',d0
beq set_option
moveq #1,d1
tst.b (a0)
beq bad_option_1
bsr issjis
bne bad_option_1
moveq #2,d1
bad_option_1:
move.l d1,-(a7)
pea -1(a0)
move.w #2,-(a7)
lea msg_illegal_option(pc),a0
bsr werror_myname_and_msg
DOS _WRITE
lea 10(a7),a7
bra usage
option_q_found:
bset #FLAG_q,d5
bclr #FLAG_v,d5
bra set_option_done
option_v_found:
bset #FLAG_v,d5
bclr #FLAG_q,d5
bra set_option_done
option_B_found:
bset #FLAG_B,d5
bclr #FLAG_C,d5
bra set_option_done
option_C_found:
bset #FLAG_C,d5
bclr #FLAG_B,d5
bra set_option_done
set_option:
bset d1,d5
set_option_done:
move.b (a0)+,d0
bne decode_opt_loop2
bra decode_opt_loop1
decode_opt_done:
bsr parse_count
*
moveq #1,d0 * 出力は
bsr is_chrdev * キャラクタ・デバイスか?
seq do_buffering(a6)
beq stdout_is_block_device * -- ブロック・デバイスである
*
* 出力はキャラクタ・デバイス
*
btst #5,d0 * '0':cooked '1':raw
bne outbuf_ok
btst #FLAG_B,d5
bne outbuf_ok
bset #FLAG_C,d5
bra outbuf_ok
stdout_is_block_device:
*
* stdoutはブロック・デバイス
*
* 出力バッファを確保する
*
move.l #OUTBUF_SIZE,d0
move.l d0,outbuf_free(a6)
bsr malloc
bmi insufficient_memory
movea.l d0,a4 * A4 : 出力バッファの先頭アドレス
movea.l d0,a5 * A5 : 出力バッファのポインタ
outbuf_ok:
*
* 入力バッファを確保する
*
move.l #$00ffffff,d0
bsr malloc
sub.l #$81000000,d0
cmp.l #READSIZE,d0
blt insufficient_memory
move.l d0,inpbuf_size(a6)
bsr malloc
bmi insufficient_memory
inpbuf_ok:
move.l d0,inpbuf(a6)
*
* 標準入力を切り替える
*
clr.w -(a7) * 標準入力を
DOS _DUP * 複製したハンドルから入力し,
addq.l #2,a7
move.l d0,stdin(a6)
bmi stdin_ok
clr.w -(a7)
DOS _CLOSE * 標準入力はクローズする.
addq.l #2,a7 * こうしないと ^C や ^S が効かない
stdin_ok:
lea msg_header2(pc),a1
st show_header(a6)
btst #FLAG_v,d5
bne do_files
sf show_header(a6)
btst #FLAG_q,d5
bne do_files
cmp.l #1,d7
shi show_header(a6)
do_files:
*
* 開始
*
tst.l d7
beq do_stdin
for_file_loop:
subq.l #1,d7
movea.l a0,a3
bsr strfor1
exg a0,a3
cmpi.b #'-',(a0)
bne do_file
tst.b 1(a0)
bne do_file
do_stdin:
lea msg_stdin(pc),a0
move.l stdin(a6),d1
bmi open_fail
bsr tail_one
bra for_file_continue
do_file:
bsr strip_excessive_slashes
clr.w -(a7)
move.l a0,-(a7)
DOS _OPEN
addq.l #6,a7
move.l d0,d1
bmi open_fail
bsr tail_one
move.w d1,-(a7)
DOS _CLOSE
addq.l #2,a7
for_file_continue:
movea.l a3,a0
bsr parse_count
tst.l d7
beq all_done
lea msg_header1(pc),a1
bra for_file_loop
all_done:
moveq #0,d1
exit_program:
move.l stdin(a6),d0
bmi exit_program_1
clr.w -(a7) * 標準入力を
move.w d0,-(a7) * 元に
DOS _DUP2 * 戻す.
DOS _CLOSE * 複製はクローズする.
exit_program_1:
move.w d1,-(a7)
DOS _EXIT2
open_fail:
lea msg_open_fail(pc),a2
bra werror_exit_2
****************************************************************
parse_count:
parse_count_loop:
tst.l d7
beq parse_count_done
move.b 1(a0),d0
cmpi.b #'+',(a0)
beq parse_count_0
cmpi.b #'-',(a0)
bne parse_count_done
cmp.b #'r',d0
bne parse_count_0
tst.b 2(a0)
bne parse_count_break
moveq #-1,d1
sf d0 * skip atou
bra parse_count_2
parse_count_0:
.if IMPLEMENT_FOLLOW
cmp.b #'f',d0
bne parse_count_1
tst.b 2(a0)
bne parse_count_break
moveq #10,d1
sf d0 * skip atou
bra parse_count_2
.endif
parse_count_1:
bsr isdigit
bne parse_count_break
st d0 * do atou
parse_count_2:
bclr #FLAG_from_top,d5
cmpi.b #'-',(a0)
beq parse_count_n
bset #FLAG_from_top,d5
parse_count_n:
addq.l #1,a0
tst.b d0
beq parse_count_unit
bsr atou
bne bad_count
parse_count_unit:
move.l d1,count(a6)
subq.l #1,d7
bclr #FLAG_byte_unit,d5
bclr #FLAG_follow,d5
bclr #FLAG_reverse,d5
bclr #FLAG_head,d5
bclr #FLAG_output_byte_unit,d5
move.b (a0),d0
beq parse_count_7
cmp.b #'l',d0
beq parse_count_3
cmp.b #'c',d0
beq parse_count_c
cmp.b #'k',d0
beq parse_count_k
btst #FLAG_from_top,d5
bne parse_count_4
cmp.b #'r',d0
bne parse_count_4
bset #FLAG_reverse,d5
bra parse_count_5
parse_count_k:
cmp.l #$400000,d1
bhs bad_count
lsl.l #8,d1
lsl.l #2,d1
move.l d1,count(a6)
parse_count_c:
bset #FLAG_byte_unit,d5
bset #FLAG_output_byte_unit,d5
parse_count_3:
addq.l #1,a0
move.b (a0),d0
beq parse_count_7
parse_count_4:
.if IMPLEMENT_FOLLOW
cmp.b #'f',d0
bne parse_count_not_follow
bset #FLAG_follow,d5
bra parse_count_5
parse_count_not_follow:
.endif
cmp.b #'-',d0
bne parse_count_7
addq.l #1,a0
bsr atou
bne bad_count
move.l d1,head_count(a6)
bset #FLAG_head,d5
bclr #FLAG_output_byte_unit,d5
move.b (a0),d0
beq parse_count_7
cmp.b #'l',d0
beq parse_count_5
cmp.b #'c',d0
beq parse_count_head_c
cmp.b #'k',d0
bne parse_count_7
cmp.l #$400000,d1
bhs bad_count
lsl.l #8,d1
lsl.l #2,d1
move.l d1,head_count(a6)
parse_count_head_c:
bset #FLAG_output_byte_unit,d5
parse_count_5:
addq.l #1,a0
parse_count_7:
tst.b (a0)+
beq parse_count_loop
bad_count:
lea msg_illegal_count(pc),a0
bsr werror_myname_and_msg
usage:
lea msg_usage(pc),a0
bsr werror
moveq #1,d1
bra exit_program
parse_count_break:
cmpi.b #'-',(a0)
bne parse_count_done
cmpi.b #'-',1(a0)
bne parse_count_done
tst.b 2(a0)
bne parse_count_done
addq.l #3,a0
subq.l #1,d7
parse_count_done:
rts
****************************************************************
* tail_one
****************************************************************
tail_one:
sf cr_pending(a6)
tst.b show_header(a6)
beq tail_one_1
move.l a0,-(a7)
movea.l a1,a0
bsr puts
movea.l (a7),a0
bsr puts
lea msg_header3(pc),a0
bsr puts
movea.l (a7)+,a0
tail_one_1:
bsr check_input_device
move.l d7,-(a7)
move.l head_count(a6),d7 * D7.L := head count
bsr do_tail_one
move.l (a7)+,d7
bsr flush_cr
flush_outbuf:
move.l d0,-(a7)
tst.b do_buffering(a6)
beq flush_done
move.l #OUTBUF_SIZE,d0
sub.l outbuf_free(a6),d0
exg a1,a4
bsr write
exg a1,a4
movea.l a4,a5
move.l #OUTBUF_SIZE,d0
move.l d0,outbuf_free(a6)
flush_done:
move.l (a7)+,d0
rts
flush_cr:
tst.b cr_pending(a6)
beq flush_cr_return
move.l d0,-(a7)
moveq #CR,d0
bsr putc
move.l (a7)+,d0
flush_cr_return:
rts
****************************************************************
do_tail_one:
bclr #FLAG_eof,d5
btst #FLAG_from_top,d5
beq tail_RelBot
****************
tail_RelTop:
move.l count(a6),d2
subq.l #1,d2
bls output_remainder_0
*
* count-1unit読みとばす
*
btst #FLAG_can_seek,d5
beq skip_head
tst.b ignore_from_ctrlz(a6)
bne skip_head
tst.b ignore_from_ctrld(a6)
bne skip_head
btst #FLAG_byte_unit,d5
beq skip_head
move.l d2,d0
bsr seek_from_top
cmp.l d2,d0
beq output_remainder_0
bsr seek_to_phigical_eof
bmi seek_fail
cmp.l d2,d0
bhs seek_fail
tail_done_0:
rts
skip_head:
bsr read_some * D4.L := 有効バイト数
beq tail_done_0
btst #FLAG_byte_unit,d5
bne skip_head_byte
skip_head_line_loop:
move.b (a1)+,d0
subq.l #1,d4
cmp.b #LF,d0
bne skip_head_line_continue
subq.l #1,d2
beq output_remainder
skip_head_line_continue:
tst.l d4
bne skip_head_line_loop
skip_head_continue:
btst #FLAG_eof,d5
beq skip_head
tail_done_1:
rts
skip_head_byte:
sub.l d4,d2
bhi skip_head_continue
neg.l d2
adda.l d4,a1
move.l d2,d4
suba.l d4,a1
bra output_remainder
****************
tail_RelBot:
btst #FLAG_can_seek,d5
beq tail_RelBot_Unseekable
****************
tail_RelBot_Seekable:
tst.l count(a6)
beq tail_done_1
*
* 論理的なファイルの終わりにシークする
*
tst.b ignore_from_ctrlz(a6)
bne seek_to_logical_eof
tst.b ignore_from_ctrld(a6)
bne seek_to_logical_eof
bsr seek_to_phigical_eof
bra seek_to_logical_eof_done
seek_to_logical_eof:
bsr read_some
move.l d0,d2 * D2.L := 読み込んだバイト数
beq seek_to_logical_eof_2 * D0.L == 0
btst #FLAG_eof,d5
beq seek_to_logical_eof
* EOF以降読み進んでしまった分戻る
move.l d4,d0
sub.l d2,d0
seek_to_logical_eof_2:
bsr seek_relative
seek_to_logical_eof_done:
bmi seek_fail
bclr #FLAG_eof,d5
* ここで,D0.L : 現在の位置
btst #FLAG_byte_unit,d5
bne tail_RelBot_Seekable_Byte
****************
tail_RelBot_Seekable_Line:
move.l d0,d2 * D2.L : 現在の位置
moveq #0,d3 * D3.L : 先走り量
move.l count(a6),d4 * D4.L : count
bsr tail_file_lines_read
beq tail_done_2
btst #FLAG_reverse,d5
bne tail_RelBot_Seekable_ReverseLine
cmpi.b #LF,-1(a1)
beq tail_RelBot_Seekable_Line_1
subq.l #1,d4 * 改行で終了していない最後の半端な行をカウントする
tail_RelBot_Seekable_Line_1:
move.l d3,d0
bsr backward_lines
bne tail_done_2
tail_RelBot_Seekable_Line_loop:
bsr tail_file_lines_read
beq output_remainder_0
move.l d3,d0
bsr backward_lines
bmi tail_done_2
bne output_remainder_0
bra tail_RelBot_Seekable_Line_loop
****************
tail_RelBot_Seekable_Byte:
cmp.l count(a6),d0
bls tail_RelBot_Seekable_Byte_1
move.l count(a6),d0
tail_RelBot_Seekable_Byte_1:
neg.l d0
bsr seek_relative
bmi seek_fail
output_remainder_0:
bsr read_some * D4.L := 有効バイト数
beq tail_done_2
output_remainder:
bsr output_buf
bmi tail_done_2
btst #FLAG_eof,d5
beq output_remainder_0
tail_done_2:
rts
****************
tail_RelBot_Seekable_ReverseLine:
move.l d3,d0
bset #FLAG_add_newline,d5
cmpi.b #LF,-1(a1)
bne Seekable_ReverseLine_1
Seekable_ReverseLine_continue:
bclr #FLAG_add_newline,d5
Seekable_ReverseLine_1:
bsr put_backward_lines
beq tail_done_2
Seekable_ReverseLine_ReadLoop:
bsr tail_file_lines_read
move.l d3,d0
beq Seekable_ReverseLine_last
bsr backward_a_line
bne Seekable_ReverseLine_PutOne
add.l d3,d6
bra Seekable_ReverseLine_ReadLoop
Seekable_ReverseLine_last:
moveq #1,d4
Seekable_ReverseLine_PutOne:
add.l d0,d2
sub.l d0,d3
move.l d4,-(a7)
move.l d3,d4
bsr output_buf
Seekable_ReverseLine_PutLoop:
move.l d6,d0
beq Seekable_ReverseLine_PutDone
cmp.l inpbuf_size(a6),d0
bls Seekable_ReverseLine_RW
move.l inpbuf_size(a6),d0
Seekable_ReverseLine_RW:
bsr read
add.l d0,d3
sub.l d0,d6
move.l d0,d4
bsr output_buf
bra Seekable_ReverseLine_PutLoop
Seekable_ReverseLine_PutDone:
bsr check_newline
move.l (a7)+,d4
subq.l #1,d4
beq tail_done_3
bsr tail_file_lines_read
move.l d3,d0
bne Seekable_ReverseLine_continue
tail_done_3:
rts
****************
tail_RelBot_Unseekable:
* バッファに入るだけデータを読む.
* バッファが溢れたら,古いデータを 1バイトずつ捨てる.
movea.l inpbuf(a6),a1
move.l inpbuf_size(a6),d0
lea (a1,d0.l),a2
moveq #0,d2 * D2 <- バッファの有効バイト数
read_to_buffer_loop:
move.l #1,-(a7)
pea charbuf(a6)
move.w d1,-(a7)
DOS _READ
lea 10(a7),a7
tst.l d0
bmi read_fail
beq read_to_buffer_eof
move.b charbuf(a6),d0
tst.b ignore_from_ctrlz(a6)
beq read_to_buffer_ctrlz_ok
cmp.b #CTRLZ,d0
beq read_to_buffer_eof
read_to_buffer_ctrlz_ok:
tst.b ignore_from_ctrld(a6)
beq read_to_buffer_ctrld_ok
cmp.b #CTRLD,d0
beq read_to_buffer_eof
read_to_buffer_ctrld_ok:
move.b d0,(a1)+
addq.l #1,d2
cmpa.l a2,a1
bne read_to_buffer_loop
movea.l inpbuf(a6),a1
bra read_to_buffer_loop
read_to_buffer_eof:
move.l d2,d0
move.l inpbuf_size(a6),d2
cmp.l d0,d2
slo d3
blo read_to_buffer_done
move.l d0,d2
movea.l inpbuf(a6),a1
read_to_buffer_done:
tst.l count(a6)
beq tail_done_3
btst #FLAG_byte_unit,d5
bne tail_RelBot_Unseekable_Byte
****************
tail_RelBot_Unseekable_Line:
tst.l d2
beq tail_done_3
movea.l a1,a2
move.l count(a6),d4 * D4.L : count
lea -1(a1,d2.l),a1
move.l a1,d0
sub.l inpbuf(a6),d0
cmp.l inpbuf_size(a6),d0
blo tail_RelBot_Unseekable_Line_1
suba.l inpbuf_size(a6),a1
tail_RelBot_Unseekable_Line_1:
btst #FLAG_reverse,d5
bne tail_RelBot_Unseekable_ReverseLine
cmpi.b #LF,(a1)
beq tail_RelBot_Unseekable_Line_2
subq.l #1,d4 * 改行で終了していない最後の半端な行をカウントする
tail_RelBot_Unseekable_Line_2:
addq.l #1,a1
move.l a1,d0
sub.l inpbuf(a6),d0
bsr backward_lines
bne tail_done_4
tst.b d3
beq tail_RelBot_Unseekable_Line_3
movem.l d0/a1,-(a7)
move.l inpbuf(a6),a1
adda.l d2,a1
sub.l d2,d0
neg.l d0
bsr backward_lines
movem.l (a7)+,d0/a1
beq insufficient_memory
bmi tail_done_4
tail_RelBot_Unseekable_Line_3:
suba.l d0,a1
move.l d0,d4
bra output_buf
****************
tail_RelBot_Unseekable_ReverseLine:
bclr #FLAG_add_newline,d5
cmpi.b #LF,(a1)
beq Unseekable_RevLine_1
bset #FLAG_add_newline,d5
Unseekable_RevLine_1:
addq.l #1,a1
move.l a1,d0
sub.l inpbuf(a6),d0
move.l d2,-(a7)
moveq #-1,d2
bsr put_backward_lines
move.l (a7)+,d2
tst.l d4
beq tail_done_4
tst.b d3
beq Unseekable_RevLine_last
move.l a1,-(a7)
move.l inpbuf(a6),a1
adda.l d2,a1
movea.l a1,a2
sub.l d2,d0
neg.l d0
bsr backward_a_line
beq insufficient_memory
movem.l d0/d4/a1,-(a7)
move.l a2,d4
sub.l a1,d4
bsr output_buf
movem.l (a7)+,d0/d4/a2 * A1->A2
movea.l (a7)+,a1
movem.l d0/d4/a2,-(a7)
move.l d6,d4
bsr output_buf
movem.l (a7)+,d0/d4/a1 * A2->A1
bsr check_newline
subq.l #1,d4
beq tail_done_4
moveq #-1,d2
bsr put_backward_lines
bne insufficient_memory
tail_done_4:
rts
Unseekable_RevLine_last:
move.l d6,d4
bra output_buf
****************
tail_RelBot_Unseekable_Byte:
cmp.l count(a6),d2
bhs tail_RelBot_Unseekable_Byte_1
tst.b d3
bne insufficient_memory
move.l d2,d4
bra tail_RelBot_Unseekable_Byte_2
tail_RelBot_Unseekable_Byte_1:
adda.l d2,a1
move.l count(a6),d4
suba.l d4,a1
tail_RelBot_Unseekable_Byte_2:
* A1 : 出力データの先頭アドレス
* D4.L : 出力データのバイト数
move.l inpbuf(a6),d2
add.l inpbuf_size(a6),d2
sub.l a1,d2
cmp.l d2,d4
bls tail_RelBot_Unseekable_Byte_3
movem.l d4,-(a7)
move.l d2,d4
bsr output_buf
movem.l (a7)+,d4
bmi tail_done_4
movea.l inpbuf(a6),a1
sub.l d2,d4
tail_RelBot_Unseekable_Byte_3:
*bra output_buf
****************************************************************
output_buf:
btst #FLAG_output_byte_unit,d5
bne output_buf_immediately
btst #FLAG_head,d5
beq output_buf_putc_loop
tst.l d7
output_buf_putc_loop2:
beq output_buf_return_1
output_buf_putc_loop:
subq.l #1,d4
bcs output_buf_return_0
move.b (a1)+,d0
btst #FLAG_C,d5
beq output_buf_putc_2
cmp.b #LF,d0
bne output_buf_putc
st cr_pending(a6) * LFの前にCRをはかせるため
output_buf_putc:
bsr flush_cr
cmp.b #CR,d0
seq cr_pending(a6)
beq output_buf_putc_loop
output_buf_putc_2:
bsr putc
btst #FLAG_head,d5
beq output_buf_putc_loop
cmp.b #LF,d0
bne output_buf_putc_loop
subq.l #1,d7
bra output_buf_putc_loop2
output_buf_immediately:
bsr flush_outbuf
btst #FLAG_head,d5
beq output_buf_immediately_1
sub.l d4,d7
bhi output_buf_immediately_1
move.l d7,d0
add.l d4,d0
bsr write
output_buf_return_1:
moveq #-1,d0
rts
output_buf_immediately_1:
move.l d4,d0
bsr write
output_buf_return_0:
moveq #0,d0
rts
*****************************************************************