File: command_file.cpp

package info (click to toggle)
bino 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,636 kB
  • sloc: cpp: 21,288; sh: 4,849; makefile: 314; sed: 16
file content (179 lines) | stat: -rw-r--r-- 5,040 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * This file is part of bino, a 3D video player.
 *
 * Copyright (C) 2012, 2015
 * Martin Lambers <marlam@marlam.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "base/exc.h"
#include "base/str.h"
#include "base/tmr.h"

#include "base/gettext.h"
#define _(string) gettext(string)

#include "command_file.h"

/* Windows portability. */
#ifndef O_NONBLOCK
# define O_NONBLOCK 0
#endif
#ifndef EWOULDBLOCK
# define EWOULDBLOCK EAGAIN
#endif


command_file::command_file(const std::string& filename) :
    _filename(filename), _fd(-1)
{
}

command_file::~command_file()
{
    deinit();
}

void command_file::init()
{
    if (_fd < 0) {
        _fd = ::open(_filename.c_str(), O_RDONLY | O_NONBLOCK);
        if (_fd < 0) {
            throw exc(str::asprintf(_("%s: %s"), _filename.c_str(), ::strerror(errno)), errno);
        }
        _is_fifo = false;
        struct stat statbuf;
        if (fstat(_fd, &statbuf) == 0 && S_ISFIFO(statbuf.st_mode))
            _is_fifo = true;
        _linebuf.clear();
        _wait_until_stop = false;
        _wait_until = -1;
    }
}

void command_file::deinit()
{
    if (_fd >= 0) {
        if (::close(_fd) != 0) {
            msg::wrn(_("%s: %s"), _filename.c_str(), ::strerror(errno));
        }
        _fd = -1;
    }
}

bool command_file::is_active() const
{
    return (_fd >= 0 || _linebuf.size() > 0);
}

bool command_file::allow_early_quit()
{
    return !is_active();
}

void command_file::process_events()
{
    if (!is_active())
        return;

    if (_wait_until_stop && dispatch::playing())
        return;

    if (_wait_until >= 0) {
        int64_t now = timer::get(timer::monotonic);
        if (now < _wait_until)
            return;
    }

    _wait_until_stop = false;
    _wait_until = -1;

    static const size_t readbuf_size = 512;
    static char readbuf[readbuf_size];

    while (_fd >= 0) {
        ssize_t r = ::read(_fd, readbuf, readbuf_size);
        if (r < 0) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                // no data to read right now; we try again later
                break;
            } else {
                // an error occured
                int errno_bak = errno;
                deinit();
                throw exc(str::asprintf(_("%s: %s"), _filename.c_str(), ::strerror(errno_bak)), errno_bak);
            }
        } else if (r == 0) {
            // EOF. Keep a FIFO open: a writer may appear later. Close other files.
            if (!_is_fifo)
                deinit();
            break;
        } else {
            // We have some data
            _linebuf.append(readbuf, r);
        }
    }
    std::string cmd;
    if (_linebuf.size() > 0) {
        size_t eol_pos = _linebuf.find_first_of('\n');
        if (eol_pos != std::string::npos) {
            // We have a complete command line. Execute the command,
            // and remember the rest of the buffer (if any) for a subsequent
            // call to this function (we execute at most one command
            // per process_events() call).
            cmd = _linebuf.substr(0, eol_pos);
            _linebuf.erase(0, eol_pos + 1);
        } else if (_fd < 0) {
            // We have a last line without EOL. Process it.
            cmd = _linebuf;
            _linebuf.clear();
        }
    }
    if (cmd.length() > 0) {
        cmd = str::sanitize(str::trim(cmd));
        if (cmd.substr(0, 4) == "wait") {
            // This command is specific to this particular controller!
            std::vector<std::string> tokens = str::tokens(cmd, " \t\r");
            if (tokens.size() == 2 && tokens[0] == "wait") {
                float seconds;
                if (tokens[1] == "stop") {
                    _wait_until_stop = true;
                    return;
                } else if (str::to(tokens[1], &seconds)) {
                    _wait_until = timer::get(timer::monotonic);
                    if (seconds > 0.0f)
                        _wait_until += seconds * 1e6f;
                    return;
                }
            }
        }
        command c;
        bool ok = dispatch::parse_command(cmd, &c);
        if (!ok) {
            msg::err(_("%s: invalid command '%s'"), _filename.c_str(), cmd.c_str());
        } else {
            send_cmd(c);
        }
    }
}