~ubuntu-branches/ubuntu/jaunty/geany/jaunty

« back to all changes in this revision

Viewing changes to src/socket.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2007-02-25 21:12:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070225211213-jk4d4vxtgji0rj74
Tags: 0.10.2-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *      socket.h - this file is part of Geany, a fast and lightweight IDE
3
3
 *
4
 
 *      Copyright 2006 Enrico Tröger <enrico.troeger@uvena.de>
 
4
 *      Copyright 2006-2007 Enrico Tröger <enrico.troeger@uvena.de>
 
5
 *      Copyright 2006-2007 Nick Treleaven <nick.treleaven@btinternet.com>
5
6
 *
6
7
 *      This program is free software; you can redistribute it and/or modify
7
8
 *      it under the terms of the GNU General Public License as published by
17
18
 *      along with this program; if not, write to the Free Software
18
19
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20
 *
20
 
 * $Id: socket.c 1052 2006-12-05 10:23:51Z ntrel $
 
21
 * $Id: socket.c 1270 2007-02-12 18:42:15Z eht16 $
21
22
 */
22
23
 
23
24
 
36
37
# include <winsock2.h>
37
38
# include <ws2tcpip.h>
38
39
#endif
 
40
#include <stdlib.h>
39
41
#include <unistd.h>
40
42
#include <fcntl.h>
41
43
 
42
44
#include "main.h"
43
45
#include "socket.h"
44
46
#include "document.h"
 
47
#include "support.h"
45
48
 
46
49
 
47
50
 
75
78
 
76
79
 
77
80
 
 
81
void send_open_command(gint sock, gint argc, gchar **argv)
 
82
{
 
83
        gint i;
 
84
        gchar *filename;
 
85
 
 
86
        g_return_if_fail(argc > 1);
 
87
        geany_debug("using running instance of Geany");
 
88
 
 
89
        if (cl_options.goto_line >= 0)
 
90
        {
 
91
                gchar *line = g_strdup_printf("%d\n", cl_options.goto_line);
 
92
                socket_fd_write_all(sock, "line\n", 5);
 
93
                socket_fd_write_all(sock, line, strlen(line));
 
94
                socket_fd_write_all(sock, ".\n", 2);
 
95
                g_free(line);
 
96
        }
 
97
 
 
98
        socket_fd_write_all(sock, "open\n", 5);
 
99
 
 
100
        for(i = 1; i < argc && argv[i] != NULL; i++)
 
101
        {
 
102
                filename = get_argv_filename(argv[i]);
 
103
 
 
104
                if (filename != NULL &&
 
105
                        g_file_test(filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
 
106
                {
 
107
                        socket_fd_write_all(sock, filename, strlen(filename));
 
108
                        socket_fd_write_all(sock, "\n", 1);
 
109
                }
 
110
                else
 
111
                {
 
112
                        g_printerr(_("Could not find file '%s'."), filename);
 
113
                        g_printerr("\n");       // keep translation from open_cl_files() in main.c.
 
114
                }
 
115
                g_free(filename);
 
116
        }
 
117
        socket_fd_write_all(sock, ".\n", 2);
 
118
}
 
119
 
 
120
 
78
121
/* (Unix domain) socket support to replace the old FIFO code
79
122
 * (taken from Sylpheed, thanks) */
80
123
gint socket_init(gint argc, gchar **argv)
118
161
        // remote command mode, here we have another running instance and want to use it
119
162
        if (argc > 1)
120
163
        {
121
 
                gint i;
122
 
                gchar *filename;
123
 
 
124
 
                geany_debug("using running instance of Geany");
125
 
 
126
 
                socket_fd_write_all(sock, "open\n", 5);
127
 
 
128
 
                for(i = 1; i < argc && argv[i] != NULL; i++)
129
 
                {
130
 
                        filename = get_argv_filename(argv[i]);
131
 
 
132
 
                        if (filename != NULL)
133
 
                        {
134
 
                                socket_fd_write_all(sock, filename, strlen(filename));
135
 
                                socket_fd_write_all(sock, "\n", 1);
136
 
                                g_free(filename);
137
 
                        }
138
 
                }
139
 
                socket_fd_write_all(sock, ".\n", 2);
 
164
                send_open_command(sock, argc, argv);
140
165
        }
141
166
 
142
167
        socket_fd_close(sock);
349
374
        sock = accept(fd, (struct sockaddr *)&caddr, &caddr_len);
350
375
 
351
376
        // first get the command
352
 
        if (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && strncmp(buf, "open", 4) == 0)
 
377
        while (socket_fd_gets(sock, buf, sizeof(buf)) != -1)
353
378
        {
354
 
                geany_debug("remote command: open");
355
 
                while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
 
379
                if (strncmp(buf, "open", 4) == 0)
356
380
                {
357
 
                        g_strstrip(buf); // remove \n char
 
381
                        while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
 
382
                        {
 
383
                                g_strstrip(buf); // remove \n char
358
384
 
359
 
                        if (g_file_test(buf, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
360
 
                                document_open_file(-1, buf, 0, FALSE, NULL, NULL);
361
 
                        else
362
 
                                geany_debug("got data from socket, but it does not look like a filename");
363
 
                }
364
 
                gtk_window_deiconify(GTK_WINDOW(app->window));
 
385
                                if (g_file_test(buf, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
 
386
                                        document_open_file(-1, buf, 0, FALSE, NULL, NULL);
 
387
                                else
 
388
                                        geany_debug("got data from socket, but it does not look like a filename");
 
389
                        }
 
390
                        gtk_window_deiconify(GTK_WINDOW(app->window));
365
391
#ifdef G_OS_WIN32
366
 
                gtk_window_present(GTK_WINDOW(app->window));
 
392
                        gtk_window_present(GTK_WINDOW(app->window));
367
393
#endif
 
394
                }
 
395
                else if (strncmp(buf, "line", 4) == 0)
 
396
                {
 
397
                        while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
 
398
                        {
 
399
                                g_strstrip(buf); // remove \n char
 
400
                                // on any error we get 0 which should be save enough as fallback
 
401
                                cl_options.goto_line = atoi(buf);
 
402
                        }
 
403
                }
368
404
        }
369
 
 
370
405
        socket_fd_close(sock);
371
406
 
372
407
        return TRUE;