-
Notifications
You must be signed in to change notification settings - Fork 0
/
netbeans.c
3520 lines (3139 loc) · 81.3 KB
/
netbeans.c
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
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
* Netbeans integration by David Weatherford
* Adopted for Win32 by Sergey Khorev
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
*/
/*
* Implements client side of org.netbeans.modules.emacs editor
* integration protocol. Be careful! The protocol uses offsets
* which are *between* characters, whereas vim uses line number
* and column number which are *on* characters.
* See ":help netbeans-protocol" for explanation.
*/
#include "vim.h"
#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
/* Note: when making changes here also adjust configure.in. */
# include <fcntl.h>
#ifdef WIN32
# ifdef DEBUG
# include <tchar.h> /* for _T definition for TRACEn macros */
# endif
# include "vimio.h"
/* WinSock API is separated from C API, thus we can't use read(), write(),
* errno... */
# define sock_errno WSAGetLastError()
# define ECONNREFUSED WSAECONNREFUSED
# ifdef EINTR
# undef EINTR
# endif
# define EINTR WSAEINTR
# define sock_write(sd, buf, len) send(sd, buf, len, 0)
# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
# define sock_close(sd) closesocket(sd)
# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
#else
# include <netdb.h>
# include <netinet/in.h>
# include <sys/socket.h>
# ifdef HAVE_LIBGEN_H
# include <libgen.h>
# endif
# define sock_errno errno
# define sock_write(sd, buf, len) write(sd, buf, len)
# define sock_read(sd, buf, len) read(sd, buf, len)
# define sock_close(sd) close(sd)
#endif
#include "version.h"
#define INET_SOCKETS
#define GUARDED 10000 /* typenr for "guarded" annotation */
#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
/* The first implementation (working only with Netbeans) returned "1.1". The
* protocol implemented here also supports A-A-P. */
static char *ExtEdProtocolVersion = "2.4";
static long pos2off __ARGS((buf_T *, pos_T *));
static pos_T *off2pos __ARGS((buf_T *, long));
static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
static long get_buf_size __ARGS((buf_T *));
static void netbeans_keystring __ARGS((int key, char *keystr));
static void special_keys __ARGS((char_u *args));
static void netbeans_connect __ARGS((void));
static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
static void nb_init_graphics __ARGS((void));
static void coloncmd __ARGS((char *cmd, ...));
static void nb_set_curbuf __ARGS((buf_T *buf));
#ifdef FEAT_GUI_MOTIF
static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
#endif
#ifdef FEAT_GUI_GTK
static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
#endif
static void nb_parse_cmd __ARGS((char_u *));
static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
static void nb_send __ARGS((char *buf, char *fun));
#ifdef WIN64
typedef __int64 NBSOCK;
#else
typedef int NBSOCK;
#endif
static NBSOCK sd = -1; /* socket fd for Netbeans connection */
#ifdef FEAT_GUI_MOTIF
static XtInputId inputHandler; /* Cookie for input */
#endif
#ifdef FEAT_GUI_GTK
static gint inputHandler; /* Cookie for input */
#endif
#ifdef FEAT_GUI_W32
static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
extern HWND s_hwnd; /* Gvim's Window handle */
#endif
static int r_cmdno; /* current command number for reply */
static int haveConnection = FALSE; /* socket is connected and
initialization is done */
#ifdef FEAT_GUI_MOTIF
static void netbeans_Xt_connect __ARGS((void *context));
#endif
#ifdef FEAT_GUI_GTK
static void netbeans_gtk_connect __ARGS((void));
#endif
#ifdef FEAT_GUI_W32
static void netbeans_w32_connect __ARGS((void));
#endif
static int dosetvisible = FALSE;
/*
* Include the debugging code if wanted.
*/
#ifdef NBDEBUG
# include "nbdebug.c"
#endif
/* Connect back to Netbeans process */
#ifdef FEAT_GUI_MOTIF
static void
netbeans_Xt_connect(void *context)
{
netbeans_connect();
if (sd > 0)
{
/* tell notifier we are interested in being called
* when there is input on the editor connection socket
*/
inputHandler = XtAppAddInput((XtAppContext)context, sd,
(XtPointer)(XtInputReadMask + XtInputExceptMask),
messageFromNetbeans, NULL);
}
}
static void
netbeans_disconnect(void)
{
if (inputHandler != (XtInputId)NULL)
{
XtRemoveInput(inputHandler);
inputHandler = (XtInputId)NULL;
}
sd = -1;
haveConnection = FALSE;
# ifdef FEAT_BEVAL
bevalServers &= ~BEVAL_NETBEANS;
# endif
}
#endif /* FEAT_MOTIF_GUI */
#ifdef FEAT_GUI_GTK
static void
netbeans_gtk_connect(void)
{
netbeans_connect();
if (sd > 0)
{
/*
* Tell gdk we are interested in being called when there
* is input on the editor connection socket
*/
inputHandler = gdk_input_add((gint)sd, (GdkInputCondition)
((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
messageFromNetbeans, NULL);
}
}
static void
netbeans_disconnect(void)
{
if (inputHandler != 0)
{
gdk_input_remove(inputHandler);
inputHandler = 0;
}
sd = -1;
haveConnection = FALSE;
# ifdef FEAT_BEVAL
bevalServers &= ~BEVAL_NETBEANS;
# endif
}
#endif /* FEAT_GUI_GTK */
#if defined(FEAT_GUI_W32) || defined(PROTO)
static void
netbeans_w32_connect(void)
{
netbeans_connect();
if (sd > 0)
{
/*
* Tell Windows we are interested in receiving message when there
* is input on the editor connection socket
*/
inputHandler = WSAAsyncSelect(sd, s_hwnd, WM_NETBEANS, FD_READ);
}
}
static void
netbeans_disconnect(void)
{
if (inputHandler == 0)
{
WSAAsyncSelect(sd, s_hwnd, 0, 0);
inputHandler = -1;
}
sd = -1;
haveConnection = FALSE;
# ifdef FEAT_BEVAL
bevalServers &= ~BEVAL_NETBEANS;
# endif
}
#endif /* FEAT_GUI_W32 */
#define NB_DEF_HOST "localhost"
#define NB_DEF_ADDR "3219"
#define NB_DEF_PASS "changeme"
static void
netbeans_connect(void)
{
#ifdef INET_SOCKETS
struct sockaddr_in server;
struct hostent * host;
# ifdef FEAT_GUI_W32
u_short port;
# else
int port;
#endif
#else
struct sockaddr_un server;
#endif
char buf[32];
char *hostname = NULL;
char *address = NULL;
char *password = NULL;
char *fname;
char *arg = NULL;
if (netbeansArg[3] == '=')
{
/* "-nb=fname": Read info from specified file. */
if (getConnInfo(netbeansArg + 4, &hostname, &address, &password)
== FAIL)
return;
}
else
{
if (netbeansArg[3] == ':')
/* "-nb:<host>:<addr>:<password>": get info from argument */
arg = netbeansArg + 4;
if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
{
/* "-nb": get info from file specified in environment */
if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
return;
}
else
{
if (arg != NULL)
{
/* "-nb:<host>:<addr>:<password>": get info from argument */
hostname = arg;
address = strchr(hostname, ':');
if (address != NULL)
{
*address++ = '\0';
password = strchr(address, ':');
if (password != NULL)
*password++ = '\0';
}
}
/* Get the missing values from the environment. */
if (hostname == NULL || *hostname == '\0')
hostname = getenv("__NETBEANS_HOST");
if (address == NULL)
address = getenv("__NETBEANS_SOCKET");
if (password == NULL)
password = getenv("__NETBEANS_VIM_PASSWORD");
/* Move values to allocated memory. */
if (hostname != NULL)
hostname = (char *)vim_strsave((char_u *)hostname);
if (address != NULL)
address = (char *)vim_strsave((char_u *)address);
if (password != NULL)
password = (char *)vim_strsave((char_u *)password);
}
}
/* Use the default when a value is missing. */
if (hostname == NULL || *hostname == '\0')
{
vim_free(hostname);
hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
}
if (address == NULL || *address == '\0')
{
vim_free(address);
address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
}
if (password == NULL || *password == '\0')
{
vim_free(password);
password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
}
if (hostname == NULL || address == NULL || password == NULL)
goto theend; /* out of memory */
#ifdef INET_SOCKETS
port = atoi(address);
if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
{
PERROR("socket() in netbeans_connect()");
goto theend;
}
/* Get the server internet address and put into addr structure */
/* fill in the socket address structure and connect to server */
memset((char *)&server, '\0', sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if ((host = gethostbyname(hostname)) == NULL)
{
if (mch_access(hostname, R_OK) >= 0)
{
/* DEBUG: input file */
sd = mch_open(hostname, O_RDONLY, 0);
goto theend;
}
PERROR("gethostbyname() in netbeans_connect()");
sd = -1;
goto theend;
}
memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
#else
if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
PERROR("socket()");
goto theend;
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, address);
#endif
/* Connect to server */
if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
{
nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno));
if (sock_errno == ECONNREFUSED)
{
sock_close(sd);
#ifdef INET_SOCKETS
if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
{
PERROR("socket()#2 in netbeans_connect()");
goto theend;
}
#else
if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
PERROR("socket()#2 in netbeans_connect()");
goto theend;
}
#endif
if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
{
int retries = 36;
int success = FALSE;
while (retries--
&& ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR)))
{
nbdebug(("retrying...\n"));
sleep(5);
if (connect(sd, (struct sockaddr *)&server,
sizeof(server)) == 0)
{
success = TRUE;
break;
}
}
if (!success)
{
/* Get here when the server can't be found. */
PERROR(_("Cannot connect to Netbeans #2"));
getout(1);
}
}
}
else
{
PERROR(_("Cannot connect to Netbeans"));
getout(1);
}
}
vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
nb_send(buf, "netbeans_connect");
sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
nb_send(buf, "externaleditor_version");
/* nb_init_graphics(); delay until needed */
haveConnection = TRUE;
theend:
vim_free(hostname);
vim_free(address);
vim_free(password);
return;
}
/*
* Obtain the NetBeans hostname, port address and password from a file.
* Return the strings in allocated memory.
* Return FAIL if the file could not be read, OK otherwise (no matter what it
* contains).
*/
static int
getConnInfo(char *file, char **host, char **port, char **auth)
{
FILE *fp;
char_u buf[BUFSIZ];
char_u *lp;
char_u *nl;
#ifdef UNIX
struct stat st;
/*
* For Unix only accept the file when it's not accessible by others.
* The open will then fail if we don't own the file.
*/
if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
{
EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
file);
return FAIL;
}
#endif
fp = mch_fopen(file, "r");
if (fp == NULL)
{
PERROR("E660: Cannot open NetBeans connection info file");
return FAIL;
}
/* Read the file. There should be one of each parameter */
while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
{
if ((nl = vim_strchr(lp, '\n')) != NULL)
*nl = 0; /* strip off the trailing newline */
if (STRNCMP(lp, "host=", 5) == 0)
{
vim_free(*host);
*host = (char *)vim_strsave(&buf[5]);
}
else if (STRNCMP(lp, "port=", 5) == 0)
{
vim_free(*port);
*port = (char *)vim_strsave(&buf[5]);
}
else if (STRNCMP(lp, "auth=", 5) == 0)
{
vim_free(*auth);
*auth = (char *)vim_strsave(&buf[5]);
}
}
fclose(fp);
return OK;
}
struct keyqueue
{
int key;
struct keyqueue *next;
struct keyqueue *prev;
};
typedef struct keyqueue keyQ_T;
static keyQ_T keyHead; /* dummy node, header for circular queue */
/*
* Queue up key commands sent from netbeans.
*/
static void
postpone_keycommand(int key)
{
keyQ_T *node;
node = (keyQ_T *)alloc(sizeof(keyQ_T));
if (keyHead.next == NULL) /* initialize circular queue */
{
keyHead.next = &keyHead;
keyHead.prev = &keyHead;
}
/* insert node at tail of queue */
node->next = &keyHead;
node->prev = keyHead.prev;
keyHead.prev->next = node;
keyHead.prev = node;
node->key = key;
}
/*
* Handle any queued-up NetBeans keycommands to be send.
*/
static void
handle_key_queue(void)
{
while (keyHead.next && keyHead.next != &keyHead)
{
/* first, unlink the node */
keyQ_T *node = keyHead.next;
keyHead.next = node->next;
node->next->prev = node->prev;
/* now, send the keycommand */
netbeans_keycommand(node->key);
/* Finally, dispose of the node */
vim_free(node);
}
}
struct cmdqueue
{
char_u *buffer;
struct cmdqueue *next;
struct cmdqueue *prev;
};
typedef struct cmdqueue queue_T;
static queue_T head; /* dummy node, header for circular queue */
/*
* Put the buffer on the work queue; possibly save it to a file as well.
*/
static void
save(char_u *buf, int len)
{
queue_T *node;
node = (queue_T *)alloc(sizeof(queue_T));
if (node == NULL)
return; /* out of memory */
node->buffer = alloc(len + 1);
if (node->buffer == NULL)
{
vim_free(node);
return; /* out of memory */
}
mch_memmove(node->buffer, buf, (size_t)len);
node->buffer[len] = NUL;
if (head.next == NULL) /* initialize circular queue */
{
head.next = &head;
head.prev = &head;
}
/* insert node at tail of queue */
node->next = &head;
node->prev = head.prev;
head.prev->next = node;
head.prev = node;
#ifdef NBDEBUG
{
static int outfd = -2;
/* possibly write buffer out to a file */
if (outfd == -3)
return;
if (outfd == -2)
{
char *file = getenv("__NETBEANS_SAVE");
if (file == NULL)
outfd = -3;
else
outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
}
if (outfd >= 0)
write(outfd, buf, len);
}
#endif
}
/*
* While there's still a command in the work queue, parse and execute it.
*/
static void
nb_parse_messages(void)
{
char_u *p;
queue_T *node;
while (head.next != &head)
{
node = head.next;
/* Locate the first line in the first buffer. */
p = vim_strchr(node->buffer, '\n');
if (p == NULL)
{
/* Command isn't complete. If there is no following buffer,
* return (wait for more). If there is another buffer following,
* prepend the text to that buffer and delete this one. */
if (node->next == &head)
return;
p = alloc((unsigned)(STRLEN(node->buffer) + STRLEN(node->next->buffer) + 1));
if (p == NULL)
return; /* out of memory */
STRCPY(p, node->buffer);
STRCAT(p, node->next->buffer);
vim_free(node->next->buffer);
node->next->buffer = p;
/* dispose of the node and buffer */
head.next = node->next;
node->next->prev = node->prev;
vim_free(node->buffer);
vim_free(node);
}
else
{
/* There is a complete command at the start of the buffer.
* Terminate it with a NUL. When no more text is following unlink
* the buffer. Do this before executing, because new buffers can
* be added while busy handling the command. */
*p++ = NUL;
if (*p == NUL)
{
head.next = node->next;
node->next->prev = node->prev;
}
/* now, parse and execute the commands */
nb_parse_cmd(node->buffer);
if (*p == NUL)
{
/* buffer finished, dispose of the node and buffer */
vim_free(node->buffer);
vim_free(node);
}
else
{
/* more follows, move to the start */
mch_memmove(node->buffer, p, STRLEN(p) + 1);
}
}
}
}
/* Buffer size for reading incoming messages. */
#define MAXMSGSIZE 4096
/*
* Read and process a command from netbeans.
*/
/*ARGSUSED*/
#if defined(FEAT_GUI_W32) || defined(PROTO)
/* Use this one when generating prototypes, the others are static. */
void
messageFromNetbeansW32()
#else
# ifdef FEAT_GUI_MOTIF
static void
messageFromNetbeans(XtPointer clientData, int *unused1, XtInputId *unused2)
# endif
# ifdef FEAT_GUI_GTK
static void
messageFromNetbeans(gpointer clientData, gint unused1,
GdkInputCondition unused2)
# endif
#endif
{
static char_u *buf = NULL;
int len;
int readlen = 0;
static int level = 0;
if (sd < 0)
{
nbdebug(("messageFromNetbeans() called without a socket\n"));
return;
}
++level; /* recursion guard; this will be called from the X event loop */
/* Allocate a buffer to read into. */
if (buf == NULL)
{
buf = alloc(MAXMSGSIZE);
if (buf == NULL)
return; /* out of memory! */
}
/* Keep on reading for as long as there is something to read. */
for (;;)
{
len = sock_read(sd, buf, MAXMSGSIZE);
if (len <= 0)
break; /* error or nothing more to read */
/* Store the read message in the queue. */
save(buf, len);
readlen += len;
if (len < MAXMSGSIZE)
break; /* did read everything that's available */
}
if (readlen <= 0)
{
/* read error or didn't read anything */
netbeans_disconnect();
nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
if (len < 0)
PERROR(_("read from Netbeans socket"));
return; /* don't try to parse it */
}
/* Parse the messages, but avoid recursion. */
if (level == 1)
nb_parse_messages();
--level;
}
/*
* Handle one NUL terminated command.
*
* format of a command from netbeans:
*
* 6:setTitle!84 "a.c"
*
* bufno
* colon
* cmd
* !
* cmdno
* args
*
* for function calls, the ! is replaced by a /
*/
static void
nb_parse_cmd(char_u *cmd)
{
char *verb;
char *q;
int bufno;
int isfunc = -1;
if (STRCMP(cmd, "DISCONNECT") == 0)
{
/* We assume the server knows that we can safely exit! */
if (sd >= 0)
sock_close(sd);
/* Disconnect before exiting, Motif hangs in a Select error
* message otherwise. */
netbeans_disconnect();
getout(0);
/* NOTREACHED */
}
if (STRCMP(cmd, "DETACH") == 0)
{
/* The IDE is breaking the connection. */
if (sd >= 0)
sock_close(sd);
netbeans_disconnect();
return;
}
bufno = strtol((char *)cmd, &verb, 10);
if (*verb != ':')
{
EMSG2("E627: missing colon: %s", cmd);
return;
}
++verb; /* skip colon */
for (q = verb; *q; q++)
{
if (*q == '!')
{
*q++ = NUL;
isfunc = 0;
break;
}
else if (*q == '/')
{
*q++ = NUL;
isfunc = 1;
break;
}
}
if (isfunc < 0)
{
EMSG2("E628: missing ! or / in: %s", cmd);
return;
}
r_cmdno = strtol(q, &q, 10);
q = (char *)skipwhite((char_u *)q);
if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
{
#ifdef NBDEBUG
/*
* This happens because the ExtEd can send a cammand or 2 after
* doing a stopDocumentListen command. It doesn't harm anything
* so I'm disabling it except for debugging.
*/
nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
EMSG("E629: bad return from nb_do_cmd");
#endif
}
}
struct nbbuf_struct
{
buf_T *bufp;
unsigned int fireChanges:1;
unsigned int initDone:1;
unsigned int insertDone:1;
unsigned int modified:1;
int nbbuf_number;
char *displayname;
int *signmap;
short_u signmaplen;
short_u signmapused;
};
typedef struct nbbuf_struct nbbuf_T;
static nbbuf_T *buf_list = 0;
static int buf_list_size = 0; /* size of buf_list */
static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
static char **globalsignmap;
static int globalsignmaplen;
static int globalsignmapused;
static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
char_u *tooltip, char_u *glyphfile,
int usefg, int fg, int usebg, int bg));
static void print_read_msg __ARGS((nbbuf_T *buf));
static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
static int curPCtype = -1;
/*
* Get the Netbeans buffer number for the specified buffer.
*/
static int
nb_getbufno(buf_T *bufp)
{
int i;
for (i = 0; i < buf_list_used; i++)
if (buf_list[i].bufp == bufp)
return i;
return -1;
}
/*
* Is this a NetBeans-owned buffer?
*/
int
isNetbeansBuffer(buf_T *bufp)
{
return usingNetbeans && bufp->b_netbeans_file;
}
/*
* NetBeans and Vim have different undo models. In Vim, the file isn't
* changed if changes are undone via the undo command. In NetBeans, once
* a change has been made the file is marked as modified until saved. It
* doesn't matter if the change was undone.
*
* So this function is for the corner case where Vim thinks a buffer is
* unmodified but NetBeans thinks it IS modified.
*/
int
isNetbeansModified(buf_T *bufp)
{
if (usingNetbeans && bufp->b_netbeans_file)
{
int bufno = nb_getbufno(bufp);
if (bufno > 0)
return buf_list[bufno].modified;
else
return FALSE;
}
else
return FALSE;
}
/*
* Given a Netbeans buffer number, return the netbeans buffer.
* Returns NULL for 0 or a negative number. A 0 bufno means a
* non-buffer related command has been sent.
*/
static nbbuf_T *
nb_get_buf(int bufno)
{
/* find or create a buffer with the given number */
int incr;
if (bufno <= 0)
return NULL;
if (!buf_list)
{
/* initialize */
buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
buf_list_size = 100;
}
if (bufno >= buf_list_used) /* new */
{
if (bufno >= buf_list_size) /* grow list */
{
incr = bufno - buf_list_size + 90;
buf_list_size += incr;
buf_list = (nbbuf_T *)vim_realloc(
buf_list, buf_list_size * sizeof(nbbuf_T));
memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
}
while (buf_list_used <= bufno)
{
/* Default is to fire text changes. */
buf_list[buf_list_used].fireChanges = 1;
++buf_list_used;
}
}
return buf_list + bufno;
}
/*
* Return the number of buffers that are modified.
*/
static int
count_changed_buffers(void)
{
buf_T *bufp;
int n;
n = 0;
for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
if (bufp->b_changed)
++n;
return n;
}
/*
* End the netbeans session.
*/
void
netbeans_end(void)
{
int i;
static char buf[128];