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