Import old 'virt-mem-new' program.
[virt-kernel-info.git] / src / internal.h
diff --git a/src/internal.h b/src/internal.h
new file mode 100644 (file)
index 0000000..7bc4832
--- /dev/null
@@ -0,0 +1,164 @@
+/* Kernel info for virtual domains.
+ * (C) Copyright 2008-2010 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef INTERNAL_H
+#define INTERNAL_H
+
+#include <libintl.h>
+
+#ifdef __GNUC__
+
+#ifndef __GNUC_PREREQ
+#define __GNUC_PREREQ(maj,min) 0
+#endif
+
+#define ATTRIBUTE_UNUSED __attribute__((__unused__))
+#define ATTRIBUTE_FORMAT(args...) __attribute__((__format__ (args)))
+#define ATTRIBUTE_NORETURN __attribute__((__noreturn__))
+#define ATTRIBUTE_CONSTRUCTOR __attribute__((__constructor__))
+
+#if __GNUC_PREREQ (3, 4)
+#define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
+#else
+#define ATTRIBUTE_RETURN_CHECK
+#endif
+
+#else  /* !__GNUC__ */
+#define ATTRIBUTE_UNUSED
+#define ATTRIBUTE_FORMAT(...)
+#define ATTRIBUTE_NORETURN
+#error "we must be able to define attribute((constructor)) for this compiler"
+#define ATTRIBUTE_RETURN_CHECK
+#endif /* __GNUC__ */
+
+/* Gettext functions.
+ *
+ * PACKAGE (usually the string "virt-kernel-info") should be the same as the
+ * $(DOMAIN) variable declared in po/Makevars.
+ */
+#define _(str) dgettext(PACKAGE, (str))
+#define N_(str) dgettext(PACKAGE, (str))
+
+/* String equality tests, suggested by Jim Meyering. */
+#define STREQ(a,b) (strcmp((a),(b)) == 0)
+#define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
+#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
+#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
+#define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
+#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
+#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
+#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
+#define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
+
+/* Each tool describes itself to the main program by
+ * defining and registering this structure.
+ */
+typedef void (*tool_run_fn) (void); /* XXX */
+
+struct tool {
+  const char *name;            /* Tool name, eg. "ps" */
+
+  int external_cmd; /* Is the tool an external command, eg. "virt-ps" */
+
+  const char *summary;         /* Tool-specific one line summary. */
+  const char *description;     /* Long description. */
+
+  tool_run_fn run_fn;          /* Tool entry point. */
+
+  /* The main program links tools after they are registered
+   * through this field.
+   */
+  struct tool *next;
+};
+
+/* Register a tool with the main program. */
+extern void register_tool (struct tool *me);
+
+/* Error printing functions:
+ *
+ * error() will print an error and exit.  Only use this if the error
+ * really is fatal.  NOT usually used inside tools, because they can
+ * be called repeatedly for different domains.
+ *
+ * warning() will print a warning message and continue.  Use this
+ * inside tools (and return), instead of exiting, because the tool
+ * can be called repeatedly for different domains.
+ *
+ * internal_error() is like error() but used for errors that shouldn't
+ * happen.
+ *
+ * INTERNAL_ERROR is also used for errors which shouldn't happen, but
+ * prints only file and line number.
+ *
+ * NOT_IMPL is used to mark functions and code sections which are not
+ * implemented.
+ *
+ * All of these functions append \n after the message.
+ */
+extern void error (const char *fs, ...)
+  ATTRIBUTE_FORMAT(printf, 1, 2) ATTRIBUTE_NORETURN;
+extern void warning (const char *fs, ...)
+  ATTRIBUTE_FORMAT(printf, 1, 2);
+extern void internal_error (const char *fs, ...)
+  ATTRIBUTE_FORMAT(printf, 1, 2) ATTRIBUTE_NORETURN;
+
+#define INTERNAL_ERROR                                                 \
+  internal_error (_("%s:%d: internal error"),                          \
+                 __FILE__, __LINE__)
+
+#define NOT_IMPL                                                       \
+  internal_error (_("%s:%d: this feature is not implemented"), \
+                   __FILE__, __LINE__)
+
+/* Output functions.  These handle CSV output automatically, but
+ * must be used with care.
+ *
+ * output_heading() is used to write the heading (title) row.
+ * The format string is converted such that any %-sequence which
+ * _isn't_ %s is turned into %s.  (So only string fields are
+ * expected in headings).
+ *
+ * output_row() is used to write each row of data.  The format
+ * string is used directly for non-CSV output, so this is mostly
+ * equivalent to printf().  However for CSV output, the format
+ * string is used to tell the types of each field, other characters
+ * being ignored.
+ *
+ * Only fairly simple format strings are understood.
+ *
+ * Usual usage is:
+ *
+ *   const char *fs = "%-9d %s";
+ *
+ *   output_heading (fs, _("PID"), _("NAME"));
+ *   for (i = 0; i < nr_procs; ++i) {
+ *     output_row (fs, proc[i].pid, proc[i].name);
+ *   }
+ *
+ * A \n is added automatically after each heading/row.
+ */
+extern void output_heading (const char *fs, ...)
+  /* not ATTRIBUTE_FORMAT */;
+extern void output_row (const char *fs, ...)
+  ATTRIBUTE_FORMAT(printf, 1, 2);
+
+/* Global flags. */
+extern int csv;
+extern int debug;
+
+#endif /* INTERNAL_H */