7bc483240358909db2ba1964771220970acabcd8
[virt-kernel-info.git] / src / internal.h
1 /* Kernel info for virtual domains.
2  * (C) Copyright 2008-2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #ifndef INTERNAL_H
20 #define INTERNAL_H
21
22 #include <libintl.h>
23
24 #ifdef __GNUC__
25
26 #ifndef __GNUC_PREREQ
27 #define __GNUC_PREREQ(maj,min) 0
28 #endif
29
30 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
31 #define ATTRIBUTE_FORMAT(args...) __attribute__((__format__ (args)))
32 #define ATTRIBUTE_NORETURN __attribute__((__noreturn__))
33 #define ATTRIBUTE_CONSTRUCTOR __attribute__((__constructor__))
34
35 #if __GNUC_PREREQ (3, 4)
36 #define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
37 #else
38 #define ATTRIBUTE_RETURN_CHECK
39 #endif
40
41 #else  /* !__GNUC__ */
42 #define ATTRIBUTE_UNUSED
43 #define ATTRIBUTE_FORMAT(...)
44 #define ATTRIBUTE_NORETURN
45 #error "we must be able to define attribute((constructor)) for this compiler"
46 #define ATTRIBUTE_RETURN_CHECK
47 #endif  /* __GNUC__ */
48
49 /* Gettext functions.
50  *
51  * PACKAGE (usually the string "virt-kernel-info") should be the same as the
52  * $(DOMAIN) variable declared in po/Makevars.
53  */
54 #define _(str) dgettext(PACKAGE, (str))
55 #define N_(str) dgettext(PACKAGE, (str))
56
57 /* String equality tests, suggested by Jim Meyering. */
58 #define STREQ(a,b) (strcmp((a),(b)) == 0)
59 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
60 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
61 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
62 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
63 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
64 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
65 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
66 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
67
68 /* Each tool describes itself to the main program by
69  * defining and registering this structure.
70  */
71 typedef void (*tool_run_fn) (void); /* XXX */
72
73 struct tool {
74   const char *name;             /* Tool name, eg. "ps" */
75
76   int external_cmd; /* Is the tool an external command, eg. "virt-ps" */
77
78   const char *summary;          /* Tool-specific one line summary. */
79   const char *description;      /* Long description. */
80
81   tool_run_fn run_fn;           /* Tool entry point. */
82
83   /* The main program links tools after they are registered
84    * through this field.
85    */
86   struct tool *next;
87 };
88
89 /* Register a tool with the main program. */
90 extern void register_tool (struct tool *me);
91
92 /* Error printing functions:
93  *
94  * error() will print an error and exit.  Only use this if the error
95  * really is fatal.  NOT usually used inside tools, because they can
96  * be called repeatedly for different domains.
97  *
98  * warning() will print a warning message and continue.  Use this
99  * inside tools (and return), instead of exiting, because the tool
100  * can be called repeatedly for different domains.
101  *
102  * internal_error() is like error() but used for errors that shouldn't
103  * happen.
104  *
105  * INTERNAL_ERROR is also used for errors which shouldn't happen, but
106  * prints only file and line number.
107  *
108  * NOT_IMPL is used to mark functions and code sections which are not
109  * implemented.
110  *
111  * All of these functions append \n after the message.
112  */
113 extern void error (const char *fs, ...)
114   ATTRIBUTE_FORMAT(printf, 1, 2) ATTRIBUTE_NORETURN;
115 extern void warning (const char *fs, ...)
116   ATTRIBUTE_FORMAT(printf, 1, 2);
117 extern void internal_error (const char *fs, ...)
118   ATTRIBUTE_FORMAT(printf, 1, 2) ATTRIBUTE_NORETURN;
119
120 #define INTERNAL_ERROR                                                  \
121   internal_error (_("%s:%d: internal error"),                           \
122                   __FILE__, __LINE__)
123
124 #define NOT_IMPL                                                        \
125   internal_error (_("%s:%d: this feature is not implemented"), \
126                     __FILE__, __LINE__)
127
128 /* Output functions.  These handle CSV output automatically, but
129  * must be used with care.
130  *
131  * output_heading() is used to write the heading (title) row.
132  * The format string is converted such that any %-sequence which
133  * _isn't_ %s is turned into %s.  (So only string fields are
134  * expected in headings).
135  *
136  * output_row() is used to write each row of data.  The format
137  * string is used directly for non-CSV output, so this is mostly
138  * equivalent to printf().  However for CSV output, the format
139  * string is used to tell the types of each field, other characters
140  * being ignored.
141  *
142  * Only fairly simple format strings are understood.
143  *
144  * Usual usage is:
145  *
146  *   const char *fs = "%-9d %s";
147  *
148  *   output_heading (fs, _("PID"), _("NAME"));
149  *   for (i = 0; i < nr_procs; ++i) {
150  *     output_row (fs, proc[i].pid, proc[i].name);
151  *   }
152  *
153  * A \n is added automatically after each heading/row.
154  */
155 extern void output_heading (const char *fs, ...)
156   /* not ATTRIBUTE_FORMAT */;
157 extern void output_row (const char *fs, ...)
158   ATTRIBUTE_FORMAT(printf, 1, 2);
159
160 /* Global flags. */
161 extern int csv;
162 extern int debug;
163
164 #endif /* INTERNAL_H */