Annotation of embedaddon/bmon/src/output.c, revision 1.1.1.1
1.1 misho 1: /*
2: * output.c Output API
3: *
4: * Copyright (c) 2001-2005 Thomas Graf <tgraf@suug.ch>
5: *
6: * Permission is hereby granted, free of charge, to any person obtaining a
7: * copy of this software and associated documentation files (the "Software"),
8: * to deal in the Software without restriction, including without limitation
9: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22: * DEALINGS IN THE SOFTWARE.
23: */
24:
25: #include <bmon/bmon.h>
26: #include <bmon/output.h>
27: #include <bmon/conf.h>
28: #include <bmon/node.h>
29: #include <bmon/signal.h>
30: #include <bmon/utils.h>
31:
32: static struct output_module *reg_pri_list;
33: static struct output_module *reg_sec_list;
34: static struct output_module *preferred;
35:
36: static inline void
37: __regiter_output_module(struct output_module *ops, struct output_module **list)
38: {
39: ops->om_next = *list;
40: *list = ops;
41: }
42:
43: void register_output_module(struct output_module *ops)
44: {
45: __regiter_output_module(ops, ®_pri_list);
46: }
47:
48: void register_secondary_output_module(struct output_module *ops)
49: {
50: __regiter_output_module(ops, ®_sec_list);
51: }
52:
53: static inline struct output_module *
54: __get_output_module(const char *name, struct output_module *list)
55: {
56: struct output_module *i;
57:
58: for (i = list; i; i = i->om_next)
59: if (!strcmp(i->om_name, name))
60: return i;
61:
62: return NULL;
63: }
64:
65: static struct output_module * get_output_module(const char *name)
66: {
67: return __get_output_module(name, reg_pri_list);
68: }
69:
70: static struct output_module * get_sec_output_module(const char *name)
71: {
72: return __get_output_module(name, reg_sec_list);
73: }
74:
75: #define FOREACH_SOM(F) \
76: do { \
77: struct output_module *i; \
78: for (i = reg_sec_list; i; i = i->om_next) \
79: if (i->om_enable && i->om_##F) \
80: i->om_##F (); \
81: } while (0)
82:
83: const char * get_preferred_output_name(void)
84: {
85: return preferred ? preferred->om_name : "none";
86: }
87:
88: static void find_preferred(int quiet)
89: {
90: if (NULL == preferred)
91: preferred = get_output_module("curses");
92:
93: if (NULL == preferred)
94: preferred = get_output_module("ascii");
95:
96: if (NULL == preferred && !quiet)
97: quit("No output module found.\n");
98: }
99:
100: void output_init(void)
101: {
102: find_preferred(0);
103:
104: if (preferred->om_init)
105: preferred->om_init();
106:
107: FOREACH_SOM(init);
108: }
109:
110: void output_pre(void)
111: {
112: find_preferred(0);
113:
114: if (preferred->om_pre)
115: preferred->om_pre();
116:
117: FOREACH_SOM(pre);
118: }
119:
120: void output_draw(void)
121: {
122: if (get_signal_output())
123: if (!is_signal_recvd())
124: return;
125:
126: find_preferred(0);
127:
128: calc_node_rates();
129:
130: if (preferred->om_draw)
131: preferred->om_draw();
132:
133: FOREACH_SOM(draw);
134: }
135:
136: void output_post(void)
137: {
138: find_preferred(0);
139:
140: if (preferred->om_post)
141: preferred->om_post();
142:
143: FOREACH_SOM(post);
144: }
145:
146: void output_shutdown(void)
147: {
148: find_preferred(1);
149:
150: if (preferred && preferred->om_shutdown)
151: preferred->om_shutdown();
152:
153: FOREACH_SOM(shutdown);
154: }
155:
156: static void list_output(void)
157: {
158: struct output_module *o;
159:
160: printf("Output modules:\n");
161: if (NULL == reg_pri_list)
162: printf("\tNo output modules found.\n");
163: else
164: for (o = reg_pri_list; o; o = o->om_next)
165: printf("\t%s\n", o->om_name);
166: }
167:
168: void set_output(const char *name)
169: {
170: static int set = 0;
171: module_conf_t *ml, *m;
172:
173: if (set)
174: return;
175: set = 1;
176:
177: if (NULL == name || !strcasecmp(name, "list")) {
178: list_output();
179: exit(0);
180: }
181:
182: ml = parse_module_param(name);
183:
184: for (m = ml; m; m = m->next) {
185: preferred = get_output_module(ml->name);
186:
187: if (NULL == preferred)
188: continue;
189:
190: if (preferred->om_set_opts)
191: preferred->om_set_opts(ml->attrs);
192:
193: if (preferred->om_probe)
194: if (preferred->om_probe())
195: return;
196: }
197:
198: quit("No (working) output module found\n");
199: }
200:
201: static void list_sec_output(void)
202: {
203: struct output_module *o;
204:
205: printf("Secondary output modules:\n");
206: if (NULL == reg_sec_list)
207: printf("\tNo secondary output modules found.\n");
208: else
209: for (o = reg_sec_list; o; o = o->om_next)
210: printf("\t%s\n", o->om_name);
211: }
212:
213: void set_sec_output(const char *name)
214: {
215: module_conf_t *ml, *m;
216:
217: if (NULL == name || !strcasecmp(name, "list")) {
218: list_sec_output();
219: exit(0);
220: }
221:
222: ml = parse_module_param(name);
223:
224: for (m = ml; m; m = m->next) {
225: struct output_module *o = get_sec_output_module(m->name);
226:
227: if (NULL == o)
228: continue;
229:
230: if (o->om_set_opts)
231: o->om_set_opts(ml->attrs);
232:
233: if (o->om_probe) {
234: if (o->om_probe() == 1)
235: o->om_enable = 1;
236: }
237: }
238: }
239:
240: void output_resize(void)
241: {
242: find_preferred(0);
243:
244: if (preferred && preferred->om_resize)
245: preferred->om_resize();
246:
247: FOREACH_SOM(resize);
248: }
249:
250: int resized;
251:
252: int got_resized(void)
253: {
254: int ret = resized;
255: resized = 0;
256: return ret;
257: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>