about summary refs log blame commit diff stats
path: root/baremetal/120allocate.subx
blob: d2726b909d108519418c09387c15096b6c0519d4 (plain) (tree)
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


































                                                                                    
       



                                                                                                                                                 

























































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                  














































                                                                                                                                                                             
























                                                                                                                                                                       

                                                                                                                                                                  




                                                                                                                                                                       
                            
# Helper to dynamically allocate memory on the heap.
#
# We'd like to be able to write tests for functions that allocate memory,
# making assertions on the precise addresses used. To achieve this we'll pass
# in an *allocation descriptor* to allocate from.
#
# Allocation descriptors are also useful outside of tests. Assembly and machine
# code are of necessity unsafe languages, and one of the most insidious kinds
# of bugs unsafe languages expose us to are dangling pointers to memory that
# has been freed and potentially even reused for something totally different.
# To reduce the odds of such "use after free" errors, SubX programs tend to not
# reclaim and reuse dynamically allocated memory. (Running out of memory is far
# easier to debug.) Long-running programs that want to reuse memory are mostly
# on their own to be careful. However, they do get one bit of help: they can
# carve out chunks of memory and then allocate from them manually using this
# very same 'allocate' helper. They just need a new allocation descriptor for
# their book-keeping.

== data

# Allocations are returned in a handle, which consists of an alloc-id and a payload.
# The alloc-id helps detect use-after-free errors.
Handle-size:  # (addr int)
  8/imm32

# A default allocation descriptor for programs to use.
Heap:  # allocation-descriptor
  # curr
  0x01000000/imm32  # 16 MB
  # limit
  0x02000000/imm32  # 32 MB

Next-alloc-id:  # int
  0x100/imm32  # save a few alloc ids for fake handles

== code
#   instruction                     effective address                                                   register    displacement    immediate
# . op          subop               mod             rm32          base        index         scale       r32
# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes

# Allocate and clear 'n' bytes of memory from an allocation-descriptor 'ad'.
# Abort if there isn't enough memory in 'ad'.
allocate:  # ad: (addr allocation-descriptor), n: int, out: (addr handle _)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    # allocate-raw(ad, n, out)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  allocate-raw/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # eax = out->payload + 4
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
    8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to eax
    05/add-to-eax  4/imm32
    # zero-out(eax, n)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    50/push-eax
    # . . call
    e8/call  zero-out/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
