aboutsummaryrefslogtreecommitdiff
path: root/0.8.0/src/gtkpaludis/vte_message_window.cc
blob: e6f8a529eb16b1d60561e8a520c9da6b4666cc76 (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 * Copyright (c) 2006 Piotr Rak <piotr.rak@gmail.com>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "vte_message_window.hh"
#include <paludis/util/fd_output_stream.hh>
#include <paludis/util/system.hh>
#include <paludis/util/log.hh>
#include <paludis/util/exception.hh>

//#ifndef _XOPEN_SOURCE
//#define _XOPEN_SOURCE don't know why vte.h has this defined
//#endif

#include <cstdlib>
#include <fcntl.h>
#include <iostream>

namespace
{
    class Pty /* this one should propably go to libpaludisutil */
    {
        int _master_fd;
        int _slave_fd;
    public:
        Pty(bool controlling_terminal);
        ~Pty();

        int master_fd() const;
        int slave_fd() const;
    };

    Pty::Pty(bool controlling_terminal) :
        _master_fd(posix_openpt(O_RDWR | (controlling_terminal ? 0 : O_NOCTTY)))
    {
        if (0 > _master_fd)
            throw paludis::InternalError(PALUDIS_HERE, "posix_openpt failed");

        char *slave_name(ptsname(_master_fd));

        try
        {
            if (0 == slave_name)
                throw paludis::InternalError(PALUDIS_HERE, "ptsname failed");

            if (0 > grantpt(_master_fd)) /* TODO: this one needs something special from user co */
                throw paludis::InternalError(PALUDIS_HERE, "grantpt failed");

            if (0 > unlockpt(_master_fd))
                throw paludis::InternalError(PALUDIS_HERE, "unlockpt failed");

            if (-1 == (_slave_fd = open(slave_name, O_RDWR)))
                throw paludis::InternalError(PALUDIS_HERE, "open slave terminal failed");
        }
        catch (...)
        {
            close(_master_fd);
            throw;
        }
    }

    Pty::~Pty()
    {
        close(_master_fd);
        close(_slave_fd);
    }

    int Pty::master_fd() const
    {
        return _master_fd;
    }

    int Pty::slave_fd() const
    {
        return _slave_fd;
    }

} /* anonymous namespace */

namespace paludis
{
    template <>
    struct Implementation<VteMessageWindow> :
        public InternalCounted<Implementation<VteMessageWindow> >
    {
        VteMessageWindow * const owner;
        Pty pty;
        FDOutputStream stream;

        Implementation(VteMessageWindow *o);

    };

    Implementation<VteMessageWindow>::Implementation(VteMessageWindow* o) :
        InternalCounted<Implementation<VteMessageWindow> >(),
        owner(o),
        pty(false),
        stream(pty.slave_fd())
    {
        set_run_command_stdout_fds(pty.slave_fd(), pty.master_fd());
        set_run_command_stderr_fds(pty.slave_fd(), pty.master_fd());

        Log::get_instance()->set_log_stream(&stream);
        Log::get_instance()->message(ll_debug, lc_no_context, "Message window initialized");
    }


    VteMessageWindow::VteMessageWindow() :
        Vte::Terminal(),
        PrivateImplementationPattern<VteMessageWindow>(new Implementation<VteMessageWindow>(this))
    {
        set_pty(dup(_imp->pty.master_fd()));
    }

    VteMessageWindow::~VteMessageWindow()
    {
        Log::get_instance()->set_log_stream(&std::cerr);
    }

} /* namespace paludis */