-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
if_cscope.c
2524 lines (2230 loc) · 57.7 KB
/
if_cscope.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:
*
* CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
* Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
*
* The basic idea/structure of cscope for Vim was borrowed from Nvi. There
* might be a few lines of code that look similar to what Nvi has.
*
* See README.txt for an overview of the Vim source code.
*/
#include "vim.h"
#if defined(FEAT_CSCOPE) || defined(PROTO)
#include <sys/types.h>
#include <sys/stat.h>
#if defined(UNIX)
# include <sys/wait.h>
#endif
#include "if_cscope.h"
static void cs_usage_msg(csid_e x);
static int cs_add(exarg_T *eap);
static void cs_stat_emsg(char *fname);
static int cs_add_common(char *, char *, char *);
static int cs_check_for_connections(void);
static int cs_check_for_tags(void);
static int cs_cnt_connections(void);
static void cs_reading_emsg(int idx);
static int cs_cnt_matches(int idx);
static char * cs_create_cmd(char *csoption, char *pattern);
static int cs_create_connection(int i);
static void do_cscope_general(exarg_T *eap, int make_split);
#ifdef FEAT_QUICKFIX
static void cs_file_results(FILE *, int *);
#endif
static void cs_fill_results(char *, int , int *, char ***,
char ***, int *);
static int cs_find(exarg_T *eap);
static int cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline);
static int cs_help(exarg_T *eap);
static void clear_csinfo(int i);
static int cs_insert_filelist(char *, char *, char *,
stat_T *);
static int cs_kill(exarg_T *eap);
static void cs_kill_execute(int, char *);
static cscmd_T * cs_lookup_cmd(exarg_T *eap);
static char * cs_make_vim_style_matches(char *, char *,
char *, char *);
static char * cs_manage_matches(char **, char **, int, mcmd_e);
static char * cs_parse_results(int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search);
static char * cs_pathcomponents(char *path);
static void cs_print_tags_priv(char **, char **, int);
static int cs_read_prompt(int);
static void cs_release_csp(int, int freefnpp);
static int cs_reset(exarg_T *eap);
static char * cs_resolve_file(int, char *);
static int cs_show(exarg_T *eap);
static csinfo_T * csinfo = NULL;
static int csinfo_size = 0; /* number of items allocated in
csinfo[] */
static int eap_arg_len; /* length of eap->arg, set in
cs_lookup_cmd() */
static cscmd_T cs_cmds[] =
{
{ "add", cs_add,
N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
{ "find", cs_find,
N_("Query for a pattern"), "find a|c|d|e|f|g|i|s|t name", 1 },
{ "help", cs_help,
N_("Show this message"), "help", 0 },
{ "kill", cs_kill,
N_("Kill a connection"), "kill #", 0 },
{ "reset", cs_reset,
N_("Reinit all connections"), "reset", 0 },
{ "show", cs_show,
N_("Show connections"), "show", 0 },
{ NULL, NULL, NULL, NULL, 0 }
};
static void
cs_usage_msg(csid_e x)
{
(void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
}
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
static enum
{
EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
} expand_what;
/*
* Function given to ExpandGeneric() to obtain the cscope command
* expansion.
*/
char_u *
get_cscope_name(expand_T *xp UNUSED, int idx)
{
int current_idx;
int i;
switch (expand_what)
{
case EXP_CSCOPE_SUBCMD:
/* Complete with sub-commands of ":cscope":
* add, find, help, kill, reset, show */
return (char_u *)cs_cmds[idx].name;
case EXP_SCSCOPE_SUBCMD:
/* Complete with sub-commands of ":scscope": same sub-commands as
* ":cscope" but skip commands which don't support split windows */
for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
if (cs_cmds[i].cansplit)
if (current_idx++ == idx)
break;
return (char_u *)cs_cmds[i].name;
case EXP_CSCOPE_FIND:
{
const char *query_type[] =
{
"a", "c", "d", "e", "f", "g", "i", "s", "t", NULL
};
/* Complete with query type of ":cscope find {query_type}".
* {query_type} can be letters (c, d, ... a) or numbers (0, 1,
* ..., 9) but only complete with letters, since numbers are
* redundant. */
return (char_u *)query_type[idx];
}
case EXP_CSCOPE_KILL:
{
static char connection[5];
/* ":cscope kill" accepts connection numbers or partial names of
* the pathname of the cscope database as argument. Only complete
* with connection numbers. -1 can also be used to kill all
* connections. */
for (i = 0, current_idx = 0; i < csinfo_size; i++)
{
if (csinfo[i].fname == NULL)
continue;
if (current_idx++ == idx)
{
vim_snprintf(connection, sizeof(connection), "%d", i);
return (char_u *)connection;
}
}
return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
}
default:
return NULL;
}
}
/*
* Handle command line completion for :cscope command.
*/
void
set_context_in_cscope_cmd(
expand_T *xp,
char_u *arg,
cmdidx_T cmdidx)
{
char_u *p;
/* Default: expand subcommands */
xp->xp_context = EXPAND_CSCOPE;
xp->xp_pattern = arg;
expand_what = (cmdidx == CMD_scscope)
? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
/* (part of) subcommand already typed */
if (*arg != NUL)
{
p = skiptowhite(arg);
if (*p != NUL) /* past first word */
{
xp->xp_pattern = skipwhite(p);
if (*skiptowhite(xp->xp_pattern) != NUL)
xp->xp_context = EXPAND_NOTHING;
else if (STRNICMP(arg, "add", p - arg) == 0)
xp->xp_context = EXPAND_FILES;
else if (STRNICMP(arg, "kill", p - arg) == 0)
expand_what = EXP_CSCOPE_KILL;
else if (STRNICMP(arg, "find", p - arg) == 0)
expand_what = EXP_CSCOPE_FIND;
else
xp->xp_context = EXPAND_NOTHING;
}
}
}
#endif /* FEAT_CMDL_COMPL */
/*
* Find the command, print help if invalid, and then call the corresponding
* command function.
*/
static void
do_cscope_general(
exarg_T *eap,
int make_split UNUSED) /* whether to split window */
{
cscmd_T *cmdp;
if ((cmdp = cs_lookup_cmd(eap)) == NULL)
{
cs_help(eap);
return;
}
if (make_split)
{
if (!cmdp->cansplit)
{
(void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
return;
}
postponed_split = -1;
postponed_split_flags = cmdmod.split;
postponed_split_tab = cmdmod.tab;
}
cmdp->func(eap);
postponed_split_flags = 0;
postponed_split_tab = 0;
}
/*
* Implementation of ":cscope" and ":lcscope"
*/
void
ex_cscope(exarg_T *eap)
{
do_cscope_general(eap, FALSE);
}
/*
* Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
*/
void
ex_scscope(exarg_T *eap)
{
do_cscope_general(eap, TRUE);
}
/*
* Implementation of ":cstag"
*/
void
ex_cstag(exarg_T *eap)
{
int ret = FALSE;
if (*eap->arg == NUL)
{
(void)EMSG(_("E562: Usage: cstag <ident>"));
return;
}
switch (p_csto)
{
case 0 :
if (cs_check_for_connections())
{
ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
FALSE, *eap->cmdlinep);
if (ret == FALSE)
{
cs_free_tags();
if (msg_col)
msg_putchar('\n');
if (cs_check_for_tags())
ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
}
}
else if (cs_check_for_tags())
{
ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
}
break;
case 1 :
if (cs_check_for_tags())
{
ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
if (ret == FALSE)
{
if (msg_col)
msg_putchar('\n');
if (cs_check_for_connections())
{
ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
FALSE, FALSE, *eap->cmdlinep);
if (ret == FALSE)
cs_free_tags();
}
}
}
else if (cs_check_for_connections())
{
ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
FALSE, *eap->cmdlinep);
if (ret == FALSE)
cs_free_tags();
}
break;
default :
break;
}
if (!ret)
{
(void)EMSG(_("E257: cstag: tag not found"));
#if defined(FEAT_QUICKFIX)
g_do_tagpreview = 0;
#endif
}
}
/*
* This simulates a vim_fgets(), but for cscope, returns the next line
* from the cscope output. should only be called from find_tags()
*
* returns TRUE if eof, FALSE otherwise
*/
int
cs_fgets(char_u *buf, int size)
{
char *p;
if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
return TRUE;
vim_strncpy(buf, (char_u *)p, size - 1);
return FALSE;
} /* cs_fgets */
/*
* Called only from do_tag(), when popping the tag stack.
*/
void
cs_free_tags(void)
{
cs_manage_matches(NULL, NULL, -1, Free);
}
/*
* Called from do_tag().
*/
void
cs_print_tags(void)
{
cs_manage_matches(NULL, NULL, -1, Print);
}
/*
* "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
*
* Checks for the existence of a |cscope| connection. If no
* parameters are specified, then the function returns:
*
* 0, if cscope was not available (not compiled in), or if there
* are no cscope connections; or
* 1, if there is at least one cscope connection.
*
* If parameters are specified, then the value of {num}
* determines how existence of a cscope connection is checked:
*
* {num} Description of existence check
* ----- ------------------------------
* 0 Same as no parameters (e.g., "cscope_connection()").
* 1 Ignore {prepend}, and use partial string matches for
* {dbpath}.
* 2 Ignore {prepend}, and use exact string matches for
* {dbpath}.
* 3 Use {prepend}, use partial string matches for both
* {dbpath} and {prepend}.
* 4 Use {prepend}, use exact string matches for both
* {dbpath} and {prepend}.
*
* Note: All string comparisons are case sensitive!
*/
#if defined(FEAT_EVAL) || defined(PROTO)
int
cs_connection(int num, char_u *dbpath, char_u *ppath)
{
int i;
if (num < 0 || num > 4 || (num > 0 && !dbpath))
return FALSE;
for (i = 0; i < csinfo_size; i++)
{
if (!csinfo[i].fname)
continue;
if (num == 0)
return TRUE;
switch (num)
{
case 1:
if (strstr(csinfo[i].fname, (char *)dbpath))
return TRUE;
break;
case 2:
if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
return TRUE;
break;
case 3:
if (strstr(csinfo[i].fname, (char *)dbpath)
&& ((!ppath && !csinfo[i].ppath)
|| (ppath
&& csinfo[i].ppath
&& strstr(csinfo[i].ppath, (char *)ppath))))
return TRUE;
break;
case 4:
if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
&& ((!ppath && !csinfo[i].ppath)
|| (ppath
&& csinfo[i].ppath
&& (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
return TRUE;
break;
}
}
return FALSE;
} /* cs_connection */
#endif
/*
* PRIVATE functions
****************************************************************************/
/*
* Add cscope database or a directory name (to look for cscope.out)
* to the cscope connection list.
*/
static int
cs_add(exarg_T *eap UNUSED)
{
char *fname, *ppath, *flags = NULL;
if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
{
cs_usage_msg(Add);
return CSCOPE_FAILURE;
}
if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
flags = strtok((char *)NULL, (const char *)" ");
return cs_add_common(fname, ppath, flags);
}
static void
cs_stat_emsg(char *fname)
{
char *stat_emsg = _("E563: stat(%s) error: %d");
char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
if (buf != NULL)
{
(void)sprintf(buf, stat_emsg, fname, errno);
(void)EMSG(buf);
vim_free(buf);
}
else
(void)EMSG(_("E563: stat error"));
}
/*
* The common routine to add a new cscope connection. Called by
* cs_add() and cs_reset(). I really don't like to do this, but this
* routine uses a number of goto statements.
*/
static int
cs_add_common(
char *arg1, /* filename - may contain environment variables */
char *arg2, /* prepend path - may contain environment variables */
char *flags)
{
stat_T statbuf;
int ret;
char *fname = NULL;
char *fname2 = NULL;
char *ppath = NULL;
int i;
#ifdef FEAT_MODIFY_FNAME
int len;
int usedlen = 0;
char_u *fbuf = NULL;
#endif
/* get the filename (arg1), expand it, and try to stat it */
if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
goto add_err;
expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
#ifdef FEAT_MODIFY_FNAME
len = (int)STRLEN(fname);
fbuf = (char_u *)fname;
(void)modify_fname((char_u *)":p", FALSE, &usedlen,
(char_u **)&fname, &fbuf, &len);
if (fname == NULL)
goto add_err;
fname = (char *)vim_strnsave((char_u *)fname, len);
vim_free(fbuf);
#endif
ret = mch_stat(fname, &statbuf);
if (ret < 0)
{
staterr:
if (p_csverbose)
cs_stat_emsg(fname);
goto add_err;
}
/* get the prepend path (arg2), expand it, and try to stat it */
if (arg2 != NULL)
{
stat_T statbuf2;
if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
goto add_err;
expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
ret = mch_stat(ppath, &statbuf2);
if (ret < 0)
goto staterr;
}
/* if filename is a directory, append the cscope database name to it */
if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
{
fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
if (fname2 == NULL)
goto add_err;
while (fname[strlen(fname)-1] == '/'
#ifdef WIN32
|| fname[strlen(fname)-1] == '\\'
#endif
)
{
fname[strlen(fname)-1] = '\0';
if (fname[0] == '\0')
break;
}
if (fname[0] == '\0')
(void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
else
(void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
ret = mch_stat(fname2, &statbuf);
if (ret < 0)
{
if (p_csverbose)
cs_stat_emsg(fname2);
goto add_err;
}
i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
}
#if defined(UNIX)
else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
#else
/* WIN32 - substitute define S_ISREG from os_unix.h */
else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
#endif
{
i = cs_insert_filelist(fname, ppath, flags, &statbuf);
}
else
{
if (p_csverbose)
(void)EMSG2(
_("E564: %s is not a directory or a valid cscope database"),
fname);
goto add_err;
}
if (i != -1)
{
if (cs_create_connection(i) == CSCOPE_FAILURE
|| cs_read_prompt(i) == CSCOPE_FAILURE)
{
cs_release_csp(i, TRUE);
goto add_err;
}
if (p_csverbose)
{
msg_clr_eos();
(void)smsg_attr(HL_ATTR(HLF_R),
(char_u *)_("Added cscope database %s"),
csinfo[i].fname);
}
}
vim_free(fname);
vim_free(fname2);
vim_free(ppath);
return CSCOPE_SUCCESS;
add_err:
vim_free(fname2);
vim_free(fname);
vim_free(ppath);
return CSCOPE_FAILURE;
} /* cs_add_common */
static int
cs_check_for_connections(void)
{
return (cs_cnt_connections() > 0);
} /* cs_check_for_connections */
static int
cs_check_for_tags(void)
{
return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
} /* cs_check_for_tags */
/*
* Count the number of cscope connections.
*/
static int
cs_cnt_connections(void)
{
short i;
short cnt = 0;
for (i = 0; i < csinfo_size; i++)
{
if (csinfo[i].fname != NULL)
cnt++;
}
return cnt;
} /* cs_cnt_connections */
static void
cs_reading_emsg(
int idx) /* connection index */
{
EMSGN(_("E262: error reading cscope connection %ld"), idx);
}
#define CSREAD_BUFSIZE 2048
/*
* Count the number of matches for a given cscope connection.
*/
static int
cs_cnt_matches(int idx)
{
char *stok;
char *buf;
int nlines = 0;
buf = (char *)alloc(CSREAD_BUFSIZE);
if (buf == NULL)
return 0;
for (;;)
{
if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
{
if (feof(csinfo[idx].fr_fp))
errno = EIO;
cs_reading_emsg(idx);
vim_free(buf);
return -1;
}
/*
* If the database is out of date, or there's some other problem,
* cscope will output error messages before the number-of-lines output.
* Display/discard any output that doesn't match what we want.
* Accept "\S*cscope: X lines", also matches "mlcscope".
* Bail out for the "Unable to search" error.
*/
if (strstr((const char *)buf, "Unable to search database") != NULL)
break;
if ((stok = strtok(buf, (const char *)" ")) == NULL)
continue;
if (strstr((const char *)stok, "cscope:") == NULL)
continue;
if ((stok = strtok(NULL, (const char *)" ")) == NULL)
continue;
nlines = atoi(stok);
if (nlines < 0)
{
nlines = 0;
break;
}
if ((stok = strtok(NULL, (const char *)" ")) == NULL)
continue;
if (strncmp((const char *)stok, "lines", 5))
continue;
break;
}
vim_free(buf);
return nlines;
} /* cs_cnt_matches */
/*
* Creates the actual cscope command query from what the user entered.
*/
static char *
cs_create_cmd(char *csoption, char *pattern)
{
char *cmd;
short search;
char *pat;
switch (csoption[0])
{
case '0' : case 's' :
search = 0;
break;
case '1' : case 'g' :
search = 1;
break;
case '2' : case 'd' :
search = 2;
break;
case '3' : case 'c' :
search = 3;
break;
case '4' : case 't' :
search = 4;
break;
case '6' : case 'e' :
search = 6;
break;
case '7' : case 'f' :
search = 7;
break;
case '8' : case 'i' :
search = 8;
break;
case '9' : case 'a' :
search = 9;
break;
default :
(void)EMSG(_("E561: unknown cscope search type"));
cs_usage_msg(Find);
return NULL;
}
/* Skip white space before the patter, except for text and pattern search,
* they may want to use the leading white space. */
pat = pattern;
if (search != 4 && search != 6)
while VIM_ISWHITE(*pat)
++pat;
if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
return NULL;
(void)sprintf(cmd, "%d%s", search, pat);
return cmd;
} /* cs_create_cmd */
/*
* This piece of code was taken/adapted from nvi. do we need to add
* the BSD license notice?
*/
static int
cs_create_connection(int i)
{
#ifdef UNIX
int to_cs[2], from_cs[2];
#endif
int len;
char *prog, *cmd, *ppath = NULL;
#ifdef WIN32
int fd;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
STARTUPINFO si;
BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
HANDLE stdin_rd, stdout_rd;
HANDLE stdout_wr, stdin_wr;
BOOL created;
# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
# define OPEN_OH_ARGTYPE intptr_t
# else
# define OPEN_OH_ARGTYPE long
# endif
#endif
#if defined(UNIX)
/*
* Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
* from_cs[0] and writes to to_cs[1].
*/
to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
{
(void)EMSG(_("E566: Could not create cscope pipes"));
err_closing:
if (to_cs[0] != -1)
(void)close(to_cs[0]);
if (to_cs[1] != -1)
(void)close(to_cs[1]);
if (from_cs[0] != -1)
(void)close(from_cs[0]);
if (from_cs[1] != -1)
(void)close(from_cs[1]);
return CSCOPE_FAILURE;
}
switch (csinfo[i].pid = fork())
{
case -1:
(void)EMSG(_("E622: Could not fork for cscope"));
goto err_closing;
case 0: /* child: run cscope. */
if (dup2(to_cs[0], STDIN_FILENO) == -1)
PERROR("cs_create_connection 1");
if (dup2(from_cs[1], STDOUT_FILENO) == -1)
PERROR("cs_create_connection 2");
if (dup2(from_cs[1], STDERR_FILENO) == -1)
PERROR("cs_create_connection 3");
/* close unused */
(void)close(to_cs[1]);
(void)close(from_cs[0]);
#else
/* WIN32 */
/* Create pipes to communicate with cscope */
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
|| !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
{
(void)EMSG(_("E566: Could not create cscope pipes"));
err_closing:
if (pipe_stdin)
{
CloseHandle(stdin_rd);
CloseHandle(stdin_wr);
}
if (pipe_stdout)
{
CloseHandle(stdout_rd);
CloseHandle(stdout_wr);
}
return CSCOPE_FAILURE;
}
#endif
/* expand the cscope exec for env var's */
if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
{
#ifdef UNIX
return CSCOPE_FAILURE;
#else
/* WIN32 */
goto err_closing;
#endif
}
expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
/* alloc space to hold the cscope command */
len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
if (csinfo[i].ppath)
{
/* expand the prepend path for env var's */
if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
{
vim_free(prog);
#ifdef UNIX
return CSCOPE_FAILURE;
#else
/* WIN32 */
goto err_closing;
#endif
}
expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
len += (int)strlen(ppath);
}
if (csinfo[i].flags)
len += (int)strlen(csinfo[i].flags);
if ((cmd = (char *)alloc(len)) == NULL)
{
vim_free(prog);
vim_free(ppath);
#ifdef UNIX
return CSCOPE_FAILURE;
#else
/* WIN32 */
goto err_closing;
#endif
}
/* run the cscope command; is there execl for non-unix systems? */
#if defined(UNIX)
(void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
#else
/* WIN32 */
(void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
#endif
if (csinfo[i].ppath != NULL)
{
(void)strcat(cmd, " -P");
(void)strcat(cmd, csinfo[i].ppath);
}
if (csinfo[i].flags != NULL)
{
(void)strcat(cmd, " ");
(void)strcat(cmd, csinfo[i].flags);
}
# ifdef UNIX
/* on Win32 we still need prog */
vim_free(prog);
# endif
vim_free(ppath);
#if defined(UNIX)
# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
/* Change our process group to avoid cscope receiving SIGWINCH. */
# if defined(HAVE_SETSID)
(void)setsid();
# else
if (setpgid(0, 0) == -1)
PERROR(_("cs_create_connection setpgid failed"));
# endif
# endif
if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
PERROR(_("cs_create_connection exec failed"));
exit(127);
/* NOTREACHED */
default: /* parent. */
/*
* Save the file descriptors for later duplication, and
* reopen as streams.
*/
if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
PERROR(_("cs_create_connection: fdopen for to_fp failed"));
if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
/* close unused */
(void)close(to_cs[0]);
(void)close(from_cs[1]);
break;
}
#else
/* WIN32 */
/* Create a new process to run cscope and use pipes to talk with it */
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; /* Hide child application window */
si.hStdOutput = stdout_wr;
si.hStdError = stdout_wr;
si.hStdInput = stdin_rd;
created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
NULL, NULL, &si, &pi);
vim_free(prog);
vim_free(cmd);