summaryrefslogtreecommitdiffstats
path: root/src/objects/process-object.c
blob: 43c0fd77793c70fd3470840902f4ebc1340f058f (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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <Python.h>
#include "pyirssi_irc.h"
#include "pymodule.h"
#include "process-object.h"
#include "pycore.h"

/* monitor "exec remove" signal */
static void process_cleanup(PROCESS_REC *process, int status)
{
    PyProcess *pyprocess = signal_get_user_data();

    if (process == pyprocess->data)
    {
        pyprocess->data = NULL;
        pyprocess->cleanup_installed = 0;
        signal_remove_data("exec remove", process_cleanup, pyprocess);
    }
}

static void PyProcess_dealloc(PyProcess *self)
{
    if (self->cleanup_installed)
        signal_remove_data("exec remove", process_cleanup, self); 

    Py_XDECREF(self->target_win);
    
    self->ob_type->tp_free((PyObject*)self);
}

static PyObject *PyProcess_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyProcess *self;

    self = (PyProcess *)type->tp_alloc(type, 0);
    if (!self)
        return NULL;

    return (PyObject *)self;
}

/* Getters */
PyDoc_STRVAR(PyProcess_id_doc,
    "ID for the process"
);
static PyObject *PyProcess_id_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyInt_FromLong(self->data->id);
}

PyDoc_STRVAR(PyProcess_name_doc,
    "Name for the process (if given)"
);
static PyObject *PyProcess_name_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->name);
}

PyDoc_STRVAR(PyProcess_args_doc,
    "The command that is being executed"
);
static PyObject *PyProcess_args_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->args);
}

PyDoc_STRVAR(PyProcess_pid_doc,
    "PID for the executed command"
);
static PyObject *PyProcess_pid_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyInt_FromLong(self->data->pid);
}

PyDoc_STRVAR(PyProcess_target_doc,
    "send text with /msg <target> ..."
);
static PyObject *PyProcess_target_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->target);
}

PyDoc_STRVAR(PyProcess_target_win_doc,
    "print text to this window"
);
static PyObject *PyProcess_target_win_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_OBJ_OR_NONE(self->target_win);
}

PyDoc_STRVAR(PyProcess_shell_doc,
    "start the program via /bin/sh"
);
static PyObject *PyProcess_shell_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyBool_FromLong(self->data->shell);
}

PyDoc_STRVAR(PyProcess_notice_doc,
    "send text with /notice, not /msg if target is set"
);
static PyObject *PyProcess_notice_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyBool_FromLong(self->data->notice);
}

PyDoc_STRVAR(PyProcess_silent_doc,
    "don't print \"process exited with level xx\""
);
static PyObject *PyProcess_silent_get(PyProcess *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyBool_FromLong(self->data->silent);
}

/* specialized getters/setters */
static PyGetSetDef PyProcess_getseters[] = {
    {"id", (getter)PyProcess_id_get, NULL,
        PyProcess_id_doc, NULL},
    {"name", (getter)PyProcess_name_get, NULL,
        PyProcess_name_doc, NULL},
    {"args", (getter)PyProcess_args_get, NULL,
        PyProcess_args_doc, NULL},
    {"pid", (getter)PyProcess_pid_get, NULL,
        PyProcess_pid_doc, NULL},
    {"target", (getter)PyProcess_target_get, NULL,
        PyProcess_target_doc, NULL},
    {"target_win", (getter)PyProcess_target_win_get, NULL,
        PyProcess_target_win_doc, NULL},
    {"shell", (getter)PyProcess_shell_get, NULL,
        PyProcess_shell_doc, NULL},
    {"notice", (getter)PyProcess_notice_get, NULL,
        PyProcess_notice_doc, NULL},
    {"silent", (getter)PyProcess_silent_get, NULL,
        PyProcess_silent_doc, NULL},
    {NULL}
};

/* Methods */
/* Methods for object */
static PyMethodDef PyProcess_methods[] = {
    {NULL}  /* Sentinel */
};

PyTypeObject PyProcessType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "Process",            /*tp_name*/
    sizeof(PyProcess),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyProcess_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "PyProcess objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyProcess_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyProcess_getseters,        /* tp_getset */
    0,          /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    0,      /* tp_init */
    0,                         /* tp_alloc */
    PyProcess_new,                 /* tp_new */
};


/* process factory function */
PyObject *pyprocess_new(void *process)
{
    PyProcess *pyprocess;

    pyprocess = py_inst(PyProcess, PyProcessType);
    if (!pyprocess)
        return NULL;

    pyprocess->data = process;
    pyprocess->cleanup_installed = 1;
    signal_add_last_data("exec remove", process_cleanup, pyprocess);

    return (PyObject *)pyprocess;
}

int process_object_init(void) 
{
    g_return_val_if_fail(py_module != NULL, 0);

    if (PyType_Ready(&PyProcessType) < 0)
        return 0;
    
    Py_INCREF(&PyProcessType);
    PyModule_AddObject(py_module, "Process", (PyObject *)&PyProcessType);

    return 1;
}