Perl で作る tiny shell

60e652d0244f47d2da4e1f7e712f9328?s=47 K
June 23, 2018

Perl で作る tiny shell

Perl で極限に機能を削った shell の作り方を紹介します

60e652d0244f47d2da4e1f7e712f9328?s=128

K

June 23, 2018
Tweet

Transcript

  1. 1FSMͰ࡞Δ
 UJOZTIFMM )PLLBJEPQN

  2. !DPEFIFY !$PEF)FY w .FSDBSJ *OD w %FWFMPQJOHNJDSPTFSWJDFT w 0LJOBXBQN w

    *MJLF1FSM94(P w IUUQTDPEFIFYIBUFCMPKQ
  3. γΣϧͬͯ஌ͬͯ·͔͢

  4. None
  5. /P

  6. $ pwd


  7. $ pwd
 /Users/codehex

  8. #BTI ;TI pTI

  9. γΣϧʹ͸
 ༷ʑͳػೳ͕͋Γ·͢

  10. ྫ͑͹

  11. ύΠϓ

  12. $ pwd | cat -n


  13. $ pwd | cat -n
 1 /Users/codehex

  14. Ξϯυ

  15. $ pwd && echo hello


  16. $ pwd && echo hello
 /Users/codehex
 hello

  17. ͜ΕΒͷ
 ࣮૷Λ஌͍ͬͯ·͔͢ʁ

  18. ࠓ೔࿩͢಺༰

  19. ࠓ೔࿩͢಺༰ w ࠷௿ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ

  20. ࠓ೔࿩͢಺༰ w ࠷௿ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ

  21. ࠷௿ݶͷγΣϧͷఆٛ UJOZTIFMMͷಈ࡞Λఆٛ͢Δ

  22. lUJOZTIFMMzNFBOT
 ΊͬͪΌখ͍͞γΣϧ

  23. Ͳ͏͍͏ಈ࡞Λ͢Δ͔

  24. Ͳ͏͍͏ಈ࡞Λ͢Δ͔ w ୯ҰίϚϯυͷ࣮ߦ $ ls  w ύΠϓͷ࣮ߦ $ ls

    | cat  w ΞϯυʹΑΔෳ਺࣮ߦ $ ls && cat  w αϒγΣϧ΍όοΫάϥϯυ࣮ߦ͸ࠓճؚΊ·ͤΜ
  25. ࠓ೔࿩͢಺༰ w ࠷௿ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ

  26. ߏจղੳ ެจఆٛͱߏจղੳثͷ࡞੒

  27. $ cat | cat -n && ls

  28. $ cat | cat -n && ls

  29. $ cat | cat -n && ls

  30. $ cat | cat -n && ls

  31. $ cat | cat -n && ls

  32. $ cat | cat -n && ls ࠨ͔Βӈʹղऍ͠·͢

  33. ߏจͷఆٛ

  34. ӈʹ࿈͍݁ͯ͘͠Α͏ʹ
 ఆٛ෇͚Λߦ͏

  35. ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |

    [0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
  36. ߏจղੳثͷ࡞੒

  37. ߏจղੳثʹඞཁͳཁૉ

  38. ߏจղੳثʹඞཁͳཁૉ w จࣈྻͷεΩϟφʔ w ಡΈࠐΈҐஔ΍ಡΈऔͬͨจࣈྻΛอ࣋͢Δ w จࣈྻͷϚονϯά w εΩϟφʔͰಘͨจࣈྻ͕ߏจఆٛʹϚονͨ͠ ͔Ͳ͏͔൑ผ͢Δ

    w Ϛονͨ͠จࣈྻʹҙຯΛ΋ͨͤΔ τʔΫϯԽ  w ߏจ໦ͷੜ੒
  39. 1FSMͰ͸ʁ

  40. ਖ਼نදݱ

  41. IUUQTQFSMEPDQFSMPSHQFSMPQIUNMD(@BTTFSUJPO@ l"VTFGVMJEJPNGPSAlexAMJLF
 TDBOOFSTJTA/\G…/gcAʜz

  42. /\G…/gc

  43. /\G…/gc w A/cANPEJpFSͰ͸ݱࡏϚον͍ͯ͠ΔҐஔΛ
 อ࣋͢Δ͜ͱ͕Ͱ͖Δɻ • pos()ͰҐஔΛऔΓग़͢͜ͱ͕Մೳ • \GͰલճAm//gAͰϚονͨ͠ҐஔΛอ࣋͢Δ͜ͱ ͕Մೳ

  44. εΩϟφʔͱϚονϯάͷ
 ໾ׂΛ΋ͭ͜ͱ͕Ͱ͖Δ ?P? W

  45. ͜͜Ͱྑ͍৘ใ

  46. ΊͬͪΌࢀߟʹͳΔϖʔδ w IUUQTHJUIVCDPNLBSVQBOFSVSBQ +40/CMPCNBTUFSMJC+40/1BSTFSQN w IUUQTHJUIVCDPNLBSVQBOFSVSB50.- 1BSTFSCMPCNBTUFSMJC50.-1BSTFSQN

  47. ࣮૷༷ࢠ

  48. ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |

    [0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
  49. $.%

  50. ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |

    [0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
  51. 4)--

  52. ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |

    [0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
  53. 1*1&

  54. ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |

    [0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
  55. "/%

  56. ໦ΛQSJOU༷ͨ͠ࢠ $ cat | cat -n && ls

  57. "/% 1*1& DBU DBU MT $ cat | cat -n

    && ls
  58. ͪͳΈʹ

  59. $ cat | cat -n && ls

  60. $ cat | cat -n && ls ࠨ͔Βӈʹղऍ͠·͢

  61. ͳͷͰ
 ໦ΛḷΔͱ͖͸

  62. "/% 1*1& DBU DBU MT $ cat | cat -n

    && ls ᶃ
  63. "/% 1*1& DBU DBU MT $ cat | cat -n

    && ls ᶃ ᶄ ᶅ
  64. "/% 1*1& DBU DBU MT $ cat | cat -n

    && ls ᶃ ᶄ ᶅ ᶆ ᶇ
  65. ͜ΕΛجʹ
 ࣮ߦ͢ΔػೳΛ࡞੒͠·͢

  66. ࠓ೔࿩͢಺༰ w ࠷௿ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ

  67. ίϚϯυͷ࣮ߦ ߏจ໦Λجʹ࣮ߦ͢Δ

  68. ࣮૷ͷલʹ

  69. ඪ४ετϦʔϜʹ͍ͭͯ

  70. w 45%*/ 4UBOEBSE*OQVU ඪ४ೖྗ w 45%065 4UBOEBSE0VUQVU ඪ४ग़ྗ w 45%&33

    4UBOEBSE&SSPS ඪ४Τϥʔग़ྗ ඪ४ετϦʔϜ
  71. ͜ΕΒ͸࣮࣭
 ϑΝΠϧͱͯ͠ѻΘΕ·͢

  72. ୯ҰίϚϯυͷ
 ετϦʔϜͷϑϩʔ

  73. $ cat

  74. $ cat cat 45%*/

  75. $ cat cat 45%*/ 45%065 45%&33

  76. $ cat GPSLΛ࢖͏ͱTIΛհ͞ͳ͍Ͱ࣮ߦՄೳ

  77. ύΠϓͷϑϩʔ

  78. $ fortune | cowsay

  79. $ fortune | cowsay 45%*/ PS/PU ᶃ

  80. $ fortune | cowsay 45%*/ PS/PU 45%065 ᶃ ᶄ

  81. $ fortune | cowsay 45%*/ PS/PU 45%065 45%*/ ᶃ ᶄ

  82. $ fortune | cowsay 45%*/ PS/PU 45%065 45%*/ 45%065 ᶃ

    ᶄ ᶅ ᶆ
  83. ͜ΕΒͷίϚϯυ͸
 ผϓϩηεͰಈ͍͍ͯ·͢

  84. $ ps a | cat 1*%

  85. ύΠϓͷ࣮૷

  86. pipe my ($r, $w);

  87. w $r͕3FBEFS $w͕8SJUFS w 8SJUFSʹσʔλ͕ॻ͖ࠐ·Εͨͱ͖
 3FBEFSͰͦͷσʔλΛಡΈऔΔ͜ͱ͕Մೳ pipe my ($r, $w);

  88. ֤ϓϩηεؒͷετϦʔϜΛ
 pipe()Ͱܨ͛Δ͜ͱͰ࣮૷Մೳ

  89. ͦͷ࣌ͷϑϩʔ

  90. $ fortune | cowsay 45%*/ PS/PU 45%065 ᶃ ᶄ

  91. $ fortune | cowsay 45%*/ PS/PU 45%065 ᶃ ᶄ ᶅ

    ᶆ 8SJUFS 3FBEFS
  92. $ fortune | cowsay 45%*/ PS/PU 45%065 45%*/ 45%065 ᶃ

    ᶄ ᶅ ᶆ 8SJUFS 3FBEFS ᶇ ᶈ ᶉ
  93. ͳͷͰ࣮࣭
 ͜ͷΑ͏ʹߟ͑ΒΕΔ

  94. $ fortune | cowsay 45%*/ PS/PU 8SJUFS 3FBEFS 45%065 ᶃ

    ᶄ ᶅ ᶆ
  95. None
  96. w ίϚϯυOPEFͰ͔ͭ਌OPEF͕ύΠϓͰ͸ͳ͍
 ৔߹͸ଈίϚϯυΛ࣮ߦ w ͦΕҎ֎͸ίϚϯυΛอ࣋͢Δ w ύΠϓOPEF͔ͭ਌͕ύΠϓOPEFͰ͸ͳ͍৔߹
 อ࣋͠ଓ͚ͨίϚϯυΛ࣮ߦ

  97. Ξϯυͷϑϩʔ

  98. $ echo hello && echo world

  99. $ echo hello && echo world ᶃ

  100. $ echo hello && echo world ᶃ ᶄ

  101. w ΞϯυOPEFʹདྷΔ·ͰʹࠨͷOPEFΛ
 શ࣮ͯߦ͢Δ w ੒ޭͨ͠৔߹ӈͷOPEFΛࠨͷOPEFͱಉ͡Α͏ ʹ࣮ߦ͢Δ

  102. ͜Ε͚ͩͰ
 UJOZTIFMM͕׬੒͠·͢ʂ

  103. ·ͱΊ

  104. ·ͱΊ w ӈʹ࿈͍݁ͯ͘͠Α͏ʹߏจఆٛͨ͠ w γΣϧͷύΠϓɺΞϯυػೳΛཧղͯ͠ɺղੳ࣌ʹ ࣮ߦ͢ΔػೳΛ࡞੒ͨ͠

  105. ࣭໰Ͳ͏ͧ https://github.com/Code-Hex/p5-Shell-Tiny