summaryrefslogtreecommitdiffstats
path: root/datatop/src/datatop_opt.c
blob: c2bb366b88eae49a1ddce77f8aaed8818c1772e5 (plain)
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
/************************************************************************
Copyright (c) 2015, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************/

/**
 * @file datatop_opt.c
 * @brief Adds getopt functionality for CLI commands.
 *
 * Contains method for getopt functionality used for parsing
 * the CLI arguments into executable commands. Handles
 * errors which arise when parsing.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <getopt.h>
#include "datatop_opt.h"
#include "datatop_interface.h"
#include "datatop_linked_list.h"
#include "datatop_fileops.h"

/**
 * @brief Populate the comand line options with sane defaults
 *
 * @param clopts Struct used to hold data regarding CLI arguments.
 */
void dtop_load_default_options(struct cli_opts *clopts)
{
	memset(clopts, 0, sizeof(struct cli_opts));

	clopts->priority = DEFAULT_NICE;
}

/**
 * @brief Parses all CLI commands for main() to execute.
 *
 * @param clopts Struct used to hold data regarding CLI arguments.
 * @param argc Parameter used to read CLI commands from.
 * @param argv Parameter used to read CLI arguments from.
 * @return PARSE_SUCCESS - CLI arguments read successfully,
 * @return PARSE_FAILURE - CLI arguments and/or input not valid.
 * @return PARSE_FORCE_EXIT - Exit immediately, print help options.
 */
int dtop_parse_cli_opts(struct cli_opts *clopts, int argc, char **argv)
{
	int option;

	if (!clopts || !*argv) {
		printf("Internal Error: Null Pointer\n");
		goto error;
	}

	while ((option = getopt(argc, argv, "phi:t:w:s:n:")) != -1) {
		switch (option) {
		case 'p':
			clopts->print_cl = OPT_CHOSE;
		break;

		case 'h':
			dtop_print_help_opts();
			return PARSE_FORCE_EXIT;
		break;

		case 'n':
			clopts->priority = strtol(optarg, 0, 10);
			if (clopts->priority > 19 || clopts->priority < -20) {
				printf("Argument for -n is not valid. ");
				printf("Must be between -20 and 19.\n");
				goto error;
			}
		break;

		case 'i':
			clopts->poll_per = strtol(optarg, 0, 10);
			if (clopts->poll_per <= 0) {
				printf("Argument for -i is not valid. ");
				printf("Must be positive integer.\n");
				goto error;
			}
		break;

		case 't':
			clopts->poll_time = strtol(optarg, 0, 10);
			clopts->poll_time_selected = POLL_TIME_SELECTED;
			if (clopts->poll_time <= 0) {
				printf("Argument for -t is not valid. ");
				printf("Must be positive integer.\n");
				goto error;
			}
		break;

		case 'w':
			if (dtop_check_writefile_access(optarg) == VALID) {
				clopts->file_name = optarg;
				clopts->print_csv = OPT_CHOSE;
			} else {
				goto error;
			}
		break;

		case 's':
			if (dtop_check_writefile_access(optarg) == VALID)
				clopts->snapshot_file = optarg;
			else
				goto error;
		break;

		case '?':
		default:
			goto error;
		}
	}

	if (clopts->poll_time == 0) {
		if (clopts->print_csv == 1)
			clopts->poll_time = POLL_NOT_SPECIFIED;
		else
			clopts->poll_time = POLL_TIME_DEFAULT;
	}
	if (clopts->poll_per == 0)
		clopts->poll_per = DEFAULT_POLL_INTERVAL;

	return PARSE_SUCCESS;

error:
	printf("See datatop -h for help\n");
	return PARSE_FAILURE;
}

/**
 * @brief Prints the options the user has for the program to terminal.
 */
void dtop_print_help_opts(void)
{
	printf("The following datatop commands are:\n");
	printf("\t-p\t\t\tPrint output to terminal\n");
	printf("\t-i , seconds\t\tSpecify polling period\n");
	printf("\t-t , seconds\t\tSpecify polling duration\n");
	printf("\t-w , file name (.csv)\tWrite output to a file\n");
	printf("\t-s , file name\t\tPrint system snapshot to a file\n");
	printf("\t-n , nice value\t\tSet niceness (default 19)\n");
	printf("\t-h\t\t\tGet help\n");
}


/**
* @brief Prints the interactive options the user can enter during runtime.
*/
void dtop_print_interactive_opts(void)
{
	printf("The following interactive commands are:\n");
	printf("\tq | quit\tTerminate program at any time\n");
	printf("\ti\t\tPrint dp differences, reset initial dp values\n");
	printf("\tl\t\tPrint dp differences since last reset\n");
	printf("\n");
}