-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpio_demo.c
61 lines (51 loc) · 1.79 KB
/
simpio_demo.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
#include "blather.h"
simpio_t simpio_actual;
simpio_t *simpio = &simpio_actual;
client_t client_actual;
client_t *client = &client_actual;
pthread_t user_thread; // thread managing user input
pthread_t background_thread;
// Worker thread to manage user input
void *user_worker(void *arg){
int count = 1;
while(!simpio->end_of_input){
simpio_reset(simpio);
iprintf(simpio, ""); // print prompt
while(!simpio->line_ready && !simpio->end_of_input){ // read until line is complete
simpio_get_char(simpio);
}
if(simpio->line_ready){
iprintf(simpio, "%2d You entered: %s\n",count,simpio->buf);
count++;
}
}
pthread_cancel(background_thread); // kill the background thread
return NULL;
}
// Worker thread to listen to the info from the server.
void *background_worker(void *arg){
char *text[3] = {
"Background text #1",
"Background text #2",
"Background text #3",
};
for(int i=0; ; i++){
sleep(3);
iprintf(simpio, "BKGND: %s\n",text[i % 3]);
}
return NULL;
}
int main(int argc, char *argv[]){
char prompt[MAXNAME];
snprintf(prompt, MAXNAME, "%s>> ","fgnd"); // create a prompt string
simpio_set_prompt(simpio, prompt); // set the prompt
simpio_reset(simpio); // initialize io
simpio_noncanonical_terminal_mode(); // set the terminal into a compatible mode
pthread_create(&user_thread, NULL, user_worker, NULL); // start user thread to read input
pthread_create(&background_thread, NULL, background_worker, NULL);
pthread_join(user_thread, NULL);
pthread_join(background_thread, NULL);
simpio_reset_terminal_mode();
printf("\n"); // newline just to make returning to the terminal prettier
return 0;
}