-
Notifications
You must be signed in to change notification settings - Fork 1
/
cut.s
1187 lines (1004 loc) · 20.8 KB
/
cut.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
* cut - cut out fields from line
*
* Itagaki Fumihiko 29-Sep-93 Create.
* Itagaki Fumihiko 26-Dec-93 Brush Up.
* Itagaki Fumihiko 02-Jan-94 入力行の長さやフィールド数の制限を無くした
* 1.0
* Itagaki Fumihiko 01-Jun-95 -f で、2バイト文字以降の 1バイト文字のデリミタが認識されない
* 不具合を修正
* 1.1
*
* Usage: cut -b <リスト> [ -nBCZ ] [ -- ] [ <ファイル> ] ...
* cut -c <リスト> [ -BCZ ] [--] [ <ファイル> ] ...
* cut -f <リスト> [ -d <デリミタ> ] [ -sBCZ ] [ -- ] [ <ファイル> ] ...
.include doscall.h
.include chrcode.h
.xref DecodeHUPAIR
.xref issjis
.xref atou
.xref strlen
.xref strcmp
.xref strfor1
.xref memmovi
.xref memmovd
.xref strip_excessive_slashes
STACKSIZE equ 2048
READ_MAX_TO_OUTPUT_TO_COOKED equ 8192
INPBUFSIZE_MIN equ 258
OUTBUF_SIZE equ 8192
CTRLD equ $04
CTRLZ equ $1A
FLAG_n equ 0 * -n
FLAG_s equ 1 * -s
FLAG_B equ 2 * -B
FLAG_C equ 3 * -C
FLAG_Z equ 4 * -Z
FLAG_eof equ 5
UNIT_b equ 0 * byte
UNIT_c equ 1 * character
UNIT_f equ 2 * field
.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 := 引数並び格納エリアの先頭アドレス
*
* 引数をデコードし,解釈する
*
moveq #0,d6 * D6.W : エラー・コード
bsr DecodeHUPAIR * 引数をデコードする
movea.l a1,a0 * A0 : 引数ポインタ
move.l d0,d7 * D7.L : 引数カウンタ
subq.l #1,d0
bne decode_opt_start
lea word_let(pc),a1
lea msg_let(pc),a2
bsr strcmp
beq joke
lea word_purse(pc),a1
lea msg_purse(pc),a2
bsr strcmp
beq joke
lea word_throat(pc),a1
lea msg_throat(pc),a2
bsr strcmp
beq joke
lea word_up(pc),a1
lea msg_up(pc),a2
bsr strcmp
beq joke
lea word_worm(pc),a1
lea msg_worm(pc),a2
bsr strcmp
beq joke
decode_opt_start:
* とりあえず field list に最大メモリを割り当てておく
move.l #$00ffffff,d0
bsr malloc
sub.l #$81000000,d0
move.l d0,d3 * D3.L : field list の容量
cmp.l #4,d3
blo insufficient_memory
bsr malloc
bmi insufficient_memory
move.l d0,list_top(a6)
movea.l d0,a1
clr.l (a1)
moveq #4,d4 * D4.L : field list 使用バイト数
moveq #0,d5 * D5.L : フラグ
move.b #-1,unit(a6)
move.w #HT,delimiter(a6)
decode_opt_loop1:
tst.l d7
beq decode_opt_done
cmpi.b #'-',(a0)
bne decode_opt_done
tst.b 1(a0)
beq decode_opt_done
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:
moveq #UNIT_b,d1
cmp.b #'b',d0
beq parse_list
moveq #UNIT_c,d1
cmp.b #'c',d0
beq parse_list
moveq #UNIT_f,d1
cmp.b #'f',d0
beq parse_list
cmp.b #'d',d0
beq parse_delimiter
moveq #FLAG_s,d1
cmp.b #'s',d0
beq set_option
moveq #FLAG_n,d1
cmp.b #'n',d0
beq set_option
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
usage:
lea msg_usage(pc),a0
bsr werror
moveq #1,d6
bra exit_program
parse_list:
tst.b unit(a6)
bpl needs_one_of_bcf
move.b d1,unit(a6)
tst.b (a0)
bne parse_list_loop
subq.l #1,d7
bcs needs_list
addq.l #1,a0
parse_list_loop:
move.b (a0)+,d0
beq decode_opt_loop1
cmp.b #',',d0
beq parse_list_loop
subq.l #1,a0
moveq #1,d1
bsr list_atou
bmi bad_list
bne parse_list_1
move.l d0,d1
cmpi.b #'-',(a0)
bne parse_list_2 * D0 == D1
parse_list_1:
* ここで
* D1.L : from
cmpi.b #'-',(a0)+
bne bad_list
bsr list_atou
bmi bad_list
beq parse_list_2
moveq #-1,d0 * MAX
parse_list_2:
exg d0,d1
* ここで
* D0.L : from
* D1.L : to
cmp.l d0,d1
blo bad_list
movea.l list_top(a6),a1
parse_list_find_ins_point:
tst.l (a1)
beq ins_list_bottom
cmp.l 4(a1),d0
bls ins_list_x
addq.l #8,a1
bra parse_list_find_ins_point
ins_list_bottom:
* D0-D1 をstoreする
addq.l #8,d4
cmp.l d4,d3
blo insufficient_memory
move.l d0,(a1)+
move.l d1,(a1)+
clr.l (a1)
bra parse_list_loop
ins_list_x:
* from <= 4(A1)
cmp.l (a1),d1
blo ins_list_less_than
* 0(A1) <= to
cmp.l (a1),d0
bhs ins_list_x_from_ok
* from < 0(A1)
move.l d0,(a1)
ins_list_x_from_ok:
* 0(A1) <= from <= 4(A1)
cmp.l 4(a1),d1
bls parse_list_loop
* 0(A1) <= from <= 4(A1) < to
lea 8(a1),a2
ins_list_x_forward:
tst.l (a2)
beq ins_list_x_forward_shrink
cmp.l (a2),d1
blo ins_list_x_forward_shrink
cmp.l 4(a2),d1
bls ins_list_x_forward_merge
addq.l #8,a2
bra ins_list_x_forward
ins_list_x_forward_merge:
move.l 4(a2),d1
addq.l #8,a2
ins_list_x_forward_shrink:
move.l d1,4(a1)
addq.l #8,a1
cmpa.l a1,a2
beq ins_list_shrink_done
ins_list_x_forward_shrink_loop:
move.l (a2)+,(a1)+
bne ins_list_x_forward_shrink_loop
ins_list_shrink_done:
move.l a1,d4
sub.l list_top(a6),d4
bra parse_list_loop
ins_list_less_than:
* to < 0(A1)
addq.l #8,d4
cmp.l d4,d3
blo insufficient_memory
movem.l d0/a0,-(a7)
movea.l list_top(a6),a0
adda.l d4,a0
move.l a0,d0
subq.l #8,d0
sub.l a1,d0
lea -8(a0),a1
bsr memmovd
movem.l (a7)+,d0/a0
move.l d0,(a1)+
move.l d1,(a1)
bra parse_list_loop
bad_list:
lea msg_bad_list(pc),a0
werror_usage:
bsr werror_myname_and_msg
bra usage
needs_one_of_bcf:
lea msg_needs_one_of_bcf(pc),a0
bra werror_usage
needs_list:
lea msg_needs_list(pc),a0
bra werror_usage
needs_delimiter:
lea msg_needs_delimiter(pc),a0
bra werror_usage
bad_delimiter:
lea msg_bad_delimiter(pc),a0
bra werror_usage
parse_delimiter:
tst.b (a0)
bne parse_delimiter_0
subq.l #1,d7
bcs needs_delimiter
addq.l #1,a0
parse_delimiter_0:
moveq #0,d0
move.b (a0)+,d0
bsr issjis
bne parse_delimiter_1
lsl.w #8,d0
move.b (a0)+,d0
beq bad_delimiter
parse_delimiter_1:
move.w d0,delimiter(a6)
tst.b (a0)+
bne bad_delimiter
bra decode_opt_loop1
option_B_found:
bclr #FLAG_C,d5
bset #FLAG_B,d5
bra set_option_done
option_C_found:
bclr #FLAG_B,d5
bset #FLAG_C,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:
tst.b unit(a6)
bmi needs_one_of_bcf
* field list を切り詰める
move.l d4,-(a7)
move.l list_top(a6),-(a7)
DOS _SETBLOCK
addq.l #8,a7
moveq #1,d0 * 出力は
bsr is_chrdev * キャラクタ・デバイスか?
seq do_buffering(a6)
beq input_max * -- block device
* character device
btst #5,d0 * '0':cooked '1':raw
bne input_max
* cooked character device
move.l #READ_MAX_TO_OUTPUT_TO_COOKED,d0
btst #FLAG_B,d5
bne inpbufsize_ok
bset #FLAG_C,d5 * 改行を変換する
bra inpbufsize_ok
input_max:
move.l #$00ffffff,d0
inpbufsize_ok:
move.l d0,read_size(a6)
* 出力バッファを確保する
tst.b do_buffering(a6)
beq outbuf_ok
move.l #OUTBUF_SIZE,d0
move.l d0,outbuf_free(a6)
bsr malloc
bmi insufficient_memory
move.l d0,outbuf_top(a6)
move.l d0,outbuf_ptr(a6)
outbuf_ok:
* 入力バッファを確保する
move.l #$00ffffff,d0
bsr malloc
sub.l #$81000000,d0
cmp.l #INPBUFSIZE_MIN,d0
blo insufficient_memory
move.l d0,inpbuf_size(a6)
bsr malloc
bmi insufficient_memory
inpbuf_ok:
move.l d0,inpbuf_top(a6)
*
* 標準入力を切り替える
*
clr.w -(a7) * 標準入力を
DOS _DUP * 複製したハンドルから入力し,
addq.l #2,a7
move.l d0,stdin(a6)
bmi start_do_files
clr.w -(a7)
DOS _CLOSE * 標準入力はクローズする.
addq.l #2,a7 * こうしないと ^C や ^S が効かない
start_do_files:
*
* 開始
*
tst.l d7
beq do_stdin
for_file_loop:
subq.l #1,d7
movea.l a0,a1
bsr strfor1
exg a0,a1
cmpi.b #'-',(a0)
bne do_file
tst.b 1(a0)
bne do_file
do_stdin:
lea msg_stdin(pc),a0
move.l stdin(a6),d0
bmi open_file_failure
bsr cut_one
bra for_file_continue
do_file:
bsr strip_excessive_slashes
clr.w -(a7)
move.l a0,-(a7)
DOS _OPEN
addq.l #6,a7
tst.l d0
bmi open_file_failure
bsr cut_one
move.w input_handle(a6),-(a7)
DOS _CLOSE
addq.l #2,a7
for_file_continue:
movea.l a1,a0
tst.l d7
bne for_file_loop
bsr flush_outbuf
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 d6,-(a7)
DOS _EXIT2
open_file_failure:
bsr werror_myname_and_msg
lea msg_open_fail(pc),a0
bsr werror
moveq #2,d6
bra for_file_continue
joke:
move.l a2,-(a7)
DOS _PRINT
addq.l #4,a7
bra exit_program_1
****************************************************************
* cut_one
****************************************************************
cut_one:
move.w d0,input_handle(a6)
btst #FLAG_Z,d5
sne terminate_by_ctrlz(a6)
sf terminate_by_ctrld(a6)
bsr is_chrdev
beq cut_one_start * -- ブロック・デバイス
btst #5,d0 * '0':cooked '1':raw
bne cut_one_start
st terminate_by_ctrlz(a6)
st terminate_by_ctrld(a6)
cut_one_start:
bclr #FLAG_eof,d5
move.l inpbuf_top(a6),inpbuf_ptr(a6)
clr.l inpbuf_remain(a6)
cut_one_loop:
movea.l list_top(a6),a3
moveq #0,d3 * D3.L : fieldno counter
move.b unit(a6),d0
beq cut_byte
subq.b #1,d0
beq cut_char
****************
cut_field:
clr.l saved_count(a6)
bsr getc
bmi cut_one_done
cut_field_getline_loop:
cmp.b #LF,d0
beq cut_field_getline_ok
bsr getc
bpl cut_field_getline_loop
cut_field_getline_ok:
movea.l inpbuf_ptr(a6),a2
move.l saved_count(a6),d4
suba.l d4,a2
move.l a2,line_top(a6)
sf d1
cut_field_loop1:
movea.l a2,a4
cut_field_loop2:
movea.l a2,a5
subq.l #1,d4
bcs cut_field_last * D4.L == -1
moveq #0,d0
move.b (a2)+,d0
cmp.b #LF,d0
beq cut_field_lf
cmp.b #CR,d0
beq cut_field_cr
bsr issjis
bne cut_field_check_delimiter
tst.l d4
beq cut_field_check_delimiter
movem.l d0,-(a7)
move.b (a2),d0
bsr issjis2
movem.l (a7)+,d0
bne cut_field_check_delimiter
subq.l #1,d4
lsl.w #8,d0
move.b (a2)+,d0
cut_field_check_delimiter:
cmp.w delimiter(a6),d0
bne cut_field_loop2
addq.l #1,d3
bsr check_list
beq cut_field_skip_field
bsr cut_field_output_field_sub
st d1
bra cut_field_loop2
cut_field_skip_field:
tst.b d1
beq cut_field_loop1
movea.l a5,a4
bra cut_field_loop2
cut_field_cr:
tst.l d4
beq cut_field_loop2
cmpi.b #LF,(a2)
bne cut_field_loop2
moveq #1,d4 * D4 := 1 : CRLF
bra cut_field_last
cut_field_lf:
moveq #0,d4 * D4 := 0 : LF
cut_field_last:
tst.l d3
beq cut_field_no_delimiter
addq.l #1,d3
bcs cut_field_output_newline
bsr check_list
beq cut_field_output_newline
bra cut_field_output_nl_field
cut_field_no_delimiter:
btst #FLAG_s,d5
bne cut_one_loop
movea.l line_top(a6),a4
cut_field_output_nl_field:
bsr cut_field_output_field_sub
cut_field_output_newline:
tst.l d4
bmi cut_one_loop
bne cut_one_crlf
bra cut_one_newline
cut_field_output_field_sub:
cmpa.l a5,a4
beq cut_field_output_field_sub_done
move.b (a4)+,d0
bsr putc
bra cut_field_output_field_sub
cut_field_output_field_sub_done:
cut_one_done:
rts
cut_field_too_long_line:
bsr werror_myname_and_msg
lea msg_too_long_line,a0
bsr werror
moveq #2,d6
bra exit_program
****************
cut_byte:
cut_byte_loop:
bsr getc
cut_byte_loop_1:
bmi cut_one_done
sf d2
cmp.b #LF,d0
beq cut_one_newline
cmp.b #CR,d0
bne cut_byte_not_cr
bsr getc_next
bmi cut_byte_not_cr
cmp.b #LF,d1
beq cut_one_crlf
cut_byte_not_cr:
tst.l (a3)
beq cut_byte_continue
addq.l #1,d3
tst.b d2
bne cut_byte_not_sjis
btst #FLAG_n,d5
beq cut_byte_not_sjis
bsr issjis
bne cut_byte_not_sjis
bsr getc_next
bmi cut_byte_not_sjis
exg d0,d1
bsr issjis2
exg d0,d1
bne cut_byte_not_sjis
sf d2
bsr check_list
beq cut_byte_broken_sjis_1
addq.l #1,d3
bsr check_list
beq cut_byte_broken_sjis_2
bsr putc
move.l d1,d0
bra cut_byte_putc
cut_byte_broken_sjis_1:
addq.l #1,d3
bsr check_list
beq cut_byte_continue
cut_byte_broken_sjis_2:
moveq #' ',d0
bra cut_byte_putc
cut_byte_not_sjis:
bsr check_list
cut_byte_test:
beq cut_byte_continue
cut_byte_putc:
bsr putc
cut_byte_continue:
tst.b d2
beq cut_byte_loop
move.l d1,d0
bra cut_byte_loop_1
****************
cut_char:
cut_char_loop:
bsr getc
cut_char_loop_1:
bmi cut_one_done
sf d2
cmp.b #LF,d0
beq cut_one_newline
cmp.b #CR,d0
bne cut_char_not_cr
bsr getc_next
bmi cut_char_not_cr
cmp.b #LF,d1
beq cut_one_crlf
cut_char_not_cr:
tst.l (a3)
beq cut_char_continue
tst.b d2
bne cut_char_test
moveq #0,d1
bsr issjis
bne cut_char_test
bsr getc_next
bmi cut_char_test
exg d0,d1
bsr issjis2
exg d0,d1
bne cut_char_test
sf d2
cut_char_test:
addq.l #1,d3
bsr check_list
beq cut_char_continue
cut_char_putc:
bsr putc
tst.b d2
bne cut_char_continue_1
move.l d1,d0
beq cut_char_continue
bsr putc
cut_char_continue:
tst.b d2
beq cut_char_loop
cut_char_continue_1:
move.l d1,d0
bra cut_char_loop_1
****************
cut_one_newline:
bsr output_newline
bra cut_one_loop
cut_one_crlf:
bsr output_crlf
bra cut_one_loop
*****************************************************************
output_newline:
btst #FLAG_C,d5
beq output_lf
output_crlf:
moveq #CR,d0
bsr putc
output_lf:
moveq #LF,d0
bra putc
*****************************************************************
check_list:
tst.l (a3)
beq check_list_return * ZF == 1
cmp.l (a3),d3
blo check_list_out
cmp.l 4(a3),d3
bls check_list_in
addq.l #8,a3
bra check_list
check_list_in:
tst.l d3 * ZF := 0
rts
check_list_out:
cmp.w d0,d0 * ZF := 1
check_list_return:
rts
*****************************************************************
getc_next:
st d2
move.l d0,d1
bsr getc
exg d0,d1
rts
*****************************************************************
getc:
movem.l d3/a3,-(a7)
movea.l inpbuf_ptr(a6),a3
move.l inpbuf_remain(a6),d3
bne getc_get1
btst #FLAG_eof,d5
bne getc_eof
move.l inpbuf_top(a6),d0
add.l inpbuf_size(a6),d0
sub.l a3,d0
bne getc_read
cmpi.b #UNIT_f,unit(a6)
bne getc_new
movem.l a0-a1,-(a7)
move.l saved_count(a6),d0
movea.l a3,a1
suba.l d0,a1
movea.l inpbuf_top(a6),a0
bsr memmovi
movea.l a0,a3
movem.l (a7)+,a0-a1
move.l inpbuf_top(a6),d0
add.l inpbuf_size(a6),d0
sub.l a3,d0
beq insufficient_memory
bra getc_read
getc_new:
movea.l inpbuf_top(a6),a3
move.l inpbuf_size(a6),d0
getc_read:
cmp.l read_size(a6),d0
bls getc_read_1
move.l read_size(a6),d0
getc_read_1:
move.l d0,-(a7)
move.l a3,-(a7)
move.w input_handle(a6),-(a7)
DOS _READ
lea 10(a7),a7
move.l d0,d3
bmi read_fail
tst.b terminate_by_ctrlz(a6)
beq trunc_ctrlz_done
moveq #CTRLZ,d0
bsr trunc
trunc_ctrlz_done:
tst.b terminate_by_ctrld(a6)
beq trunc_ctrld_done
moveq #CTRLD,d0
bsr trunc
trunc_ctrld_done:
tst.l d3
beq getc_eof
getc_get1:
subq.l #1,d3
moveq #0,d0
move.b (a3)+,d0
addq.l #1,saved_count(a6)
getc_done:
move.l a3,inpbuf_ptr(a6)
move.l d3,inpbuf_remain(a6)
movem.l (a7)+,d3/a3
tst.l d0
rts
getc_eof:
bset #FLAG_eof,d5
moveq #-1,d0
bra getc_done
read_fail:
bsr werror_myname_and_msg
lea msg_read_fail(pc),a0
bra werror_exit_3
*****************************************************************
trunc:
movem.l d1/a0,-(a7)
move.l d3,d1
beq trunc_done
movea.l a3,a0
trunc_find_loop:
cmp.b (a0)+,d0
beq trunc_found
subq.l #1,d1
bne trunc_find_loop
bra trunc_done
trunc_found:
move.l a0,d3
subq.l #1,d3
sub.l a3,d3
bset #FLAG_eof,d5
trunc_done:
movem.l (a7)+,d1/a0
rts
*****************************************************************
flush_outbuf:
move.l d0,-(a7)
tst.b do_buffering(a6)
beq flush_return
move.l #OUTBUF_SIZE,d0
sub.l outbuf_free(a6),d0
beq flush_return
move.l d0,-(a7)
move.l outbuf_top(a6),-(a7)
move.w #1,-(a7)
DOS _WRITE
lea 10(a7),a7
tst.l d0
bmi write_fail
cmp.l -4(a7),d0
blo write_fail
move.l outbuf_top(a6),d0
move.l d0,outbuf_ptr(a6)
move.l #OUTBUF_SIZE,d0
move.l d0,outbuf_free(a6)
flush_return:
move.l (a7)+,d0
rts
*****************************************************************
putc:
movem.l d0/a0,-(a7)