-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.c
444 lines (370 loc) · 10.2 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "options.h"
#include "preferences.h"
#include <assert.h>
#include <ctype.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
enum {
OPT_COUNTING = 1,
OPT_FRAMEWORK_OFF,
OPT_FRAMEWORK_ON,
OPT_HELP,
OPT_ORDER,
OPT_SECONDARY_ORDER,
OPT_SLEEP,
OPT_INTERVAL,
OPT_SAMPLES,
OPT_NCOLS,
OPT_NPROCS,
OPT_STATS,
OPT_PID,
OPT_USER,
OPT_DEBUGLOG,
OPT_U_SORT,
OPT_SWAP,
OPT_MMR_OFF,
OPT_MMR_ON,
/*compat/deprecated options*/
OPT_ACCUM,
OPT_DELTA,
OPT_ABSOLUTE
};
enum {
TOP_OPTION_REQUIRED = 1, /* A -flag value pair is specified as required. */
TOP_OPTION_SET /* The option is a boolean to enable/disable something. */
};
struct top_option {
const char *option_string;
int option_flag;
int option_value;
};
/* Return true if found, and false if not. */
/* The caller of this is expected to have initialized the *offset value to a valid argv array
* offset. */
bool
top_option_get(int argc, char *argv[], struct top_option *opts, int *offset, int *optvalue,
char **argresult)
{
int opti;
assert(*offset < argc);
/* Set the argument result (AKA the option value) to NULL. */
*argresult = NULL;
*optvalue = -1;
for (opti = 0; opts[opti].option_string; ++opti) {
if (TOP_OPTION_REQUIRED == opts[opti].option_flag) {
/* First check for an exact match. */
/* Otherwise check for a single char option with a value like -n4. */
if (!strcmp(argv[*offset], opts[opti].option_string)) {
if ((*offset + 1) >= argc) {
/* The option was something like -stats without a value. */
return false;
}
*argresult = argv[*offset + 1];
*optvalue = opts[opti].option_value;
*offset += 2;
return true;
} else if (2 == strlen(opts[opti].option_string) && strlen(argv[*offset]) >= 2
&& opts[opti].option_string[0] == argv[*offset][0]
&& opts[opti].option_string[1] == argv[*offset][1]) {
/* We found a pattern like -n123 and the argresult should be 123. */
*argresult = argv[*offset] + 2;
*optvalue = opts[opti].option_value;
*offset += 1;
return true;
}
} else {
/* TOP_OPTION_SET */
if (!strcmp(argv[*offset], opts[opti].option_string)) {
*optvalue = opts[opti].option_value;
*offset += 1;
return true;
}
}
}
return false;
}
/* Note: it's important that options with the same prefix have the long option in this struct first.
*/
static struct top_option opts[] = { { "-stats", TOP_OPTION_REQUIRED, OPT_STATS },
{ "-ncols", TOP_OPTION_REQUIRED, OPT_NCOLS }, { "-pid", TOP_OPTION_REQUIRED, OPT_PID },
{ "-user", TOP_OPTION_REQUIRED, OPT_USER }, { "-c", TOP_OPTION_REQUIRED, OPT_COUNTING },
{ "-F", TOP_OPTION_SET, OPT_FRAMEWORK_OFF }, { "-f", TOP_OPTION_SET, OPT_FRAMEWORK_ON },
{ "-h", TOP_OPTION_SET, OPT_HELP }, { "-o", TOP_OPTION_REQUIRED, OPT_ORDER },
{ "-O", TOP_OPTION_REQUIRED, OPT_SECONDARY_ORDER }, { "-s", TOP_OPTION_REQUIRED, OPT_SLEEP },
{ "-i", TOP_OPTION_REQUIRED, OPT_INTERVAL }, { "-l", TOP_OPTION_REQUIRED, OPT_SAMPLES },
{ "-n", TOP_OPTION_REQUIRED, OPT_NPROCS }, { "-U", TOP_OPTION_REQUIRED, OPT_USER },
{ "-u", TOP_OPTION_SET, OPT_U_SORT }, { "-S", TOP_OPTION_SET, OPT_SWAP },
{ "-R", TOP_OPTION_SET, OPT_MMR_OFF }, { "-r", TOP_OPTION_SET, OPT_MMR_ON },
/*compat/deprecated options*/
{ "-a", TOP_OPTION_SET, OPT_ACCUM }, { "-d", TOP_OPTION_SET, OPT_DELTA },
{ "-e", TOP_OPTION_SET, OPT_ABSOLUTE }, { NULL, 0, 0 } };
void
top_options_init(void)
{
/* do nothing */
}
void
top_options_usage(FILE *fp, char *argv0)
{
fprintf(fp,
"%s usage: %s\n"
"\t\t[-a | -d | -e | -c <mode>]\n"
"\t\t[-F | -f]\n"
"\t\t[-h]\n"
"\t\t[-i <interval>]\n"
"\t\t[-l <samples>]\n"
"\t\t[-ncols <columns>]\n"
"\t\t[-o <key>] [-O <secondaryKey>]\n"
"\t\t\tkeys: pid (default), command, cpu, cpu_me, cpu_others, csw,\n"
"\t\t\t\ttime, threads, ports, mregion, mem, rprvt, purg, vsize, vprvt,\n"
"\t\t\t\tkprvt, kshrd, pgrp, ppid, state, uid, wq, faults, cow, user,\n"
"\t\t\t\tmsgsent, msgrecv, sysbsd, sysmach, pageins, boosts, instrs, cycles\n"
"\t\t[-R | -r]\n"
"\t\t[-S]\n"
"\t\t[-s <delay>]\n"
"\t\t[-n <nprocs>]\n"
"\t\t[-stats <key(s)>]\n"
"\t\t[-pid <processid>]\n"
"\t\t[-user <username>]\n"
"\t\t[-U <username>]\n"
"\t\t[-u]\n",
argv0, argv0);
fprintf(fp, "\n");
}
static bool
string_is_integer(const char *s)
{
const char *b = s;
bool indicator = false;
/*skip whitespace*/
for (; *s && isspace((int)*s); ++s)
/*empty body*/;
if ('-' == *s || '+' == *s) {
++s;
indicator = true;
}
for (; *s && isdigit((int)*s); ++s)
/*empty body*/;
/*
* At this point we could have trailing whitespace, but
* in the use case that is not a real problem. If this is
* reused we might want the whitespace handled here.
*/
if ('\0' == *s && b != s) {
/* Check if it was just - or + */
if (indicator) {
if ((s - b) > 1) {
return true;
} else {
return false;
}
} else {
return true;
}
}
return false;
}
/* Return true if an error occurred. */
bool
top_options_parse(int argc, char *argv[])
{
int offset = 1;
while (offset < argc) {
char *optarg;
int optvalue;
if (false == top_option_get(argc, argv, opts, &offset, &optvalue, &optarg)) {
fprintf(stderr, "invalid option or syntax: %s\n", argv[offset]);
return true;
}
switch (optvalue) {
case OPT_COUNTING:
if (top_prefs_set_mode(optarg)) {
fprintf(stderr, "invalid argument for -c: %s\n", optarg);
return true;
}
break;
case OPT_FRAMEWORK_OFF:
top_prefs_set_frameworks(false);
break;
case OPT_FRAMEWORK_ON:
top_prefs_set_frameworks(true);
break;
case OPT_HELP:
top_options_usage(stdout, argv[0]);
exit(EXIT_SUCCESS);
break;
case OPT_INTERVAL: {
int n;
if (!string_is_integer(optarg)) {
fprintf(stderr, "invalid interval number (not an integer): %s\n", optarg);
return true;
}
n = atoi(optarg);
if (n < 1) {
fprintf(stderr, "invalid argument for -i: %s\n", optarg);
return true;
}
top_prefs_set_frameworks_interval(n);
} break;
case OPT_SAMPLES: {
int s;
if (!string_is_integer(optarg)) {
fprintf(stderr, "invalid number of samples (not an integer): %s\n", optarg);
return true;
}
s = atoi(optarg);
if (s < 0) {
fprintf(stderr, "invalid number of samples: %d\n", s);
return true;
}
top_prefs_set_samples(s);
} break;
case OPT_NCOLS: {
int n;
if (!string_is_integer(optarg)) {
fprintf(stderr, "invalid argument for -ncols (not an integer): %s\n", optarg);
return true;
}
n = atoi(optarg);
if (n < 1) {
fprintf(stderr, "-ncols requires an argument > 0\n");
return true;
}
top_prefs_set_ncols(n);
} break;
case OPT_NPROCS: {
int n;
if (!string_is_integer(optarg)) {
fprintf(stderr, "invalid argument for -n (not an integer): %s\n", optarg);
return true;
}
n = atoi(optarg);
if (n < 0) {
fprintf(stderr, "-n argument may not be negative: %s\n", optarg);
return true;
}
top_prefs_set_nprocs(n);
} break;
case OPT_ORDER:
if (top_prefs_set_sort(optarg)) {
fprintf(stderr, "invalid argument -o: %s\n", optarg);
return true;
}
break;
case OPT_SECONDARY_ORDER:
if (top_prefs_set_secondary_sort(optarg)) {
fprintf(stderr, "invalid argument -O: %s\n", optarg);
return true;
}
break;
case OPT_SLEEP: {
int s;
if (!string_is_integer(optarg)) {
fprintf(stderr,
"invalid argument for sleep interval (not an integer):"
" %s\n",
optarg);
return true;
}
s = atoi(optarg);
if (s < 0) {
fprintf(stderr, "invalid argument for -s: %s\n", optarg);
return true;
}
top_prefs_set_sleep(s);
} break;
case OPT_STATS:
if (top_prefs_set_stats(optarg)) {
fprintf(stderr, "invalid argument for -stats: %s\n", optarg);
return true;
}
break;
case OPT_PID: {
pid_t p;
if (!string_is_integer(optarg)) {
fprintf(stderr, "invalid -pid argument (not an integer): %s\n", optarg);
return true;
}
p = atoi(optarg);
if (p < 0) {
fprintf(stderr, "pid arguments can not be < 0: %s\n", optarg);
return true;
}
top_prefs_add_pid(p);
} break;
case OPT_USER: {
struct passwd *pw;
pw = getpwnam(optarg);
if (NULL == pw) {
endpwent();
fprintf(stderr, "invalid user: %s\n", optarg);
return true;
}
top_prefs_set_user(optarg);
top_prefs_set_user_uid(pw->pw_uid);
endpwent();
} break;
case OPT_U_SORT: {
if (top_prefs_set_sort("cpu") || top_prefs_set_secondary_sort("time")) {
fprintf(stderr,
"An unexpected error occurred while performing the -u "
"preference setting.\n");
abort();
}
} break;
case OPT_MMR_OFF:
top_prefs_set_mmr(false);
break;
case OPT_MMR_ON:
top_prefs_set_mmr(true);
break;
case OPT_SWAP:
top_prefs_set_swap(true);
break;
/*compat/deprecated options*/
case OPT_ACCUM:
if (top_prefs_set_mode("a")) {
fprintf(stderr, "An internal top error has occurred unexpectedly!\n");
abort();
}
break;
case OPT_DELTA:
if (top_prefs_set_mode("d")) {
fprintf(stderr, "An internal top error has occurred unexpectedly!\n");
abort();
}
break;
case OPT_ABSOLUTE:
if (top_prefs_set_mode("e")) {
fprintf(stderr, "An internal top error has occurred unexpectedly!\n");
abort();
}
break;
} /*end switch*/
}
return false;
}