summaryrefslogtreecommitdiff
path: root/src/core/cmdline.c
blob: b37e1d7e0a598d99833f857b43116f34136e51c1 (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
/* BSD-2-Clause license
 * 
 * Copyright (c) 2018-2023 NST <www.newinfosec.ru>, sss <sss at dark-alexandr dot net>. 
 * 
 */
 
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config_file.h"
#include "globals.h"

static void
print_help(const char *prog_path)
{
	printf("Usage: %s [-c:d....]\nOptions:\n"
	       "\t-c\t--config_file\t\"Use settings from"
	       " specified file instead of default config file\"\n"
	       "\t-d\t--daemon\t\"Daemon mode (fork to background)\"\n"
	       "\t--http_port\t\"Set port for http/ws server\"\n"
	       "\t--help\t\"Print tthis help message\"\n",
	    prog_path);
	exit(EXIT_FAILURE);
}

void
handle_cmdline_args(int argc, char **argv)
{
	int c;
	char config_file_path[_POSIX_PATH_MAX] = {0};
	bool daemon = false, wrong_options = false;
	while (1)
	{
		int option_index = 0;
		static struct option long_options[] = {
		    {"config_file", required_argument, 0, 0},
		    {"daemon",      no_argument,	    0, 0},
		    {"ws_port",	required_argument, 0, 0},
		    {"help",	     no_argument,	  0, 0}
		       };
		c = getopt_long(
		    argc, argv, "c:dp:", long_options, &option_index);
		if (c == -1)
			break;
		switch (c)
		{
			case 0:
			{
				if (!strcmp(long_options[option_index].name,
					"config_file")
				    && optarg)
				{
					strncpy(config_file_path, optarg,
					    _POSIX_PATH_MAX - 1);
					config_file_path[_POSIX_PATH_MAX - 1]
					    = 0;
				}
				else if (!strcmp(
					     long_options[option_index].name,
					     "ws_port")
					 && optarg)
					g_globals.settings.ws_port
					    = atoi(optarg);
				else if (!strcmp(
					     long_options[option_index].name,
					     "daemon"))
					daemon = true;
				else if (!strcmp(
					     long_options[option_index].name,
					     "help"))
					print_help(argv[0]);
			}
			break;
			case 'c':
			{
				if (optarg) //clang static analyzer...
				{
					strncpy(config_file_path, optarg,
					    _POSIX_PATH_MAX - 1);
					config_file_path[_POSIX_PATH_MAX - 1]
					    = 0;
				}
			}
			break;
			case 'p':
			{
				if (optarg) //clang static analyzer...
				{
					g_globals.settings.ws_port
					    = atoi(optarg);
				}
			}
			break;
			case 'd':
			{
				daemon = true;
			}
			break;
			case '?':
			{
				wrong_options = true;
			}
			break;
			default:
				break;
		}
	}
	if (wrong_options)
	{
		print_help(argv[0]);
	}
	else
	{
		g_globals.settings.daemon = daemon;
		find_config_file(config_file_path);
	}
}