Subversion Repositories HomeAutomation

Rev

Rev 1986 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1986 Rev 1987
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include <string>
21
#include <string>
22
#include <vector>
22
#include <vector>
23
#include <iostream>
23
#include <iostream>
24
 
24
 
25
#include <signal.h>
25
#include <signal.h>
26
#include <stdio.h>
26
#include <stdio.h>
27
#include <stdlib.h>
27
#include <stdlib.h>
28
#include <pwd.h>
28
#include <pwd.h>
29
 
29
 
30
#include <boost/lexical_cast.hpp>
30
#include <boost/lexical_cast.hpp>
31
#include <boost/thread/mutex.hpp>
31
#include <boost/thread/mutex.hpp>
32
#include <boost/thread/condition.hpp>
32
#include <boost/thread/condition.hpp>
33
#include <boost/thread/locks.hpp>
33
#include <boost/thread/locks.hpp>
34
#include <boost/algorithm/string.hpp>
34
#include <boost/algorithm/string.hpp>
35
#include <boost/program_options.hpp>
35
#include <boost/program_options.hpp>
36
 
36
 
37
#include "config.h"
37
#include "config.h"
38
 
38
 
39
#include "net/Manager.h"
39
#include "net/Manager.h"
40
#include "net/Subscriber.h"
40
#include "net/Subscriber.h"
41
#include "net/types.h"
41
#include "net/types.h"
42
 
42
 
43
#include "common/common.h"
43
#include "common/common.h"
-
 
44
#include "common/log.h"
44
 
45
 
45
#include <readline/readline.h>
46
#include <readline/readline.h>
46
#include <readline/history.h>
47
#include <readline/history.h>
47
 
48
 
48
using namespace atom;
49
using namespace atom;
49
 
50
 
50
bool finish = false;
51
bool finish = false;
51
common::StringList autocomplete_list;
52
common::StringList autocomplete_list;
52
char* buffer = NULL;
53
char* buffer = NULL;
53
boost::condition on_message_condition;
54
boost::condition on_message_condition;
54
struct termios original_flags;
55
struct termios original_flags;
55
std::string history_filename;
56
std::string history_filename;
56
 
57
 
57
void Handler(int status);
58
void Handler(int status);
58
void CleanUp();
59
void CleanUp();
59
 
60
 
60
char* AutoCompleteGet(const char* text, int state);
61
char* AutoCompleteGet(const char* text, int state);
61
static char** AutoComplete(const char* text, int start, int end);
62
static char** AutoComplete(const char* text, int start, int end);
62
 
63
 
63
std::string GetUserHomeDirectory()
64
std::string GetUserHomeDirectory()
64
{
65
{
65
    return std::string(getpwuid(getuid())->pw_dir);
66
    return std::string(getpwuid(getuid())->pw_dir);
66
}
67
}
67
 
68
 
68
class ConsoleClient : public net::Subscriber
69
class ConsoleClient : public net::Subscriber
69
{
70
{
70
public:
71
public:
71
    typedef boost::shared_ptr<ConsoleClient> Pointer;
72
    typedef boost::shared_ptr<ConsoleClient> Pointer;
72
   
73
   
73
    ConsoleClient(std::string address, unsigned int port)
74
    ConsoleClient(std::string address, unsigned int port)
74
    {
75
    {
75
        this->client_id_ = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, address, port);
76
        this->client_id_ = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, address, port);
76
    }
77
    }
77
   
78
   
78
    virtual ~ConsoleClient()
79
    virtual ~ConsoleClient()
79
    {
80
    {
80
        net::Manager::Instance()->Disconnect(this->client_id_);
81
        net::Manager::Instance()->Disconnect(this->client_id_);
81
 
82
 
82
        this->io_service_.stop();
83
        this->io_service_.stop();
83
    }
84
    }
84
   
85
   
85
    std::string GetPrompt()
86
    std::string GetPrompt()
86
    {
87
    {
87
        return this->prompt_;
88
        return this->prompt_;
88
    }
89
    }
89
   
90
   
90
    void SendResponse(std::string payload)
91
    void SendResponse(std::string payload)
