annotate src/if_sniff.c @ 1442:bce7c73fd56d v7.1.157

updated for version 7.1-157
author vimboss
date Tue, 20 Nov 2007 11:31:26 +0000
parents 0f9f4761ad9c
children b9740fb41986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * if_sniff.c Interface between Vim and SNiFF+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 * $Id$
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 #include "vim.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 # include <stdio.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 # include <fcntl.h>
714
0f9f4761ad9c updated for version 7.0216
vimboss
parents: 426
diff changeset
15 # include "vimio.h"
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 # include <process.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 # include <string.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 # include <assert.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 # ifdef FEAT_GUI_X11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 # include "gui_x11.pro"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 # include "os_unixx.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 static int sniffemacs_pid;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 int fd_from_sniff;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 int sniff_connected = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 int sniff_request_waiting = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 int want_sniff_request = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 #define MAX_REQUEST_LEN 512
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 #define NEED_SYMBOL 2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 #define EMPTY_SYMBOL 4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 #define NEED_FILE 8
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 #define SILENT 16
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 #define DISCONNECT 32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 #define CONNECT 64
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 #define RQ_NONE 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 #define RQ_SIMPLE 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 #define RQ_CONTEXT NEED_FILE + NEED_SYMBOL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 #define RQ_SCONTEXT NEED_FILE + NEED_SYMBOL + EMPTY_SYMBOL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 #define RQ_NOSYMBOL NEED_FILE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 #define RQ_SILENT RQ_NOSYMBOL + SILENT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 #define RQ_CONNECT RQ_NONE + CONNECT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 #define RQ_DISCONNECT RQ_SIMPLE + DISCONNECT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 struct sn_cmd
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 char *cmd_name;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 char cmd_code;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 char *cmd_msg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 int cmd_type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 struct sn_cmd_list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 struct sn_cmd* sniff_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 struct sn_cmd_list* next_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 static struct sn_cmd sniff_cmds[] =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 { "toggle", 'e', N_("Toggle implementation/definition"),RQ_SCONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 { "superclass", 's', N_("Show base class of"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 { "overridden", 'm', N_("Show overridden member function"),RQ_SCONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 { "retrieve-file", 'r', N_("Retrieve from file"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 { "retrieve-project",'p', N_("Retrieve from project"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 { "retrieve-all-projects",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 'P', N_("Retrieve from all projects"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 { "retrieve-next", 'R', N_("Retrieve"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 { "goto-symbol", 'g', N_("Show source of"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 { "find-symbol", 'f', N_("Find symbol"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 { "browse-class", 'w', N_("Browse class"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 { "hierarchy", 't', N_("Show class in hierarchy"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 { "restr-hier", 'T', N_("Show class in restricted hierarchy"),RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 { "xref-to", 'x', N_("Xref refers to"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 { "xref-by", 'X', N_("Xref referred by"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 { "xref-has", 'c', N_("Xref has a"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 { "xref-used-by", 'C', N_("Xref used by"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 { "show-docu", 'd', N_("Show docu of"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 { "gen-docu", 'D', N_("Generate docu for"), RQ_CONTEXT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 { "connect", 'y', NULL, RQ_CONNECT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 { "disconnect", 'q', NULL, RQ_DISCONNECT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 { "font-info", 'z', NULL, RQ_SILENT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 { "update", 'u', NULL, RQ_SILENT },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 { NULL, '\0', NULL, 0}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 static char *SniffEmacs[2] = {"sniffemacs", (char *)NULL}; /* Yes, Emacs! */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 static int fd_to_sniff;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 static int sniff_will_disconnect = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 static char msg_sniff_disconnect[] = N_("Cannot connect to SNiFF+. Check environment (sniffemacs must be found in $PATH).\n");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 static char sniff_rq_sep[] = " ";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 static struct sn_cmd_list *sniff_cmd_ext = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 /* Initializing vim commands
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 * executed each time vim connects to Sniff
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 static char *init_cmds[]= {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 "augroup sniff",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 "autocmd BufWritePost * sniff update",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 "autocmd BufReadPost * sniff font-info",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 "autocmd VimLeave * sniff disconnect",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 "augroup END",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 "let g:sniff_connected = 1",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 "if ! exists('g:sniff_mappings_sourced')|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 "if ! exists('g:sniff_mappings')|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 "if exists('$SNIFF_DIR4')|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 "let g:sniff_mappings='$SNIFF_DIR4/config/integrations/vim/sniff.vim'|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 "else|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 "let g:sniff_mappings='$SNIFF_DIR/config/sniff.vim'|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 "endif|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 "endif|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 "let g:sniff_mappings=expand(g:sniff_mappings)|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 "if filereadable(g:sniff_mappings)|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 "execute 'source' g:sniff_mappings|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 "let g:sniff_mappings_sourced=1|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 "endif|"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 "endif",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 /*-------- Function Prototypes ----------------------------------*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 static int ConnectToSniffEmacs __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 static void sniff_connect __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 static void HandleSniffRequest __ARGS((char* buffer));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 static int get_request __ARGS((int fd, char *buf, int maxlen));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 static void WriteToSniff __ARGS((char *str));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 static void SendRequest __ARGS((struct sn_cmd *command, char* symbol));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 static void vi_msg __ARGS((char *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 static void vi_error_msg __ARGS((char *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 static char *vi_symbol_under_cursor __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 static void vi_open_file __ARGS((char *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 static char *vi_buffer_name __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 static buf_T *vi_find_buffer __ARGS((char *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 static void vi_exec_cmd __ARGS((char *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 static void vi_set_cursor_pos __ARGS((long char_nr));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 static long vi_cursor_pos __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 /* debug trace */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 #if 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 static FILE* _tracefile = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 #define SNIFF_TRACE_OPEN(file) if (!_tracefile) _tracefile = fopen(file, "w")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 #define SNIFF_TRACE(msg) fprintf(_tracefile, msg); fflush(_tracefile);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 #define SNIFF_TRACE1(msg, arg) fprintf(_tracefile, msg,arg); fflush(_tracefile);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 #define SNIFF_TRACE_CLOSE fclose(_tracefile); _tracefile=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 #define SNIFF_TRACE_OPEN(file)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 #define SNIFF_TRACE(msg)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 #define SNIFF_TRACE1(msg, arg)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 #define SNIFF_TRACE_CLOSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 /*-------- Windows Only Declarations -----------------------------*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 static int sniff_request_processed=1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 static HANDLE sniffemacs_handle=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 static HANDLE readthread_handle=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 static HANDLE handle_to_sniff=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 static HANDLE handle_from_sniff=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 struct sniffBufNode
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 struct sniffBufNode *next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 int bufLen;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 char buf[MAX_REQUEST_LEN];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 static struct sniffBufNode *sniffBufStart=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 static struct sniffBufNode *sniffBufEnd=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 static HANDLE hBufferMutex=NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 # ifdef FEAT_GUI_W32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 extern HWND s_hwnd; /* gvim's Window handle */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 * some helper functions for Windows port only
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 static HANDLE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 ExecuteDetachedProgram(char *szBinary, char *szCmdLine,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 HANDLE hStdInput, HANDLE hStdOutput)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 BOOL bResult;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 DWORD nError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 PROCESS_INFORMATION aProcessInformation;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 PROCESS_INFORMATION *pProcessInformation= &aProcessInformation;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197 STARTUPINFO aStartupInfo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 STARTUPINFO *pStartupInfo= &aStartupInfo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 DWORD dwCreationFlags= 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 char szPath[512];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201 HINSTANCE hResult;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 hResult = FindExecutable(szBinary, ".", szPath);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 if ((int)hResult <= 32)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 /* can't find the exe file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 ZeroMemory(pStartupInfo, sizeof(*pStartupInfo));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211 pStartupInfo->dwFlags= STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 pStartupInfo->hStdInput = hStdInput;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 pStartupInfo->hStdOutput = hStdOutput;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 pStartupInfo->wShowWindow= SW_HIDE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 pStartupInfo->cb = sizeof(STARTUPINFO);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 bResult= CreateProcess(
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 szPath,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 szCmdLine,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 NULL, /* security attr for process */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 NULL, /* security attr for primary thread */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 TRUE, /* DO inherit stdin and stdout */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223 dwCreationFlags, /* creation flags */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 NULL, /* environment */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 ".", /* current directory */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 pStartupInfo, /* startup info: NULL crashes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 pProcessInformation /* process information: NULL crashes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 );
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 nError= GetLastError();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 if (bResult)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 CloseHandle(pProcessInformation->hThread);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 CloseHandle(hStdInput);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 CloseHandle(hStdOutput);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 return(pProcessInformation->hProcess);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 return(NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 * write to the internal Thread / Thread communications buffer.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 * Return TRUE if successful, FALSE else.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 static BOOL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 writeToBuffer(char *msg, int len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 DWORD dwWaitResult; /* Request ownership of mutex. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 struct sniffBufNode *bn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 int bnSize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 SNIFF_TRACE1("writeToBuffer %d\n", len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 bnSize = sizeof(struct sniffBufNode) - MAX_REQUEST_LEN + len + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 if (bnSize < 128) bnSize = 128; /* minimum length to avoid fragmentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 bn = (struct sniffBufNode *)malloc(bnSize);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 if (!bn)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 return FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 memcpy(bn->buf, msg, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 bn->buf[len]='\0'; /* terminate CString for added safety */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 bn->next = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 bn->bufLen = len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 /* now, acquire a Mutex for adding the string to our linked list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 dwWaitResult = WaitForSingleObject(
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 hBufferMutex, /* handle of mutex */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 1000L); /* one-second time-out interval */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267 if (dwWaitResult == WAIT_OBJECT_0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 /* The thread got mutex ownership. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 if (sniffBufEnd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 sniffBufEnd->next = bn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273 sniffBufEnd = bn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 sniffBufStart = sniffBufEnd = bn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277 /* Release ownership of the mutex object. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278 if (! ReleaseMutex(hBufferMutex))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 /* Deal with error. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 return TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 /* Cannot get mutex ownership due to time-out or mutex object abandoned. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 free(bn);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 return FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 * read from the internal Thread / Thread communications buffer.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 * Return TRUE if successful, FALSE else.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 ReadFromBuffer(char *buf, int maxlen)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 DWORD dwWaitResult; /* Request ownership of mutex. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 int theLen;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 struct sniffBufNode *bn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 dwWaitResult = WaitForSingleObject(
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302 hBufferMutex, /* handle of mutex */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 1000L); /* one-second time-out interval */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 if (dwWaitResult == WAIT_OBJECT_0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 if (!sniffBufStart)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 /* all pending Requests Processed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 theLen = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 bn = sniffBufStart;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 theLen = bn->bufLen;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 SNIFF_TRACE1("ReadFromBuffer %d\n", theLen);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 if (theLen >= maxlen)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318 /* notify the user of buffer overflow? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 theLen = maxlen-1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 memcpy(buf, bn->buf, theLen);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 buf[theLen] = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 if (! (sniffBufStart = bn->next))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 sniffBufEnd = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 sniff_request_processed = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 free(bn);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 if (! ReleaseMutex(hBufferMutex))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332 /* Deal with error. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 return theLen;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 /* Cannot get mutex ownership due to time-out or mutex object abandoned. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 /* on Win32, a separate Thread reads the input pipe. get_request is not needed here. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342 static void __cdecl
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 SniffEmacsReadThread(void *dummy)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 static char ReadThreadBuffer[MAX_REQUEST_LEN];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 int ReadThreadLen=0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 int result=0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 int msgLen=0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 char *msgStart, *msgCur;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 SNIFF_TRACE("begin thread\n");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 /* Read from the pipe to SniffEmacs */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353 while (sniff_connected)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 if (!ReadFile(handle_from_sniff,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 ReadThreadBuffer + ReadThreadLen, /* acknowledge rest in buffer */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357 MAX_REQUEST_LEN - ReadThreadLen,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 &result,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359 NULL))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361 DWORD err = GetLastError();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 result = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365 if (result < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 /* probably sniffemacs died... log the Error? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 sniff_disconnect(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 else if (result > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372 ReadThreadLen += result-1; /* total length of valid chars */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373 for(msgCur=msgStart=ReadThreadBuffer; ReadThreadLen > 0; msgCur++, ReadThreadLen--)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375 if (*msgCur == '\0' || *msgCur == '\r' || *msgCur == '\n')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 msgLen = msgCur-msgStart; /* don't add the CR/LF chars */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378 if (msgLen > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379 writeToBuffer(msgStart, msgLen);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380 msgStart = msgCur + 1; /* over-read single CR/LF chars */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 /* move incomplete message to beginning of buffer */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 ReadThreadLen = msgCur - msgStart;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 if (ReadThreadLen > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 mch_memmove(ReadThreadBuffer, msgStart, ReadThreadLen);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 if (sniff_request_processed)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391 /* notify others that new data has arrived */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392 sniff_request_processed = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
393 sniff_request_waiting = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
394 #ifdef FEAT_GUI_W32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
395 PostMessage(s_hwnd, WM_USER, (WPARAM)0, (LPARAM)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
396 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
397 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
398 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400 SNIFF_TRACE("end thread\n");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 #endif /* WIN32 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403 /*-------- End of Windows Only Declarations ------------------------*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406 /* ProcessSniffRequests
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
407 * Function that should be called from outside
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
408 * to process the waiting sniff requests
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
410 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411 ProcessSniffRequests()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413 static char buf[MAX_REQUEST_LEN];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 while (sniff_connected)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419 len = ReadFromBuffer(buf, sizeof(buf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 len = get_request(fd_from_sniff, buf, sizeof(buf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 if (len < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425 vi_error_msg(_("E274: Sniff: Error during read. Disconnected"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426 sniff_disconnect(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429 else if (len > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430 HandleSniffRequest( buf );
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435 if (sniff_will_disconnect) /* Now the last msg has been processed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436 sniff_disconnect(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 static struct sn_cmd *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 find_sniff_cmd(cmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
441 char *cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
442 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 struct sn_cmd *sniff_cmd = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
445 for(i=0; sniff_cmds[i].cmd_name; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
446 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 if (!strcmp(cmd, sniff_cmds[i].cmd_name))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
448 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 sniff_cmd = &sniff_cmds[i];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 if (!sniff_cmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
454 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
455 struct sn_cmd_list *list = sniff_cmd_ext;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
456 while(list)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
458 if (!strcmp(cmd, list->sniff_cmd->cmd_name))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
460 sniff_cmd = list->sniff_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
463 list = list->next_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 return sniff_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470 add_sniff_cmd(cmd, def, msg)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 char *cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472 char *def;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 char *msg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 int rc = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476 if (def != NULL && def[0] != NUL && find_sniff_cmd(cmd) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 struct sn_cmd_list *list = sniff_cmd_ext;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 struct sn_cmd *sniff_cmd = (struct sn_cmd*)malloc(sizeof(struct sn_cmd));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 struct sn_cmd_list *cmd_node = (struct sn_cmd_list*)malloc(sizeof(struct sn_cmd_list));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 int rq_type = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483 /* unescape message text */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484 char *p = msg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485 char *end = p+strlen(msg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486 while(*p)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488 if (*p == '\\')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 mch_memmove(p,p+1,end-p);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492 SNIFF_TRACE1("request name = %s\n",cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493 SNIFF_TRACE1("request def = %s\n",def);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 SNIFF_TRACE1("request msg = %s\n",msg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
495
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
496 while(list && list->next_cmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 list = list->next_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498 if (!list)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499 sniff_cmd_ext = cmd_node;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501 list->next_cmd = cmd_node;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
502
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503 sniff_cmd->cmd_name = cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504 sniff_cmd->cmd_code = def[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505 sniff_cmd->cmd_msg = msg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506 switch(def[1])
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508 case 'f':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509 rq_type = RQ_NOSYMBOL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 case 's':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 rq_type = RQ_CONTEXT;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514 case 'S':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 rq_type = RQ_SCONTEXT;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517 default:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 rq_type = RQ_SIMPLE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521 sniff_cmd->cmd_type = rq_type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 cmd_node->sniff_cmd = sniff_cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 cmd_node->next_cmd = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524 rc = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526 return rc;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529 /* ex_sniff
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530 * Handle ":sniff" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 ex_sniff(eap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534 exarg_T *eap;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 char_u *arg = eap->arg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 char_u *symbol = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538 char_u *cmd = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 SNIFF_TRACE_OPEN("if_sniff.log");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541 if (ends_excmd(*arg)) /* no request: print available commands */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544 msg_start();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545 msg_outtrans_attr((char_u *)"-- SNiFF+ commands --", hl_attr(HLF_T));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546 for(i=0; sniff_cmds[i].cmd_name; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 msg_putchar('\n');
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549 msg_outtrans((char_u *)":sniff ");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 msg_outtrans((char_u *)sniff_cmds[i].cmd_name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 msg_putchar('\n');
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553 msg_outtrans((char_u *)_("SNiFF+ is currently "));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554 if (!sniff_connected)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 msg_outtrans((char_u *)_("not "));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 msg_outtrans((char_u *)_("connected"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 msg_end();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 else /* extract command name and symbol if present */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 symbol = skiptowhite(arg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562 cmd = vim_strnsave(arg, (int)(symbol-arg));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563 symbol = skipwhite(symbol);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 if (ends_excmd(*symbol))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 symbol = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 if (!strcmp((char *)cmd, "addcmd"))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 char_u *def = skiptowhite(symbol);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 char_u *name = vim_strnsave(symbol, (int)(def-symbol));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570 char_u *msg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 def = skipwhite(def);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572 msg = skiptowhite(def);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 def = vim_strnsave(def, (int)(msg-def));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 msg = skipwhite(msg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 if (ends_excmd(*msg))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 msg = vim_strsave(name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578 msg = vim_strnsave(msg, (int)(skiptowhite_esc(msg)-msg));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579 if (!add_sniff_cmd((char*)name, (char*)def, (char*)msg))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 vim_free(msg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 vim_free(def);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583 vim_free(name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588 struct sn_cmd* sniff_cmd = find_sniff_cmd((char*)cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589 if (sniff_cmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590 SendRequest(sniff_cmd, (char *)symbol);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592 EMSG2(_("E275: Unknown SNiFF+ request: %s"), cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 vim_free(cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 sniff_connect()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
602 if (sniff_connected)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604 if (ConnectToSniffEmacs())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605 vi_error_msg(_("E276: Error connecting to SNiFF+"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
609
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610 for (i = 0; init_cmds[i]; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 vi_exec_cmd(init_cmds[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616 sniff_disconnect(immediately)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617 int immediately;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 if (!sniff_connected)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621 if (immediately)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623 vi_exec_cmd("augroup sniff");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 vi_exec_cmd("au!");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
625 vi_exec_cmd("augroup END");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 vi_exec_cmd("unlet g:sniff_connected");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
627 sniff_connected = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628 want_sniff_request = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629 sniff_will_disconnect = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 #ifdef FEAT_GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 if (gui.in_use)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 gui_mch_wait_for_chars(0L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635 while(sniffBufStart != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 struct sniffBufNode *node = sniffBufStart;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638 sniffBufStart = sniffBufStart->next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 free(node);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641 sniffBufStart = sniffBufEnd = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642 sniff_request_processed = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
643 CloseHandle(handle_to_sniff);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
644 CloseHandle(handle_from_sniff);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
645 WaitForSingleObject(sniffemacs_handle, 1000L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
646 CloseHandle(sniffemacs_handle);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
647 sniffemacs_handle = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
648 WaitForSingleObject(readthread_handle, 1000L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
649 readthread_handle = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
650 CloseHandle(hBufferMutex);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
651 hBufferMutex = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
652 SNIFF_TRACE_CLOSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
653 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
654 close(fd_to_sniff);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
655 close(fd_from_sniff);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
656 wait(NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
657 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
658 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
659 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
660 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
661 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
662 _sleep(2);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
663 if (!sniff_request_processed)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
664 ProcessSniffRequests();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
665 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
666 sleep(2); /* Incoming msg could disturb edit */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
667 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
668 sniff_will_disconnect = 1; /* We expect disconnect msg in 2 secs */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
669 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
670 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
671
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
672
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
673 /* ConnectToSniffEmacs
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
674 * Connect to Sniff: returns 1 on error
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
675 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
676 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
677 ConnectToSniffEmacs()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
678 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
679 #ifdef WIN32 /* Windows Version of the Code */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
680 HANDLE ToSniffEmacs[2], FromSniffEmacs[2];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
681 SECURITY_ATTRIBUTES sa;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
682
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
683 sa.nLength = sizeof(sa);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
684 sa.lpSecurityDescriptor = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
685 sa.bInheritHandle = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
686
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
687 if (! CreatePipe(&ToSniffEmacs[0], &ToSniffEmacs[1], &sa, 0))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
688 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
689 if (! CreatePipe(&FromSniffEmacs[0], &FromSniffEmacs[1], &sa, 0))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
690 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
691
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
692 sniffemacs_handle = ExecuteDetachedProgram(SniffEmacs[0], SniffEmacs[0],
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
693 ToSniffEmacs[0], FromSniffEmacs[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
694
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
695 if (sniffemacs_handle)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
696 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
697 handle_to_sniff = ToSniffEmacs[1];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
698 handle_from_sniff = FromSniffEmacs[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
699 sniff_connected = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
700 hBufferMutex = CreateMutex(
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
701 NULL, /* no security attributes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
702 FALSE, /* initially not owned */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
703 "SniffReadBufferMutex"); /* name of mutex */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
704 if (hBufferMutex == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
705 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
706 /* Check for error. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
707 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
708 readthread_handle = (HANDLE)_beginthread(SniffEmacsReadThread, 0, NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
709 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
710 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
711 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
712 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
713 /* error in spawn() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
714 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
715 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
716
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
717 #else /* UNIX Version of the Code */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
718 int ToSniffEmacs[2], FromSniffEmacs[2];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
719
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
720 pipe(ToSniffEmacs);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
721 pipe(FromSniffEmacs);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
722
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
723 /* fork */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
724 if ((sniffemacs_pid=fork()) == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
725 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
726 /* child */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
727
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
728 /* prepare communication pipes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
729 close(ToSniffEmacs[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
730 close(FromSniffEmacs[0]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
731
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
732 dup2(ToSniffEmacs[0],fileno(stdin)); /* write to ToSniffEmacs[1] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
733 dup2(FromSniffEmacs[1],fileno(stdout));/* read from FromSniffEmacs[0] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
734
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
735 close(ToSniffEmacs[0]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
736 close(FromSniffEmacs[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
737
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
738 /* start sniffemacs */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
739 execvp (SniffEmacs[0], SniffEmacs);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
740 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
741 /* FILE *out = fdopen(FromSniffEmacs[1], "w"); */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
742 sleep(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
743 fputs(_(msg_sniff_disconnect), stdout);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
744 fflush(stdout);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
745 sleep(3);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
746 #ifdef FEAT_GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
747 if (gui.in_use)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
748 gui_exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
749 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
750 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
751 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
752 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
753 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
754 else if (sniffemacs_pid > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
755 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
756 /* parent process */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
757 close(ToSniffEmacs[0]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
758 fd_to_sniff = ToSniffEmacs[1];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
759 close(FromSniffEmacs[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
760 fd_from_sniff = FromSniffEmacs[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
761 sniff_connected = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
762 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
763 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
764 else /* error in fork() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
765 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
766 #endif /* UNIX Version of the Code */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
767 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
768
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
769
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
770 /* HandleSniffRequest
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
771 * Handle one request from SNiFF+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
772 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
773 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
774 HandleSniffRequest(buffer)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
775 char *buffer;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
776 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
777 char VICommand[MAX_REQUEST_LEN];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
778 char command;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
779 char *arguments;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
780 char *token;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
781 char *argv[3];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
782 int argc = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
783 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
784
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
785 const char *SetTab = "set tabstop=%d";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
786 const char *SelectBuf = "buf %s";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
787 const char *DeleteBuf = "bd %s";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
788 const char *UnloadBuf = "bun %s";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
789 const char *GotoLine = "%d";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
790
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
791 command = buffer[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
792 arguments = &buffer[1];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
793 token = strtok(arguments, sniff_rq_sep);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
794 while(argc <3)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
795 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
796 if (token)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
797 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
798 argv[argc] = (char*)vim_strsave((char_u *)token);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
799 token = strtok(0, sniff_rq_sep);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
800 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
801 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
802 argv[argc] = strdup("");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
803 argc++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
804 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
805
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
806 switch (command)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
807 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
808 case 'o' : /* visit file at char pos */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
809 case 'O' : /* visit file at line number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
810 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
811 char *file = argv[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
812 int position = atoi(argv[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
813
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
814 buf = vi_find_buffer(file);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
815 setpcmark(); /* insert current pos in jump list [mark.c]*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
816 if (!buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
817 vi_open_file(file);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
818 else if (buf!=curbuf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
819 {
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
820 vim_snprintf(VICommand, sizeof(VICommand),
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
821 (char *)SelectBuf, file);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
822 vi_exec_cmd(VICommand);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
823 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
824 if (command == 'o')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
825 vi_set_cursor_pos((long)position);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
826 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
827 {
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
828 vim_snprintf(VICommand, sizeof(VICommand),
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
829 (char *)GotoLine, (int)position);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
830 vi_exec_cmd(VICommand);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
831 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
832 checkpcmark(); /* [mark.c] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
833 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
834 if (gui.in_use && !gui.in_focus) /* Raise Vim Window */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
835 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
836 # ifdef FEAT_GUI_W32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
837 SetForegroundWindow(s_hwnd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
838 # else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
839 extern Widget vimShell;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
840
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
841 XSetInputFocus(gui.dpy, XtWindow(vimShell), RevertToNone,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
842 CurrentTime);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
843 XRaiseWindow(gui.dpy, XtWindow(vimShell));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
844 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
845 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
846 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
847 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
848 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
849 case 'p' : /* path of file has changed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
850 /* when changing from shared to private WS (checkout) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
851 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
852 char *file = argv[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
853 char *new_path = argv[1];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
854
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
855 buf = vi_find_buffer(file);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
856 if (buf && !buf->b_changed) /* delete buffer only if not modified */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
857 {
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
858 vim_snprintf(VICommand, sizeof(VICommand),
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
859 (char *)DeleteBuf, file);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
860 vi_exec_cmd(VICommand);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
861 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
862 vi_open_file(new_path);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
863 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
864 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
865 case 'w' : /* writability has changed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
866 /* Sniff sends request twice,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
867 * but only the last one is the right one */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
868 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
869 char *file = argv[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
870 int writable = atoi(argv[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
871
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
872 buf = vi_find_buffer(file);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
873 if (buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
874 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
875 buf->b_p_ro = !writable;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
876 if (buf != curbuf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
877 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
878 buf->b_flags |= BF_CHECK_RO + BF_NEVERLOADED;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
879 if (writable && !buf->b_changed)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
880 {
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
881 vim_snprintf(VICommand, sizeof(VICommand),
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
882 (char *)UnloadBuf, file);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
883 vi_exec_cmd(VICommand);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
884 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
885 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
886 else if (writable && !buf->b_changed)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
887 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
888 vi_exec_cmd("e");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
889 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
890 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
891 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
892 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
893 case 'h' : /* highlight info */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
894 break; /* not implemented */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
895
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
896 case 't' : /* Set tab width */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
897 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
898 int tab_width = atoi(argv[1]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
899
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
900 if (tab_width > 0 && tab_width <= 16)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
901 {
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
902 vim_snprintf(VICommand, sizeof(VICommand),
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
903 (char *)SetTab, tab_width);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
904 vi_exec_cmd(VICommand);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
905 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
906 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
907 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
908 case '|':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
909 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
910 /* change the request separator */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
911 sniff_rq_sep[0] = arguments[0];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
912 /* echo the request */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
913 WriteToSniff(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
914 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
915 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
916 case 'A' : /* Warning/Info msg */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
917 vi_msg(arguments);
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
918 if (!strncmp(arguments, "Disconnected", 12))
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
919 sniff_disconnect(1); /* unexpected disconnection */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
920 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
921 case 'a' : /* Error msg */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
922 vi_error_msg(arguments);
426
eaf8b83ac767 updated for version 7.0111
vimboss
parents: 272
diff changeset
923 if (!strncmp(arguments, "Cannot connect", 14))
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
924 sniff_disconnect(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
925 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
926
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
927 default :
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
928 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
929 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
930 while(argc)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
931 vim_free(argv[--argc]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
932 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
933
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
934
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
935 #ifndef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
936 /* get_request
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
937 * read string from fd up to next newline (excluding the nl),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
938 * returns length of string
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
939 * 0 if no data available or no complete line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
940 * <0 on error
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
941 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
942 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
943 get_request(fd, buf, maxlen)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
944 int fd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
945 char *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
946 int maxlen;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
947 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
948 static char inbuf[1024];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
949 static int pos = 0, bytes = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
950 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
951 #ifdef HAVE_SELECT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
952 struct timeval tval;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
953 fd_set rfds;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
954
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
955 FD_ZERO(&rfds);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
956 FD_SET(fd, &rfds);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
957 tval.tv_sec = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
958 tval.tv_usec = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
959 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
960 struct pollfd fds;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
961
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
962 fds.fd = fd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
963 fds.events = POLLIN;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
964 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
965
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
966 for (len = 0; len < maxlen; len++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
967 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
968 if (pos >= bytes) /* end of buffer reached? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
969 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
970 #ifdef HAVE_SELECT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
971 if (select(fd + 1, &rfds, NULL, NULL, &tval) > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
972 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
973 if (poll(&fds, 1, 0) > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
974 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
975 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
976 pos = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
977 bytes = read(fd, inbuf, sizeof(inbuf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
978 if (bytes <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
979 return bytes;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
980 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
981 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
982 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
983 pos = pos-len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
984 buf[0] = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
985 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
986 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
987 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
988 if ((buf[len] = inbuf[pos++]) =='\n')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
989 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
990 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
991 buf[len] = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
992 return len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
993 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
994 #endif /* WIN32 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
995
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
996
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
997 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
998 SendRequest(command, symbol)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
999 struct sn_cmd *command;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1000 char *symbol;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1001 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1002 int cmd_type = command->cmd_type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1003 static char cmdstr[MAX_REQUEST_LEN];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1004 static char msgtxt[MAX_REQUEST_LEN];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1005 char *buffer_name = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1006
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1007 if (cmd_type == RQ_CONNECT)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1008 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1009 sniff_connect();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1010 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1011 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1012 if (!sniff_connected && !(cmd_type & SILENT))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1013 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1014 vi_error_msg(_("E278: SNiFF+ not connected"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1015 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1016 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1017
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1018 if (cmd_type & NEED_FILE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1019 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1020 if (!curbuf->b_sniff)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1021 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1022 if (!(cmd_type & SILENT))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1023 vi_error_msg(_("E279: Not a SNiFF+ buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1024 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1025 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1026 buffer_name = vi_buffer_name();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1027 if (buffer_name == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1028 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1029 if (cmd_type & NEED_SYMBOL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1030 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1031 if (cmd_type & EMPTY_SYMBOL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1032 symbol = " ";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1033 else if (!symbol && !(symbol = vi_symbol_under_cursor()))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1034 return; /* error msg already displayed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1035 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1036
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1037 if (symbol)
272
ddada568db54 updated for version 7.0073
vimboss
parents: 7
diff changeset
1038 vim_snprintf(cmdstr, sizeof(cmdstr), "%c%s%s%ld%s%s\n",
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1039 command->cmd_code,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1040 buffer_name,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1041 sniff_rq_sep,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1042 vi_cursor_pos(),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1043 sniff_rq_sep,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1044 symbol
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1045 );
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1046 else
272
ddada568db54 updated for version 7.0073
vimboss
parents: 7
diff changeset
1047 vim_snprintf(cmdstr, sizeof(cmdstr), "%c%s\n",
ddada568db54 updated for version 7.0073
vimboss
parents: 7
diff changeset
1048 command->cmd_code, buffer_name);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1049 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1050 else /* simple request */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1051 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1052 cmdstr[0] = command->cmd_code;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1053 cmdstr[1] = '\n';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1054 cmdstr[2] = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1055 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1056 if (command->cmd_msg && !(cmd_type & SILENT))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1057 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1058 if ((cmd_type & NEED_SYMBOL) && !(cmd_type & EMPTY_SYMBOL))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1059 {
272
ddada568db54 updated for version 7.0073
vimboss
parents: 7
diff changeset
1060 vim_snprintf(msgtxt, sizeof(msgtxt), "%s: %s",
ddada568db54 updated for version 7.0073
vimboss
parents: 7
diff changeset
1061 _(command->cmd_msg), symbol);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1062 vi_msg(msgtxt);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1063 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1064 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1065 vi_msg(_(command->cmd_msg));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1066 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1067 WriteToSniff(cmdstr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1068 if (cmd_type & DISCONNECT)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1069 sniff_disconnect(0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1070 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1071
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1072
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1073
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1074 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1075 WriteToSniff(str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1076 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1077 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1078 int bytes;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1079 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1080 if (! WriteFile(handle_to_sniff, str, strlen(str), &bytes, NULL))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1081 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1082 DWORD err=GetLastError();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1083 bytes = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1084 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1085 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1086 bytes = write(fd_to_sniff, str, strlen(str));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1087 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1088 if (bytes<0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1089 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1090 vi_msg(_("Sniff: Error during write. Disconnected"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1091 sniff_disconnect(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1092 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1093 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1094
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1095 /*-------- vim helping functions --------------------------------*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1096
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1097 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1098 vi_msg(str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1099 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1100 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1101 if (str != NULL && *str != NUL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1102 MSG((char_u *)str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1103 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1104
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1105 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1106 vi_error_msg(str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1107 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1108 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1109 if (str != NULL && *str != NUL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1110 EMSG((char_u *)str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1111 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1113 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1114 vi_open_file(fname)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1115 char *fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1116 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1117 ++no_wait_return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1118 do_ecmd(0, (char_u *)fname, NULL, NULL, ECMD_ONE, ECMD_HIDE+ECMD_OLDBUF);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1119 curbuf->b_sniff = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1120 --no_wait_return; /* [ex_docmd.c] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1121 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1122
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1123 static buf_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1124 vi_find_buffer(fname)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1125 char *fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1126 { /* derived from buflist_findname() [buffer.c] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1127 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1128
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1129 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1130 if (buf->b_sfname != NULL && fnamecmp(fname, buf->b_sfname) == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1131 return (buf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1132 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1133 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1134
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1135
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1136 static char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1137 vi_symbol_under_cursor()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1138 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1139 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1140 char *symbolp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1141 char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1142 static char sniff_symbol[256];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1143
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1144 len = find_ident_under_cursor((char_u **)&symbolp, FIND_IDENT);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1145 /* [normal.c] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1146 if (len <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1147 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1148 for (p=sniff_symbol; len; len--)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1149 *p++ = *symbolp++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1150 *p = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1151 return sniff_symbol;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1152 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1153
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1154
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1155 static char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1156 vi_buffer_name()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1157 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1158 return (char *)curbuf->b_sfname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1159 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1160
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1161 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1162 vi_exec_cmd(vicmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1163 char *vicmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1164 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1165 do_cmdline_cmd((char_u *)vicmd); /* [ex_docmd.c] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1166 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1167
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1168 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1169 * Set cursor on character position
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1170 * derived from cursor_pos_info() [buffer.c]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1171 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1172 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1173 vi_set_cursor_pos(char_pos)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1174 long char_pos;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1175 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1176 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1177 long char_count = 1; /* first position = 1 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1178 int line_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1179 int eol_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1180
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1181 if (char_pos == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1182 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1183 char_pos = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1184 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1185 if (get_fileformat(curbuf) == EOL_DOS)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1186 eol_size = 2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1187 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1188 eol_size = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1189 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1190 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1191 line_size = STRLEN(ml_get(lnum)) + eol_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1192 if (char_count+line_size > char_pos) break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1193 char_count += line_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1194 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1195 curwin->w_cursor.lnum = lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1196 curwin->w_cursor.col = char_pos - char_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1197 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1198
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1199 static long
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1200 vi_cursor_pos()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1201 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1202 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1203 long char_count=1; /* sniff starts with pos 1 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1204 int line_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1205 int eol_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1206
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1207 if (curbuf->b_p_tx)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1208 eol_size = 2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1209 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1210 eol_size = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1211 for (lnum = 1; lnum < curwin->w_cursor.lnum; ++lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1212 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1213 line_size = STRLEN(ml_get(lnum)) + eol_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1214 char_count += line_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1215 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1216 return char_count + curwin->w_cursor.col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1217 }