Repository navigation
Expand file tree
/
Copy pathrules.scm
More file actions
179 lines (179 loc) · 6.15 KB
/
Copy pathrules.scm
File metadata and controls
179 lines (179 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(define-library (gfcode plan-mode rules)
(export safe-command? plan-lines step-done? plan-complete? planning-tools)
(import (scheme base)
(scheme char)
(only (srfi srfi-13) string-prefix?)
(liii string)
(liii list)
) ;import
(begin
;; A small shell grammar, not a blacklist. Unknown programs, switches and
;; expansions are rejected. Quote delimiters never start a new command.
(define (read-only? argv)
(and (pair? argv)
(let ((cmd (car argv)) (args (cdr argv)))
(cond ((member cmd '("pwd" "whoami")) (null? args))
((member cmd '("ls" "cat" "wc" "head" "tail"))
(every
(lambda (a)
(or (not (string-prefix? "-" a)) (member a '("-l" "-a"
"-la" "-n")))
) ;lambda
args
) ;every
) ;
((equal? cmd "git")
(member args
'(("status")
("status" "--short")
("diff")
("diff" "--stat")
("log")
("log" "--oneline")
("branch")
("remote" "-v")
("ls-files"))
) ;member
) ;
(else #f)
) ;cond
) ;let
) ;and
) ;define
(define (safe-command? input)
(define (push-word chars words)
(if (null? chars) words (cons (list->string (reverse chars)) words))
) ;define
(define (plain? ch)
(or (char-alphabetic? ch) (char-numeric? ch) (memv ch '(#\/ #\. #\- #\_
#\:)))
) ;define
(define (scan cs quote-char word words)
(let ((argv (lambda () (reverse (push-word word words)))))
(cond
((null? cs) (and (not quote-char) (read-only? (argv)) #t))
((plain? (car cs)) (scan (cdr cs) quote-char (cons (car cs) word) words))
((and quote-char (eqv? quote-char (car cs))) (scan (cdr cs) #f word words))
((and (not quote-char) (memv (car cs) '(#\' #\")))
(scan (cdr cs) (car cs) word words)
) ;
((eqv? (car cs) #\space)
(if quote-char
(scan (cdr cs) quote-char (cons #\space word) words)
(scan (cdr cs) #f '() (push-word word words))
) ;if
) ;
((and (not quote-char) (memv (car cs) '(#\| #\&)) (read-only? (argv)))
(cond
((and (pair? (cdr cs)) (eqv? (car cs) (cadr cs))) (scan (cddr cs) #f '() '()))
((eqv? (car cs) #\|) (scan (cdr cs) #f '() '()))
(else #f)
) ;cond
) ;
(else #f)
) ;cond
) ;let
) ;define
(and (string? input) (scan (string->list input) #f '() '()))
) ;define
(define (planning-tools tools)
(filter (lambda (n)
(member n '("read" "ls" "find" "grep" "bash" "questionnaire"
"web_search"))
) ;lambda
tools
) ;filter
) ;define
(define (numbered-step line)
(let ((i (string-skip line char-numeric?)) (n (string-length line)))
(and i
(> i 0)
(< (+ i 2) n)
(memv (string-ref line i) '(#\. #\)))
(char-whitespace? (string-ref line (+ i 1)))
(string-trim-both (substring line (+ i 2) n))
) ;and
) ;let
) ;define
(define (plan-lines text)
(let loop
((ls (if (string? text) (string-split text "\n") '()))
(previous '())
(current #f)
) ;
(if (null? ls)
(if (pair? current) (reverse current) previous)
(let* ((line (string-trim-both (car ls))) (step (and current (numbered-step line))))
(cond
((member (string-downcase line) '("plan:" "**plan:**" "**plan**:"))
(loop (cdr ls) (if (pair? current) (reverse current) previous) '())
) ;
(step (loop (cdr ls) previous (cons step current)))
(else (loop (cdr ls) previous current))
) ;cond
) ;let*
) ;if
) ;let
) ;define
(define (words text)
(string-tokenize
(list->string
(map (lambda (c) (if (char-alphanumeric? c) (char-downcase c) #\space))
(string->list text)
) ;map
) ;list->string
) ;string-tokenize
) ;define
(define (char-alphanumeric? c)
(or (char-alphabetic? c) (char-numeric? c))
) ;define
(define (starts-with-words? xs prefix)
(or (null? prefix)
(and (pair? xs)
(equal? (car xs) (car prefix))
(starts-with-words? (cdr xs) (cdr prefix))
) ;and
) ;or
) ;define
(define (contains-phrase? xs phrases)
(and (pair? xs)
(or (any (lambda (p) (starts-with-words? xs p)) phrases)
(contains-phrase? (cdr xs) phrases)
) ;or
) ;and
) ;define
(define (step-done? text n)
(and (string? text)
(let ((num (number->string n)))
(contains-phrase? (words text)
(cons (list "done" num)
(apply append
(map (lambda (word) (list (list "step" num word) (list "step" num "is" word)))
'("done" "complete" "completed" "finished")
) ;map
) ;apply
) ;cons
) ;contains-phrase?
) ;let
) ;and
) ;define
(define (plan-complete? text)
(and (string? text)
(contains-phrase? (words text)
(apply append
(map (lambda (word)
(list (list "plan" "is" word)
(list "plan" word)
(list "all" "steps" "are" word)
(list "all" "steps" word)
(list "every" "step" "is" word)
) ;list
) ;lambda
'("done" "complete" "completed" "finished")
) ;map
) ;apply
) ;contains-phrase?
) ;and
) ;define
) ;begin
) ;define-library