annotate src/if_sniff.c @ 6044:b43ce6bb5d79

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