smrtlink.cpp 3.54 KB
Newer Older
/jdi/'s avatar
basics  
/jdi/ committed
1
2
3
4
5
//============================================================================
// Name        : smrtlink.cpp
// Author      : jdi
// Version     :
// Copyright   : GPL v2
/jdi/'s avatar
/jdi/ committed
6
// Description : SmrtLink in C++, Ansi-style
/jdi/'s avatar
basics  
/jdi/ committed
7
8
9
10
11
12
13
14
15
16
17
//============================================================================

#include <cstring>
#include <getopt.h>
#include <unistd.h>

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>

/jdi/'s avatar
/jdi/ committed
18
#include "Options.h"
/jdi/'s avatar
/jdi/ committed
19
#include "Host.h"
/jdi/'s avatar
basics  
/jdi/ committed
20
21
22
23
24
25
#include "Program.h"

#define no_argument 0
#define required_argument 1
#define optional_argument 2

/jdi/'s avatar
/jdi/ committed
26
Options options;
/jdi/'s avatar
basics  
/jdi/ committed
27

/jdi/'s avatar
/jdi/ committed
28
29
30
31
constexpr unsigned int caseArg(const char* str, int h = 0) {
	return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
}

/jdi/'s avatar
/jdi/ committed
32
33
int main(int argc, char *argv[]) {
	int opt, index;
/jdi/'s avatar
basics  
/jdi/ committed
34
35

	const struct option longopts[] = { { "version", no_argument, 0, 'v' }, {
/jdi/'s avatar
/jdi/ committed
36
			"help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, {
/jdi/'s avatar
/jdi/ committed
37
38
			"permanent", no_argument, 0, 's' }, { "password", required_argument,
			0, 'p' }, { "user",
/jdi/'s avatar
/jdi/ committed
39
40
	required_argument, 0, 'u' }, { "interface", required_argument, 0, 'i' }, {
			"header", required_argument, 0, 'b' }, { "hex", required_argument,
/jdi/'s avatar
/jdi/ committed
41
			0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout",
/jdi/'s avatar
/jdi/ committed
42
43
	required_argument, 0, 't' }, { "wait",
	required_argument, 0, 'w' }, { 0, 0, 0, 0 }, };
/jdi/'s avatar
basics  
/jdi/ committed
44
45
46

	Program p = Program();

/jdi/'s avatar
/jdi/ committed
47
	while ((opt = getopt_long(argc, argv, "bhrsvxp:u:i:f:t:", longopts, &index))
/jdi/'s avatar
/jdi/ committed
48
			!= -1) {
/jdi/'s avatar
basics  
/jdi/ committed
49
50
51
		switch (opt) {

		case 'h':
/jdi/'s avatar
/jdi/ committed
52
53
54
55
			fprintf(stderr, VERSION);
			fprintf(stderr, USAGE, argv[0]);
			fprintf(stderr, HELP);
			exit(EXIT_SUCCESS);
/jdi/'s avatar
basics  
/jdi/ committed
56
57
58
			break;

		case 'v':
/jdi/'s avatar
/jdi/ committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
			fprintf(stderr, VERSION);
			exit(EXIT_SUCCESS);
			break;

		case 'r':
			options.flags |= FLAG_REVERSE;
			break;

		case 'b':
			options.flags |= FLAG_HEADER;
			break;

		case 'x':
			options.flags |= FLAG_HEX;
/jdi/'s avatar
basics  
/jdi/ committed
73
74
			break;

/jdi/'s avatar
/jdi/ committed
75
76
77
78
		case 's':
			options.flags |= FLAG_PERMANENT;
			break;

/jdi/'s avatar
/jdi/ committed
79
80
81
82
		case 'w':
			options.flags |= FLAG_WAIT;
			break;

/jdi/'s avatar
/jdi/ committed
83
84
85
86
87
88
89
90
		case 't':
			options.timeout = atoi(optarg);
			break;

		case 'f':
			options.file = std::string(optarg);
			break;

/jdi/'s avatar
basics  
/jdi/ committed
91
		case 'p':
/jdi/'s avatar
/jdi/ committed
92
			options.password = std::string(optarg);
/jdi/'s avatar
/jdi/ committed
93
94
95
			break;

		case 'u':
/jdi/'s avatar
/jdi/ committed
96
			options.user = std::string(optarg);
/jdi/'s avatar
basics  
/jdi/ committed
97
98
			break;

/jdi/'s avatar
/jdi/ committed
99
		case 'i':
/jdi/'s avatar
/jdi/ committed
100
			options.interface = std::string(optarg);
/jdi/'s avatar
basics  
/jdi/ committed
101
102
103
104
			break;

		default: /* '?' */
			fprintf(stderr, "Unknown option\n");
/jdi/'s avatar
/jdi/ committed
105
			fprintf(stderr, USAGE, argv[0]);
/jdi/'s avatar
basics  
/jdi/ committed
106
107
108
109
			exit(EXIT_FAILURE);
		}
	}

/jdi/'s avatar
/jdi/ committed
110
111
112
	if (optind >= argc) {
		fprintf(stderr, "Command expected\n");
		fprintf(stderr, USAGE, argv[0]);
/jdi/'s avatar
basics  
/jdi/ committed
113
114
115
		exit(EXIT_FAILURE);
	}

/jdi/'s avatar
/jdi/ committed
116
	if (optind < argc) {
/jdi/'s avatar
/jdi/ committed
117
118
119
120
121
122
123
124
125
126
		std::string cmd = std::string(argv[optind++]);

		switch (caseArg(cmd.c_str())) {
		case caseArg("get"):
		case caseArg("set"):
		case caseArg("reboot"):
		case caseArg("reset"):
		case caseArg("save"):
		case caseArg("restore"):
		case caseArg("flash"):
/jdi/'s avatar
/jdi/ committed
127
128
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
/jdi/'s avatar
/jdi/ committed
129
130
131
			break;

		case caseArg("list"):
/jdi/'s avatar
/jdi/ committed
132
133
			if (p.list())
				exit(EXIT_SUCCESS);
/jdi/'s avatar
/jdi/ committed
134
135
136
			break;

		case caseArg("sniff"):
/jdi/'s avatar
/jdi/ committed
137
138
			if (p.sniff())
				exit(EXIT_SUCCESS);
/jdi/'s avatar
/jdi/ committed
139
140
141
			break;

		case caseArg("encode"):
/jdi/'s avatar
/jdi/ committed
142
143
144
145
146
147
148
149
150
			if (optind < argc) {
				std::string s(argv[optind]);
				optind++;
				if (p.encode(s))
					exit(EXIT_SUCCESS);
			} else {
				fprintf(stderr, "Argument expected after encode\n");
				exit(EXIT_FAILURE);
			}
/jdi/'s avatar
/jdi/ committed
151
152
153
154
155
156
157
158
159
160
			break;

		case caseArg("help"):
			fprintf(stderr, VERSION);
			fprintf(stderr, USAGE, argv[0]);
			fprintf(stderr, HELP);
			exit(EXIT_SUCCESS);
			break;

		default:
/jdi/'s avatar
/jdi/ committed
161
162
			printf("Unknown command: %s\n", argv[optind]);
			optind++;
/jdi/'s avatar
/jdi/ committed
163
164
165
166
167
			while (optind < argc) {
				printf("->%s\n", argv[optind]);
				optind++;
			}
			exit(EXIT_FAILURE);
/jdi/'s avatar
basics  
/jdi/ committed
168
169
		}
	}
/jdi/'s avatar
/jdi/ committed
170
	exit(EXIT_FAILURE);
/jdi/'s avatar
basics  
/jdi/ committed
171
172
}