91
    {
92
    {
92
        std::string packet = "RESP";
93
        std::string packet = "RESP";
93
        packet += common::PadNumber(payload.length() + 1, 4);
94
        packet += common::PadNumber(payload.length() + 1, 4);
94
        packet += payload;
95
        packet += payload;
95
       
96
       
96
        net::Manager::Instance()->SendTo(this->client_id_, packet);
97
        net::Manager::Instance()->SendTo(this->client_id_, common::Byteset(packet.begin(), packet.end()));
97
    }
98
    }
98
   
99
   
99
    void AutoCompleteRequest(unsigned int arg_index, std::string commandline)
100
    void AutoCompleteRequest(unsigned int arg_index, std::string commandline)
100
    {
101
    {
101
        std::string payload = common::PadNumber(arg_index, 4);
102
        std::string payload = common::PadNumber(arg_index, 4);
102
        payload += commandline;
103
        payload += commandline;
103
       
104
       
104
        std::string packet = "COMP";
105
        std::string packet = "COMP";
105
        packet += common::PadNumber(payload.length() + 1, 4);
106
        packet += common::PadNumber(payload.length() + 1, 4);
106
        packet += payload;
107
        packet += payload;
107
       
108
       
108
        net::Manager::Instance()->SendTo(this->client_id_, packet);
109
        net::Manager::Instance()->SendTo(this->client_id_, common::Byteset(packet.begin(), packet.end()));
109
    }
110
    }
110
   
111
   
111
private:
112
private:
112
    net::SocketId client_id_;
113
    net::SocketId client_id_;
113
    std::string prompt_;
114
    std::string prompt_;
114
    std::vector<unsigned char> buffer_;
115
    std::vector<unsigned char> buffer_;
115
   
116
   
116
    void SlotOnNewStateHandler(net::SocketId client_id, net::ClientState client_state)
117
    void SlotOnNewStateHandler(net::SocketId client_id, net::ClientState client_state)
117
    {
118
    {
118
        if (client_state != net::CLIENT_STATE_CONNECTED)
119
        if (client_state != net::CLIENT_STATE_CONNECTED)
119
        {
120
        {
120
            std::cout << "Disconnected from server." << std::endl;
121
            std::cout << "Disconnected from server." << std::endl;
121
           
122
           
122
            this->client_id_ = 0;
123
            this->client_id_ = 0;
123
            finish = true;
124
            finish = true;
124
            on_message_condition.notify_all();
125
            on_message_condition.notify_all();
125
        }
126
        }
126
    }
127
    }
127
   
128
   
128
    void SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
129
    void SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
129
    {
130
    {
130
    }
131
    }
131
   
132
   
132
    void SlotOnNewDataHandler(net::SocketId client_id, common::Byteset data)
133
    void SlotOnNewDataHandler(net::SocketId client_id, common::Byteset data)