$allocate:end:
    # . restore registers
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# Claim the next 'n' bytes of memory starting at ad->curr and update ad->curr.
# Abort if there isn't enough memory in 'ad'.
allocate-raw:  # ad: (addr allocation-descriptor), n: int, out: (addr handle _)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    53/push-ebx
    56/push-esi
    57/push-edi
    # ecx = ad
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # edx = out
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0x10/disp8      .                 # copy *(ebp+16) to edx
    # ebx = n
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   0xc/disp8       .                 # copy *(ebp+12) to ebx
    # out->alloc-id = Next-alloc-id
    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Next-alloc-id/disp32              # copy *Next-alloc-id to eax
    89/copy                         0/mod/indirect  2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to *edx
    # out->payload = ad->curr
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
$allocate-raw:save-payload-in-eax:
    89/copy                         1/mod/*+disp8   2/rm32/edx    .           .             .           0/r32/eax   4/disp8         .                 # copy eax to *(edx+4)
    # *out->payload = Next-alloc-id
    8b/copy                         1/mod/*+disp8   2/rm32/edx    .           .             .           7/r32/edi   4/disp8         .                 # copy *(edx+4) to edi
    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           6/r32/esi   Next-alloc-id/disp32              # copy *Next-alloc-id to esi
    89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           6/r32/esi   .               .                 # copy esi to *edi
$allocate-raw:increment-next-alloc-id:
    # increment *Next-alloc-id
    ff          0/subop/increment   0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32              # increment *Next-alloc-id
    # check if there's enough space
    # TODO: move this check up before any state updates when we support error recovery
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/eax  3/index/ebx   .           0/r32/eax   4/disp8         .                 # copy eax+ebx+4 to eax
    3b/compare                      1/mod/*+disp8   1/rm32/ecx    .           .             .           0/r32/eax   4/disp8         .                 # compare eax with *(ecx+4)
    73/jump-if->=-signed  $allocate-raw:abort/disp8
$allocate-raw:commit:
    # ad->curr += n+4
    89/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy eax to *ecx
$allocate-raw:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

$allocate-raw:abort:
    (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "allocate: failed" 3)  # 3=cyan
    {
      eb/jump loop/disp8
    }
    # never gets here

test-allocate-raw-success:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # var ad/ecx: allocation-descriptor containing 16 bytes
    # . var end/ecx: (addr byte)
    89/<- %ecx 4/r32/esp
    81 5/subop/subtract %esp 0x10/imm32
    # . var start/edx: (addr byte) = end - 0x16
    89/<- %edx 4/r32/esp
    # . ad = {start, end}
    51/push-ecx
    52/push-edx
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # var expected-payload/ebx = ad->curr
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           3/r32/ebx   .               .                 # copy *ecx to ebx
    # var h/edx: handle = {0, 0}
    68/push  0/imm32
    68/push  0/imm32
    89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
    # *Next-alloc-id = 0x34
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  0x34/imm32  # copy to *Next-alloc-id
    # allocate-raw(ad, 3, h)
    # . . push args
    52/push-edx
    68/push  3/imm32
    51/push-ecx
    # . . call
    e8/call  allocate-raw/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->alloc-id, 0x34, msg)
    # . . push args
    68/push  "F - test-allocate-raw-success: sets alloc-id in handle"/imm32
    68/push  0x34/imm32
    ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload, expected-payload, msg)
    # . . push args
    68/push  "F - test-allocate-raw-success: sets payload in handle"/imm32
    53/push-ebx
    ff          6/subop/push        1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # push *(edx+4)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload->alloc-id, 0x34, msg)
    # . . push args
    68/push  "F - test-allocate-raw-success: sets alloc-id in payload"/imm32
    68/push  0x34/imm32
    ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(*Next-alloc-id, 0x35, msg)
    # . . push args
    68/push  "F - test-allocate-raw-success: increments Next-alloc-id"/imm32
    68/push  0x35/imm32
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32              # push *Next-alloc-id
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(ad->curr - expected-payload, 3 + 4 for alloc-id, msg)
    # . . push args
    68/push  "F - test-allocate-raw-success: updates allocation descriptor"/imm32
    68/push  7/imm32
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
    29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # subtract ebx from eax
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # clean up
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  0x100/imm32 # copy to *Next-alloc-id
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

lookup:  # h: (handle _T) -> result/eax: (addr _T)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    # eax = 0
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    # ecx = handle->alloc_id
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # if (ecx == 0) return 0
    81          7/subop/compare     3/mod/direct    1/rm32/ecx    .           .             .           .           .               0/imm32           # compare ecx
    74/jump-if-=  $lookup:end/disp8
    # eax = handle->address (payload)
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
    # if (ecx != *eax) abort
    39/compare                      0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # compare *eax and ecx
    75/jump-if-!=  $lookup:abort/disp8
    # add 4 to eax
    05/add-to-eax  4/imm32
$lookup:end:
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

$lookup:abort:
    (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "lookup: failed" 3)  # 3=cyan
    {
      eb/jump loop/disp8
    }
    # never gets here

test-lookup-success:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # var ad/ebx: allocation-descriptor containing 16 bytes
    # . var end/ecx: (addr byte)
    89/<- %ecx 4/r32/esp
    # . var start/edx: (addr byte) = end - 16
    81 5/subop/subtract %esp 0x10/imm32
    89/<- %edx 4/r32/esp
    # . ad = {start, end}
    51/push-ecx
    52/push-edx
    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
    # var handle/ecx: handle
    68/push  0/imm32/address
    68/push  0/imm32/alloc-id
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # var old-top/edx = ad->curr
    8b/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           2/r32/edx   .               .                 # copy *ebx to edx
    # allocate-raw(ad, 2, handle)
    # . . push args
    51/push-ecx
    68/push  2/imm32/size
    53/push-ebx
    # . . call
    e8/call  allocate-raw/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # eax = lookup(handle)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   1/rm32/ecx    .           .             .           .           4/disp8         .                 # push *(ecx+4)
    ff          6/subop/push        0/mod/indirect  1/rm32/ecx    .           .             .           .           .               .                 # push *ecx
    # . . call
    e8/call  lookup/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # eax contains old top of heap, except skipping the alloc-id in the payload
    # . check-ints-equal(eax, old-top+4, msg)
    # . . push args
    68/push  "F - test-lookup-success"/imm32
    81          0/subop/add         3/mod/direct    2/rm32/edx    .           .             .           .           .               4/imm32           # add to edx
    52/push-edx
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # clean up
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  0x100/imm32 # copy to *Next-alloc-id
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x20/imm32        # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-lookup-null-returns-null:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # var handle/ecx: handle
    68/push  0/imm32/address
    68/push  0/imm32/alloc-id
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # eax = lookup(handle)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   1/rm32/ecx    .           .             .           .           4/disp8         .                 # push *(ecx+4)
    ff          6/subop/push        0/mod/indirect  1/rm32/ecx    .           .             .           .           .               .                 # push *ecx
    # . . call
    e8/call  lookup/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-lookup-null-returns-null"/imm32
    68/push  0/imm32
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

_pending-test-lookup-failure:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # var ad/ecx: allocation-descriptor containing 16 bytes
    # . var end/ecx: (addr byte)
    89/<- %ecx 4/r32/esp
    81 5/subop/subtract %esp 0xc00/imm32
    # . var start/edx: (addr byte) = end - 0x16
    89/<- %edx 4/r32/esp
    # . ad = {start, end}
    51/push-ecx
    52/push-edx
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # var h1/ecx: handle
    68/push  0/imm32/address
    68/push  0/imm32/alloc-id
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # var old_top/ebx = ad->curr
    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           3/r32/ebx   .               .                 # copy *esi to ebx
    # first allocation, to h1
    # . allocate(ad, 2, h1)
    # . . push args
    51/push-ecx
    68/push  2/imm32/size
    56/push-esi
    # . . call
    e8/call  allocate/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # reset ad->curr to mimic reclamation
    89/copy                         0/mod/indirect  6/rm32/esi    .           .             .           3/r32/ebx   .               .                 # copy ebx to *esi
    # second allocation that returns the same address as the first
    # var h2/edx: handle
    68/push  0/imm32/address
    68/push  0/imm32/alloc-id
    89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
    # . allocate(ad, 2, h2)
    # . . push args
    52/push-edx
    68/push  2/imm32/size
    56/push-esi
    # . . call
    e8/call  allocate/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h1->address, h2->address, msg)
    # . . push args
    68/push  "F - test-lookup-failure"/imm32
    ff          6/subop/push        1/mod/*+disp8   2/rm32/ecx    .           .             .           .           4/disp8         .                 # push *(edx+4)
    ff          6/subop/push        1/mod/*+disp8   1/rm32/ecx    .           .             .           .           4/disp8         .                 # push *(ecx+4)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # lookup(h1) should crash
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   1/rm32/ecx    .           .             .           .           4/disp8         .                 # push *(ecx+4)
    ff          6/subop/push        0/mod/indirect  1/rm32/ecx    .           .             .           .           .               .                 # push *ecx
    # . . call
    e8/call  lookup/disp32
    # should never get past this point
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # clean up
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  0x100/imm32 # copy to *Next-alloc-id
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# when comparing handles, just treat them as pure values
handle-equal?:  # a: (handle _T), b: (handle _T) -> result/eax: boolean
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    # eax = false
    b8/copy-to-eax  0/imm32/false
$handle-equal?:compare-alloc-id:
    # ecx = a->alloc_id
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # if (ecx != b->alloc_id) return false
    39/compare                      1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x10/disp8      .                 # compare ecx and *(ebp+16)
    75/jump-if-!=  $handle-equal?:end/disp8
$handle-equal?:compare-address:
    # ecx = handle->address
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0xc/disp8       .                 # copy *(ebp+12) to ecx
    # if (ecx != b->address) return false
    39/compare                      1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x14/disp8      .                 # compare ecx and *(ebp+20)
    75/jump-if-!=  $handle-equal?:end/disp8
$handle-equal?:return-true:
    # return true
    b8/copy-to-eax  1/imm32/true
$handle-equal?:end:
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

copy-handle:  # src: (handle _T), dest: (addr handle _T)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    51/push-ecx
    # ecx = dest
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x10/disp8      .                 # copy *(ebp+16) to ecx
    # *dest = src
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to eax
    89/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy eax to *ecx
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
    89/copy                         1/mod/*+disp8   1/rm32/ecx    .           .             .           0/r32/eax   4/disp8         .                 # copy eax to *(ecx+4)
$copy-handle:end:
    # . restore registers
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# helper: create a nested allocation descriptor (useful for tests)
allocate-region:  # ad: (addr allocation-descriptor), n: int, out: (addr handle allocation-descriptor)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    51/push-ecx
    # allocate(ad, n, out)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  allocate/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # eax = out->payload
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
    8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to eax
    # skip payload->allocid
    05/add-to-eax  4/imm32
    # if (eax == 0) abort
    3d/compare-eax-and  0/imm32
    74/jump-if-=  $allocate-region:abort/disp8
    # earmark 8 bytes at the start for a new allocation descriptor
    # . *eax = eax + 8
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy eax to ecx
    81          0/subop/add         3/mod/direct    1/rm32/ecx    .           .             .           .           .               8/imm32           # add to ecx
    89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
    # . *(eax+4) = eax + n
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy eax to ecx
    03/add                          1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0xc/disp8       .                 # add *(ebp+12) to ecx
    89/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   4/disp8         .                 # copy ecx to *(eax+4)
    # . restore registers
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# We could create a more general '$abort' jump target, but then we'd need to do
# a conditional jump followed by loading the error message and an unconditional
# jump. Or we'd need to unconditionally load the error message before a
# conditional jump, even if it's unused the vast majority of the time. This way
# we bloat a potentially cold segment in RAM so we can abort with a single
# instruction.
$allocate-region:abort:
    (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "allocate-region: failed to allocate" 3)  # 3=cyan
    {
      eb/jump loop/disp8
    }
    # never gets here

# Claim the next 'n+4' bytes of memory and initialize the first 4 to n.
# Abort if there isn't enough memory in 'ad'.
allocate-array:  # ad: (addr allocation-descriptor), n: int, out: (addr handle _)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    # ecx = n
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0xc/disp8       .                 # copy *(ebp+12) to ecx
    # var size/edx: int = n+4
    8d/copy-address                 1/mod/*+disp8   1/rm32/ecx    .           .             .           2/r32/edx   4/disp8         .                 # copy ecx+4 to edx
    # allocate(ad, size, out)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    52/push-edx
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  allocate/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # *out->payload = n
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
    8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to eax
    # . skip payload->allocid
    05/add-to-eax  4/imm32
    # .
    89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
$allocate-array:end:
    # . restore registers
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-allocate-array:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # var ad/ecx: allocation-descriptor containing 16 bytes
    # . var end/ecx: (addr byte)
    89/<- %ecx 4/r32/esp
    81 5/subop/subtract %esp 0xc00/imm32
    # . var start/edx: (addr byte) = end - 0x16
    89/<- %edx 4/r32/esp
    # . ad = {start, end}
    51/push-ecx
    52/push-edx
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # var expected-payload/ebx = ad->curr
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           3/r32/ebx   .               .                 # copy *ecx to ebx
    # var h/edx: handle = {0, 0}
    68/push  0/imm32
    68/push  0/imm32
    89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
    # *Next-alloc-id = 0x34
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  0x34/imm32  # copy to *Next-alloc-id
    # allocate-array(ad, 3, h)
    # . . push args
    52/push-edx
    68/push  3/imm32
    51/push-ecx
    # . . call
    e8/call  allocate-array/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->alloc-id, 0x34, msg)
    # . . push args
    68/push  "F - test-allocate-array: sets alloc-id in handle"/imm32
    68/push  0x34/imm32
    ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload, expected-payload, msg)
    # . . push args
    68/push  "F - test-allocate-array: sets payload in handle"/imm32
    53/push-ebx
    ff          6/subop/push        1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # push *(edx+4)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload->alloc-id, 0x34, msg)
    # . . push args
    68/push  "F - test-allocate-array: sets alloc-id in payload"/imm32
    68/push  0x34/imm32
    ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload->size, 3, msg)
    # . . push args
    68/push  "F - test-allocate-array: sets array size in payload"/imm32
    68/push  3/imm32
    ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(*Next-alloc-id, 0x35, msg)
    # . . push args
    68/push  "F - test-allocate-array: increments Next-alloc-id"/imm32
    68/push  0x35/imm32
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32              # push *Next-alloc-id
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(ad->curr - expected-payload, 3 + 4 for alloc-id + 4 for array size, msg)
    # . . push args
    68/push  "F - test-allocate-array: updates allocation descriptor"/imm32
    68/push  0xb/imm32
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
    29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # subtract ebx from eax
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # clean up
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  1/imm32     # copy to *Next-alloc-id
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

copy-array:  # ad: (addr allocation-descriptor), src: (addr array _T), out: (addr handle array _T)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    56/push-esi
    # esi = src
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
    # var size/ecx: int = src->size+4
    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # copy *esi to ecx
    81          0/subop/add         3/mod/direct    1/rm32/ecx    .           .             .           .           .               4/imm32           # add to ecx
    # allocate(ad, size, out)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    51/push-ecx
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  allocate/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # var payload/eax: (addr byte) = out->payload
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
    8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to eax
    # . skip payload->allocid
    05/add-to-eax  4/imm32
    # var max/ecx: (addr byte) = payload + size
    01/add                          3/mod/direct    1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # add eax to ecx
    # _append-4(payload, max, src, &src->data[src->size])
    # . . push &src->data[src->size]
    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           2/r32/edx   .               .                 # copy *esi to edx
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  2/index/edx   .           2/r32/edx   4/disp8         .                 # copy esi+edx+4 to edx
    52/push-edx
    # . . push src
    56/push-esi
    # . . push max
    51/push-ecx
    # . . push payload
    50/push-eax
    # . . call
    e8/call  _append-4/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
$copy-array:end:
    # . restore registers
    5e/pop-to-esi
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-copy-array:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # var src/esi: (addr array int) = [3, 4, 5]
    68/push  5/imm32
    68/push  4/imm32
    68/push  3/imm32
    68/push  0xc/imm32/size
    89/copy                         3/mod/direct    6/rm32/esi    .           .             .           4/r32/esp   .               .                 # copy esp to esi
    # var ad/ecx: allocation-descriptor containing 16 bytes
    # . var end/ecx: (addr byte)
    89/<- %ecx 4/r32/esp
    81 5/subop/subtract %esp 0xc00/imm32
    # . var start/edx: (addr byte) = end - 0x16
    89/<- %edx 4/r32/esp
    # . ad = {start, end}
    51/push-ecx
    52/push-edx
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # var expected-payload/ebx = ad->curr
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           3/r32/ebx   .               .                 # copy *ecx to ebx
    # var h/edx: handle = {0, 0}
    68/push  0/imm32
    68/push  0/imm32
    89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
    # *Next-alloc-id = 0x34
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  0x34/imm32  # copy to *Next-alloc-id
    # copy-array(ad, src, h)
    # . . push args
    52/push-edx
    56/push-esi
    51/push-ecx
    # . . call
    e8/call  copy-array/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->alloc-id, 0x34, msg)
    # . . push args
    68/push  "F - test-copy-array: sets alloc-id in handle"/imm32
    68/push  0x34/imm32
    ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload, expected-payload, msg)
    # . . push args
    68/push  "F - test-copy-array: sets payload in handle"/imm32
    53/push-ebx
    ff          6/subop/push        1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # push *(edx+4)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(h->payload->alloc-id, 0x34, msg)
    # . . push args
    68/push  "F - test-copy-array: sets alloc-id in payload"/imm32
    68/push  0x34/imm32
    ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # var payload/eax: (addr int) = lookup(h)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # push *(edx+4)
    ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
    # . . call
    e8/call  lookup/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(payload->size, 0xc, msg)
    # . . push args
    68/push  "F - test-copy-array: sets array size in payload"/imm32
    68/push  0xc/imm32
    ff          6/subop/push        0/mod/indirect  0/rm32/eax    .           .             .           .           .               .                 # push *eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(*Next-alloc-id, 0x35, msg)
    # . . push args
    68/push  "F - test-copy-array: increments Next-alloc-id"/imm32
    68/push  0x35/imm32
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32              # push *Next-alloc-id
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # check-ints-equal(ad->curr - expected-payload, 12 + 4 for alloc-id + 4 for size, msg)
    # . . push args
    68/push  "F - test-copy-array: updates allocation descriptor"/imm32
    68/push  0x14/imm32
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
    29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # subtract ebx from eax
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # clean up
    c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           Next-alloc-id/disp32  1/imm32     # copy to *Next-alloc-id
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x20/imm32        # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# Fill a region of memory with zeroes.
zero-out:  # start: (addr byte), size: int
    # pseudocode:
    #   curr/esi = start
    #   i/ecx = 0
    #   while true
    #     if (i >= size) break
    #     *curr = 0
    #     ++curr
    #     ++i
    #
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    56/push-esi
    # curr/esi = start
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
    # var i/ecx: int = 0
    31/xor                          3/mod/direct    1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # clear ecx
    # edx = size
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
$zero-out:loop:
    # if (i >= size) break
    39/compare                      3/mod/direct    1/rm32/ecx    .           .             .           2/r32/edx   .               .                 # compare ecx with edx
    7d/jump-if->=  $zero-out:end/disp8
    # *curr = 0
    c6          0/subop/copy-byte   0/mod/direct    6/rm32/esi    .           .             .           .           .               0/imm8            # copy byte to *esi
    # ++curr
    46/increment-esi
    # ++i
    41/increment-ecx
    eb/jump  $zero-out:loop/disp8
$zero-out:end:
    # . restore registers
    5e/pop-to-esi
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-zero-out:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # region/ecx = 34, 35, 36, 37
    68/push  0x37363534/imm32
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # zero-out(ecx, 3)
    # . . push args
    68/push  3/imm32/size
    51/push-ecx
    # . . call
    e8/call  zero-out/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # first 3 bytes cleared, fourth left alone
    # . check-ints-equal(*ecx, 0x37000000, msg)
    # . . push args
    68/push  "F - test-zero-out"/imm32
    68/push  0x37000000/imm32
    ff          6/subop/push        0/mod/indirect  1/rm32/ecx    .           .             .           .           .               .                 # push *ecx
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# . . vim:nowrap:textwidth=0
an class="se">\*(R", it either restores closed tabs (uq), removes tags (ut), clears the copy/cut buffer (ud), starts the reversed visual mode (uV) or clears the selection (uv). .IP "f" 14 .IX Item "f" Quickly navigate by entering a part of the filename. .IP "Space" 14 .IX Item "Space" Mark a file. .IP "v" 14 .IX Item "v" Toggle the mark-status of all files .IP "V" 14 .IX Item "V" Starts the visual mode, which selects all files between the starting point and the cursor until you press \s-1ESC. \s0 To unselect files in the same way, use \*(L"uV\*(R". .IP "/" 14 Search for files in the current directory. .IP ":" 14 Open the console. .IP "!" 14 Open the console with the content \*(L"shell \*(R" so you can quickly run commands .IP "@" 14 Open the console with the content \*(L"shell \f(CW%s\fR\*(R", placing the cursor before the \&\*(L" \f(CW%s\fR\*(R" so you can quickly run commands with the current selection as the argument. .IP "r" 14 .IX Item "r" Open the console with the content \*(L"open with \*(R" so you can decide which program to use to open the current file selection. .IP "cd" 14 .IX Item "cd" Open the console with the content \*(L"cd \*(R" .IP "Alt\-\fIN\fR" 14 .IX Item "Alt-N" Open a tab. N has to be a number from 0 to 9. If the tab doesn't exist yet, it will be created. .IP "gn, ^N" 14 .IX Item "gn, ^N" Create a new tab. .IP "gt, gT" 14 .IX Item "gt, gT" Go to the next or previous tab. You can also use \s-1TAB\s0 and \s-1SHIFT+TAB\s0 instead. .IP "gc, ^W" 14 .IX Item "gc, ^W" Close the current tab. The last tab cannot be closed this way. .IP "M" 14 .IX Item "M" A key chain that allows you to quickly change the line mode of all the files of the current directory. For a more permanent solution, use the command \&\*(L"default_linemode\*(R" in your rc.conf. .SS "READLINE-LIKE \s-1BINDINGS IN THE CONSOLE\s0" .IX Subsection "READLINE-LIKE BINDINGS IN THE CONSOLE" .IP "^B, ^F" 14 .IX Item "^B, ^F" Move left and right (B for back, F for forward) .IP "^P, ^N" 14 .IX Item "^P, ^N" Move up and down (P for previous, N for Next) .IP "^A, ^E" 14 .IX Item "^A, ^E" Move to the start or to the end .IP "^D" 14 .IX Item "^D" Delete the current character. .IP "^H" 14 .IX Item "^H" Backspace. .SH "MOUSE BUTTONS" .IX Header "MOUSE BUTTONS" .IP "Left Mouse Button" 4 .IX Item "Left Mouse Button" Click on something and you'll move there. To run a file, \*(L"enter\*(R" it, like a directory, by clicking on the preview. .IP "Right Mouse Button" 4 .IX Item "Right Mouse Button" Enter a directory or run a file. .IP "Scroll Wheel" 4 .IX Item "Scroll Wheel" Scrolls up or down. You can point at the column of the parent directory while scrolling to switch directories. .SH "SETTINGS" .IX Header "SETTINGS" This section lists all built-in settings of ranger. The valid types for the value are in [brackets]. The hotkey to toggle the setting is in <brokets>, if a hotkey exists. .PP Settings can be changed in the file \fI~/.config/ranger/rc.conf\fR or on the fly with the command \fB:set option value\fR. Examples: .PP .Vb 2 \& set column_ratios 1,2,3 \& set show_hidden true .Ve .PP The different types of settings and an example for each type: .PP .Vb 7 \& setting type | example values \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& bool | true, false \& integer | 1, 23, 1337 \& string | foo, hello world \& list | 1,2,3,4 \& none | none .Ve .PP You can view a list of all settings and their current values by pressing \*(L"3?\*(R" in ranger. .IP "automatically_count_files [bool]" 4 .IX Item "automatically_count_files [bool]" Should ranger count and display the number of files in each directory as soon as it's visible? This gets slow with remote file sytems. Turning it off will still allow you to see the number of files after entering the directory. .IP "autosave_bookmarks [bool]" 4 .IX Item "autosave_bookmarks [bool]" Save bookmarks (used with mX and `X) instantly? This helps to synchronize bookmarks between multiple ranger instances but leads to *slight* performance loss. When false, bookmarks are saved when ranger is exited. .IP "autoupdate_cumulative_size [bool]" 4 .IX Item "autoupdate_cumulative_size [bool]" You can display the \*(L"real\*(R" cumulative size of directories by using the command :get_cumulative_size or typing \*(L"dc\*(R". The size is expensive to calculate and will not be updated automatically. You can choose to update it automatically though by turning on this option. .IP "cd_bookmarks [bool]" 4 .IX Item "cd_bookmarks [bool]" Specify whether bookmarks should be included in the tab completion of the \*(L"cd\*(R" command. .IP "collapse_preview [bool] <zc>" 4 .IX Item "collapse_preview [bool] <zc>" When no preview is visible, should the last column be squeezed to make use of the whitespace? .IP "colorscheme [string]" 4 .IX Item "colorscheme [string]" Which colorscheme to use? These colorschemes are available by default: \&\fBdefault\fR, \fBjungle\fR, \fBsnow\fR. Snow is a monochrome scheme, jungle replaces blue directories with green ones for better visibility on certain terminals. .IP "column_ratios [list]" 4 .IX Item "column_ratios [list]" How many columns are there, and what are their relative widths? For example, a value of 1,1,1 would mean 3 evenly sized columns. 1,1,1,1,4 means 5 columns with the preview column being as large as the other columns combined. .IP "confirm_on_delete [string]" 4 .IX Item "confirm_on_delete [string]" Ask for a confirmation when running the \*(L"delete\*(R" command? Valid values are \&\*(L"always\*(R" (default), \*(L"never\*(R", \*(L"multiple\*(R". With \*(L"multiple\*(R", ranger will ask only if you delete multiple files at once. .IP "dirname_in_tabs [bool]" 4 .IX Item "dirname_in_tabs [bool]" Display the directory name in tabs? .IP "display_size_in_main_column [bool]" 4 .IX Item "display_size_in_main_column [bool]" Display the file size in the main column? .IP "display_size_in_status_bar [bool]" 4 .IX Item "display_size_in_status_bar [bool]" Display the file size in the status bar? .IP "display_tags_in_all_columns [bool]" 4 .IX Item "display_tags_in_all_columns [bool]" Display tags in all columns? .IP "draw_borders [bool]" 4 .IX Item "draw_borders [bool]" Draw borders around columns? .IP "draw_progress_bar_in_statusbar [bool]" 4 .IX Item "draw_progress_bar_in_statusbar [bool]" Draw a progress bar in the status bar which displays the average state of all currently running tasks which support progress bars? .IP "flushinput [bool] <zi>" 4 .IX Item "flushinput [bool] <zi>" Flush the input after each key hit? One advantage is that when scrolling down with \*(L"j\*(R", ranger stops scrolling instantly when you release the key. One disadvantage is that when you type commands blindly, some keys might get lost. .IP "hidden_filter [string]" 4 .IX Item "hidden_filter [string]" A regular expression pattern for files which should be hidden. For example, this pattern will hide all files that start with a dot or end with a tilde. .Sp .Vb 1 \& set hidden_filter ^\e.|~$ .Ve .IP "idle_delay [integer]" 4 .IX Item "idle_delay [integer]" The delay that ranger idly waits for user input, in milliseconds, with a resolution of 100ms. Lower delay reduces lag between directory updates but increases \s-1CPU\s0 load. .IP "max_console_history_size [integer, none]" 4 .IX Item "max_console_history_size [integer, none]" How many console commands should be kept in history? \*(L"none\*(R" will disable the limit. .IP "max_history_size [integer, none]" 4 .IX Item "max_history_size [integer, none]" How many directory changes should be kept in history? .IP "metadata_deep_search [bool]" 4 .IX Item "metadata_deep_search [bool]" When the metadata manager module looks for metadata, should it only look for a \&\*(L".metadata.json\*(R" file in the current directory, or do a deep search and check all directories above the current one as well? .IP "mouse_enabled [bool] <zm>" 4 .IX Item "mouse_enabled [bool] <zm>" Enable mouse input? .IP "padding_right [bool]" 4 .IX Item "padding_right [bool]" When collapse_preview is on and there is no preview, should there remain a little padding on the right? This allows you to click into that space to run the file. .IP "preview_directories [bool] <zP>" 4 .IX Item "preview_directories [bool] <zP>" Preview directories in the preview column? .IP "preview_files [bool] <zp>" 4 .IX Item "preview_files [bool] <zp>" Preview files in the preview column? .IP "preview_images [bool]" 4 .IX Item "preview_images [bool]" Draw images inside the console with the external program w3mimgpreview? .IP "preview_max_size [int]" 4 .IX Item "preview_max_size [int]" Avoid previewing files that exceed a certain size, in bytes. Use a value of 0 to disable this feature. .IP "preview_script [string, none]" 4 .IX Item "preview_script [string, none]" Which script should handle generating previews? If the file doesn't exist, or use_preview_script is off, ranger will handle previews itself by just printing the content. .IP "save_console_history [bool]" 4 .IX Item "save_console_history [bool]" Should the console history be saved on exit? If disabled, the console history is reset when you restart ranger. .IP "scroll_offset [integer]" 4 .IX Item "scroll_offset [integer]" Try to keep this much space between the top/bottom border when scrolling. .IP "shorten_title [integer]" 4 .IX Item "shorten_title [integer]" Trim the title of the window if it gets long? The number defines how many directories are displayed at once. A value of 0 turns off this feature. .IP "show_cursor [bool]" 4 .IX Item "show_cursor [bool]" Always show the terminal cursor? .IP "show_hidden_bookmarks [bool]" 4 .IX Item "show_hidden_bookmarks [bool]" Show dotfiles in the bookmark preview window? (Type ') .IP "show_hidden [bool] <zh>, <^H>" 4 .IX Item "show_hidden [bool] <zh>, <^H>" Show hidden files? .IP "sort_case_insensitive [bool] <zc>" 4 .IX Item "sort_case_insensitive [bool] <zc>" Sort case-insensitively? If true, \*(L"a\*(R" will be listed before \*(L"B\*(R" even though its \s-1ASCII\s0 value is higher. .IP "sort_directories_first [bool] <zd>" 4 .IX Item "sort_directories_first [bool] <zd>" Sort directories first? .IP "sort_reverse [bool] <or>" 4 .IX Item "sort_reverse [bool] <or>" Reverse the order of files? .IP "sort [string] <oa>, <ob>, <oc>, <om>, <on>, <ot>, <os>, <oz>" 4 .IX Item "sort [string] <oa>, <ob>, <oc>, <om>, <on>, <ot>, <os>, <oz>" Which sorting mechanism should be used? Choose one of \fBatime\fR, \fBbasename\fR, \&\fBctime\fR, \fBmtime\fR, \fBnatural\fR, \fBtype\fR, \fBsize\fR, \fBrandom\fR .Sp Note: You can reverse the order by typing an uppercase second letter in the key combination, e.g. \*(L"oN\*(R" to sort from Z to A. .IP "status_bar_on_top [bool]" 4 .IX Item "status_bar_on_top [bool]" Put the status bar at the top of the window? .IP "tilde_in_titlebar [bool]" 4 .IX Item "tilde_in_titlebar [bool]" Abbreviate \f(CW$HOME\fR with ~ in the title bar (first line) of ranger? .IP "unicode_ellipsis [bool]" 4 .IX Item "unicode_ellipsis [bool]" Use a unicode \*(L"...\*(R" character instead of \*(L"~\*(R" to mark cut-off filenames? .IP "update_title [bool]" 4 .IX Item "update_title [bool]" Set a window title? .IP "update_tmux_title [bool]" 4 .IX Item "update_tmux_title [bool]" Set the title to \*(L"ranger\*(R" in the tmux program? .IP "use_preview_script [bool] <zv>" 4 .IX Item "use_preview_script [bool] <zv>" Use the preview script defined in the setting \fIpreview_script\fR? .IP "vcs_aware [bool]" 4 .IX Item "vcs_aware [bool]" Gather and display data about version control systems. Supported vcs: git, hg. .IP "vcs_backend_git, vcs_backend_hg, vcs_backend_bzr [string]" 4 .IX Item "vcs_backend_git, vcs_backend_hg, vcs_backend_bzr [string]" Sets the state for the version control backend. The possible values are: .Sp .Vb 3 \& disabled don\*(Aqt display any information. \& local display only local state. \& enabled display both, local and remote state. May be slow for hg and bzr. .Ve .IP "xterm_alt_key [bool]" 4 .IX Item "xterm_alt_key [bool]" Enable this if key combinations with the Alt Key don't work for you. (Especially on xterm) .SH "COMMANDS" .IX Header "COMMANDS" You can enter the commands in the console which is opened by pressing \*(L":\*(R". .PP You can always get a list of the currently existing commands by typing \*(L"2?\*(R" in ranger. For your convenience, this is a list of the \*(L"public\*(R" commands including their parameters, excluding descriptions: .PP .Vb 10 \& alias [newcommand] [oldcommand] \& bulkrename \& cd [directory] \& chain command1[; command2[; command3...]] \& chmod octal_number \& cmap key command \& console [\-pSTARTPOSITION] command \& copycmap key newkey [newkey2...] \& copymap key newkey [newkey2...] \& copypmap key newkey [newkey2...] \& copytmap key newkey [newkey2...] \& cunmap keys... \& default_linemode [path=regexp | tag=tags] linemodename \& delete \& edit [filename] \& eval [\-q] python_code \& filter [string] \& filter_inode_type [dfl] \& find pattern \& flat level \& grep pattern \& linemode linemodename \& load_copy_buffer \& map key command \& mark pattern \& mark_tag [tags] \& meta key value \& mkdir dirname \& open_with [application] [flags] [mode] \& pmap key command \& prompt_metadata [key1 [key2 [...]]] \& punmap keys... \& quit \& quit! \& relink newpath \& rename_append \& rename newname \& save_copy_buffer \& scout [\-FLAGS] pattern \& search pattern \& search_inc pattern \& set option value \& setlocal [path=<path>] option value \& shell [\-FLAGS] command \& terminal \& tmap key command \& touch filename \& travel pattern \& tunmap keys... \& unmap keys... \& unmark pattern \& unmark_tag [tags] .Ve .PP There are additional commands which are directly translated to python functions, one for every method in the ranger.core.actions.Actions class. They are not documented here, since they are mostly for key bindings, not to be typed in by a user. Read the source if you are interested in them. .PP These are the public commands including their descriptions: .IP "alias [\fInewcommand\fR] [\fIoldcommand\fR]" 2 .IX Item "alias [newcommand] [oldcommand]" Copies the oldcommand as newcommand. .IP "bulkrename" 2 .IX Item "bulkrename" This command opens a list of selected files in an external editor. After you edit and save the file, it will generate a shell script which does bulk renaming according to the changes you did in the file. .Sp This shell script is opened in an editor for you to review. After you close it, it will be executed. .IP "cd [\fIdirectory\fR]" 2 .IX Item "cd [directory]" The cd command changes the directory. The command \f(CW\*(C`:cd \-\*(C'\fR is equivalent to typing ``. .IP "chain \fIcommand1\fR[; \fIcommand2\fR[; \fIcommand3\fR...]]" 2 .IX Item "chain command1[; command2[; command3...]]" Combines multiple commands into one, separated by semicolons. .IP "chmod \fIoctal_number\fR" 2 .IX Item "chmod octal_number" Sets the permissions of the selection to the octal number. .Sp The octal number is between 000 and 777. The digits specify the permissions for the user, the group and others. A 1 permits execution, a 2 permits writing, a 4 permits reading. Add those numbers to combine them. So a 7 permits everything. .Sp Key bindings in the form of [\-+]<who><what> and <octal>= also exist. For example, \fB+ar\fR allows reading for everyone, \-ow forbids others to write and 777= allows everything. .Sp See also: man 1 chmod .IP "cmap \fIkey\fR \fIcommand\fR" 2 .IX Item "cmap key command" Binds keys for the console. Works like the \f(CW\*(C`map\*(C'\fR command. .IP "console [\-p\fIN\fR] \fIcommand\fR" 2 .IX Item "console [-pN] command" Opens the console with the command already typed in. The cursor is placed at \&\fIN\fR. .IP "copycmap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2 .IX Item "copycmap key newkey [newkey2 ...]" See \f(CW\*(C`copymap\*(C'\fR .IP "copymap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2 .IX Item "copymap key newkey [newkey2 ...]" Copies the keybinding \fIkey\fR to \fInewkey\fR in the \*(L"browser\*(R" context. This is a deep copy, so if you change the new binding (or parts of it) later, the old one is not modified. .Sp To copy key bindings of the console, taskview, or pager use \*(L"copycmap\*(R", \&\*(L"copytmap\*(R" or \*(L"copypmap\*(R". .IP "copypmap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2 .IX Item "copypmap key newkey [newkey2 ...]" See \f(CW\*(C`copymap\*(C'\fR .IP "copytmap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2 .IX Item "copytmap key newkey [newkey2 ...]" See \f(CW\*(C`copymap\*(C'\fR .IP "cunmap [\fIkeys...\fR]" 2 .IX Item "cunmap [keys...]" Removes key mappings of the console. Works like the \f(CW\*(C`unmap\*(C'\fR command. .IP "default_linemode [\fIpath=regexp\fR | \fItag=tags\fR] \fIlinemodename\fR" 2 .IX Item "default_linemode [path=regexp | tag=tags] linemodename" Sets the default linemode. See \fIlinemode\fR command. .Sp Examples: .Sp Set the global default linemode to \*(L"permissions\*(R": :default_linemode permissions .Sp Set the default linemode to \*(L"permissions\*(R" for all files tagged with \*(L"p\*(R" or \*(L"P\*(R": :default_linemode tag=pP permissions .Sp Set the default linemode for all files in ~/books/ to \*(L"metatitle\*(R": :default_linemode path=/home/.*?/books/.* metatitle .IP "delete" 2 .IX Item "delete" Destroy all files in the selection with a roundhouse kick. ranger will ask for a confirmation if you attempt to delete multiple (marked) files or non-empty directories. This can be changed by modifying the setting \*(L"confirm_on_delete\*(R". .IP "edit [\fIfilename\fR]" 2 .IX Item "edit [filename]" Edit the current file or the file in the argument. .IP "eval [\fI\-q\fR] \fIpython_code\fR" 2 .IX Item "eval [-q] python_code" Evaluates the python code. `fm' is a reference to the \s-1FM\s0 instance. To display text, use the function `p'. The result is displayed on the screen unless you use the \*(L"\-q\*(R" option. .Sp Examples: :eval fm :eval len(fm.tabs) :eval p(\*(L"Hello World!\*(R") .IP "filter [\fIstring\fR]" 2 .IX Item "filter [string]" Displays only the files which contain the \fIstring\fR in their basename. Running this command without any parameter will reset the fitler. .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "filter_inode_type [dfl]" 2 .IX Item "filter_inode_type [dfl]" Displays only the files of specified inode type. To display only directories, use the 'd' parameter. To display only files, use the 'f' parameter. To display only links, use the 'l' parameter. Parameters can be combined. To remove this filter, use no parameter. .IP "find \fIpattern\fR" 2 .IX Item "find pattern" Search files in the current directory that contain the given (case-insensitive) string in their name as you type. Once there is an unambiguous result, it will be run immediately. (Or entered, if it's a directory.) .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "flat level" 2 .IX Item "flat level" Flattens the directory view up to the specified level. Level \-1 means infinite level. Level 0 means standard view without flattened directory view. Level values \-2 and less are invalid. .IP "grep \fIpattern\fR" 2 .IX Item "grep pattern" Looks for a string in all marked files or directories. .IP "linemode \fIlinemodename\fR" 2 .IX Item "linemode linemodename" Sets the linemode of all files in the current directory. The linemode may be: .Sp .Vb 5 \& "filename": display each line as "<basename>...<size>" \& "permissions": display each line as "<permissions> <owner> <group> <basename>" \& "metatitle": display metadata from .metadata.json files if \& available, fall back to the "filename" linemode if no \& metadata was found. See :meta command. .Ve .IP "load_copy_buffer" 2 .IX Item "load_copy_buffer" Load the copy buffer from \fI~/.config/ranger/copy_buffer\fR. This can be used to pass the list of copied files to another ranger instance. .IP "map \fIkey\fR \fIcommand\fR" 2 .IX Item "map key command" Assign the key combination to the given command. Whenever you type the key/keys, the command will be executed. Additionally, if you use a quantifier when typing the key, like 5j, it will be passed to the command as the attribute \&\*(L"self.quantifier\*(R". .Sp The keys you bind with this command are accessible in the file browser only, not in the console, task view or pager. To bind keys there, use the commands \&\*(L"cmap\*(R", \*(L"tmap\*(R" or \*(L"pmap\*(R". .IP "mark \fIpattern\fR" 2 .IX Item "mark pattern" Mark all files matching the regular expression pattern. .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "mark_tag [\fItags\fR]" 2 .IX Item "mark_tag [tags]" Mark all tags that are tagged with either of the given tags. When leaving out the tag argument, all tagged files are marked. .IP "meta \fIkey\fR \fIvalue\fR" 2 .IX Item "meta key value" Set the metadata of the currently highlighted file. Example: .Sp .Vb 2 \& :meta title The Hitchhiker\*(Aqs Guide to the Galaxy \& :meta year 1979 .Ve .Sp This metadata can be displayed by, for example, using the \*(L"metatitle\*(R" line mode by typing Mt. .IP "mkdir \fIdirname\fR" 2 .IX Item "mkdir dirname" Creates a directory with the name \fIdirname\fR. .IP "open_with [\fIapplication\fR] [\fIflags\fR] [\fImode\fR]" 2 .IX Item "open_with [application] [flags] [mode]" Open the selected files with the given application, unless it is omitted, in which case the default application is used. \fIflags\fR change the way the application is executed and are described in their own section in this man page. The \fImode\fR is a number that specifies which application to use. The list of applications is generated by the external file opener \*(L"rifle\*(R" and can be displayed when pressing \*(L"r\*(R" in ranger. .Sp Note that if you specify an application, the mode is ignored. .IP "pmap \fIkey\fR \fIcommand\fR" 2 .IX Item "pmap key command" Binds keys for the pager. Works like the \f(CW\*(C`map\*(C'\fR command. .IP "prompt_metadata [\fIkeys ...\fR]" 2 .IX Item "prompt_metadata [keys ...]" Prompt the user to input metadata with the \f(CW\*(C`meta\*(C'\fR command for multiple keys in a row. .IP "punmap [\fIkeys ...\fR]" 2 .IX Item "punmap [keys ...]" Removes key mappings of the pager. Works like the \f(CW\*(C`unmap\*(C'\fR command. .IP "quit" 2 .IX Item "quit" Like quit!, but closes only this tab if multiple tabs are open. .IP "quit!" 2 .IX Item "quit!" Quit ranger. The current directory will be bookmarked as ' so you can re-enter it by typing `` or '' the next time you start ranger. .IP "relink \fInewpath\fR" 2 .IX Item "relink newpath" Change the link destination of the current symlink file to <newpath>. First <tab> will load the original link. .IP "rename_append" 2 .IX Item "rename_append" Opens the console with \*(L":rename <current file>\*(R" with the cursor automatically placed before the file extension .IP "rename \fInewname\fR" 2 .IX Item "rename newname" Rename the current file. If a file with that name already exists, the renaming will fail. Also try the key binding A for appending something to a file name. .IP "save_copy_buffer" 2 .IX Item "save_copy_buffer" Save the copy buffer to \fI~/.config/ranger/copy_buffer\fR. This can be used to pass the list of copied files to another ranger instance. .IP "scout [\-\fIflags\fR...] [\-\-] \fIpattern\fR" 2 .IX Item "scout [-flags...] [--] pattern" Swiss army knife command for searching, traveling and filtering files. The command takes various flags as arguments which can be used to influence its behaviour: .Sp .Vb 10 \& \-a = automatically open a file on unambiguous match \& \-e = open the selected file when pressing enter \& \-f = filter files that match the current search pattern \& \-g = interpret pattern as a glob pattern \& \-i = ignore the letter case of the files \& \-k = keep the console open when changing a directory with the command \& \-l = letter skipping; e.g. allow "rdme" to match the file "readme" \& \-m = mark the matching files after pressing enter \& \-M = unmark the matching files after pressing enter \& \-p = permanent filter: hide non\-matching files after pressing enter \& \-s = smart case; like \-i unless pattern contains upper case letters \& \-t = apply filter and search pattern as you type \& \-v = inverts the match .Ve .Sp Multiple flags can be combined. For example, \*(L":scout \-gpt\*(R" would create a :filter\-like command using globbing. .IP "search \fIpattern\fR" 2 .IX Item "search pattern" Search files in the current directory that match the given (case insensitive) regular expression pattern. .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "search_inc \fIpattern\fR" 2 .IX Item "search_inc pattern" Search files in the current directory that match the given (case insensitive) regular expression pattern. This command gets you to matching files as you type. .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "set \fIoption\fR \fIvalue\fR" 2 .IX Item "set option value" Assigns a new value to an option. Valid options are listed in the settings section. Use tab completion to get the current value of an option, though this doesn't work for functions and regular expressions. Valid values are: .Sp .Vb 7 \& setting type | example values \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& bool | true, false \& integer | 1, 23, 1337 \& string | foo, hello world \& list | 1,2,3,4 \& none | none .Ve .IP "setlocal [path=\fIpath\fR] \fIoption\fR \fIvalue\fR" 2 .IX Item "setlocal [path=path] option value" Assigns a new value to an option, but locally for the directory given by \&\fIpath\fR. This means, that this option only takes effect when visiting that directory. If no path is given, uses the current directory. .IP "shell [\-\fIflags\fR] \fIcommand\fR" 2 .IX Item "shell [-flags] command" Run a shell command. \fIflags\fR are discussed in their own section. .IP "terminal" 2 .IX Item "terminal" Spawns the \fIx\-terminal-emulator\fR starting in the current directory. .IP "tmap \fIkey\fR \fIcommand\fR" 2 .IX Item "tmap key command" Binds keys for the taskview. Works like the \f(CW\*(C`map\*(C'\fR command. .IP "touch \fIfilename\fR" 2 .IX Item "touch filename" Creates an empty file with the name \fIfilename\fR, unless it already exists. .IP "travel \fIpattern\fR" 2 .IX Item "travel pattern" Filters the current directory for files containing the letters in the string, possibly with other letters in between. The filter is applied as you type. When only one directory is left, it is entered and the console is automatically reopened, allowing for fast travel. To close the console, press \s-1ESC\s0 or execute a file. .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "tunmap [\fIkeys ...\fR]" 2 .IX Item "tunmap [keys ...]" Removes key mappings of the taskview. Works like the \f(CW\*(C`unmap\*(C'\fR command. .IP "unmap [\fIkeys\fR ...]" 2 .IX Item "unmap [keys ...]" Removes the given key mappings in the \*(L"browser\*(R" context. To unmap key bindings in the console, taskview, or pager use \*(L"cunmap\*(R", \*(L"tunmap\*(R" or \*(L"punmap\*(R". .IP "unmark \fIpattern\fR" 2 .IX Item "unmark pattern" Unmark all files matching a regular expression pattern. .Sp This command is based on the \fIscout\fR command and supports all of its options. .IP "unmark_tag [\fItags\fR]" 2 .IX Item "unmark_tag [tags]" Unmark all tags that are tagged with either of the given tags. When leaving out the tag argument, all tagged files are unmarked. .SH "FILES" .IX Header "FILES" ranger reads several configuration files which are located in \&\fI\f(CI$HOME\fI/.config/ranger\fR or \fI\f(CI$XDG_CONFIG_HOME\fI/ranger\fR if \f(CW$XDG_CONFIG_HOME\fR is defined. You can use the \-\-copy\-config option to obtain the default configuration files. Each of the files contains further documentation. .PP You don't need to copy the whole file though, most configuration files are overlaid on top of the defaults (\fIcommands.py\fR, \fIrc.conf\fR) or can be sub-classed (\fIcolorschemes\fR). .PP When starting ranger with the \fB\-\-clean\fR option, it will not access or create any of these files. .SS "\s-1CONFIGURATION\s0" .IX Subsection "CONFIGURATION" .IP "rc.conf" 10 .IX Item "rc.conf" Contains a list of commands which are executed on startup. Mostly key bindings and settings are defined here. .IP "commands.py" 10 .IX Item "commands.py" A python module that defines commands which can be used in ranger's console by typing \*(L":\*(R" or in the rc.conf file. .IP "rifle.conf" 10 .IX Item "rifle.conf" This is the configuration file for the built-in file launcher called \*(L"rifle\*(R". .IP "scope.sh" 10 .IX Item "scope.sh" This is a script that handles file previews. When the options \&\fIuse_preview_script\fR and \fIpreview_files\fR are set, the program specified in the option \fIpreview_script\fR is run and its output and/or exit code determines rangers reaction. .IP "colorschemes/" 10 .IX Item "colorschemes/" Colorschemes can be placed here. .IP "plugins/" 10 .IX Item "plugins/" Plugins can be placed here. .SS "\s-1STORAGE\s0" .IX Subsection "STORAGE" .IP "bookmarks" 10 .IX Item "bookmarks" This file contains a list of bookmarks. The syntax is /^(.):(.*)$/. The first character is the bookmark key and the rest after the colon is the path to the file. In ranger, bookmarks can be set by typing m<key>, accessed by typing \&'<key> and deleted by typing um<key>. .IP "copy_buffer" 10 .IX Item "copy_buffer" When running the command :save_copy_buffer, the paths of all currently copied files are saved in this file. You can later run :load_copy_buffer to copy the same files again, pass them to another ranger instance or process them in a script. .IP "history" 10 .IX Item "history" Contains a list of commands that have been previously typed in. .IP "tagged" 10 .IX Item "tagged" Contains a list of tagged files. The syntax is /^(.:)?(.*)$/ where the first letter is the optional name of the tag and the rest after the optional colon is the path to the file. In ranger, tags can be set by pressing t and removed with T. To assign a named tag, type "<tagname>. .SH "ENVIRONMENT" .IX Header "ENVIRONMENT" These environment variables have an effect on ranger: .IP "\s-1RANGER_LEVEL\s0" 8 .IX Item "RANGER_LEVEL" ranger sets this environment variable to \*(L"1\*(R" or increments it if it already exists. External programs can determine whether they were spawned from ranger by checking for this variable. .IP "\s-1RANGER_LOAD_DEFAULT_RC\s0" 8 .IX Item "RANGER_LOAD_DEFAULT_RC" If this variable is set to \s-1FALSE,\s0 ranger will not load the default rc.conf. This can save time if you copied the whole rc.conf to ~/.config/ranger/ and don't need the default one at all. .IP "\s-1EDITOR\s0" 8 .IX Item "EDITOR" Defines the editor to be used for the \*(L"E\*(R" key. Defaults to \*(L"nano\*(R". .IP "\s-1SHELL\s0" 8 .IX Item "SHELL" Defines the shell that ranger is going to use with the :shell command and the \*(L"S\*(R" key. Defaults to \*(L"/bin/sh\*(R". .IP "\s-1TERMCMD\s0" 8 .IX Item "TERMCMD" Defines the terminal emulator command that ranger is going to use with the :terminal command and the \*(L"t\*(R" run flag. Defaults to \*(L"xterm\*(R". .IP "\s-1XDG_CONFIG_HOME\s0" 8 .IX Item "XDG_CONFIG_HOME" Specifies the directory for configuration files. Defaults to \fI\f(CI$HOME\fI/.config\fR. .IP "\s-1PYTHONOPTIMIZE\s0" 8 .IX Item "PYTHONOPTIMIZE" This variable determines the optimize level of python. .Sp Using PYTHONOPTIMIZE=1 (like python \-O) will make python discard assertion statements. You will gain efficiency at the cost of losing some debug info. .Sp Using PYTHONOPTIMIZE=2 (like python \-OO) will additionally discard any docstrings. Using this will disable the <F1> key on commands. .IP "W3MIMGDISPLAY_PATH" 8 .IX Item "W3MIMGDISPLAY_PATH" By changing this variable, you can change the path of the executable file for image previews. By default, it is set to \fI/usr/lib/w3m/w3mimgdisplay\fR. .SH "EXAMPLES" .IX Header "EXAMPLES" There are various examples on how to extend ranger with plugins or combine ranger with other programs. These can be found in the \&\fI/usr/share/doc/ranger/examples/\fR directory, or the \fIdoc/ranger/\fR that is provided along with the source code. .SH "LICENSE" .IX Header "LICENSE" \&\s-1GNU\s0 General Public License 3 or (at your option) any later version. .SH "LINKS" .IX Header "LINKS" .IP "Download: <http://ranger.nongnu.org/ranger\-stable.tar.gz>" 4 .IX Item "Download: <http://ranger.nongnu.org/ranger-stable.tar.gz>" .PD 0 .IP "The project page: <http://ranger.nongnu.org/>" 4 .IX Item "The project page: <http://ranger.nongnu.org/>" .IP "The mailing list: <http://savannah.nongnu.org/mail/?group=ranger>" 4 .IX Item "The mailing list: <http://savannah.nongnu.org/mail/?group=ranger>" .IP "\s-1IRC\s0 channel: #ranger on freenode.net" 4 .IX Item "IRC channel: #ranger on freenode.net" .PD .PP ranger is maintained with the git version control system. To fetch a fresh copy, run: .PP .Vb 1 \& git clone git://git.savannah.nongnu.org/ranger.git .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fIrifle\fR\|(1) .SH "BUGS" .IX Header "BUGS" Report bugs here: <https://github.com/hut/ranger/issues> .PP Please include as much relevant information as possible. For the most diagnostic output, run ranger like this: \f(CW\*(C`PYTHONOPTIMIZE= ranger \-\-debug\*(C'\fR