Revision 11217a75 QMP/qmp-shell

b/QMP/qmp-shell
145 145
        else:
146 146
            return self._execute_cmd(cmdline)
147 147

  
148
class HMPShell(QMPShell):
149
    def __init__(self, address):
150
        QMPShell.__init__(self, address)
151
        self.__cpu_index = 0
152

  
153
    def __cmd_completion(self):
154
        for cmd in self.__cmd_passthrough('help')['return'].split('\r\n'):
155
            if cmd and cmd[0] != '[' and cmd[0] != '\t':
156
                name = cmd.split()[0] # drop help text
157
                if name == 'info':
158
                    continue
159
                if name.find('|') != -1:
160
                    # Command in the form 'foobar|f' or 'f|foobar', take the
161
                    # full name
162
                    opt = name.split('|')
163
                    if len(opt[0]) == 1:
164
                        name = opt[1]
165
                    else:
166
                        name = opt[0]
167
                self._completer.append(name)
168
                self._completer.append('help ' + name) # help completion
169

  
170
    def __info_completion(self):
171
        for cmd in self.__cmd_passthrough('info')['return'].split('\r\n'):
172
            if cmd:
173
                self._completer.append('info ' + cmd.split()[1])
174

  
175
    def __other_completion(self):
176
        # special cases
177
        self._completer.append('help info')
178

  
179
    def _fill_completion(self):
180
        self.__cmd_completion()
181
        self.__info_completion()
182
        self.__other_completion()
183

  
184
    def __cmd_passthrough(self, cmdline, cpu_index = 0):
185
        return self.cmd_obj({ 'execute': 'human-monitor-command', 'arguments':
186
                              { 'command-line': cmdline,
187
                                'cpu-index': cpu_index } })
188

  
189
    def _execute_cmd(self, cmdline):
190
        if cmdline.split()[0] == "cpu":
191
            # trap the cpu command, it requires special setting
192
            try:
193
                idx = int(cmdline.split()[1])
194
                if not 'return' in self.__cmd_passthrough('info version', idx):
195
                    print 'bad CPU index'
196
                    return True
197
                self.__cpu_index = idx
198
            except ValueError:
199
                print 'cpu command takes an integer argument'
200
                return True
201
        resp = self.__cmd_passthrough(cmdline, self.__cpu_index)
202
        if resp is None:
203
            print 'Disconnected'
204
            return False
205
        assert 'return' in resp or 'error' in resp
206
        if 'return' in resp:
207
            # Success
208
            if len(resp['return']) > 0:
209
                print resp['return'],
210
        else:
211
            # Error
212
            print '%s: %s' % (resp['error']['class'], resp['error']['desc'])
213
        return True
214

  
215
    def show_banner(self):
216
        QMPShell.show_banner(self, msg='Welcome to the HMP shell!')
217

  
148 218
def die(msg):
149 219
    sys.stderr.write('ERROR: %s\n' % msg)
150 220
    sys.exit(1)
......
156 226
    sys.exit(1)
157 227

  
158 228
def main():
229
    addr = ''
159 230
    try:
160 231
        if len(sys.argv) == 2:
161 232
            qemu = QMPShell(sys.argv[1])
233
            addr = sys.argv[1]
234
        elif len(sys.argv) == 3:
235
            if sys.argv[1] != '-H':
236
                fail_cmdline(sys.argv[1])
237
            qemu = HMPShell(sys.argv[2])
238
            addr = sys.argv[2]
162 239
        else:
163 240
                fail_cmdline()
164 241
    except QMPShellBadPort:
......
171 248
    except qmp.QMPCapabilitiesError:
172 249
        die('Could not negotiate capabilities')
173 250
    except qemu.error:
174
        die('Could not connect to %s' % sys.argv[1])
251
        die('Could not connect to %s' % addr)
175 252

  
176 253
    qemu.show_banner()
177 254
    while qemu.read_exec_command('(QEMU) '):

Also available in: Unified diff