133
    {
134
    {
134
        for (unsigned int n = 0; n < data.GetSize(); n++)
135
        for (unsigned int n = 0; n < data.size(); n++)
135
        {
136
        {
136
            this->buffer_.push_back(data[n]);
137
            this->buffer_.push_back(data[n]);
137
        }
138
        }
138
 
139
 
139
        while (this->buffer_.size() >= 8)
140
        while (this->buffer_.size() >= 8)
140
        {
141
        {
141
            std::string command = "";
142
            std::string command = "";
142
            command += (char)this->buffer_[0];
143
            command += (char)this->buffer_[0];
143
            command += (char)this->buffer_[1];
144
            command += (char)this->buffer_[1];
144
            command += (char)this->buffer_[2];
145
            command += (char)this->buffer_[2];
145
            command += (char)this->buffer_[3];
146
            command += (char)this->buffer_[3];
146
 
147
 
147
            std::string payload_length_str = "";
148
            std::string payload_length_str = "";
148
            payload_length_str += (char)this->buffer_[4];
149
            payload_length_str += (char)this->buffer_[4];
149
            payload_length_str += (char)this->buffer_[5];
150
            payload_length_str += (char)this->buffer_[5];
150
            payload_length_str += (char)this->buffer_[6];
151
            payload_length_str += (char)this->buffer_[6];
151
            payload_length_str += (char)this->buffer_[7];
152
            payload_length_str += (char)this->buffer_[7];
152
           
153
           
153
            unsigned int payload_length = boost::lexical_cast<unsigned int>(payload_length_str);
154
            unsigned int payload_length = boost::lexical_cast<unsigned int>(payload_length_str) - 1;
154
           
155
           
155
            if (this->buffer_.size() - 8 < payload_length)
156
            if (this->buffer_.size() - 8 < payload_length)
156
            {
157
            {
157
                return;
158
                return;
158
            }
159
            }
159
         
160
         
160
            std::string payload = "";
161
            std::string payload = "";
161
            for (unsigned int n = 8; n < payload_length + 8; n++)
162
            for (unsigned int n = 8; n < payload_length + 8; n++)
162
            {
163
            {
163
                payload += (char)this->buffer_[n];
164
                payload += (char)this->buffer_[n];
164
            }
165
            }
165
           
166
           
166
            this->buffer_.erase(this->buffer_.begin(), this->buffer_.begin() + payload_length + 8);
167
            this->buffer_.erase(this->buffer_.begin(), this->buffer_.begin() + payload_length + 8);
167
           
168
           
168
            if (command == "TEXT")
169
            if (command == "TEXT")
169
            {
170
            {
170
                std::cout << payload << std::flush;
171
                std::cout << payload << std::flush;
171
            }
172
            }
172
            else if (command == "PROM")
173
            else if (command == "PROM")
173
            {
174
            {
174
                this->prompt_ = payload;
175
                this->prompt_ = payload;
175
                on_message_condition.notify_all();
176
                on_message_condition.notify_all();
176
            }
177
            }
177
            else if (command == "COMP")
178
            else if (command == "COMP")
178
            {
179
            {
179
                autocomplete_list.clear();
180
                autocomplete_list.clear();
180
               
181
               
181
                if (payload.length() > 0)
182
                if (payload.length() > 0)
182
                {
183
                {
183
                    boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
184
                    boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
184
                }
185
                }
185
               
186
               
186
                on_message_condition.notify_all();
187
                on_message_condition.notify_all();
187
            }
188
            }
188
            else
189
            else
189
            {
190
            {
190
                std::cerr << "Could not parse package." << std::endl;
191
                std::cerr << "Could not parse package." << std::endl;
191
                finish = true;
192
                finish = true;
192
                on_message_condition.notify_all();
193
                on_message_condition.notify_all();
193
                break;
194
                break;
194
            }
195
            }
195
        }
196
        }
196
    }
197
    }
197
};
198
};
198
 
199
 
199
ConsoleClient::Pointer cc;
200
ConsoleClient::Pointer cc;
200
 
201
 
201
 
202
 
202
int main(int argc, char **argv)
203
int main(int argc, char **argv)
203
{
204
{
204
    // Signal handlers
205
    // Signal handlers
205
    signal(SIGTERM, Handler);
206
    signal(SIGTERM, Handler);
206
    signal(SIGINT, Handler);
207
    signal(SIGINT, Handler);
207
    signal(SIGQUIT, Handler);
208
    signal(SIGQUIT, Handler);
208
    signal(SIGABRT, Handler);
209
    signal(SIGABRT, Handler);
209
    signal(SIGPIPE, Handler);
210
    signal(SIGPIPE, Handler);
210
   
211
   
211
    boost::mutex guard_mutex;
212
    boost::mutex guard_mutex;
212
   
213
   
213
    // Setup readline
214
    // Setup readline
214
    history_filename = GetUserHomeDirectory() + "/.atomic_history";
215
    history_filename = GetUserHomeDirectory() + "/.atomic_history";
215
    read_history(history_filename.data());
216
    read_history(history_filename.data());
216
   
217
   
217
    rl_attempted_completion_function = AutoComplete;
218
    rl_attempted_completion_function = AutoComplete;
218
   
219
   
219
    // Save command line state
220
    // Save command line state
220
    tcgetattr(fileno(stdin), &original_flags);
221
    tcgetattr(fileno(stdin), &original_flags);
221
   
222
   
222
    // Parse commandline
223
    // Parse commandline
223
    boost::program_options::options_description command_line;
224
    boost::program_options::options_description command_line;
224
    boost::program_options::variables_map variable_map;
225
    boost::program_options::variables_map variable_map;
-
 
226
   
-
 
227
    //log::SetLevel(log::LOG_LEVEL_ALL);
225
   
228
   
226
    command_line.add_options()
229
    command_line.add_options()
227
    ("help,h",    "produce help message")
230
    ("help,h",    "produce help message")
228
    ("command,c", boost::program_options::value<std::string>()->default_value(""), "command")
231
    ("command,c", boost::program_options::value<std::string>()->default_value(""), "command")
229
    ("server,s",  boost::program_options::value<std::string>()->default_value("localhost"), "server address")
232
    ("server,s",  boost::program_options::value<std::string>()->default_value("localhost"), "server address")
230
    ("port,p",    boost::program_options::value<unsigned int>()->default_value(1202), "server port");
233
    ("port,p",    boost::program_options::value<unsigned int>()->default_value(1202), "server port");
231
   
234
   
232
    try
235
    try
233
    {
236
    {
234
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map);
237
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map);
235
    }
238
    }
