blob: 7a44253151c6799c34e6cfbb7eef1826656293f5 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005 *
6 * The basic idea/structure of cscope for Vim was borrowed from Nvi. There
7 * might be a few lines of code that look similar to what Nvi has.
8 *
9 * See README.txt for an overview of the Vim source code.
10 */
11
12#include "vim.h"
13
14#if defined(FEAT_CSCOPE) || defined(PROTO)
15
16#include <string.h>
17#include <errno.h>
18#include <assert.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#if defined(UNIX)
22# include <sys/wait.h>
23#else
24 /* not UNIX, must be WIN32 */
25# include <io.h>
26# include <fcntl.h>
27# include <process.h>
28# define STDIN_FILENO 0
29# define STDOUT_FILENO 1
30# define STDERR_FILENO 2
31# define pipe(fds) _pipe(fds, 256, O_TEXT|O_NOINHERIT)
32#endif
33#include "if_cscope.h"
34
35static void cs_usage_msg __ARGS((csid_e x));
36static int cs_add __ARGS((exarg_T *eap));
37static void cs_stat_emsg __ARGS((char *fname));
38static int cs_add_common __ARGS((char *, char *, char *));
39static int cs_check_for_connections __ARGS((void));
40static int cs_check_for_tags __ARGS((void));
41static int cs_cnt_connections __ARGS((void));
42static void cs_reading_emsg __ARGS((int idx));
43static int cs_cnt_matches __ARGS((int idx));
44static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
45static int cs_create_connection __ARGS((int i));
46static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
Bram Moolenaarc716c302006-01-21 22:12:51 +000047#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void cs_file_results __ARGS((FILE *, int *));
Bram Moolenaarc716c302006-01-21 22:12:51 +000049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050static void cs_fill_results __ARGS((char *, int , int *, char ***,
51 char ***, int *));
52static int cs_find __ARGS((exarg_T *eap));
53static int cs_find_common __ARGS((char *opt, char *pat, int, int ));
54static int cs_help __ARGS((exarg_T *eap));
55static void cs_init __ARGS((void));
56static void clear_csinfo __ARGS((int i));
57static int cs_insert_filelist __ARGS((char *, char *, char *,
58 struct stat *));
59static int cs_kill __ARGS((exarg_T *eap));
60static void cs_kill_execute __ARGS((int, char *));
61static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
62static char * cs_make_vim_style_matches __ARGS((char *, char *,
63 char *, char *));
64static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
65static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
66static char * cs_pathcomponents __ARGS((char *path));
67static void cs_print_tags_priv __ARGS((char **, char **, int));
68static int cs_read_prompt __ARGS((int ));
69static void cs_release_csp __ARGS((int, int freefnpp));
70static int cs_reset __ARGS((exarg_T *eap));
71static char * cs_resolve_file __ARGS((int, char *));
72static int cs_show __ARGS((exarg_T *eap));
73
74
75static csinfo_T csinfo[CSCOPE_MAX_CONNECTIONS];
76static cscmd_T cs_cmds[] =
77{
78 { "add", cs_add,
79 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
80 { "find", cs_find,
81 N_("Query for a pattern"), FIND_USAGE, 1 },
82 { "help", cs_help,
83 N_("Show this message"), "help", 0 },
84 { "kill", cs_kill,
85 N_("Kill a connection"), "kill #", 0 },
86 { "reset", cs_reset,
87 N_("Reinit all connections"), "reset", 0 },
88 { "show", cs_show,
89 N_("Show connections"), "show", 0 },
90 { NULL }
91};
92
93 static void
94cs_usage_msg(x)
95 csid_e x;
96{
97 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
98}
99
100/*
101 * PRIVATE: do_cscope_general
102 *
103 * find the command, print help if invalid, and the then call the
104 * corresponding command function,
105 * called from do_cscope and do_scscope
106 */
107 static void
108do_cscope_general(eap, make_split)
109 exarg_T *eap;
110 int make_split; /* whether to split window */
111{
112 cscmd_T *cmdp;
113
114 cs_init();
115 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
116 {
117 cs_help(eap);
118 return;
119 }
120
121#ifdef FEAT_WINDOWS
122 if (make_split)
123 {
124 if (!cmdp->cansplit)
125 {
126 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
127 return;
128 }
129 postponed_split = -1;
130 postponed_split_flags = cmdmod.split;
131 }
132#endif
133
134 cmdp->func(eap);
135
136#ifdef FEAT_WINDOWS
137 postponed_split_flags = 0;
138#endif
139}
140
141/*
142 * PUBLIC: do_cscope
143 */
144 void
145do_cscope(eap)
146 exarg_T *eap;
147{
148 do_cscope_general(eap, FALSE);
149}
150
151/*
152 * PUBLIC: do_scscope
153 *
154 * same as do_cscope, but splits window, too.
155 */
156 void
157do_scscope(eap)
158 exarg_T *eap;
159{
160 do_cscope_general(eap, TRUE);
161}
162
163/*
164 * PUBLIC: do_cstag
165 *
166 */
167 void
168do_cstag(eap)
169 exarg_T *eap;
170{
171 int ret = FALSE;
172
173 cs_init();
174
175 if (eap->arg == NULL || strlen((const char *)(eap->arg)) == 0)
176 {
177 (void)EMSG(_("E562: Usage: cstag <ident>"));
178 return;
179 }
180
181 switch (p_csto)
182 {
183 case 0 :
184 if (cs_check_for_connections())
185 {
186 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE);
187 if (ret == FALSE)
188 {
189 cs_free_tags();
190 if (msg_col)
191 msg_putchar('\n');
192
193 if (cs_check_for_tags())
194 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
195 }
196 }
197 else if (cs_check_for_tags())
198 {
199 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
200 }
201 break;
202 case 1 :
203 if (cs_check_for_tags())
204 {
205 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
206 if (ret == FALSE)
207 {
208 if (msg_col)
209 msg_putchar('\n');
210
211 if (cs_check_for_connections())
212 {
213 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
214 FALSE);
215 if (ret == FALSE)
216 cs_free_tags();
217 }
218 }
219 }
220 else if (cs_check_for_connections())
221 {
222 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE);
223 if (ret == FALSE)
224 cs_free_tags();
225 }
226 break;
227 default :
228 break;
229 }
230
231 if (!ret)
232 {
233 (void)EMSG(_("E257: cstag: tag not found"));
234#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
235 g_do_tagpreview = 0;
236#endif
237 }
238
239} /* do_cscope */
240
241
242/*
243 * PUBLIC: cs_find
244 *
245 * this simulates a vim_fgets(), but for cscope, returns the next line
246 * from the cscope output. should only be called from find_tags()
247 *
248 * returns TRUE if eof, FALSE otherwise
249 */
250 int
251cs_fgets(buf, size)
252 char_u *buf;
253 int size;
254{
255 char *p;
256
257 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
258 return TRUE;
259
260 if ((int)strlen(p) > size)
261 {
262 strncpy((char *)buf, p, size - 1);
263 buf[size] = '\0';
264 }
265 else
266 (void)strcpy((char *)buf, p);
267
268 return FALSE;
269} /* cs_fgets */
270
271
272/*
273 * PUBLIC: cs_free_tags
274 *
275 * called only from do_tag(), when popping the tag stack
276 */
277 void
278cs_free_tags()
279{
280 cs_manage_matches(NULL, NULL, -1, Free);
281}
282
283
284/*
285 * PUBLIC: cs_print_tags
286 *
287 * called from do_tag()
288 */
289 void
290cs_print_tags()
291{
292 cs_manage_matches(NULL, NULL, -1, Print);
293}
294
295
296/*
297 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
298 *
299 * Checks for the existence of a |cscope| connection. If no
300 * parameters are specified, then the function returns:
301 *
302 * 0, if cscope was not available (not compiled in), or if there
303 * are no cscope connections; or
304 * 1, if there is at least one cscope connection.
305 *
306 * If parameters are specified, then the value of {num}
307 * determines how existence of a cscope connection is checked:
308 *
309 * {num} Description of existence check
310 * ----- ------------------------------
311 * 0 Same as no parameters (e.g., "cscope_connection()").
312 * 1 Ignore {prepend}, and use partial string matches for
313 * {dbpath}.
314 * 2 Ignore {prepend}, and use exact string matches for
315 * {dbpath}.
316 * 3 Use {prepend}, use partial string matches for both
317 * {dbpath} and {prepend}.
318 * 4 Use {prepend}, use exact string matches for both
319 * {dbpath} and {prepend}.
320 *
321 * Note: All string comparisons are case sensitive!
322 */
323#if defined(FEAT_EVAL) || defined(PROTO)
324 int
325cs_connection(num, dbpath, ppath)
326 int num;
327 char_u *dbpath;
328 char_u *ppath;
329{
330 int i;
331
332 if (num < 0 || num > 4 || (num > 0 && !dbpath))
333 return FALSE;
334
335 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
336 {
337 if (!csinfo[i].fname)
338 continue;
339
340 if (num == 0)
341 return TRUE;
342
343 switch (num)
344 {
345 case 1:
346 if (strstr(csinfo[i].fname, (char *)dbpath))
347 return TRUE;
348 break;
349 case 2:
350 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
351 return TRUE;
352 break;
353 case 3:
354 if (strstr(csinfo[i].fname, (char *)dbpath)
355 && ((!ppath && !csinfo[i].ppath)
356 || (ppath
357 && csinfo[i].ppath
358 && strstr(csinfo[i].ppath, (char *)ppath))))
359 return TRUE;
360 break;
361 case 4:
362 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
363 && ((!ppath && !csinfo[i].ppath)
364 || (ppath
365 && csinfo[i].ppath
366 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
367 return TRUE;
368 break;
369 }
370 }
371
372 return FALSE;
373} /* cs_connection */
374#endif
375
376
377/*
378 * PRIVATE functions
379 ****************************************************************************/
380
381/*
382 * PRIVATE: cs_add
383 *
384 * add cscope database or a directory name (to look for cscope.out)
385 * the the cscope connection list
386 *
387 * MAXPATHL 256
388 */
389/* ARGSUSED */
390 static int
391cs_add(eap)
392 exarg_T *eap;
393{
394 char *fname, *ppath, *flags = NULL;
395
396 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
397 {
398 cs_usage_msg(Add);
399 return CSCOPE_FAILURE;
400 }
401 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
402 flags = strtok((char *)NULL, (const char *)" ");
403
404 return cs_add_common(fname, ppath, flags);
405}
406
407 static void
408cs_stat_emsg(fname)
409 char *fname;
410{
411 char *stat_emsg = _("E563: stat(%s) error: %d");
412 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
413
414 if (buf != NULL)
415 {
416 (void)sprintf(buf, stat_emsg, fname, errno);
417 (void)EMSG(buf);
418 vim_free(buf);
419 }
420 else
421 (void)EMSG(_("E563: stat error"));
422}
423
424
425/*
426 * PRIVATE: cs_add_common
427 *
428 * the common routine to add a new cscope connection. called by
429 * cs_add() and cs_reset(). i really don't like to do this, but this
430 * routine uses a number of goto statements.
431 */
432 static int
433cs_add_common(arg1, arg2, flags)
434 char *arg1; /* filename - may contain environment variables */
435 char *arg2; /* prepend path - may contain environment variables */
436 char *flags;
437{
438 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000439 int ret;
440 char *fname = NULL;
441 char *fname2 = NULL;
442 char *ppath = NULL;
443 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000446 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 goto add_err;
448
449 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
450 ret = stat(fname, &statbuf);
451 if (ret < 0)
452 {
453staterr:
454 if (p_csverbose)
455 cs_stat_emsg(fname);
456 goto add_err;
457 }
458
459 /* get the prepend path (arg2), expand it, and try to stat it */
460 if (arg2 != NULL)
461 {
462 struct stat statbuf2;
463
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000464 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 goto add_err;
466
467 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
468 ret = stat(ppath, &statbuf2);
469 if (ret < 0)
470 goto staterr;
471 }
472
473 /* if filename is a directory, append the cscope database name to it */
474 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
475 {
476 fname2 = (char *)alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
477 if (fname2 == NULL)
478 goto add_err;
479
480 while (fname[strlen(fname)-1] == '/'
481#ifdef WIN32
482 || fname[strlen(fname)-1] == '\\'
483#endif
484 )
485 {
486 fname[strlen(fname)-1] = '\0';
487 if (strlen(fname) == 0)
488 break;
489 }
490 if (fname[0] == '\0')
491 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
492 else
493 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
494
495 ret = stat(fname2, &statbuf);
496 if (ret < 0)
497 {
498 if (p_csverbose)
499 cs_stat_emsg(fname2);
500 goto add_err;
501 }
502
503 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
504 }
505#if defined(UNIX)
506 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
507#else
508 /* substitute define S_ISREG from os_unix.h */
509 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
510#endif
511 {
512 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
513 }
514 else
515 {
516 if (p_csverbose)
517 (void)EMSG2(
518 _("E564: %s is not a directory or a valid cscope database"),
519 fname);
520 goto add_err;
521 }
522
523 if (i != -1)
524 {
525 if (cs_create_connection(i) == CSCOPE_FAILURE
526 || cs_read_prompt(i) == CSCOPE_FAILURE)
527 {
528 cs_release_csp(i, TRUE);
529 goto add_err;
530 }
531
532 if (p_csverbose)
533 {
534 msg_clr_eos();
535 (void)smsg_attr(hl_attr(HLF_R),
536 (char_u *)_("Added cscope database %s"),
537 csinfo[i].fname);
538 }
539 }
540
541 vim_free(fname);
542 vim_free(fname2);
543 vim_free(ppath);
544 return CSCOPE_SUCCESS;
545
546add_err:
547 vim_free(fname2);
548 vim_free(fname);
549 vim_free(ppath);
550 return CSCOPE_FAILURE;
551} /* cs_add_common */
552
553
554 static int
555cs_check_for_connections()
556{
557 return (cs_cnt_connections() > 0);
558} /* cs_check_for_connections */
559
560
561 static int
562cs_check_for_tags()
563{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000564 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565} /* cs_check_for_tags */
566
567
568/*
569 * PRIVATE: cs_cnt_connections
570 *
571 * count the number of cscope connections
572 */
573 static int
574cs_cnt_connections()
575{
576 short i;
577 short cnt = 0;
578
579 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
580 {
581 if (csinfo[i].fname != NULL)
582 cnt++;
583 }
584 return cnt;
585} /* cs_cnt_connections */
586
587 static void
588cs_reading_emsg(idx)
589 int idx; /* connection index */
590{
591 EMSGN(_("E262: error reading cscope connection %ld"), idx);
592}
593
594#define CSREAD_BUFSIZE 2048
595/*
596 * PRIVATE: cs_cnt_matches
597 *
598 * count the number of matches for a given cscope connection.
599 */
600 static int
601cs_cnt_matches(idx)
602 int idx;
603{
604 char *stok;
605 char *buf;
606 int nlines;
607
608 buf = (char *)alloc(CSREAD_BUFSIZE);
609 if (buf == NULL)
610 return 0;
611 for (;;)
612 {
613 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
614 {
615 if (feof(csinfo[idx].fr_fp))
616 errno = EIO;
617
618 cs_reading_emsg(idx);
619
620 vim_free(buf);
621 return -1;
622 }
623
624 /*
625 * If the database is out of date, or there's some other problem,
626 * cscope will output error messages before the number-of-lines output.
627 * Display/discard any output that doesn't match what we want.
628 */
629 if ((stok = strtok(buf, (const char *)" ")) == NULL)
630 continue;
631 if (strcmp((const char *)stok, "cscope:"))
632 continue;
633
634 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
635 continue;
636 nlines = atoi(stok);
637 if (nlines < 0)
638 {
639 nlines = 0;
640 break;
641 }
642
643 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
644 continue;
645 if (strncmp((const char *)stok, "lines", 5))
646 continue;
647
648 break;
649 }
650
651 vim_free(buf);
652 return nlines;
653} /* cs_cnt_matches */
654
655
656/*
657 * PRIVATE: cs_create_cmd
658 *
659 * Creates the actual cscope command query from what the user entered.
660 */
661 static char *
662cs_create_cmd(csoption, pattern)
663 char *csoption;
664 char *pattern;
665{
666 char *cmd;
667 short search;
668
669 switch (csoption[0])
670 {
671 case '0' : case 's' :
672 search = 0;
673 break;
674 case '1' : case 'g' :
675 search = 1;
676 break;
677 case '2' : case 'd' :
678 search = 2;
679 break;
680 case '3' : case 'c' :
681 search = 3;
682 break;
683 case '4' : case 't' :
684 search = 4;
685 break;
686 case '6' : case 'e' :
687 search = 6;
688 break;
689 case '7' : case 'f' :
690 search = 7;
691 break;
692 case '8' : case 'i' :
693 search = 8;
694 break;
695 default :
696 (void)EMSG(_("E561: unknown cscope search type"));
697 cs_usage_msg(Find);
698 return NULL;
699 }
700
701 if ((cmd = (char *)alloc(strlen(pattern) + 2)) == NULL)
702 return NULL;
703
704 (void)sprintf(cmd, "%d%s", search, pattern);
705
706 return cmd;
707} /* cs_create_cmd */
708
709
710/*
711 * PRIVATE: cs_create_connection
712 *
713 * This piece of code was taken/adapted from nvi. do we need to add
714 * the BSD license notice?
715 */
716 static int
717cs_create_connection(i)
718 int i;
719{
720 int to_cs[2], from_cs[2], len;
721 char *prog, *cmd, *ppath = NULL;
722#ifndef UNIX
723 int in_save, out_save, err_save;
724 int ph;
725# ifdef FEAT_GUI
726 HWND activewnd = NULL;
727 HWND consolewnd = NULL;
728# endif
729#endif
730
731 /*
732 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
733 * from_cs[0] and writes to to_cs[1].
734 */
735 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
736 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
737 {
738 (void)EMSG(_("E566: Could not create cscope pipes"));
739err_closing:
740 if (to_cs[0] != -1)
741 (void)close(to_cs[0]);
742 if (to_cs[1] != -1)
743 (void)close(to_cs[1]);
744 if (from_cs[0] != -1)
745 (void)close(from_cs[0]);
746 if (from_cs[1] != -1)
747 (void)close(from_cs[1]);
748 return CSCOPE_FAILURE;
749 }
750
751#if defined(UNIX)
752 switch (csinfo[i].pid = fork())
753 {
754 case -1:
755 (void)EMSG(_("E622: Could not fork for cscope"));
756 goto err_closing;
757 case 0: /* child: run cscope. */
758#else
759 in_save = dup(STDIN_FILENO);
760 out_save = dup(STDOUT_FILENO);
761 err_save = dup(STDERR_FILENO);
762#endif
763 if (dup2(to_cs[0], STDIN_FILENO) == -1)
764 PERROR("cs_create_connection 1");
765 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
766 PERROR("cs_create_connection 2");
767 if (dup2(from_cs[1], STDERR_FILENO) == -1)
768 PERROR("cs_create_connection 3");
769
770 /* close unused */
771#if defined(UNIX)
772 (void)close(to_cs[1]);
773 (void)close(from_cs[0]);
774#else
775 /* On win32 we must close opposite ends because we are the parent */
776 (void)close(to_cs[0]);
777 to_cs[0] = -1;
778 (void)close(from_cs[1]);
779 from_cs[1] = -1;
780#endif
781 /* expand the cscope exec for env var's */
782 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
783 {
784#ifdef UNIX
785 return CSCOPE_FAILURE;
786#else
787 goto err_closing;
788#endif
789 }
790 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
791
792 /* alloc space to hold the cscope command */
793 len = strlen(prog) + strlen(csinfo[i].fname) + 32;
794 if (csinfo[i].ppath)
795 {
796 /* expand the prepend path for env var's */
797 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
798 {
799 vim_free(prog);
800#ifdef UNIX
801 return CSCOPE_FAILURE;
802#else
803 goto err_closing;
804#endif
805 }
806 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
807
808 len += strlen(ppath);
809 }
810
811 if (csinfo[i].flags)
812 len += strlen(csinfo[i].flags);
813
814 if ((cmd = (char *)alloc(len)) == NULL)
815 {
816 vim_free(prog);
817 vim_free(ppath);
818#ifdef UNIX
819 return CSCOPE_FAILURE;
820#else
821 goto err_closing;
822#endif
823 }
824
825 /* run the cscope command; is there execl for non-unix systems? */
826#if defined(UNIX)
827 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
828#else
829 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
830#endif
831 if (csinfo[i].ppath != NULL)
832 {
833 (void)strcat(cmd, " -P");
834 (void)strcat(cmd, csinfo[i].ppath);
835 }
836 if (csinfo[i].flags != NULL)
837 {
838 (void)strcat(cmd, " ");
839 (void)strcat(cmd, csinfo[i].flags);
840 }
841# ifdef UNIX
842 /* on Win32 we still need prog */
843 vim_free(prog);
844# endif
845 vim_free(ppath);
846
847#if defined(UNIX)
848 if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
849 PERROR(_("cs_create_connection exec failed"));
850
851 exit(127);
852 /* NOTREACHED */
853 default: /* parent. */
854#else
855# ifdef FEAT_GUI
856 activewnd = GetForegroundWindow(); /* on win9x cscope steals focus */
857 /* Dirty hack to hide annoying console window */
858 if (AllocConsole())
859 {
860 char *title;
861 title = (char *)alloc(1024);
862 if (title == NULL)
863 FreeConsole();
864 else
865 {
866 GetConsoleTitle(title, 1024); /* save for future restore */
867 SetConsoleTitle(
868 "GVIMCS{5499421B-CBEF-45b0-85EF-38167FDEA5C5}GVIMCS");
869 Sleep(40); /* as stated in MS KB we must wait 40 ms */
870 consolewnd = FindWindow(NULL,
871 "GVIMCS{5499421B-CBEF-45b0-85EF-38167FDEA5C5}GVIMCS");
872 if (consolewnd != NULL)
873 ShowWindow(consolewnd, SW_HIDE);
874 SetConsoleTitle(title);
875 vim_free(title);
876 }
877 }
878# endif
879 /* May be use &shell, &shellquote etc */
880# ifdef __BORLANDC__
881 /* BCC 5.5 uses a different function name for spawnlp */
882 ph = spawnlp(P_NOWAIT, prog, cmd, NULL);
883# else
884 ph = _spawnlp(_P_NOWAIT, prog, cmd, NULL);
885# endif
886 vim_free(prog);
887 vim_free(cmd);
888# ifdef FEAT_GUI
889 /* Dirty hack part two */
890 if (activewnd != NULL)
891 /* restoring focus */
892 SetForegroundWindow(activewnd);
893 if (consolewnd != NULL)
894 FreeConsole();
895
896# endif
897 if (ph == -1)
898 {
899 PERROR(_("cs_create_connection exec failed"));
900 (void)EMSG(_("E623: Could not spawn cscope process"));
901 goto err_closing;
902 }
903 /* else */
904 csinfo[i].pid = 0;
905 csinfo[i].hProc = (HANDLE)ph;
906
907#endif /* !UNIX */
908 /*
909 * Save the file descriptors for later duplication, and
910 * reopen as streams.
911 */
912 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
913 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
914 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
915 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
916
917#if defined(UNIX)
918 /* close unused */
919 (void)close(to_cs[0]);
920 (void)close(from_cs[1]);
921
922 break;
923 }
924#else
925 /* restore stdhandles */
926 dup2(in_save, STDIN_FILENO);
927 dup2(out_save, STDOUT_FILENO);
928 dup2(err_save, STDERR_FILENO);
929 close(in_save);
930 close(out_save);
931 close(err_save);
932#endif
933 return CSCOPE_SUCCESS;
934} /* cs_create_connection */
935
936
937/*
938 * PRIVATE: cs_find
939 *
940 * query cscope using command line interface. parse the output and use tselect
941 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
942 *
943 * returns TRUE if we jump to a tag or abort, FALSE if not.
944 */
945 static int
946cs_find(eap)
947 exarg_T *eap;
948{
949 char *opt, *pat;
950
951 if (cs_check_for_connections() == FALSE)
952 {
953 (void)EMSG(_("E567: no cscope connections"));
954 return FALSE;
955 }
956
957 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
958 {
959 cs_usage_msg(Find);
960 return FALSE;
961 }
962
963 pat = opt + strlen(opt) + 1;
964 if (pat == NULL || (pat != NULL && pat[0] == '\0'))
965 {
966 cs_usage_msg(Find);
967 return FALSE;
968 }
969
970 return cs_find_common(opt, pat, eap->forceit, TRUE);
971} /* cs_find */
972
973
974/*
975 * PRIVATE: cs_find_common
976 *
977 * common code for cscope find, shared by cs_find() and do_cstag()
978 */
979 static int
980cs_find_common(opt, pat, forceit, verbose)
981 char *opt;
982 char *pat;
983 int forceit;
984 int verbose;
985{
986 int i;
987 char *cmd;
988 char **matches, **contexts;
989 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches, matched;
990#ifdef FEAT_QUICKFIX
991 char cmdletter;
992 char *qfpos;
993#endif
994
995 /* create the actual command to send to cscope */
996 cmd = cs_create_cmd(opt, pat);
997 if (cmd == NULL)
998 return FALSE;
999
1000 /* send query to all open connections, then count the total number
1001 * of matches so we can alloc matchesp all in one swell foop
1002 */
1003 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1004 nummatches[i] = 0;
1005 totmatches = 0;
1006 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1007 {
1008 if (csinfo[i].fname == NULL)
1009 continue;
1010
1011 /* send cmd to cscope */
1012 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1013 (void)fflush(csinfo[i].to_fp);
1014
1015 nummatches[i] = cs_cnt_matches(i);
1016
1017 if (nummatches[i] > -1)
1018 totmatches += nummatches[i];
1019
1020 if (nummatches[i] == 0)
1021 (void)cs_read_prompt(i);
1022 }
1023 vim_free(cmd);
1024
1025 if (totmatches == 0)
1026 {
1027 char *nf = _("E259: no matches found for cscope query %s of %s");
1028 char *buf;
1029
1030 if (!verbose)
1031 return FALSE;
1032
1033 buf = (char *)alloc(strlen(opt) + strlen(pat) + strlen(nf));
1034 if (buf == NULL)
1035 (void)EMSG(nf);
1036 else
1037 {
1038 sprintf(buf, nf, opt, pat);
1039 (void)EMSG(buf);
1040 vim_free(buf);
1041 }
1042 return FALSE;
1043 }
1044
1045#ifdef FEAT_QUICKFIX
1046 /* get cmd letter */
1047 switch (opt[0])
1048 {
1049 case '0' :
1050 cmdletter = 's';
1051 break;
1052 case '1' :
1053 cmdletter = 'g';
1054 break;
1055 case '2' :
1056 cmdletter = 'd';
1057 break;
1058 case '3' :
1059 cmdletter = 'c';
1060 break;
1061 case '4' :
1062 cmdletter = 't';
1063 break;
1064 case '6' :
1065 cmdletter = 'e';
1066 break;
1067 case '7' :
1068 cmdletter = 'f';
1069 break;
1070 case '8' :
1071 cmdletter = 'i';
1072 break;
1073 default :
1074 cmdletter = opt[0];
1075 }
1076
1077 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1078 if (qfpos != NULL)
1079 {
1080 qfpos++;
1081 /* next symbol must be + or - */
1082 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1083 {
1084 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1085 char *buf = (char *)alloc(strlen(nf));
1086
1087 /* strlen will be enough because we use chars */
1088 if (buf != NULL)
1089 {
1090 sprintf(buf, nf, *qfpos, *(qfpos-1));
1091 (void)EMSG(buf);
1092 vim_free(buf);
1093 }
1094 return FALSE;
1095 }
1096 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001097 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 /* fill error list */
1100 FILE *f;
1101 char_u *tmp = vim_tempname('c');
1102
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001103 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 cs_file_results(f, nummatches);
1105 fclose(f);
1106 /* '-' starts a new error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001107 if (qf_init(NULL, tmp, (char_u *)"%f%*\\t%l%*\\t%m", *qfpos == '-') > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109# ifdef FEAT_WINDOWS
1110 if (postponed_split != 0)
1111 {
1112 win_split(postponed_split > 0 ? postponed_split : 0,
1113 postponed_split_flags);
1114# ifdef FEAT_SCROLLBIND
1115 curwin->w_p_scb = FALSE;
1116# endif
1117 postponed_split = 0;
1118 }
1119# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001120 qf_jump(NULL, 0, 0, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122 mch_remove(tmp);
1123 vim_free(tmp);
1124 return TRUE;
1125 }
1126 else
1127#endif /* FEAT_QUICKFIX */
1128 {
1129 /* read output */
1130 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1131 &contexts, &matched);
1132 if (matches == NULL)
1133 return FALSE;
1134
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001135 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1138 }
1139
1140} /* cs_find_common */
1141
1142/*
1143 * PRIVATE: cs_help
1144 *
1145 * print help
1146 */
1147/* ARGSUSED */
1148 static int
1149cs_help(eap)
1150 exarg_T *eap;
1151{
1152 cscmd_T *cmdp = cs_cmds;
1153
1154 (void)MSG_PUTS(_("cscope commands:\n"));
1155 while (cmdp->name != NULL)
1156 {
1157 (void)smsg((char_u *)_("%-5s: %-30s (Usage: %s)"),
1158 cmdp->name, _(cmdp->help), cmdp->usage);
1159 if (strcmp(cmdp->name, "find") == 0)
1160 MSG_PUTS(FIND_HELP);
1161 cmdp++;
1162 }
1163
1164 wait_return(TRUE);
1165 return 0;
1166} /* cs_help */
1167
1168
1169/*
1170 * PRIVATE: cs_init
1171 *
1172 * initialize cscope structure if not already
1173 */
1174 static void
1175cs_init()
1176{
1177 short i;
1178 static int init_already = FALSE;
1179
1180 if (init_already)
1181 return;
1182
1183 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1184 clear_csinfo(i);
1185
1186 init_already = TRUE;
1187} /* cs_init */
1188
1189 static void
1190clear_csinfo(i)
1191 int i;
1192{
1193 csinfo[i].fname = NULL;
1194 csinfo[i].ppath = NULL;
1195 csinfo[i].flags = NULL;
1196#if defined(UNIX)
1197 csinfo[i].st_dev = (dev_t)0;
1198 csinfo[i].st_ino = (ino_t)0;
1199#else
1200 csinfo[i].nVolume = 0;
1201 csinfo[i].nIndexHigh = 0;
1202 csinfo[i].nIndexLow = 0;
1203#endif
1204 csinfo[i].pid = -1;
1205 csinfo[i].fr_fp = NULL;
1206 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001207#if defined(WIN32)
1208 csinfo[i].hProc = NULL;
1209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210}
1211
1212#ifndef UNIX
1213static char *GetWin32Error __ARGS((void));
1214
1215 static char *
1216GetWin32Error()
1217{
1218 char *msg = NULL;
1219 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1220 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1221 if (msg != NULL)
1222 {
1223 /* remove trailing \r\n */
1224 char *pcrlf = strstr(msg, "\r\n");
1225 if (pcrlf != NULL)
1226 *pcrlf = '\0';
1227 }
1228 return msg;
1229}
1230#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * PRIVATE: cs_insert_filelist
1234 *
1235 * insert a new cscope database filename into the filelist
1236 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001237/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 static int
1239cs_insert_filelist(fname, ppath, flags, sb)
1240 char *fname;
1241 char *ppath;
1242 char *flags;
1243 struct stat *sb;
1244{
1245 short i, j;
1246#ifndef UNIX
1247 HANDLE hFile;
1248 BY_HANDLE_FILE_INFORMATION bhfi;
1249
1250 vim_memset(&bhfi, 0, sizeof(bhfi));
1251 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1252 if (!mch_windows95())
1253 {
1254 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1255 FILE_ATTRIBUTE_NORMAL, NULL);
1256 if (hFile == INVALID_HANDLE_VALUE)
1257 {
1258 if (p_csverbose)
1259 {
1260 char *cant_msg = _("E625: cannot open cscope database: %s");
1261 char *winmsg = GetWin32Error();
1262
1263 if (winmsg != NULL)
1264 {
1265 (void)EMSG2(cant_msg, winmsg);
1266 LocalFree(winmsg);
1267 }
1268 else
1269 /* subst filename if can't get error text */
1270 (void)EMSG2(cant_msg, fname);
1271 }
1272 return -1;
1273 }
1274 if (!GetFileInformationByHandle(hFile, &bhfi))
1275 {
1276 CloseHandle(hFile);
1277 if (p_csverbose)
1278 (void)EMSG(_("E626: cannot get cscope database information"));
1279 return -1;
1280 }
1281 CloseHandle(hFile);
1282 }
1283#endif
1284
1285 i = -1; /* can be set to the index of an empty item in csinfo */
1286 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1287 {
1288 if (csinfo[j].fname != NULL
1289#if defined(UNIX)
1290 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1291#else
1292 /* compare pathnames first */
1293 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
1294 /* if not Windows 9x, test index file atributes too */
1295 || (!mch_windows95()
1296 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1297 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1298 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1299#endif
1300 )
1301 {
1302 if (p_csverbose)
1303 (void)EMSG(_("E568: duplicate cscope database not added"));
1304 return -1;
1305 }
1306
1307 if (csinfo[j].fname == NULL && i == -1)
1308 i = j; /* remember first empty entry */
1309 }
1310
1311 if (i == -1)
1312 {
1313 if (p_csverbose)
1314 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1315 return -1;
1316 }
1317
1318 if ((csinfo[i].fname = (char *)alloc(strlen(fname)+1)) == NULL)
1319 return -1;
1320
1321 (void)strcpy(csinfo[i].fname, (const char *)fname);
1322
1323 if (ppath != NULL)
1324 {
1325 if ((csinfo[i].ppath = (char *)alloc(strlen(ppath) + 1)) == NULL)
1326 {
1327 vim_free(csinfo[i].fname);
1328 csinfo[i].fname = NULL;
1329 return -1;
1330 }
1331 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1332 } else
1333 csinfo[i].ppath = NULL;
1334
1335 if (flags != NULL)
1336 {
1337 if ((csinfo[i].flags = (char *)alloc(strlen(flags) + 1)) == NULL)
1338 {
1339 vim_free(csinfo[i].fname);
1340 vim_free(csinfo[i].ppath);
1341 csinfo[i].fname = NULL;
1342 csinfo[i].ppath = NULL;
1343 return -1;
1344 }
1345 (void)strcpy(csinfo[i].flags, (const char *)flags);
1346 } else
1347 csinfo[i].flags = NULL;
1348
1349#if defined(UNIX)
1350 csinfo[i].st_dev = sb->st_dev;
1351 csinfo[i].st_ino = sb->st_ino;
1352
1353#else
1354 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1355 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1356 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1357#endif
1358 return i;
1359} /* cs_insert_filelist */
1360
1361
1362/*
1363 * PRIVATE: cs_lookup_cmd
1364 *
1365 * find cscope command in command table
1366 */
1367 static cscmd_T *
1368cs_lookup_cmd(eap)
1369 exarg_T *eap;
1370{
1371 cscmd_T *cmdp;
1372 char *stok;
1373 size_t len;
1374
1375 if (eap->arg == NULL)
1376 return NULL;
1377
1378 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1379 return NULL;
1380
1381 len = strlen(stok);
1382 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1383 {
1384 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1385 return (cmdp);
1386 }
1387 return NULL;
1388} /* cs_lookup_cmd */
1389
1390
1391/*
1392 * PRIVATE: cs_kill
1393 *
1394 * nuke em
1395 */
1396/* ARGSUSED */
1397 static int
1398cs_kill(eap)
1399 exarg_T *eap;
1400{
1401 char *stok;
1402 short i;
1403
1404 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1405 {
1406 cs_usage_msg(Kill);
1407 return CSCOPE_FAILURE;
1408 }
1409
1410 /* only single digit positive and negative integers are allowed */
1411 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1412 || (strlen(stok) < 3 && stok[0] == '-'
1413 && VIM_ISDIGIT((int)(stok[1]))))
1414 i = atoi(stok);
1415 else
1416 {
1417 /* It must be part of a name. We will try to find a match
1418 * within all the names in the csinfo data structure
1419 */
1420 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1421 {
1422 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1423 break;
1424 }
1425 }
1426
1427 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1428 && i != -1)
1429 {
1430 if (p_csverbose)
1431 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1432 }
1433 else
1434 {
1435 if (i == -1)
1436 {
1437 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1438 {
1439 if (csinfo[i].fname)
1440 cs_kill_execute(i, csinfo[i].fname);
1441 }
1442 }
1443 else
1444 cs_kill_execute(i, stok);
1445 }
1446
1447 return 0;
1448} /* cs_kill */
1449
1450
1451/*
1452 * PRIVATE: cs_kill_execute
1453 *
1454 * Actually kills a specific cscope connection.
1455 */
1456 static void
1457cs_kill_execute(i, cname)
1458 int i; /* cscope table index */
1459 char *cname; /* cscope database name */
1460{
1461 if (p_csverbose)
1462 {
1463 msg_clr_eos();
1464 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1465 (char_u *)_("cscope connection %s closed"), cname);
1466 }
1467 cs_release_csp(i, TRUE);
1468}
1469
1470
1471/*
1472 * PRIVATE: cs_make_vim_style_matches
1473 *
1474 * convert the cscope output into into a ctags style entry (as might be found
1475 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1476 * the type of the tag you are looking for. for example, in Darren Hiebert's
1477 * ctags (the one that comes with vim), #define's use a line number to find the
1478 * tag in a file while function definitions use a regexp search pattern.
1479 *
1480 * i'm going to always use the line number because cscope does something
1481 * quirky (and probably other things i don't know about):
1482 *
1483 * if you have "# define" in your source file, which is
1484 * perfectly legal, cscope thinks you have "#define". this
1485 * will result in a failed regexp search. :(
1486 *
1487 * besides, even if this particular case didn't happen, the search pattern
1488 * would still have to be modified to escape all the special regular expression
1489 * characters to comply with ctags formatting.
1490 */
1491 static char *
1492cs_make_vim_style_matches(fname, slno, search, tagstr)
1493 char *fname;
1494 char *slno;
1495 char *search;
1496 char *tagstr;
1497{
1498 /* vim style is ctags:
1499 *
1500 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1501 *
1502 * but as mentioned above, we'll always use the line number and
1503 * put the search pattern (if one exists) as "extra"
1504 *
1505 * buf is used as part of vim's method of handling tags, and
1506 * (i think) vim frees it when you pop your tags and get replaced
1507 * by new ones on the tag stack.
1508 */
1509 char *buf;
1510 int amt;
1511
1512 if (search != NULL)
1513 {
1514 amt = strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6;
1515 if ((buf = (char *)alloc(amt)) == NULL)
1516 return NULL;
1517
1518 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1519 }
1520 else
1521 {
1522 amt = strlen(fname) + strlen(slno) + strlen(tagstr) + 5;
1523 if ((buf = (char *)alloc(amt)) == NULL)
1524 return NULL;
1525
1526 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1527 }
1528
1529 return buf;
1530} /* cs_make_vim_style_matches */
1531
1532
1533/*
1534 * PRIVATE: cs_manage_matches
1535 *
1536 * this is kind of hokey, but i don't see an easy way round this..
1537 *
1538 * Store: keep a ptr to the (malloc'd) memory of matches originally
1539 * generated from cs_find(). the matches are originally lines directly
1540 * from cscope output, but transformed to look like something out of a
1541 * ctags. see cs_make_vim_style_matches for more details.
1542 *
1543 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1544 * the next line from the cscope output. it basically keeps track of which
1545 * lines have been "used" and returns the next one.
1546 *
1547 * Free: frees up everything and resets
1548 *
1549 * Print: prints the tags
1550 */
1551 static char *
1552cs_manage_matches(matches, contexts, totmatches, cmd)
1553 char **matches;
1554 char **contexts;
1555 int totmatches;
1556 mcmd_e cmd;
1557{
1558 static char **mp = NULL;
1559 static char **cp = NULL;
1560 static int cnt = -1;
1561 static int next = -1;
1562 char *p = NULL;
1563
1564 switch (cmd)
1565 {
1566 case Store:
1567 assert(matches != NULL);
1568 assert(totmatches > 0);
1569 if (mp != NULL || cp != NULL)
1570 (void)cs_manage_matches(NULL, NULL, -1, Free);
1571 mp = matches;
1572 cp = contexts;
1573 cnt = totmatches;
1574 next = 0;
1575 break;
1576 case Get:
1577 if (next >= cnt)
1578 return NULL;
1579
1580 p = mp[next];
1581 next++;
1582 break;
1583 case Free:
1584 if (mp != NULL)
1585 {
1586 if (cnt > 0)
1587 while (cnt--)
1588 {
1589 vim_free(mp[cnt]);
1590 if (cp != NULL)
1591 vim_free(cp[cnt]);
1592 }
1593 vim_free(mp);
1594 vim_free(cp);
1595 }
1596 mp = NULL;
1597 cp = NULL;
1598 cnt = 0;
1599 next = 0;
1600 break;
1601 case Print:
1602 cs_print_tags_priv(mp, cp, cnt);
1603 break;
1604 default: /* should not reach here */
1605 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1606 return NULL;
1607 }
1608
1609 return p;
1610} /* cs_manage_matches */
1611
1612
1613/*
1614 * PRIVATE: cs_parse_results
1615 *
1616 * parse cscope output
1617 */
1618 static char *
1619cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1620 int cnumber;
1621 char *buf;
1622 int bufsize;
1623 char **context;
1624 char **linenumber;
1625 char **search;
1626{
1627 int ch;
1628 char *p;
1629 char *name;
1630
1631 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1632 {
1633 if (feof(csinfo[cnumber].fr_fp))
1634 errno = EIO;
1635
1636 cs_reading_emsg(cnumber);
1637
1638 return NULL;
1639 }
1640
1641 /* If the line's too long for the buffer, discard it. */
1642 if ((p = strchr(buf, '\n')) == NULL)
1643 {
1644 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1645 ;
1646 return NULL;
1647 }
1648 *p = '\0';
1649
1650 /*
1651 * cscope output is in the following format:
1652 *
1653 * <filename> <context> <line number> <pattern>
1654 */
1655 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1656 return NULL;
1657 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1658 return NULL;
1659 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1660 return NULL;
1661 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1662
1663 /* --- nvi ---
1664 * If the file is older than the cscope database, that is,
1665 * the database was built since the file was last modified,
1666 * or there wasn't a search string, use the line number.
1667 */
1668 if (strcmp(*search, "<unknown>") == 0)
1669 *search = NULL;
1670
1671 name = cs_resolve_file(cnumber, name);
1672 return name;
1673}
1674
Bram Moolenaarc716c302006-01-21 22:12:51 +00001675#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
1677 * PRIVATE: cs_file_results
1678 *
1679 * write cscope find results to file
1680 */
1681 static void
1682cs_file_results(f, nummatches_a)
1683 FILE *f;
1684 int *nummatches_a;
1685{
1686 int i, j;
1687 char *buf;
1688 char *search, *slno;
1689 char *fullname;
1690 char *cntx;
1691 char *context;
1692
1693 buf = (char *)alloc(CSREAD_BUFSIZE);
1694 if (buf == NULL)
1695 return;
1696
1697 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1698 {
1699 if (nummatches_a[i] < 1)
1700 continue;
1701
1702 for (j = 0; j < nummatches_a[i]; j++)
1703 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001704 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1705 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 continue;
1707
1708 context = (char *)alloc(strlen(cntx)+5);
1709 if (context==NULL)
1710 continue;
1711
1712 if (strcmp(cntx, "<global>")==0)
1713 strcpy(context, "<<global>>");
1714 else
1715 sprintf(context, "<<%s>>", cntx);
1716
1717 if (search==NULL)
1718 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1719 else
1720 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1721
1722 vim_free(context);
1723 vim_free(fullname);
1724 } /* for all matches */
1725
1726 (void)cs_read_prompt(i);
1727
1728 } /* for all cscope connections */
1729 vim_free(buf);
1730}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733/*
1734 * PRIVATE: cs_fill_results
1735 *
1736 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1737 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001738 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 */
1740 static void
1741cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1742 char *tagstr;
1743 int totmatches;
1744 int *nummatches_a;
1745 char ***matches_p;
1746 char ***cntxts_p;
1747 int *matched;
1748{
1749 int i, j;
1750 char *buf;
1751 char *search, *slno;
1752 int totsofar = 0;
1753 char **matches = NULL;
1754 char **cntxts = NULL;
1755 char *fullname;
1756 char *cntx;
1757
1758 assert(totmatches > 0);
1759
1760 buf = (char *)alloc(CSREAD_BUFSIZE);
1761 if (buf == NULL)
1762 return;
1763
1764 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1765 goto parse_out;
1766 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1767 goto parse_out;
1768
1769 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1770 {
1771 if (nummatches_a[i] < 1)
1772 continue;
1773
1774 for (j = 0; j < nummatches_a[i]; j++)
1775 {
1776 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1777 &slno, &search)) == NULL)
1778 continue;
1779
1780 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1781 search, tagstr);
1782
1783 vim_free(fullname);
1784
1785 if (strcmp(cntx, "<global>") == 0)
1786 cntxts[totsofar] = NULL;
1787 else
1788 /* note: if vim_strsave returns NULL, then the context
1789 * will be "<global>", which is misleading.
1790 */
1791 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1792
1793 if (matches[totsofar] != NULL)
1794 totsofar++;
1795
1796 } /* for all matches */
1797
1798 (void)cs_read_prompt(i);
1799
1800 } /* for all cscope connections */
1801
1802parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001803 if (totsofar == 0)
1804 {
1805 /* No matches, free the arrays and return NULL in "*matches_p". */
1806 vim_free(matches);
1807 matches = NULL;
1808 vim_free(cntxts);
1809 cntxts = NULL;
1810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 *matched = totsofar;
1812 *matches_p = matches;
1813 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 vim_free(buf);
1816} /* cs_fill_results */
1817
1818
1819/* get the requested path components */
1820 static char *
1821cs_pathcomponents(path)
1822 char *path;
1823{
1824 int i;
1825 char *s;
1826
1827 if (p_cspc == 0)
1828 return path;
1829
1830 s = path + strlen(path) - 1;
1831 for (i = 0; i < p_cspc; ++i)
1832 while (s > path && *--s != '/'
1833#ifdef WIN32
1834 && *--s != '\\'
1835#endif
1836 )
1837 ;
1838 if ((s > path && *s == '/')
1839#ifdef WIN32
1840 || (s > path && *s == '\\')
1841#endif
1842 )
1843 ++s;
1844 return s;
1845}
1846
1847/*
1848 * PRIVATE: cs_print_tags_priv
1849 *
1850 * called from cs_manage_matches()
1851 */
1852 static void
1853cs_print_tags_priv(matches, cntxts, num_matches)
1854 char **matches;
1855 char **cntxts;
1856 int num_matches;
1857{
1858 char *buf = NULL;
1859 int bufsize = 0; /* Track available bufsize */
1860 int newsize = 0;
1861 char *ptag;
1862 char *fname, *lno, *extra, *tbuf;
1863 int i, idx, num;
1864 char *globalcntx = "GLOBAL";
1865 char *cntxformat = " <<%s>>";
1866 char *context;
1867 char *cstag_msg = _("Cscope tag: %s");
1868 char *csfmt_str = "%4d %6s ";
1869
1870 assert (num_matches > 0);
1871
1872 if ((tbuf = (char *)alloc(strlen(matches[0]) + 1)) == NULL)
1873 return;
1874
1875 strcpy(tbuf, matches[0]);
1876 ptag = strtok(tbuf, "\t");
1877
1878 newsize = strlen(cstag_msg) + strlen(ptag);
1879 buf = (char *)alloc(newsize);
1880 if (buf != NULL)
1881 {
1882 bufsize = newsize;
1883 (void)sprintf(buf, cstag_msg, ptag);
1884 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
1885 }
1886
1887 vim_free(tbuf);
1888
1889 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
1890 msg_advance(msg_col + 2);
1891 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
1892
1893 num = 1;
1894 for (i = 0; i < num_matches; i++)
1895 {
1896 idx = i;
1897
1898 /* if we really wanted to, we could avoid this malloc and strcpy
1899 * by parsing matches[i] on the fly and placing stuff into buf
1900 * directly, but that's too much of a hassle
1901 */
1902 if ((tbuf = (char *)alloc(strlen(matches[idx]) + 1)) == NULL)
1903 continue;
1904 (void)strcpy(tbuf, matches[idx]);
1905
1906 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
1907 continue;
1908 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
1909 continue;
1910 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
1911 {
1912 /* if NULL, then no "extra", although in cscope's case, there
1913 * should always be "extra".
1914 */
1915 extra = NULL;
1916 }
1917
1918 extra = lno + strlen(lno) + 1;
1919
1920 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
1921
1922 /* hopefully 'num' (num of matches) will be less than 10^16 */
1923 newsize = strlen(csfmt_str) + 16 + strlen(lno);
1924 if (bufsize < newsize)
1925 {
1926 buf = (char *)vim_realloc(buf, newsize);
1927 if (buf == NULL)
1928 bufsize = 0;
1929 else
1930 bufsize = newsize;
1931 }
1932 if (buf != NULL)
1933 {
1934 /* csfmt_str = "%4d %6s "; */
1935 (void)sprintf(buf, csfmt_str, num, lno);
1936 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
1937 }
1938 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
1939
1940 /* compute the required space for the context */
1941 if (cntxts[idx] != NULL)
1942 context = cntxts[idx];
1943 else
1944 context = globalcntx;
1945 newsize = strlen(context) + strlen(cntxformat);
1946
1947 if (bufsize < newsize)
1948 {
1949 buf = (char *)vim_realloc(buf, newsize);
1950 if (buf == NULL)
1951 bufsize = 0;
1952 else
1953 bufsize = newsize;
1954 }
1955 if (buf != NULL)
1956 {
1957 (void)sprintf(buf, cntxformat, context);
1958
1959 /* print the context only if it fits on the same line */
1960 if (msg_col + (int)strlen(buf) >= (int)Columns)
1961 msg_putchar('\n');
1962 msg_advance(12);
1963 MSG_PUTS_LONG(buf);
1964 msg_putchar('\n');
1965 }
1966 if (extra != NULL)
1967 {
1968 msg_advance(13);
1969 MSG_PUTS_LONG(extra);
1970 }
1971
1972 vim_free(tbuf); /* only after printing extra due to strtok use */
1973
1974 if (msg_col)
1975 msg_putchar('\n');
1976
1977 ui_breakcheck();
1978 if (got_int)
1979 {
1980 got_int = FALSE; /* don't print any more matches */
1981 break;
1982 }
1983
1984 num++;
1985 } /* for all matches */
1986
1987 vim_free(buf);
1988} /* cs_print_tags_priv */
1989
1990
1991/*
1992 * PRIVATE: cs_read_prompt
1993 *
1994 * read a cscope prompt (basically, skip over the ">> ")
1995 */
1996 static int
1997cs_read_prompt(i)
1998 int i;
1999{
2000 int ch;
2001 char *buf = NULL; /* buffer for possible error message from cscope */
2002 int bufpos = 0;
2003 char *cs_emsg;
2004 int maxlen;
2005 static char *eprompt = "Press the RETURN key to continue:";
2006 int epromptlen = strlen(eprompt);
2007 int n;
2008
2009 cs_emsg = _("E609: Cscope error: %s");
2010 /* compute maximum allowed len for Cscope error message */
2011 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2012
2013 for (;;)
2014 {
2015 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2016 /* if there is room and char is printable */
2017 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2018 {
2019 if (buf == NULL) /* lazy buffer allocation */
2020 buf = (char *)alloc(maxlen);
2021 if (buf != NULL)
2022 {
2023 /* append character to the message */
2024 buf[bufpos++] = ch;
2025 buf[bufpos] = NUL;
2026 if (bufpos >= epromptlen
2027 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2028 {
2029 /* remove eprompt from buf */
2030 buf[bufpos - epromptlen] = NUL;
2031
2032 /* print message to user */
2033 (void)EMSG2(cs_emsg, buf);
2034
2035 /* send RETURN to cscope */
2036 (void)putc('\n', csinfo[i].to_fp);
2037 (void)fflush(csinfo[i].to_fp);
2038
2039 /* clear buf */
2040 bufpos = 0;
2041 buf[bufpos] = NUL;
2042 }
2043 }
2044 }
2045
2046 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2047 {
2048 if (n > 0)
2049 ch = getc(csinfo[i].fr_fp);
2050 if (ch == EOF)
2051 {
2052 PERROR("cs_read_prompt EOF");
2053 if (buf != NULL && buf[0] != NUL)
2054 (void)EMSG2(cs_emsg, buf);
2055 else if (p_csverbose)
2056 cs_reading_emsg(i); /* don't have additional information */
2057 cs_release_csp(i, TRUE);
2058 vim_free(buf);
2059 return CSCOPE_FAILURE;
2060 }
2061
2062 if (ch != CSCOPE_PROMPT[n])
2063 {
2064 ch = EOF;
2065 break;
2066 }
2067 }
2068
2069 if (ch == EOF)
2070 continue; /* didn't find the prompt */
2071 break; /* did find the prompt */
2072 }
2073
2074 vim_free(buf);
2075 return CSCOPE_SUCCESS;
2076}
2077
2078
2079/*
2080 * PRIVATE: cs_release_csp
2081 *
2082 * does the actual free'ing for the cs ptr with an optional flag of whether
2083 * or not to free the filename. called by cs_kill and cs_reset.
2084 */
2085 static void
2086cs_release_csp(i, freefnpp)
2087 int i;
2088 int freefnpp;
2089{
2090#if defined(UNIX)
2091 int pstat;
2092#else
2093 /*
2094 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2095 */
2096 if (csinfo[i].to_fp != NULL)
2097 {
2098 (void)fputs("q\n", csinfo[i].to_fp);
2099 (void)fflush(csinfo[i].to_fp);
2100 }
2101 /* give cscope chance to exit normally */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002102 if (csinfo[i].hProc != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 && WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2104 TerminateProcess(csinfo[i].hProc, 0);
2105#endif
2106
2107 if (csinfo[i].fr_fp != NULL)
2108 (void)fclose(csinfo[i].fr_fp);
2109 if (csinfo[i].to_fp != NULL)
2110 (void)fclose(csinfo[i].to_fp);
2111
2112 /*
2113 * Safety check: If the PID would be zero here, the entire X session would
2114 * be killed. -1 and 1 are dangerous as well.
2115 */
2116#if defined(UNIX)
2117 if (csinfo[i].pid > 1)
2118 {
2119 kill(csinfo[i].pid, SIGTERM);
2120 (void)waitpid(csinfo[i].pid, &pstat, 0);
2121 }
2122#endif
2123
2124 if (freefnpp)
2125 {
2126 vim_free(csinfo[i].fname);
2127 vim_free(csinfo[i].ppath);
2128 vim_free(csinfo[i].flags);
2129 }
2130
2131 clear_csinfo(i);
2132} /* cs_release_csp */
2133
2134
2135/*
2136 * PRIVATE: cs_reset
2137 *
2138 * calls cs_kill on all cscope connections then reinits
2139 */
2140/* ARGSUSED */
2141 static int
2142cs_reset(eap)
2143 exarg_T *eap;
2144{
2145 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2146 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002147 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
2149 /* malloc our db and ppath list */
2150 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2151 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2152 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2153 if (dblist == NULL || pplist == NULL || fllist == NULL)
2154 {
2155 vim_free(dblist);
2156 vim_free(pplist);
2157 vim_free(fllist);
2158 return CSCOPE_FAILURE;
2159 }
2160
2161 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2162 {
2163 dblist[i] = csinfo[i].fname;
2164 pplist[i] = csinfo[i].ppath;
2165 fllist[i] = csinfo[i].flags;
2166 if (csinfo[i].fname != NULL)
2167 cs_release_csp(i, FALSE);
2168 }
2169
2170 /* rebuild the cscope connection list */
2171 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2172 {
2173 if (dblist[i] != NULL)
2174 {
2175 cs_add_common(dblist[i], pplist[i], fllist[i]);
2176 if (p_csverbose)
2177 {
2178 /* dont' use smsg_attr because want to display
2179 * connection number in the same line as
2180 * "Added cscope database..."
2181 */
2182 sprintf(buf, " (#%d)", i);
2183 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2184 }
2185 }
2186 vim_free(dblist[i]);
2187 vim_free(pplist[i]);
2188 vim_free(fllist[i]);
2189 }
2190 vim_free(dblist);
2191 vim_free(pplist);
2192 vim_free(fllist);
2193
2194 if (p_csverbose)
2195 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2196 return CSCOPE_SUCCESS;
2197} /* cs_reset */
2198
2199
2200/*
2201 * PRIVATE: cs_resolve_file
2202 *
2203 * construct the full pathname to a file found in the cscope database.
2204 * (Prepends ppath, if there is one and if it's not already prepended,
2205 * otherwise just uses the name found.)
2206 *
2207 * we need to prepend the prefix because on some cscope's (e.g., the one that
2208 * ships with Solaris 2.6), the output never has the prefix prepended.
2209 * contrast this with my development system (Digital Unix), which does.
2210 */
2211 static char *
2212cs_resolve_file(i, name)
2213 int i;
2214 char *name;
2215{
2216 char *fullname;
2217 int len;
2218
2219 /*
2220 * ppath is freed when we destroy the cscope connection.
2221 * fullname is freed after cs_make_vim_style_matches, after it's been
2222 * copied into the tag buffer used by vim
2223 */
2224 len = strlen(name) + 2;
2225 if (csinfo[i].ppath != NULL)
2226 len += strlen(csinfo[i].ppath);
2227
2228 if ((fullname = (char *)alloc(len)) == NULL)
2229 return NULL;
2230
2231 /*
2232 * note/example: this won't work if the cscope output already starts
2233 * "../.." and the prefix path is also "../..". if something like this
2234 * happens, you are screwed up and need to fix how you're using cscope.
2235 */
2236 if (csinfo[i].ppath != NULL &&
2237 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2238 (name[0] != '/')
2239#ifdef WIN32
2240 && name[0] != '\\' && name[1] != ':'
2241#endif
2242 )
2243 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2244 else
2245 (void)sprintf(fullname, "%s", name);
2246
2247 return fullname;
2248} /* cs_resolve_file */
2249
2250
2251/*
2252 * PRIVATE: cs_show
2253 *
2254 * show all cscope connections
2255 */
2256/* ARGSUSED */
2257 static int
2258cs_show(eap)
2259 exarg_T *eap;
2260{
2261 short i;
2262 if (cs_cnt_connections() == 0)
2263 MSG_PUTS(_("no cscope connections\n"));
2264 else
2265 {
2266 MSG_PUTS_ATTR(
2267 _(" # pid database name prepend path\n"),
2268 hl_attr(HLF_T));
2269 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2270 {
2271 if (csinfo[i].fname == NULL)
2272 continue;
2273
2274 if (csinfo[i].ppath != NULL)
2275 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2276 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2277 else
2278 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2279 i, (long)csinfo[i].pid, csinfo[i].fname);
2280 }
2281 }
2282
2283 wait_return(TRUE);
2284 return CSCOPE_SUCCESS;
2285} /* cs_show */
2286
2287#endif /* FEAT_CSCOPE */
2288
2289/* the end */