Debian Codegraph @

lib/ninix/dll/ssu.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # ssu.rb - a ssu compatible Saori module for ninix
4 # Copyright (C) 2003-2019 by Shyouzou Sugitani <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License (version 2) as
8 # published by the Free Software Foundation. It is distributed in the
9 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the GNU General Public License for more details.
12 #
13
14 # TODO:
15 # - if, switch等の引数計算(calcを使用)
16
17 require_relative "../dll"
18 require_relative "../logging"
19
20
21 module Ssu
22
23 class Saori < DLL::SAORI
24 FUNCTION = {'is_empty' => ['ssu_is_empty', [0, 1]],
25 'is_digit' => ['ssu_is_digit', [1]],
26 'is_alpha' => ['ssu_is_alpha', [1]],
27 'iflist' => ['ssu_iflist', [nil]],
28 'length' => ['ssu_length', [1]],
29 'zen2han' => ['ssu_zen2han', [1]],
30 'han2zen' => ['ssu_han2zen', [1]],
31 'kata2hira,' => ['ssu_kata2hira', [1]],
32 'hira2kata' => ['ssu_hira2kata', [1]],
33 'sprintf' => ['ssu_sprintf', [nil]],
34 'calc' => ['ssu_calc', [1]],
35 'calc_float' => ['ssu_calc_float', [1]],
36 'compare_tail' => ['ssu_compare_tail', [2]],
37 'compare_head' => ['ssu_compare_head', [2]],
38 'compare' => ['ssu_compare', [2]],
39 'count' => ['ssu_count', [2]],
40 'erase_first' => ['ssu_erase_first', [2]],
41 'erase' => ['ssu_erase', [2]],
42 'replace' => ['ssu_replace', [3]],
43 'replace_first' => ['ssu_replace_first', [3]],
44 'split' => ['ssu_split', [1, 2, 3]],
45 'substr' => ['ssu_substr', [2, 3]],
46 'nswitch' => ['ssu_nswitch', [nil]],
47 'switch' => ['ssu_switch', [nil]],
48 'if' => ['ssu_if', [2, 3]],
49 'unless' => ['ssu_unless', [2, 3]],}
50 FUNC_NAME = FUNCTION.keys
51
52 def initialize
53 super()
54 end
55
56 def request(req)
57 req_type, argument, charset = evaluate_request(req)
58 result =
59 case req_type
60 when nil
61 RESPONSE[400]
62 when 'GET Version'
63 RESPONSE[204]
64 when 'EXECUTE'
65 execute(argument, charset)
66 else
67 RESPONSE[400]
68 end
69 return result
70 end
71
72 def execute(args, charset)
73 return RESPONSE[400] if args.nil? or args.empty?
74 return RESPONSE[400] unless FUNCTION.include?(args[0])
75 name = args[0]
76 args = args[1..-1]
77 if FUNCTION[name][1] == [nil]
78 #pass
79 elsif not FUNCTION[name][1].include?(args.length)
80 return RESPONSE[400]
81 end
82 @value = []
83 result = method(FUNCTION[name][0]).call(args)
84 return RESPONSE[204] if result.nil? or result.to_s.empty?
85 s = "SAORI/1.0 200 OK\r\nResult: #{result}\r\n"
86 unless @value.empty?
87 for i in [email protected]
88 s = [s, "Value#{i}: #{@value[i]}\r\n"].join("")
89 end
90 end
91 s = [s, "Charset: #{charset}\r\n"].join("")
92 s = [s, "\r\n"].join("")
93 return s.encode(charset, :invalid => :replace, :undef => :replace)
94 end
95
96 def ssu_is_empty(args)
97 if args.empty? or args[0].empty?
98 return 1
99 else
100 return 0
101 end
102 end
103
104 def ssu_is_digit(args)
105 s = args[0]
106 for i in 0..s.length-1
107 if s[i] =~ /[[:digit:]]/
108 next
109 else
110 return 0
111 end
112 end
113 return 1
114 end
115
116 def ssu_is_alpha(args)
117 s = args[0]
118 for i in 0..s.length-1
119 if s[i] =~ /[[:alpha:]]/ # /(?a:[[:alpha:]])/
120 next
121 else
122 return 0
123 end
124 end
125 return 1
126 end
127
128 def ssu_length(args)
129 args[0].length
130 end
131
132 def ssu_substr(args)
133 s = args[0]
134 if ssu_is_digit([args[1]]) == 1
135 start = ssu_zen2han([args[1]]).to_i
136 return '' if start > s.length
137 else
138 return ''
139 end
140 if args.length == 2
141 end_ = s.length
142 elsif args.length == 3 and ssu_is_digit([args[2]]) == 1
143 end_ = (start + ssu_zen2han([args[2]]).to_i)
144 else
145 return ''
146 end
147 return s[start..end_-1]
148 end
149
150 def ssu_sprintf(args)
151 return sprintf(args[0], *args[1..-1])
152 end
153
154 ZEN = {'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
155 '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
156 '.' => '.', '+' => '+', '−' => '-',}
157 HAN = {'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
158 '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
159 '.' => '.', '+' => '+', '-' => "FF0D".to_i(16).chr('UTF-8'),}
160
161 def ssu_zen2han(args)
162 s = args[0]
163 buf = ''
164 for i in 0..s.length-1
165 c = s[i]
166 if ZEN.include?(c)
167 buf = [buf, ZEN[c]].join("")
168 else
169 buf = [buf, s[i]].join("")
170 end
171 end
172 return buf
173 end
174
175 def ssu_han2zen(args)
176 s = args[0]
177 buf = ''
178 for i in 0..s.length-1
179 c = s[i]
180 if HAN.include?(c)
181 buf = [buf, HAN[c]].join("")
182 else
183 buf = [buf, s[i]].join("")
184 end
185 end
186 return buf
187 end
188
189 RE_CONDITION = Regexp.new('(>=|<=|>|<|==|!=|>=|<=|>|<|!=|==)')
190
191 def eval_condition(left, ope, right)
192 if ssu_is_digit([left]) == 1 and \
193 ssu_is_digit([right]) == 1
194 left = ssu_zen2han([left]).to_f
195 right = ssu_zen2han([right]).to_f
196 elsif ['>', '>', '>=', '>=', '<', '<', '<=', '<='].include?(ope)
197 left = left.length
198 right = right.length
199 end
200 case ope
201 when '>', '>'
202 (left > right)
203 when '>=', '>='
204 (left >= right)
205 when '<', '<'
206 (left < right)
207 when '<=', '<='
208 (left <= right)
209 when '==', '=='
210 (left == right)
211 when '!=', '!='
212 (left != right)
213 else
214 false # 'should not reach here'
215 end
216 end
217
218 def ssu_if(args)
219 condition = args[0]
220 match = RE_CONDITION.match(condition)
221 unless match.nil?
222 left = match.pre_match
223 ope = match[0]
224 right = match.post_match
225 result = eval_condition(left, ope, right)
226 if result
227 return args[1]
228 end
229 end
230 if args.length == 3
231 return args[2]
232 else
233 return ''
234 end
235 end
236
237 def ssu_unless(args)
238 condition = args[0]
239 match = RE_CONDITION.match(condition)
240 unless match.nil?
241 left = match.pre_match
242 ope = match[0]
243 right = match.post_match
244 result = eval_condition(left, ope, right)
245 unless result
246 return args[1]
247 end
248 end
249 if args.length == 3
250 return args[2]
251 else
252 return ''
253 end
254 end
255
256 def ssu_iflist(args)
257 left = args[0]
258 i = 1
259 while true
260 break if args[i..-1].length < 2
261 ope_right = args[i]
262 match = RE_CONDITION.match(ope_right) # FIXME: 左辺に演算子を入れた場合にも動作するようにする
263 unless match.nil?
264 next unless match.pre_match.strip.empty?
265 ope = match[0]
266 right = match.post_match
267 result = eval_condition(left, ope, right)
268 if result
269 return args[i + 1]
270 end
271 end
272 i += 2
273 end
274 return ''
275 end
276
277 def ssu_nswitch(args)
278 num = args[0]
279 if ssu_is_digit([num]) == 1
280 num = ssu_zen2han([num]).to_i
281 if 0 < num and num < args.length
282 return args[num]
283 end
284 end
285 return ''
286 end
287
288 def ssu_count(args)
289 args[0].scan(args[1]).size
290 end
291
292 def ssu_compare(args)
293 if args[0] == args[1]
294 return 1
295 else
296 return 0
297 end
298 end
299
300 def ssu_compare_head(args)
301 s0 = args[0]
302 s1 = args[1]
303 if s1.start_with?(s0)
304 return 1
305 else
306 return 0
307 end
308 end
309
310 def ssu_compare_tail(args)
311 s0 = args[0]
312 s1 = args[1]
313 if s1.end_with?(s0)
314 return 1
315 else
316 return 0
317 end
318 end
319
320 def ssu_erase(args)
321 args[0].gsub(args[1], '')
322 end
323
324 def ssu_erase_first(args)
325 tmp = args[0].partition(args[1])
326 return tmp[0] + tmp[2]
327 end
328
329 def ssu_replace(args)
330 args[0].gsub(args[1], args[2])
331 end
332
333 def ssu_replace_first(args)
334 tmp = args[0].partition(args[1])
335 if tmp[1].empty?
336 return tmp[0]
337 else
338 return tmp[0] + args[2] + tmp[2]
339 end
340 end
341
342 def ssu_split(args) ## FIXME: 空要素分割
343 s0 = args[0]
344 if args.length >= 2
345 s1 = args[1]
346 else
347 s1 = ' ' ## FIXME
348 end
349 if args.length == 3
350 num = args[2]
351 if ssu_is_digit([num]) == 1
352 num = ssu_zen2han([num]).to_i
353 end
354 value_list = s0.split(s1, num + 1)
355 else
356 value_list = s0.split(s1, -1)
357 end
358 @value = value_list
359 return value_list.length
360 end
361
362 def ssu_switch(args)
363 left = args[0]
364 i = 1
365 while true
366 break if args[i..-1].length < 2
367 right = args[i]
368 if left == right # FIXME: 左辺、右辺が数値でなくともエラーにならない
369 return args[i + 1]
370 end
371 i += 2
372 end
373 return ''
374 end
375
376 def ssu_kata2hira(args) ## FIXME: not supported yet
377 ''
378 end
379
380 def ssu_hira2kata(args) ## FIXME: not supported yet
381 ''
382 end
383
384 def ssu_calc(args) ## FIXME: not supported yet
385 0
386 end
387
388 def ssu_calc_float(args) ## FIXME: not supported yet
389 0
390 end
391
392 def evaluate_request(req)
393 req_type = nil
394 argument = []
395 charset = 'CP932' # default
396 header = req.lines
397 line = header.shift
398 if line.nil?
399 return req_type, argument, charset
400 end
401 line = line.force_encoding(charset).strip.encode('utf-8', :invalid => :replace, :undef => :replace)
402 if line.empty?
403 return req_type, argument, charset
404 end
405 for request in ['EXECUTE', 'GET Version']
406 if line.start_with?(request)
407 req_type = request
408 break
409 end
410 end
411 for line in header
412 line = line.force_encoding(charset).strip.encode('utf-8', :invalid => :replace, :undef => :replace)
413 next if line.empty?
414 next unless line.include?(':')
415 key, value = line.split(':', 2)
416 key.strip!
417 value.strip!
418 if key == 'Charset'
419 if Encoding.name_list.include?(value)
420 charset = value
421 else
422 Logging::Logging.warning('Unsupported charset #{value}')
423 end
424 end
425 if key.start_with?('Argument') ## FIXME
426 argument << value
427 else
428 next
429 end
430 end
431 return req_type, argument, charset
432 end
433 end
434 end