236
    catch (boost::program_options::unknown_option e)
239
    catch (boost::program_options::unknown_option e)
237
    {
240
    {
238
        std::cerr << e.what() << std::endl;
241
        std::cerr << e.what() << std::endl;
239
        std::cout << command_line << std::endl;
242
        std::cout << command_line << std::endl;
240
        CleanUp();
243
        CleanUp();
241
        return EXIT_FAILURE;
244
        return EXIT_FAILURE;
242
    }
245
    }
243
    catch (boost::program_options::invalid_syntax e)
246
    catch (boost::program_options::invalid_syntax e)
244
    {
247
    {
245
        std::cerr << e.what() << std::endl;
248
        std::cerr << e.what() << std::endl;
246
        std::cout << command_line << std::endl;
249
        std::cout << command_line << std::endl;
247
        CleanUp();
250
        CleanUp();
248
        return EXIT_FAILURE;
251
        return EXIT_FAILURE;
249
    }
252
    }
250
   
253
   
251
    if (variable_map.count("help") != 0)
254
    if (variable_map.count("help") != 0)
252
    {
255
    {
253
        std::cout << command_line << std::endl;
256
        std::cout << command_line << std::endl;
254
        CleanUp();
257
        CleanUp();
255
        return EXIT_SUCCESS;
258
        return EXIT_SUCCESS;
256
    }
259
    }
257
   
260
   
258
    net::Manager::Create();
261
    net::Manager::Create();
259
 
262
 
260
    if (variable_map["command"].as<std::string>() == "")
263
    if (variable_map["command"].as<std::string>() == "")
261
    {
264
    {
262
        std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl;
265
        std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl;
263
        std::cout << "\033[29;1mReleased under " + std::string(LICENSE) + ".\033[0m" << std::endl;
266
        std::cout << "\033[29;1mReleased under " + std::string(LICENSE) + ".\033[0m" << std::endl;
264
        std::cout << "Written by Mattias Runge 2010." << std::endl;
267
        std::cout << "Written by Mattias Runge 2010." << std::endl;
265
       
268
       
266
        std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl;
269
        std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl;
267
    }
270
    }
268
   
271
   
269
    try
272
    try
270
    {
273
    {
271
        cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>()));
274
        cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>()));
272
    }
275
    }
273
    catch (std::runtime_error& e)
276
    catch (std::runtime_error& e)
274
    {
277
    {
275
        std::cerr << "Connection error: " << e.what() << std::endl;
278
        std::cerr << "Connection error: " << e.what() << std::endl;
276
        CleanUp();
279
        CleanUp();
277
        return EXIT_FAILURE;
280
        return EXIT_FAILURE;
278
    }
281
    }
279
   
282
   
280
    if (variable_map["command"].as<std::string>() != "")
283
    if (variable_map["command"].as<std::string>() != "")
