summaryrefslogblamecommitdiffstats
path: root/src/main.c
blob: df55b80be0d63ba74466cbbfb7c2c1ed577ef482 (plain) (tree)
1
2
3
4
5
6
7
8
9

                         

                       
              
      
               

                                   
                
                                          
      


                                        
                            
                                                







                                                                                 



                                                           


                                                                        











                                                                             

                                        




                                                                 

                                






                                                                 

                                



                                                               




                                                                       


                                       
                
                       
      

                                                      

                  
#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;
}