4dffd4aa53fdb04437204a0d43a857db5d9cb2f2
[polyglot.git] / engine.c
1 // engine.c\r
2 \r
3 // includes\r
4 \r
5 #include <stdlib.h>\r
6 #include <stdarg.h>\r
7 #include <string.h>\r
8 #include <errno.h>\r
9 \r
10 \r
11 #include "engine.h"\r
12 #include "option.h"\r
13 #include "pipex.h"\r
14 #include "util.h"\r
15 \r
16 // defines\r
17 \r
18 #define StringSize 4096\r
19 \r
20 // variables\r
21 \r
22 static int write_index = 0;\r
23 static char write_buffer[StringSize];\r
24 engine_t Engine[1];\r
25 \r
26 // functions\r
27 \r
28 // set_affinity()\r
29 \r
30 void set_affinity(engine_t *engine, int value){\r
31     pipex_set_affinity(engine->pipex,value);\r
32 }\r
33 \r
34 // engine_set_nice_value()\r
35 \r
36 void engine_set_nice_value(engine_t *engine, int value){\r
37     pipex_set_priority(engine->pipex,value);\r
38 }\r
39 \r
40 // engine_send_queue()\r
41 \r
42 void engine_send_queue(engine_t * engine, const char *format, ...) {\r
43     if(write_index>=StringSize){\r
44         my_fatal("engine_send_queue(): write_buffer overflow\n");\r
45     }\r
46     write_index += vsnprintf(write_buffer + write_index,\r
47                             StringSize-write_index,\r
48                              format,\r
49                             (va_list) (&format + 1));\r
50 }\r
51 \r
52 // engine_send()\r
53 \r
54 void engine_send(engine_t * engine, const char *format, ...) {\r
55     if(write_index>=StringSize){\r
56         my_fatal("engine_send(): write_buffer overflow\n");\r
57     }\r
58     vsnprintf(write_buffer + write_index,\r
59               StringSize-write_index,\r
60               format,\r
61               (va_list) (&format + 1));\r
62     pipex_writeln(engine->pipex,write_buffer);\r
63     write_index = 0;\r
64 }\r
65 \r
66 // engine_close()\r
67 \r
68 void engine_close(engine_t * engine){\r
69     char string[StringSize];\r
70     pipex_send_eof(engine->pipex);\r
71         // TODO: Timeout\r
72     while (!engine_eof(engine)) { \r
73       engine_get(Engine,string);\r
74     }\r
75     pipex_exit(engine->pipex);\r
76 }\r
77 \r
78 // engine_open()\r
79 \r
80 void engine_open(engine_t * engine){\r
81     int affinity;\r
82     char *my_dir;\r
83     if( (my_dir = my_getcwd( NULL, 0 )) == NULL )\r
84         my_fatal("engine_open(): no current directory: %s\n",strerror(errno));\r
85     if(my_chdir(option_get_string("EngineDir"))){\r
86         my_fatal("engine_open(): cannot change directory: %s\n",strerror(errno));\r
87     }\r
88     pipex_open(engine->pipex,"Engine",option_get_string("EngineCommand"));\r
89     if(pipex_active(engine->pipex)){\r
90             //play with affinity (bad idea)\r
91         affinity=option_get_int("Affinity");\r
92         if(affinity!=-1) set_affinity(engine,affinity); //AAA\r
93             //lets go back\r
94         my_chdir(my_dir);\r
95             // set a low priority\r
96         if (option_get_bool("UseNice")){\r
97             my_log("POLYGLOT Adjust Engine Piority\n");\r
98             engine_set_nice_value(engine, option_get_int("NiceValue"));\r
99         }\r
100     }\r
101     \r
102 }\r
103 \r
104 bool engine_active(engine_t *engine){\r
105     return pipex_active(engine->pipex);\r
106 }\r
107 \r
108 bool engine_eof(engine_t *engine){\r
109     return pipex_eof(engine->pipex);\r
110 }\r
111 \r
112 bool engine_get_non_blocking(engine_t * engine, char *string){\r
113     return pipex_readln_nb(engine->pipex,string);\r
114  }\r
115 \r
116 void engine_get(engine_t * engine, char *string){\r
117     pipex_readln(engine->pipex,string);\r
118 }\r
119 \r
120 \r