1 /* Kernel info for virtual domains.
2 * (C) Copyright 2008-2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* Generic output routines which can write either plain
20 * text tables or CSV files.
32 static char *convert_fs_to_strings (const char *fs);
33 static void output_to_csv (int to_strings, const char *fs, va_list args);
36 output_heading (const char *fs, ...)
43 /* Convert each %..x in the format string into %..s */
44 char *fs2 = convert_fs_to_strings (fs);
49 output_to_csv (1, fs, args);
55 output_row (const char *fs, ...)
66 output_to_csv (0, fs, args);
71 /* Return a newly allocated format string with each '%..x' sequence
72 * converted to '%..s'. We only attempt to parse simple format
73 * strings, and we assume the length of the format string won't
76 static const char *end_of_specifier = "diouxXeEfFgGaAcsp%";
79 convert_fs_to_strings (const char *fs)
86 for (i = 0; i < len; ++i) {
88 /* Search for the end of the %-sequence. */
90 while (i < len && strchr (end_of_specifier, fs2[i]) == NULL);
93 internal_error (_("unrecognized format string in output_heading function"));
99 return fs2; /* caller frees */
102 /* Output a row to a CSV file. The format string is just used to
103 * get the correct types for each parameter - any non-%-specifiers
104 * in the format string are ignored. If to_strings is true, then
105 * each parameter is assumed to be a string.
107 * The CSV output is very conservative, designed to be correct
108 * rather than concise.
111 output_to_csv (int to_strings, const char *fs, va_list args)
113 int i, j, len, comma = 0;
118 for (i = 0; i < len; ++i) {
119 /* Look for the next %-specifier. */
122 do { fs2[j++] = fs[i++]; }
123 while (j < sizeof (fs2) - 1 &&
125 strchr (end_of_specifier, fs[i]) == NULL);
127 if (j == sizeof (fs2) - 1 || i == len)
128 internal_error (_("unrecognized format string in output_* function"));
141 /* Convert the next parameter into a string. */
142 if (vasprintf (&str, fs2, args) == -1)
143 internal_error (_("unable to convert next argument to string using '%s'"),
147 /* Output the next parameter as a CSV field. */
153 for (j = 0; j < len2; ++j) {