Rename REAMDE to README.md
[nit.git] / src / nitdbg_client.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Lucas Bajolet <lucas.bajolet@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Client for the nit debugger nitdbg-server
18 #
19 # Can send commands to the debugger
20 module nitdbg_client
21
22 import socket
23 import toolcontext
24
25 redef class ToolContext
26
27 var opt_host_address: OptionString = new OptionString("Sets the host to debug from, use IPV4 only (Defaults to 127.0.0.1)", "--host")
28 var opt_debug_port: OptionInt = new OptionInt("Sets the debug port (Defaults to 22125) - Must be contained between 0 and 65535", 22125, "--port")
29
30 redef init
31 do
32 super
33 self.option_context.add_option(self.opt_host_address)
34 self.option_context.add_option(self.opt_debug_port)
35 end
36
37 end
38
39 redef class String
40
41 # Checks if the actual string is a valid IPv4 address
42 # That is, if the pattern is int.int.int.int where each int must be between 0 and 255
43 fun is_valid_ipv4_address: Bool
44 do
45 var components = self.split_with(".")
46 if components.length != 4 then return false
47 for i in components do
48 if not i.is_numeric or not (i.to_i <= 255 and i.to_i >= 0) then return false
49 end
50 return true
51 end
52
53 end
54
55 # Create a tool context to handle options and paths
56 var toolcontext = new ToolContext
57 toolcontext.tooldescription = "Usage: nitdbg_client [OPTION]...\nConnects to a nitdbg_server and controls it."
58 toolcontext.accept_no_arguments = true
59 toolcontext.process_options(args)
60
61 # If the port value is not an Int between 0 and 65535 (Mandatory according to the norm)
62 # Print the usage
63 if toolcontext.opt_debug_port.value < 0 or toolcontext.opt_debug_port.value > 65535 then
64 toolcontext.option_context.usage
65 return
66 end
67
68 var debug: TCPStream
69
70 # An IPV4 address does always complies to this form : x.x.x.x
71 # Where x is an integer whose value is >=0 and <= 255
72 if toolcontext.opt_host_address.value != null then
73 if toolcontext.opt_host_address.value.is_valid_ipv4_address then
74 debug = new TCPStream.connect(toolcontext.opt_host_address.value.as(not null), toolcontext.opt_debug_port.value)
75 else
76 toolcontext.option_context.usage
77 return
78 end
79 else
80 debug = new TCPStream.connect("127.0.0.1", toolcontext.opt_debug_port.value)
81 end
82
83 print "[HOST ADDRESS] : {debug.address}"
84 print "[HOST] : {debug.host}"
85 print "[PORT] : {debug.port}"
86 print "Connecting ... {debug.connected}"
87
88 while debug.connected do
89 if stdin.poll_in then debug.write_ln(gets)
90 while debug.ready_to_read(50) do printn debug.read(200)
91 end
92