281
    {
284
    {
282
        boost::mutex::scoped_lock guard(guard_mutex);
285
        boost::mutex::scoped_lock guard(guard_mutex);
283
        on_message_condition.wait(guard);
286
        on_message_condition.wait(guard);
284
       
287
       
285
        if (!finish)
288
        if (!finish)
286
        {
289
        {
287
            cc->SendResponse(variable_map["command"].as<std::string>());
290
            cc->SendResponse(variable_map["command"].as<std::string>());
288
           
291
           
289
            if (!finish)
292
            if (!finish)
290
            {
293
            {
291
                on_message_condition.wait(guard);
294
                on_message_condition.wait(guard);
292
            }
295
            }
293
        }
296
        }
294
    }
297
    }
295
    else
298
    else
296
    {
299
    {
297
        while (true)
300
        while (true)
298
        {
301
        {
299
            boost::mutex::scoped_lock guard(guard_mutex);
302
            boost::mutex::scoped_lock guard(guard_mutex);
300
            on_message_condition.wait(guard);
303
            on_message_condition.wait(guard);
301
           
304
           
302
            if (finish)
305
            if (finish)
303
            {
306
            {
304
                break;
307
                break;
305
            }
308
            }
306
           
309
           
307
            while ((buffer = readline(cc->GetPrompt().data())) != NULL)
310
            while ((buffer = readline(cc->GetPrompt().data())) != NULL)
308
            {
311
            {
309
                if (finish)
312
                if (finish)
310
                {
313
                {
311
                    break;
314
                    break;
312
                }
315
                }
313
               
316
               
314
                if (strlen(buffer) == 0)
317
                if (strlen(buffer) == 0)
315
                {
318
                {
316
                    continue;
319
                    continue;
317
                }
320
                }
318
               
321
               
319
                break;
322
                break;
320
            }
323
            }
321
           
324
           
322
            if (finish)
325
            if (finish)
323
            {
326
            {
324
                break;
327
                break;
325
            }
328
            }
326
           
329
           
327
            cc->SendResponse(buffer);
330
            cc->SendResponse(buffer);
328
            add_history(buffer);
331
            add_history(buffer);
329
        }
332
        }
330
    }
333
    }
331
   
334
   
332
    CleanUp();
335
    CleanUp();
333
   
336
   
334
    return EXIT_SUCCESS;
337
    return EXIT_SUCCESS;
335
}
338
}
336
 
339
 
337
static char** AutoComplete(const char* text, int start, int end)
340
static char** AutoComplete(const char* text, int start, int end)
338
{
341
{
339
    unsigned int count = 0;
342
    unsigned int count = 0;
340
    boost::mutex guard_mutex;
343
    boost::mutex guard_mutex;
341
 
344
 
342
    for (unsigned int n = 0; n < (unsigned int)start; n++)
345
    for (unsigned int n = 0; n < (unsigned int)start; n++)
343
    {
346
    {
344
        if (rl_line_buffer[n] == ' ')
347
        if (rl_line_buffer[n] == ' ')
345
        {
348
        {
346
            count++;
349
            count++;
347
        }
350
        }
348
    }
351
    }
349
   
352
   
350
    cc->AutoCompleteRequest(count, rl_line_buffer);
353
    cc->AutoCompleteRequest(count, rl_line_buffer);
351
   
354
   
352
    boost::mutex::scoped_lock guard(guard_mutex);
355
    boost::mutex::scoped_lock guard(guard_mutex);
353
    on_message_condition.wait(guard);
356
    on_message_condition.wait(guard);
354
   
357
   
355
    /*if (autocomplete_list.size() == 0)
358
    /*if (autocomplete_list.size() == 0)
356
    {
359
    {
357
        return NULL;
360
        return NULL;
358
    }*/
361
    }*/
359
   
362
   
360
    return rl_completion_matches(text, &AutoCompleteGet);
363
    return rl_completion_matches(text, &AutoCompleteGet);
361
}
364
}
362
 
365
 
