-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetbeans.c
3489 lines (3103 loc) · 85.5 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 noet:
*
* 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.
*
* The Netbeans messages are received and queued in the gui event loop, or in
* the select loop when Vim runs in a terminal. These messages are processed
* by netbeans_parse_messages() which is invoked in the idle loop when Vim is
* waiting for user input. The function netbeans_parse_messages() is also
* called from the ":sleep" command, to allow the execution of test cases that
* may not invoke the idle loop.
*/
#include "vim.h"
#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
#ifndef MSWIN
# include <netdb.h>
# ifdef HAVE_LIBGEN_H
# include <libgen.h>
# endif
#endif
#include "version.h"
#define GUARDED 10000 // typenr for "guarded" annotation
#define GUARDEDOFFSET 1000000 // base for "guarded" sign id's
#define MAX_COLOR_LENGTH 32 // max length of color name in defineAnnoType
// The first implementation (working only with Netbeans) returned "1.1". The
// protocol implemented here also supports A-A-P.
static char *ExtEdProtocolVersion = "2.5";
static long pos2off(buf_T *, pos_T *);
static pos_T *off2pos(buf_T *, long);
static pos_T *get_off_or_lnum(buf_T *buf, char_u **argp);
static long get_buf_size(buf_T *);
static int netbeans_keystring(char_u *keystr);
static void special_keys(char_u *args);
static int getConnInfo(char *file, char **host, char **port, char **password);
static void nb_init_graphics(void);
static void coloncmd(char *cmd, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
static void nb_set_curbuf(buf_T *buf);
static void nb_parse_cmd(char_u *);
static int nb_do_cmd(int, char_u *, int, int, char_u *);
static void nb_send(char *buf, char *fun);
static void nb_free(void);
#define NETBEANS_OPEN (channel_can_write_to(nb_channel))
static channel_T *nb_channel = NULL;
static int r_cmdno; // current command number for reply
static int dosetvisible = FALSE;
/*
* Include the debugging code if wanted.
*/
#ifdef NBDEBUG
# include "nbdebug.c"
#endif
static int needupdate = 0;
static int inAtomic = 0;
/*
* Callback invoked when the channel is closed.
*/
static void
nb_channel_closed(void)
{
nb_channel = NULL;
}
/*
* Close the connection and cleanup.
* May be called when the socket was closed earlier.
*/
static void
netbeans_close(void)
{
if (NETBEANS_OPEN)
{
netbeans_send_disconnect();
if (nb_channel != NULL)
{
// Close the socket and remove the input handlers.
channel_close(nb_channel, TRUE);
channel_clear(nb_channel);
}
nb_channel = NULL;
}
#ifdef FEAT_BEVAL_GUI
bevalServers &= ~BEVAL_NETBEANS;
#endif
needupdate = 0;
inAtomic = 0;
nb_free();
// remove all signs and update the screen after gutter removal
coloncmd(":sign unplace *");
changed_window_setting();
update_screen(UPD_CLEAR);
setcursor();
cursor_on();
out_flush_cursor(TRUE, FALSE);
}
#define NB_DEF_HOST "localhost"
#define NB_DEF_ADDR "3219"
#define NB_DEF_PASS "changeme"
static int
netbeans_connect(char *params, int doabort)
{
int port;
char buf[32];
char *hostname = NULL;
char *address = NULL;
char *password = NULL;
char *fname;
char *arg = NULL;
if (*params == '=')
{
// "=fname": Read info from specified file.
if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
return FAIL;
}
else
{
if (*params == ':')
// ":<host>:<addr>:<password>": get info from argument
arg = params + 1;
if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
{
// "": get info from file specified in environment
if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
return FAIL;
}
else
{
if (arg != NULL)
{
// ":<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)
{
port = atoi(address);
nb_channel = channel_open(hostname, port, 3000, nb_channel_closed);
if (nb_channel != NULL)
{
// success
# ifdef FEAT_BEVAL_GUI
bevalServers |= BEVAL_NETBEANS;
# endif
// success, login
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");
}
}
if (nb_channel == NULL && doabort)
getout(1);
vim_free(hostname);
vim_free(address);
vim_free(password);
return NETBEANS_OPEN ? OK : FAIL;
}
/*
* 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 *nlp;
#ifdef UNIX
stat_T 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)
{
nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
file));
semsg(_(e_wrong_access_mode_for_netbeans_connection_info_file_str),
file);
return FAIL;
}
#endif
fp = mch_fopen(file, "r");
if (fp == NULL)
{
nbdebug(("Cannot open NetBeans connection info file\n"));
PERROR(e_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 ((nlp = vim_strchr(lp, '\n')) != NULL)
*nlp = 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
{
char_u *keystr;
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.
* We store the string, because it may depend on the global mod_mask and
* :nbkey doesn't have a key number.
*/
static void
postpone_keycommand(char_u *keystr)
{
keyQ_T *node;
node = ALLOC_ONE(keyQ_T);
if (node == NULL)
return; // out of memory, drop the key
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->keystr = vim_strsave(keystr);
}
/*
* Handle any queued-up NetBeans keycommands to be send.
*/
static void
handle_key_queue(void)
{
int postponed = FALSE;
while (!postponed && 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. This may cause it to be postponed again
// and change keyHead.
if (node->keystr != NULL)
postponed = !netbeans_keystring(node->keystr);
vim_free(node->keystr);
// Finally, dispose of the node
vim_free(node);
}
}
/*
* While there's still a command in the work queue, parse and execute it.
*/
void
netbeans_parse_messages(void)
{
readq_T *node;
char_u *buffer;
char_u *p;
int own_node;
while (nb_channel != NULL)
{
node = channel_peek(nb_channel, PART_SOCK);
if (node == NULL)
break; // nothing to read
// Locate the end of the first line in the first buffer.
p = channel_first_nl(node);
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 (channel_collapse(nb_channel, PART_SOCK, TRUE) == FAIL)
return;
continue;
}
// 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)
{
own_node = TRUE;
buffer = channel_get(nb_channel, PART_SOCK, NULL);
// "node" is now invalid!
}
else
{
own_node = FALSE;
buffer = node->rq_buffer;
}
// Now, parse and execute the commands. This may set nb_channel to
// NULL if the channel is closed.
nb_parse_cmd(buffer);
if (own_node)
// buffer finished, dispose of it
vim_free(buffer);
else if (nb_channel != NULL)
// more follows, move it to the start
channel_consume(nb_channel, PART_SOCK, (int)(p - buffer));
}
}
/*
* 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!
// Disconnect before exiting, Motif hangs in a Select error
// message otherwise.
netbeans_close();
getout(0);
// NOTREACHED
}
if (STRCMP(cmd, "DETACH") == 0)
{
buf_T *buf;
FOR_ALL_BUFFERS(buf)
buf->b_has_sign_column = FALSE;
// The IDE is breaking the connection.
netbeans_close();
return;
}
bufno = strtol((char *)cmd, &verb, 10);
if (*verb != ':')
{
nbdebug((" missing colon: %s\n", cmd));
semsg(e_missing_colon_str, 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)
{
nbdebug((" missing ! or / in: %s\n", cmd));
semsg(e_missing_bang_or_slash_in_str, 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 command 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(e_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 = NULL;
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 = NULL;
static int globalsignmaplen = 0;
static int globalsignmapused = 0;
static int mapsigntype(nbbuf_T *, int localsigntype);
static void addsigntype(nbbuf_T *, int localsigntype, char_u *typeName,
char_u *tooltip, char_u *glyphfile,
char_u *fg, char_u *bg);
static void print_read_msg(nbbuf_T *buf);
static void print_save_msg(nbbuf_T *buf, off_T nchars);
static int curPCtype = -1;
/*
* Free netbeans resources.
*/
static void
nb_free(void)
{
keyQ_T *key_node = keyHead.next;
nbbuf_T buf;
int i;
// free the netbeans buffer list
for (i = 0; i < buf_list_used; i++)
{
buf = buf_list[i];
vim_free(buf.displayname);
vim_free(buf.signmap);
if (buf.bufp != NULL && buf_valid(buf.bufp))
{
buf.bufp->b_netbeans_file = FALSE;
buf.bufp->b_was_netbeans_file = FALSE;
}
}
VIM_CLEAR(buf_list);
buf_list_size = 0;
buf_list_used = 0;
// free the queued key commands
while (key_node != NULL && key_node != &keyHead)
{
keyQ_T *next = key_node->next;
vim_free(key_node->keystr);
vim_free(key_node);
if (next == &keyHead)
{
keyHead.next = &keyHead;
keyHead.prev = &keyHead;
break;
}
key_node = next;
}
// free the queued netbeans commands
if (nb_channel != NULL)
channel_clear(nb_channel);
}
/*
* 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 NETBEANS_OPEN && 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 (isNetbeansBuffer(bufp))
{
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 = alloc_clear(100 * sizeof(nbbuf_T));
buf_list_size = 100;
}
if (bufno >= buf_list_used) // new
{
if (bufno >= buf_list_size) // grow list
{
nbbuf_T *t_buf_list = buf_list;
size_t bufsize;
incr = bufno - buf_list_size + 90;
buf_list_size += incr;
bufsize = buf_list_size * sizeof(nbbuf_T);
if (bufsize == 0 || bufsize / sizeof(nbbuf_T)
!= (size_t)buf_list_size)
{
// list size overflow, bail out
return NULL;
}
buf_list = vim_realloc(buf_list, bufsize);
if (buf_list == NULL)
{
vim_free(t_buf_list);
buf_list_size = 0;
return NULL;
}
vim_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_ALL_BUFFERS(bufp)
if (bufp->b_changed)
++n;
return n;
}
/*
* End the netbeans session.
*/
void
netbeans_end(void)
{
int i;
static char buf[128];
if (!NETBEANS_OPEN)
return;
for (i = 0; i < buf_list_used; i++)
{
if (!buf_list[i].bufp)
continue;
if (netbeansForcedQuit)
{
// mark as unmodified so NetBeans won't put up dialog on "killed"
sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
nbdebug(("EVT: %s", buf));
nb_send(buf, "netbeans_end");
}
sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
nbdebug(("EVT: %s", buf));
// nb_send(buf, "netbeans_end"); avoid "write failed" messages
nb_send(buf, NULL);
buf_list[i].bufp = NULL;
}
}
/*
* Send a message to netbeans.
* When "fun" is NULL no error is given.
*/
static void
nb_send(char *buf, char *fun)
{
if (nb_channel != NULL)
channel_send(nb_channel, PART_SOCK, (char_u *)buf,
(int)STRLEN(buf), fun);
}
/*
* Some input received from netbeans requires a response. This function
* handles a response with no information (except the command number).
*/
static void
nb_reply_nil(int cmdno)
{
char reply[32];
nbdebug(("REP %d: <none>\n", cmdno));
// Avoid printing an annoying error message.
if (!NETBEANS_OPEN)
return;
sprintf(reply, "%d\n", cmdno);
nb_send(reply, "nb_reply_nil");
}
/*
* Send a response with text.
* "result" must have been quoted already (using nb_quote()).
*/
static void
nb_reply_text(int cmdno, char_u *result)
{
char_u *reply;
nbdebug(("REP %d: %s\n", cmdno, (char *)result));
reply = alloc(STRLEN(result) + 32);
sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
nb_send((char *)reply, "nb_reply_text");
vim_free(reply);
}
/*
* Send a response with a number result code.
*/
static void
nb_reply_nr(int cmdno, long result)
{
char reply[32];
nbdebug(("REP %d: %ld\n", cmdno, result));
sprintf(reply, "%d %ld\n", cmdno, result);
nb_send(reply, "nb_reply_nr");
}
/*
* Encode newline, ret, backslash, double quote for transmission to NetBeans.
*/
static char_u *
nb_quote(char_u *txt)
{
char_u *buf = alloc(2 * STRLEN(txt) + 1);
char_u *p = txt;
char_u *q = buf;
if (buf == NULL)
return NULL;
for (; *p; p++)
{
switch (*p)
{
case '\"':
case '\\':
*q++ = '\\'; *q++ = *p; break;
// case '\t':
// *q++ = '\\'; *q++ = 't'; break;
case '\n':
*q++ = '\\'; *q++ = 'n'; break;
case '\r':
*q++ = '\\'; *q++ = 'r'; break;
default:
*q++ = *p;
break;
}
}
*q = '\0';
return buf;
}
/*
* Remove top level double quotes; convert backslashed chars.
* Returns an allocated string (NULL for failure).
* If "endp" is not NULL it is set to the character after the terminating
* quote.
*/
static char *
nb_unquote(char_u *p, char_u **endp)
{
char *result = 0;
char *q;
int done = 0;
// result is never longer than input
result = alloc_clear(STRLEN(p) + 1);
if (result == NULL)
return NULL;
if (*p++ != '"')
{
nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
p));
result[0] = NUL;
return result;
}
for (q = result; !done && *p != NUL;)
{
switch (*p)
{
case '"':
/*
* Unbackslashed dquote marks the end, if first char was dquote.
*/
done = 1;
break;
case '\\':
++p;
switch (*p)
{
case '\\': *q++ = '\\'; break;
case 'n': *q++ = '\n'; break;
case 't': *q++ = '\t'; break;
case 'r': *q++ = '\r'; break;
case '"': *q++ = '"'; break;
case NUL: --p; break;
// default: skip over illegal chars
}
++p;
break;
default:
*q++ = *p++;
}
}
if (endp != NULL)
*endp = p;
return result;
}
/*
* Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
* current buffer. Remove to end of line when "last" is MAXCOL.
*/
static void
nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
{
char_u *oldtext, *newtext;
int oldlen;
int lastbyte = last;
oldtext = ml_get(lnum);
oldlen = (int)STRLEN(oldtext);
if (first >= (colnr_T)oldlen || oldlen == 0) // just in case
return;
if (lastbyte >= oldlen)
lastbyte = oldlen - 1;
newtext = alloc(oldlen - (int)(lastbyte - first));
if (newtext == NULL)
return;
mch_memmove(newtext, oldtext, first);
STRMOVE(newtext + first, oldtext + lastbyte + 1);
nbdebug((" NEW LINE %ld: %s\n", lnum, newtext));
ml_replace(lnum, newtext, FALSE);
}
/*
* Replace the "first" line with the concatenation of the "first" and
* the "other" line. The "other" line is not removed.
*/
static void
nb_joinlines(linenr_T first, linenr_T other)
{
int len_first, len_other;
char_u *p;
len_first = (int)STRLEN(ml_get(first));
len_other = (int)STRLEN(ml_get(other));
p = alloc(len_first + len_other + 1);
if (p == NULL)
return;
mch_memmove(p, ml_get(first), len_first);
mch_memmove(p + len_first, ml_get(other), len_other + 1);
ml_replace(first, p, FALSE);
}
#define SKIP_STOP 2
#define streq(a,b) (strcmp(a,b) == 0)
/*
* Do the actual processing of a single netbeans command or function.
* The difference between a command and function is that a function
* gets a response (it's required) but a command does not.
* For arguments see comment for nb_parse_cmd().
*/
static int
nb_do_cmd(
int bufno,
char_u *cmd,
int func,
int cmdno,
char_u *args) // points to space before arguments or NUL
{
int do_update = 0;
long off = 0;
nbbuf_T *buf = nb_get_buf(bufno);
static int skip = 0;
int retval = OK;
char *cp; // for when a char pointer is needed
nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
if (func)
{
// =====================================================================