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