363
char* AutoCompleteGet(const char* text, int state)
366
char* AutoCompleteGet(const char* text, int state)
364
{
367
{
365
    static int index = 0;
368
    static int index = 0;
366
    int length = strlen(text);
369
    int length = strlen(text);
367
    std::string name;
370
    std::string name;
368
   
371
   
369
    if (!state) // First run
372
    if (!state) // First run
370
    {
373
    {
371
        index = 0;
374
        index = 0;
372
    }
375
    }
373
   
376
   
374
    while (autocomplete_list.size() > (unsigned int) index)
377
    while (autocomplete_list.size() > (unsigned int) index)
375
    {
378
    {
376
        name = autocomplete_list[index];
379
        name = autocomplete_list[index];
377
       
380
       
378
        index++;
381
        index++;
379
       
382
       
380
        if (strncmp(name.data(), text, length) == 0)
383
        if (strncmp(name.data(), text, length) == 0)
381
        {
384
        {
382
            char *result = (char*)malloc(name.size() + 1);
385
            char *result = (char*)malloc(name.size() + 1);
383
            strcpy(result, name.data());
386
            strcpy(result, name.data());
384
           
387
           
385
            return result;
388
            return result;
386
        }
389
        }
387
    }
390
    }
388
   
391
   
389
    return NULL;
392
    return NULL;
390
}
393
}
391
 
394
 
392
void CleanUp()
395
void CleanUp()
393
{
396
{
394
    std::cout << std::endl;
397
    std::cout << std::endl;
395
    //std::cout << "Cleaning up..." << std::endl;
398
    //std::cout << "Cleaning up..." << std::endl;
396
 
399
 
397
    write_history(history_filename.data());
400
    write_history(history_filename.data());
398
   
401
   
399
    cc.reset();
402
    cc.reset();
400
   
403
   
401
    net::Manager::Delete();
404
    net::Manager::Delete();
402
   
405
   
403
    if (buffer != NULL)
406
    if (buffer != NULL)
404
    {
407
    {
405
        free(buffer);
408
        free(buffer);
406
    }
409
    }
407
   
410
   
408
    //std::cout << "Thank you for using Atom. Goodbye!" << std::endl;
411
    //std::cout << "Thank you for using Atom. Goodbye!" << std::endl;
409
   
412
   
410
    tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore
413
    tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore
411
}
414
}
412
 
415
 
413
void Handler(int status)
416
void Handler(int status)
414
{
417
{
415
    std::string signal_name = "Unknown";
418
    std::string signal_name = "Unknown";
416
   
419
   
417
    switch (status)
420
    switch (status)
418
    {
421
    {
419
        case SIGTERM:
422
        case SIGTERM:
420
        {
423
        {
421
            signal_name = "Terminate";
424
            signal_name = "Terminate";
422
            break;
425
            break;
423
        }  
426
        }  
424
        case SIGINT:
427
        case SIGINT:
425
        {
428
        {
426
            signal_name = "Interrupt";
429
            signal_name = "Interrupt";
427
            break;
430
            break;
428
        }  
431
        }  
429
        case SIGQUIT:
432
        case SIGQUIT:
430
        {
433
        {
431
            signal_name = "Quit";
434
            signal_name = "Quit";
432
            break;
435
            break;
433
        }  
436
        }  
434
        case SIGABRT:
437
        case SIGABRT:
435
        {
438
        {
436
            signal_name = "Abort";
439
            signal_name = "Abort";
437
            break;
440
            break;
438
        }  
441
        }  
439
        case SIGIO:
442
        case SIGIO:
440
        {
443
        {
441
            signal_name = "I/O";
444
            signal_name = "I/O";
442
            break;
445
            break;
443
        }  
446
        }  
444
        case SIGPIPE:
447
        case SIGPIPE:
445
        {
448
        {
446
            signal_name = "Pipe";
449
            signal_name = "Pipe";
447
            break;
450
            break;
448
        }
451
        }
449
    }
452
    }
450
   
453
   
451
   
454
   
452
    if (status != SIGPIPE)
455
    if (status != SIGPIPE)
453
    {
456
    {
454
        CleanUp();
457
        CleanUp();
455
        exit(0);
458
        exit(0);
456
    }
459
    }
457
}
460
}
458
 
461
 
459
 
462