-
Notifications
You must be signed in to change notification settings - Fork 0
/
lishp.hy
executable file
·142 lines (113 loc) · 2.92 KB
/
lishp.hy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/env hy
(import os sys re)
(import [signal :as sign])
;; shell builtins
(defn echo [args]
(print (remove-quotes args)))
(defn clear [args]
(print "\033c"))
(defn pwd [args]
(print (.getcwd os)))
(defn cd [args]
(.chdir os args))
;; globals
(setv *sys-path* (.split (.getenv os "PATH") ":"))
(setv *username* (.getenv os "LOGNAME"))
(setv *nodename* (get (.uname os) 1))
(setv *prompt* (+ *username* "@" *nodename* "% "))
(setv *msg* (+ "Lishp shell v0.0.1 running on "
(get (.uname os) 0) " "
(get (.uname os) 4) "\n"))
(setv *cmds*
{"echo" echo
"clear" clear
"pwd" pwd
"cd" cd})
;; helper functions
(defn lower [str]
(.lower str))
(defn strip [str]
(.strip str))
(defn remove-quotes [str]
(-> str
(.strip "\"")
(.strip "\'")
(.replace "\"" " ")
(.replace "\'" " ")))
(defn exists-in-path? [cmd]
(setv result False)
(for [path *sys-path*]
(if (.isfile (. os path) (+ path "/" cmd))
(setv result True)
False))
result)
(defn get-absolute-path [cmd]
(setv abs-path "")
(for [path *sys-path*]
(if (.isfile (. os path) (+ path "/" cmd))
(setv abs-path (+ path "/" cmd))
False))
abs-path)
(defn split-arg-list [args]
(if args
(->> (.split args " ")
(filter (fn [arg] (not (= arg ""))))
(list))
[""]))
(defn arg-list-not-empty? [args]
(if (get args 0)
True
False))
(defn get-final-args [cmd args]
(setv arg-list (split-arg-list args))
(if (arg-list-not-empty? arg-list)
(+ [cmd] () arg-list)
[cmd]))
;; handle interrupt signals
(defn sig-handler [sig frame]
(cond
[(= sig (. sign *sigint*)) (print "^C")]
[(= sig (. sign *sigtstp*)) (print "^Z")]))
;; Interrupt signal
((. sign signal) (. sign *sigint*) sig-handler)
((. sign signal) (. sign *sigtstp*) sig-handler)
;; process input
(defn tokenize-arg [arg]
(setv patt (.compile re "(\w+)(.*)"))
(-> (.findall patt arg)
(get 0)
(list)))
;; TODO: only lower cmd not args
(defn lower-list [tokens]
(list (map lower tokens)))
(defn strip-list [tokens]
(list (map strip tokens)))
(defn exec [cmd args]
(setv pid (.fork os))
(if (= pid 0)
(.execvp os
cmd
(get-final-args cmd args))
(.waitpid os pid 0)))
(defn exec-cmd [tokens]
(setv cmd (get tokens 0))
(setv args (get tokens 1))
(cond [(in cmd *cmds*) ((get *cmds* cmd) args)]
[(exists-in-path? cmd) (exec cmd args)]
[(= 1 1) (print (+ "lishp: command '" cmd "' not found."))]))
(defn process-arg [arg]
(cond [(or (= arg "e") (= arg "exit"))
(.exit sys 0)]
[(not (strip arg)) (+ 1 1)]
[(= 1 1) (-> arg
(tokenize-arg)
(lower-list)
(strip-list)
(exec-cmd))]))
;; start lishp
(print *msg*)
(defn main []
(setv arg (input *prompt*))
(process-arg arg))
(while True
(main))