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