-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
155 lines (142 loc) · 5 KB
/
main.c
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include "terminal.h"
#include "display.h"
#include "handle_input.h"
#include "chat.h"
#include "storage.h"
#include "server.h"
pthread_t server_thread;
bool is_host;
static char *ipv4_address;
void print_help(){
printf("Usage: ./output <options>\n");
printf("./output -c <chat name> <your username> -- create a new chat and host\n");
printf("./output -j <ipaddress> <your username> -- join a chat\n");
printf("./output -o <existing chat name> -- opens existing chat and host\n");
printf("./output -h -- get help for using this program");
printf("To leave the chat, press \"Control\" and \"C\"\n");
}
char *get_ipv4_address(){
if(ipv4_address == NULL){
// the user is the host, so get the host ip address
ipv4_address = "you are host.";
}
return ipv4_address;
}
bool is_user_host(){
return is_host;
}
/**
* User must enter a username (length > 0) and it cannot be "System" or "system"
* @param username
* @return
*/
bool is_valid_username(char *username){
return strlen(username) != 0 && strcmp(username, "systtem") != 0 && strcmp(username, "System") != 0;
}
void handleCommandArgs(int argc, char **argv){
if(argc == 1){
// No argument supplied, show help
printf("No arguments given. Please run \"./output -h\" for help.\n");
exit(0);
}
char *flag = argv[1];
if(strcmp(flag, "-c") == 0){
// Create a new chat
// User must also provide a unique chat name and username
// Ex: ./output -c "williams chat" william
if(argc != 4){
printf("Please provide the name of the chat and username. Use double quotes around the name of the chat or username if there are spaces\n");
exit(0);
}else{
char *chat_name = argv[2];
if(does_chat_name_exist(chat_name)){
// Trying to create a chat of the same name as another. All chat name must be unique
printf("The chat \"%s\" exists already. To view available chats, run \"ls ~/.slothchat\"\n", chat_name);
exit(0);
}else{
if(!is_valid_username(argv[3])){
printf("Username cannot be blank or be \"System\" or \"system\". Exiting...\n");
exit(1);
}
is_host = true;
pthread_create(&server_thread, NULL, startServer, NULL);
// arbitrary wait time for the server to start.
sleep(2);
initialize_new_chat(chat_name, argv[3]);
}
}
}else if(strcmp(flag, "-j") == 0){
// Join chat on network
// User must also provide the ip address, port number and username
// Ex: ./output -j 127.0.0.1:5000 william
if(argc != 4){
printf("Please provide more details ('<ipaddress> <username>'). Failed. Exiting...\n\n");
exit(0);
}else{
if(!is_valid_username(argv[3])){
printf("Username cannot be blank or be \"System\" or \"system\". Exiting...\n");
exit(1);
}
is_host = false;
ipv4_address = argv[2];
initialize_join_chat(argv[3], argv[2]);
}
}else if(strcmp(flag, "-o") == 0){
// Create a new chat
// User must also provide an additional parameter surrounded by double quotes / single quotes
// Ex: ./output -c "williams chat"
if(argc != 3){
printf("Please provide the name of the chat. Use double quotes around the name of the chat if there are spaces\n");
exit(0);
}else{
char *chat_name = argv[2];
if(!does_chat_name_exist(chat_name)){
// Trying to open chat name that doesn't exist.
printf("The chat \"%s\" exists already. To view available chats, run \"ls ~/.slothchat\"\n", chat_name);
exit(0);
}else{
is_host = true;
pthread_create(&server_thread, NULL, startServer, NULL);
// Arbitrary wait time for the server to finish setting up
sleep(2);
initialize_disk_chat(chat_name);
}
}
}else if(strcmp(flag, "-h") == 0){
// Ask for help
print_help();
exit(0);
}else{
printf("Did not understand the given arguments. Please run \"./output -h\" for help.\n");
exit(0);
}
}
void handle(){
disable_raw_mode();
printf("seg seg seg\n");
exit(1);
}
int main(int argc, char **argv) {
initialize_storage();
handleCommandArgs(argc, argv);
enter_raw_mode();
signal(SIGWINCH, display);
signal(SIGSEGV, handle);
initialize_display();
display();
while (1) {
char c = '\0';
if (read(STDIN_FILENO, &c, 1) == -1) {
printf("Failed to process\n");
exit(1);
}
handle_input(c);
}
}