Print this page
5218 posix definition of NULL
correct unistd.h and iso/stddef_iso.h
update gate source affected
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/powertop/common/suggestions.c
+++ new/usr/src/cmd/powertop/common/suggestions.c
1 1 /*
2 2 * Copyright 2009, Intel Corporation
3 3 * Copyright 2009, Sun Microsystems, Inc
4 4 *
5 5 * This file is part of PowerTOP
6 6 *
7 7 * This program file is free software; you can redistribute it and/or modify it
8 8 * under the terms of the GNU General Public License as published by the
9 9 * Free Software Foundation; version 2 of the License.
10 10 *
11 11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * for more details.
15 15 *
16 16 * You should have received a copy of the GNU General Public License
17 17 * along with this program in a file named COPYING; if not, write to the
18 18 * Free Software Foundation, Inc.,
19 19 * 51 Franklin Street, Fifth Floor,
20 20 * Boston, MA 02110-1301 USA
21 21 *
22 22 * Authors:
23 23 * Arjan van de Ven <arjan@linux.intel.com>
24 24 * Eric C Saxe <eric.saxe@sun.com>
25 25 * Aubrey Li <aubrey.li@intel.com>
26 26 */
27 27
28 28 /*
29 29 * GPL Disclaimer
30 30 *
31 31 * For the avoidance of doubt, except that if any license choice other
32 32 * than GPL or LGPL is available it will apply instead, Sun elects to
33 33 * use only the General Public License version 2 (GPLv2) at this time
34 34 * for any software where a choice of GPL license versions is made
35 35 * available with the language indicating that GPLv2 or any later
36 36 * version may be used, or where a choice of which version of the GPL
37 37 * is applied is otherwise unspecified.
38 38 */
39 39
40 40 #include <unistd.h>
41 41 #include <stdio.h>
42 42 #include <stdlib.h>
43 43 #include <string.h>
44 44 #include "powertop.h"
45 45
46 46 /*
47 47 * Default number of intervals we display a suggestion before moving
48 48 * to the next.
49 49 */
50 50 #define PT_SUGG_DEF_SLICE 3
51 51
52 52 /*
53 53 * Global pointer to the current suggestion.
54 54 */
55 55 sugg_t *g_curr_sugg;
56 56
57 57 /*
58 58 * Head of the list of suggestions.
59 59 */
60 60 static sugg_t *sugg;
61 61
62 62 /*
63 63 * Add a new suggestion. Only one suggestion per text allowed.
64 64 */
65 65 void
66 66 pt_sugg_add(char *text, int weight, char key, char *sb_msg, sugg_func_t *func)
67 67 {
68 68 sugg_t *new, *n, *pos = NULL;
69 69
70 70 /*
71 71 * Text is a required field for suggestions
72 72 */
73 73 if (text == NULL)
74 74 return;
75 75
76 76 if (sugg == NULL) {
77 77 /*
78 78 * Creating first element
79 79 */
80 80 if ((new = calloc(1, sizeof (sugg_t))) == NULL)
81 81 return;
82 82
83 83 if (sb_msg != NULL)
84 84 new->sb_msg = strdup(sb_msg);
85 85
86 86 if (text != NULL)
87 87 new->text = strdup(text);
88 88
89 89 new->weight = weight;
90 90 new->key = key;
91 91 new->func = func;
92 92 new->slice = 0;
93 93
94 94 sugg = new;
95 95 new->prev = NULL;
96 96 new->next = NULL;
97 97 } else {
98 98 for (n = sugg; n != NULL; n = n->next) {
99 99 if (strcmp(n->text, text) == 0)
100 100 return;
101 101
102 102 if (weight > n->weight && pos == NULL)
103 103 pos = n;
104 104 }
105 105 /*
106 106 * Create a new element
107 107 */
108 108 if ((new = calloc(1, sizeof (sugg_t))) == NULL)
109 109 return;
110 110
111 111 if (sb_msg != NULL)
112 112 new->sb_msg = strdup(sb_msg);
113 113
114 114 new->text = strdup(text);
115 115
116 116 new->weight = weight;
117 117 new->key = key;
118 118 new->func = func;
119 119 new->slice = 0;
120 120
121 121 if (pos == NULL) {
122 122 /*
123 123 * Ordering placed the new element at the end
124 124 */
125 125 for (n = sugg; n->next != NULL; n = n->next)
126 126 ;
127 127
128 128 n->next = new;
129 129 new->prev = n;
130 130 new->next = NULL;
131 131 } else {
132 132 if (pos == sugg) {
133 133 /*
134 134 * Ordering placed the new element at the start
135 135 */
136 136 new->next = sugg;
137 137 new->prev = sugg;
138 138 sugg->prev = new;
139 139 sugg = new;
140 140 } else {
141 141 /*
142 142 * Ordering placed the new element somewhere in
143 143 * the middle
144 144 */
145 145 new->next = pos;
146 146 new->prev = pos->prev;
147 147 pos->prev->next = new;
148 148 pos->prev = new;
149 149 }
150 150 }
151 151 }
152 152 }
153 153
154 154 /*
155 155 * Removes a suggestion, returning 0 if not found and 1 if so.
156 156 */
157 157 int
158 158 pt_sugg_remove(sugg_func_t *func)
159 159 {
160 160 sugg_t *n;
161 161 int ret = 0;
162 162
163 163 for (n = sugg; n != NULL; n = n->next) {
164 164 if (n->func == func) {
165 165 /* Removing the first element */
166 166 if (n == sugg) {
167 167 if (sugg->next == NULL) {
168 168 /* Removing the only element */
169 169 sugg = NULL;
170 170 } else {
171 171 sugg = n->next;
172 172 sugg->prev = NULL;
173 173 }
174 174 } else {
175 175 if (n->next == NULL) {
176 176 /* Removing the last element */
177 177 n->prev->next = NULL;
178 178 } else {
179 179 /* Removing an intermediate element */
180 180 n->prev->next = n->next;
181 181 n->next->prev = n->prev;
182 182 }
183 183 }
184 184
185 185 /*
186 186 * If this suggestions is currently being suggested,
187 187 * remove it and update the screen.
188 188 */
189 189 if (n == g_curr_sugg) {
190 190 if (n->sb_msg != NULL) {
191 191 pt_display_mod_status_bar(n->sb_msg);
192 192 pt_display_status_bar();
193 193 }
194 194 if (n->text != NULL)
195 195 pt_display_suggestions(NULL);
196 196 }
197 197
198 198 free(n);
199 199 ret = 1;
200 200 }
201 201 }
202 202
203 203 return (ret);
204 204 }
205 205
206 206 /*
207 207 * Chose a suggestion to display. The list of suggestions is ordered by weight,
208 208 * so we only worry about fariness here. Each suggestion, starting with the
209 209 * first (the 'heaviest') is displayed during PT_SUGG_DEF_SLICE intervals.
210 210 */
211 211 void
212 212 pt_sugg_pick(void)
213 213 {
214 214 sugg_t *n;
215 215
216 216 if (sugg == NULL) {
217 217 g_curr_sugg = NULL;
218 218 return;
219 219 }
220 220
221 221 search:
222 222 for (n = sugg; n != NULL; n = n->next) {
223 223
224 224 if (n->slice++ < PT_SUGG_DEF_SLICE) {
225 225
226 226 /*
227 227 * Don't need to re-suggest the current suggestion.
228 228 */
229 229 if (g_curr_sugg == n && !g_sig_resize)
230 230 return;
231 231
232 232 /*
233 233 * Remove the current suggestion from screen.
234 234 */
235 235 if (g_curr_sugg != NULL) {
236 236 if (g_curr_sugg->sb_msg != NULL) {
237 237 pt_display_mod_status_bar(
238 238 g_curr_sugg->sb_msg);
239 239 pt_display_status_bar();
240 240 }
241 241 if (g_curr_sugg->text != NULL)
242 242 pt_display_suggestions(NULL);
243 243 }
244 244
245 245 if (n->sb_msg != NULL) {
246 246 pt_display_mod_status_bar(n->sb_msg);
247 247 pt_display_status_bar();
248 248 }
249 249
250 250 pt_display_suggestions(n->text);
251 251
252 252 g_curr_sugg = n;
253 253
254 254 return;
255 255 }
256 256 }
257 257
258 258 /*
259 259 * All suggestions have run out of slice quotas, so we restart.
260 260 */
↓ open down ↓ |
260 lines elided |
↑ open up ↑ |
261 261 for (n = sugg; n != NULL; n = n->next)
262 262 n->slice = 0;
263 263
264 264 goto search;
265 265 }
266 266
267 267 void
268 268 pt_sugg_as_root(void)
269 269 {
270 270 pt_sugg_add("Suggestion: run as root to get suggestions"
271 - " for reducing system power consumption", 40, NULL, NULL,
271 + " for reducing system power consumption", 40, '\0', NULL,
272 272 NULL);
273 273 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX