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