summaryrefslogtreecommitdiff
path: root/src/shell.c
blob: ffd5cc747bdcd5a112cecf772b65a1084e7ff9ab (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

/* OpenIDA - Outil d'analyse de fichiers binaires
 * shell.c - panneau d'affichage du shell Python
 *
 * Copyright (C) 2009-2011 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 "shell.h"


#include <pty.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <vte/reaper.h>
#include <vte/vte.h>


#include <config.h>


#include <Python.h>


static pid_t child = 0;


#define _(str) str



/* Réagit à la terminaison d'un shell Python lancé. */
static void terminal_child_exited_cb(VteReaper *, GPid, gint, VteTerminal *);

/* Lance un nouveau shell Python dans un terminal donné. */
static void run_new_shell_in_terminal(VteTerminal *);

/* Réagit aux premières lignes de l'interpréteur Python. */
void python_started_cb(VteTerminal *, gpointer);



/* Exécute un shell interprétant du Python. */
static gpointer python_shell_thread(gpointer);


        int amaster;
        int aslave;
        char name[64];


/******************************************************************************
*                                                                             *
*  Paramètres  : ref = adresse de l'espace de référencements.                 *
*                                                                             *
*  Description : Construit le panneau d'affichage des symboles.               *
*                                                                             *
*  Retour      : Adresse du panneau mis en place.                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GtkWidget *build_shell_panel(GObject *ref)
{
    GtkWidget *result;                      /* Panneau à retourner         */
    GtkWidget *term;                        /* Terminal proprement dit     */



    GError *error;                          /* Bilan de création de thread */



    term = vte_terminal_new();

    result = gtk_scrolled_window_new(NULL, GTK_ADJUSTMENT(VTE_TERMINAL(term)->adjustment));
    gtk_widget_show(result);

    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(result), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(result), GTK_SHADOW_IN);

    gtk_widget_show(term);
    gtk_container_add(GTK_CONTAINER(result), term);


    vte_terminal_set_cursor_blinks(VTE_TERMINAL(term), TRUE);


#if 1
    //vte_terminal_fork_command (term, "/bin/sh", NULL, NULL, "/", 0, 0, 0);
    return result;
#endif


    openpty(&amaster, &aslave, name, NULL, NULL);


        fprintf(stderr, "m = %d, s = %d, n = \"%s\"\n", amaster,
    aslave, name);


        vte_terminal_set_pty(VTE_TERMINAL(term), amaster);


    if (!g_thread_create((GThreadFunc)python_shell_thread, term, FALSE, &error))
    {
        printf ("Failed to create the thread: %s\n", error->message);
    }




    /*
    g_signal_connect(vte_reaper_get(), "child-exited",
					  G_CALLBACK(terminal_child_exited_cb), term);


    run_new_shell_in_terminal(VTE_TERMINAL(term));
    */


    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : reaper = surveillant des processus exécuté dans le terminal. *
*                pid    = identifiant du processus terminé.                   *
*                status = informations sur la sortie de l'interpréteur.       *
*                term   = terminal affiché à l'écran.                         *
*                                                                             *
*  Description : Réagit à la terminaison d'un shell Python lancé.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void terminal_child_exited_cb(VteReaper *reaper, GPid pid, gint status, VteTerminal *term)
{

    //vte_terminal_feed(term, ("Exit detected ; lauching an new Python shell...\n\r"), -1);


    run_new_shell_in_terminal(term);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : term = terminal affiché à l'écran.                           *
*                                                                             *
*  Description : Lance un nouveau shell Python dans un terminal donné.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void run_new_shell_in_terminal(VteTerminal *term)
{
    gdouble refval;                         /* Attente active (bouh !)      */
    size_t len;                             /* Longueur de chaîne          */

    char *args[] = { "python", NULL };

#ifdef DEBUG
    char *env[] = { NULL, NULL };
#else
    char **env = NULL;
#endif

    refval = gtk_adjustment_get_value(GTK_ADJUSTMENT(term->adjustment));

#ifdef DEBUG
    len = strlen("PYTHONPATH=") + strlen(PACKAGE_SOURCE_DIR) + strlen("/src/plugins/pyoida");

    env[0] = (char *)calloc(len + 1, sizeof(char));

    strcpy(env[0], "PYTHONPATH=");
    strcat(env[0], PACKAGE_SOURCE_DIR);
    strcat(env[0], "/src/plugins/pyoida");
#endif

    child = vte_terminal_fork_command(VTE_TERMINAL(term), args[0], args,
                                                           env, NULL, 0, 0, 0);

    vte_reaper_add_child(child);

    printf("ref :: %g\n", refval);

    //while (refval == gtk_adjustment_get_value(GTK_ADJUSTMENT(term->adjustment)));


    g_signal_connect(term, "contents-changed",
					  G_CALLBACK(python_started_cb), NULL);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : term = terminal affiché à l'écran.                           *
*                data = adresse non utilisée ici.                             *
*                                                                             *
*  Description : Réagit aux premières lignes de l'interpréteur Python.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void python_started_cb(VteTerminal *term, gpointer data)
{
    static int counter = 0;                 /* Saut du prologue            */

    if (counter++ == 0) return;
    else counter = 0;

    vte_terminal_feed_child(term, "import pyoida\n", -1);

    vte_terminal_feed_child(term, "from pyoida import *\n", -1);

    g_signal_handlers_disconnect_by_func(term, G_CALLBACK(python_started_cb), NULL);

}



/******************************************************************************
*                                                                             *
*  Paramètres  : ?????????????????????????????????????????????????            *
*                                                                             *
*  Description : Exécute un shell interprétant du Python.                     *
*                                                                             *
*  Retour      : ???                                                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static gpointer python_shell_thread(gpointer data)
{
    FILE *stream;

    int stdout2;
    FILE *out2;

    printf("==== FE :: '%s'\n", ptsname(amaster));
    printf("==== FE :: '%s'\n", ptsname(aslave));




    //stream = fopen("/dev/pts/21", "w+");
    stream = fdopen(aslave, "w+");
    if (stream == NULL)
    {
        perror("fdopen");
        return NULL;
    }


    /* Mise à jour des entrées/sorties */

    stdout2 = dup(STDOUT_FILENO);
    out2 = fdopen(stdout2, "w");

    fprintf(out2, ">>> stdout2 :: %d\n", stdout2);


#if 1
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    dup(aslave);
    dup(aslave);
    dup(aslave);
#endif



    Py_Initialize();


    //amaster = STDIN_FILENO;

    /*
    write(amaster, "import pyoida\n", strlen("import pyoida\n"));
    write(amaster, "from pyoida import *\n", strlen("from pyoida import *\n"));

    write(amaster, "logger.log_simple_message(2, 'uhih')\n", strlen("logger.log_simple_message(2, 'uhih')\n"));

    write(amaster, "for i in pyoida.current_binary().lines():\n", strlen("for i in pyoida.current_binary().lines():\n"));
    write(amaster, "\tprint i\n", strlen("\tprint i\n"));
    */


    write(amaster, "execfile('test.py')", strlen("execfile('test.py')"));



    fprintf(out2, ">>> stdout2 :: %d (inside)\n", stdout2);



    printf("+++++++++++++++++++++++++++looping\n");

    PyRun_AnyFile(stream, NULL);

    printf("===========================looping\n");


    Py_Finalize();



    return NULL;

}