annotate src/if_sniff.c @ 9:6a13985590e6

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