-
Notifications
You must be signed in to change notification settings - Fork 0
/
lys-server
280 lines (240 loc) · 8.02 KB
/
lys-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
% lys - lilypond compile server
% Uses lilypond to compile file submitted with lyc, the lys client program.
% Notes
% This is lilypond code, not Scheme, but it is composed entirely of guile
% procedures. This will run using the guile version 1.8 that is installed
% locally inside the lilypond distribution.
\version "2.19.40"
#(begin
;; TCP server side of lilypond compile server
(use-modules
(ice-9 format)
(ice-9 rdelim)
(ice-9 receive)
(ice-9 string-fun)
(srfi srfi-1)
(srfi srfi-37)
(local timing)
(local path)
(local print)
(local getopt-long-acb)
(local typed-opts))
;; program version
(define version "0.1")
;; port
(define port-number 12000)
(define (main args)
;; process server args
(let* ((opt-spec
'((version (single-char #\v) (value #f))
(help (single-char #\h) (value #f))
(port (single-char #\p) (value #t))
(daemon (single-char #\d) (value #f))))
;; catch any option processing errors.
(opts
(catch #t
(lambda ()
(getopt-long args opt-spec))
(lambda (key . parameters)
(begin
(format
#t "option processing error: ~a: ~a\n" key parameters)
(primitive-exit 1)))))
(help-set (option-ref opts 'help #f))
(version-set (option-ref opts 'version #f))
(daemon-set (option-ref opts 'daemon #f))
(port (inexact->exact (string->number (option-ref opts 'port (number->string port-number)))))
)
(prnt "port " port)
(prnt "args: " args)
(prnt opt-spec)
(prnt opts)
(if help-set
(begin
(show-usage)
(primitive-exit 0)))
(if version-set
(begin
(show-version)
(primitive-exit 0)))
;; tcp server
(let ((sock-server (socket PF_INET SOCK_STREAM 0)))
(setsockopt sock-server SOL_SOCKET SO_REUSEADDR 1)
(bind sock-server AF_INET INADDR_ANY port)
(listen sock-server 5)
(format #t "lilypond compile server listening on port ~a\n" port)
(prnt port)
(while #t
(let* ((connection (accept' sock-server))
(sock-client (car connection))
(client-details (cdr connection)))
(format #t "client connection from host: ~a\n"
(hostent:name
(gethostbyaddr (sockaddr:addr client-details))))
(fork-process-twice sock-client)
)))))
;; accept that handles EINTR
(define accept'
(lambda (sock)
(let loop ()
(catch 'system-error
(lambda () (accept sock))
(lambda (key . parameters)
(begin
;;(format #t "key ~a parameters ~a\n" key parameters)
;; EINTR is normal and expected here
(loop) ;; try again after EINTR
))))))
;; fork child. double fork to decouple fully.
(define fork-process-twice
(lambda (sock)
(let* ((pid (primitive-fork)))
(if (zero? pid)
(begin
(let* ((pid (primitive-fork)))
(if (zero? pid) (child-process sock)) ;; run in grandchild
(primitive-exit) ;; child exit
))
(waitpid pid)))))
;; child process to do the work
(define child-process
(lambda (sock)
;; read from the client
;; In order to output in the directory the client is run from
;; the client sends us its working directory first.
;; Then we get the lilypond options, and next the filename, a line at a time.
(let* ((client-working-dir (read-line sock 'trim))
(lilypond-args (string-split (read-line sock 'trim) #\space))
(filename (read-line sock 'trim))
(start-time (start-timer))
(current-dir (getcwd))
(dir (dirname filename))
(file (basename filename))
)
(format #t "current working directory for server: ~a\n" current-dir)
(format #t "current working directory for client: ~a\n" client-working-dir)
(format #t "lilypond args: ~a\n" lilypond-args)
(format #t "args is list? ~a\n" (list? lilypond-args))
(format #t "args is string? ~a\n" (string? lilypond-args))
;; (format #t "string-split ~a\n" (list? (string-split lilypond-args #\space)))
;; (format #t "car list: ~a\n" (car lilypond-args))
(format #t "file: ~a\n" filename)
(format #t "dir: ~a\n" dir)
(format #t "file: ~a\n" file)
(chdir client-working-dir)
;; lilypond outputs messages on stderr.
;; send all output to the client.
(redirect-port sock (current-error-port))
(catch #t
(lambda ()
(begin
;;(ly:set-option 'verbose #t)
;; do the main work - compile the source file.
(ly:parse-file filename)
))
(lambda (key . parameters)
(begin
(format #t "caught lilypond processing error: ~a: ~a\n" key parameters)
(shutdown sock 1)
(primitive-exit 1)
)))
(chdir current-dir)
;; send compile time to client
(let* ((time-taken (elapsed-time start-time (stop-timer))))
(format
sock "Compile completed in ~a\n" (format-elapsed-time time-taken)))
;; exit
(shutdown sock 2)
(close-port sock)
(primitive-exit 0))))
;; Process command line arguments and options.
;;
;; Uses SRFI-37 args-fold.
;; The args-fold function takes a list of seeds at the end which represent
;; state as the last arguments. This gets unwieldy quickly, so we use a single
;; alist to make the code more compact and readable.
(define process-args
(lambda args
(let* (
;; options alist
;; settings here are the defaults for the option values
(opts '(
(includes () std #f)
(files () std #f) ;; all the non option items (operands)
(help #f std #f)
(version #f std #f)
(dbackend "ps" minus-d #f)
(dloglevel 1 minus-d #f)
(dwarning-as-error #f minus-d #f)
))
;; process the options with args-fold
(opts (args-fold
;; args
args
;; options list
(let ((call-proc-and-exit
(lambda (p)
(lambda (option name arg opts)
(p)
(primitive-exit 0)
))))
(list
(option '(#\I "include") #t #f
(lambda (option name arg opts)
(set-typed-option-field!
opts 'includes opt-val (cons arg (get-typed-option-field opts 'includes opt-val)))
(set-typed-option-field! opts 'includes opt-state #t)
opts
))
;; '-d' options
(option '("dbackend") #f #t
(lambda (option name arg opts)
(set-typed-option-field! opts 'dbackend opt-val arg)
(set-typed-option-field! opts 'dbackend opt-state #t)
opts
))
(option '("dwarning-as-error") #f #t
(lambda (option name arg opts)
(set-typed-option-field! opts 'dwarning-as-error opt-val arg)
(set-typed-option-field! opts 'dwarning-as-error opt-state #t)
opts
))
(option '(#\h "help") #f #t
(call-proc-and-exit show-usage))
(option '(#\v "version") #f #t
(call-proc-and-exit show-version))
))
;; unrecognized options procedure
(lambda (option name arg opts)
(scm-error 'misc-error #f "unrecognized option: ~a" (list name) #f))
;; operand procedure
(lambda (operand opts)
(set! opts
(set-typed-option-field!
opts 'files opt-val (cons operand (get-typed-option-field opts 'files opt-val))))
(set-typed-option-field! opts 'files opt-state #t)
opts)
;; state (referred to as seeds in the doc)
opts)))
;; can do some post processing on opts here such as reversing the list options
(set-typed-option-field! opts 'includes opt-val (reverse (get-typed-option-field opts 'includes opt-val)))
(set-typed-option-field! opts 'files opt-val (reverse (get-typed-option-field opts 'files opt-val)))
;; return updated opts
opts
)))
(define (show-usage)
(prnt "Usage: lys [OPTION]...\n")
(prnt "Run lys, the lilypond compile server.\n")
(prnt "Options:")
(prnt " -d --daemon run as a daemon")
(prnt " -p --port PORT listen on port PORT")
(prnt " -h --help show usage")
(prnt " -v --version show program version")
)
(define (show-version)
(format #t "lyc ~a \n" version)))
% run server
#(main (cdr (program-arguments)))
% Local variables:
% mode: scheme
% End: