summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: df55b80be0d63ba74466cbbfb7c2c1ed577ef482 (plain) (blame)
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
#define _XOPEN_SOURCE 600
#include <stdio.h>
/* #define DC_NETREQ */
#ifdef DC_NETREQ
FILE * netreq;
#endif
#include <ui.c>
int main (int argc, char ** argv) {
	srand(time(NULL));
#ifdef DC_NETREQ
	netreq = fopen("netreq.log", "w");
#endif
	int rs = 0;
	int opt;
	pthread_t api_thread, ui_thread;
	int api_ret, ui_ret;
	struct dc_client * c = dc_client_init();
	struct dc_thread_control t = {
		.power_api = 0,
		.power_ui = 1, /* so we don't start without user interface lol */
		.clients = &c,
		.clients_sizeof = 1,
		.cout = stdout,
		.cin = stdin,
		.cerr = stderr
	};
	while ((opt = getopt(argc, argv, "e:p:h")) != -1) {
		switch (opt) {
			case 'h':
				fprintf(stdout, DC_I18N_USAGE, argv[0]);
				rs = 4;
				goto rc;
				break;
			case 'e':
				c->email = malloc(strlen(optarg)+1);
				strcpy(c->email, optarg);
				break;
			case 'p':
				c->password = malloc(strlen(optarg)+1);
				strcpy(c->password, optarg);
				break;
			default:
				fprintf(stderr, DC_I18N_UNREC_ARG "\n", opt);
				dc_client_free(c);
				rs = 1;
				goto rc;
		}
	}
	if (!c->email) {
		if (!getenv("DC_E")) {
			fprintf(stderr, DC_I18N_MISSING_EP "\n");
			rs = 2;
			goto rc;
		}
		c->email = malloc(strlen(getenv("DC_E"))+1);
		strcpy(c->email, getenv("DC_E"));
	}
	if (!c->password) {
		if (!getenv("DC_P")) {
			fprintf(stderr, DC_I18N_MISSING_EP "\n");
			rs = 3;
			goto rc;
		}
		c->password = malloc(strlen(getenv("DC_P"))+1);
		strcpy(c->password, getenv("DC_P"));
	}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
	api_ret = pthread_create(&api_thread, NULL, dc_api_thread, &t);
	ui_ret = pthread_create(&ui_thread, NULL, dc_ui_thread, &t);
#pragma GCC diagnostic pop
	pthread_join(api_thread, NULL);
	pthread_join(ui_thread, NULL);
	rc:
#ifdef DC_NETREQ
	fclose(netreq);
#endif
	dc_client_free(c);
	if (api_ret || ui_ret); /* to hide warnings */
	return rs;
}