2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
25 #include "qemu/qemu_monitor.h"
26 #include "qemu/qemu_monitor_json.h"
28 #define REAL_SYM(realFunc) \
30 if (!realFunc && !(realFunc = dlsym(RTLD_NEXT, __FUNCTION__))) { \
31 fprintf(stderr, "Cannot find real '%s' symbol\n", \
37 static bool first
= true;
40 printLineSkipEmpty(const char *line
,
45 for (p
= line
; *p
; p
++) {
46 if (p
[0] == '\n' && p
[1] == '\n')
54 static int (*realQemuMonitorSend
)(qemuMonitorPtr mon
,
55 qemuMonitorMessagePtr msg
);
58 qemuMonitorSend(qemuMonitorPtr mon
,
59 qemuMonitorMessagePtr msg
)
63 REAL_SYM(realQemuMonitorSend
);
65 if (!(reformatted
= virJSONStringReformat(msg
->txBuffer
, true))) {
66 fprintf(stderr
, "Failed to reformat command string '%s'\n", msg
->txBuffer
);
73 printLineSkipEmpty("\n", stdout
);
75 printLineSkipEmpty(reformatted
, stdout
);
76 VIR_FREE(reformatted
);
78 return realQemuMonitorSend(mon
, msg
);
82 static int (*realQemuMonitorJSONIOProcessLine
)(qemuMonitorPtr mon
,
84 qemuMonitorMessagePtr msg
);
87 qemuMonitorJSONIOProcessLine(qemuMonitorPtr mon
,
89 qemuMonitorMessagePtr msg
)
91 virJSONValuePtr value
= NULL
;
95 REAL_SYM(realQemuMonitorJSONIOProcessLine
);
97 ret
= realQemuMonitorJSONIOProcessLine(mon
, line
, msg
);
100 if (!(value
= virJSONValueFromString(line
)) ||
101 !(json
= virJSONValueToString(value
, true))) {
102 fprintf(stderr
, "Failed to reformat reply string '%s'\n", line
);
106 /* Ignore QMP greeting */
107 if (virJSONValueObjectHasKey(value
, "QMP"))
113 printLineSkipEmpty("\n", stdout
);
115 printLineSkipEmpty(json
, stdout
);
120 virJSONValueFree(value
);