-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
108 lines (92 loc) · 1.96 KB
/
main.cpp
File metadata and controls
108 lines (92 loc) · 1.96 KB
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
//main.cpp
#include <iostream>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include "parser.h"
#include "sequencer.h"
#include "error.h"
sem_t sem;
Sequencer seq;
Sequencer *seqPointer=&seq;
bool exitFlag=false;
void *thread(void *arg) {
char *ret;
//printf("thread() entered with argument '%s'\n",(char *)arg);
if ((ret = (char*) malloc(20)) == NULL) {
perror("malloc() error");
exit(2);
}
//strcpy(ret, "This is a test");
while(!exitFlag) {
sem_wait(&sem);
seq.loop();
sem_post(&sem);
usleep(100);
sem_wait(&sem);
seq.send_events();
sem_post(&sem);
}
pthread_exit(ret);
}
void sigterm_exit(int sig) {
printf("sigterm_exit\n");
seq.stop();
exit(0);
}
int main(int argc, char *argv[]) {
if(argc!=2) {
printError("Expected 1 argument\n");
}
Parser p(&seq,argv[1]);
seq.parser=&p;
seq.open_seq("default","seq");
seq.init_queue();
seq.start_queue();
p.parse();
signal(SIGINT, sigterm_exit);
signal(SIGTERM, sigterm_exit);
seq.send_sequences();
if(seq.continueFlag) {
pthread_t thid;
void *ret;
if (sem_init(&sem, 0, 1) == -1) printError("Error creating semaphore");
if (pthread_create(&thid, NULL, thread, (void *)"thread 1") != 0) {
printError("Error creating thread");
}
while(1) {
char *line=readline (">");
if(line) {
if (line[0]) {
if(line[0]=='.') {
free(line);
break;
}
add_history(line);
sem_wait(&sem);
p.parse(line);
sem_post(&sem);
usleep(100);
sem_wait(&sem);
seq.send_sequences();
sem_post(&sem);
}
free(line);
}
}
seq.stop();
exitFlag=true;
if (pthread_join(thid, &ret) != 0) {
printError("Error ending thread");
}
} else {
while(1) {
seq.loop();
usleep(100000);
seq.send_events();
}
}
}