comparison src/if_cscope.c @ 1931:3f661e42f262 v7.2.228

updated for version 7.2-228
author vimboss
date Fri, 10 Jul 2009 13:11:26 +0000
parents e5602d92da8c
children 85da03763130
comparison
equal deleted inserted replaced
1930:89d1e8ed6c1d 1931:3f661e42f262
44 static void cs_fill_results __ARGS((char *, int , int *, char ***, 44 static void cs_fill_results __ARGS((char *, int , int *, char ***,
45 char ***, int *)); 45 char ***, int *));
46 static int cs_find __ARGS((exarg_T *eap)); 46 static int cs_find __ARGS((exarg_T *eap));
47 static int cs_find_common __ARGS((char *opt, char *pat, int, int, int)); 47 static int cs_find_common __ARGS((char *opt, char *pat, int, int, int));
48 static int cs_help __ARGS((exarg_T *eap)); 48 static int cs_help __ARGS((exarg_T *eap));
49 static void cs_init __ARGS((void));
50 static void clear_csinfo __ARGS((int i)); 49 static void clear_csinfo __ARGS((int i));
51 static int cs_insert_filelist __ARGS((char *, char *, char *, 50 static int cs_insert_filelist __ARGS((char *, char *, char *,
52 struct stat *)); 51 struct stat *));
53 static int cs_kill __ARGS((exarg_T *eap)); 52 static int cs_kill __ARGS((exarg_T *eap));
54 static void cs_kill_execute __ARGS((int, char *)); 53 static void cs_kill_execute __ARGS((int, char *));
64 static int cs_reset __ARGS((exarg_T *eap)); 63 static int cs_reset __ARGS((exarg_T *eap));
65 static char * cs_resolve_file __ARGS((int, char *)); 64 static char * cs_resolve_file __ARGS((int, char *));
66 static int cs_show __ARGS((exarg_T *eap)); 65 static int cs_show __ARGS((exarg_T *eap));
67 66
68 67
69 static csinfo_T csinfo[CSCOPE_MAX_CONNECTIONS]; 68 static csinfo_T * csinfo = NULL;
69 static int csinfo_size = 0; /* number of items allocated in
70 csinfo[] */
71
70 static int eap_arg_len; /* length of eap->arg, set in 72 static int eap_arg_len; /* length of eap->arg, set in
71 cs_lookup_cmd() */ 73 cs_lookup_cmd() */
72 static cscmd_T cs_cmds[] = 74 static cscmd_T cs_cmds[] =
73 { 75 {
74 { "add", cs_add, 76 { "add", cs_add,
142 * redundant. */ 144 * redundant. */
143 return (char_u *)query_type[idx]; 145 return (char_u *)query_type[idx];
144 } 146 }
145 case EXP_CSCOPE_KILL: 147 case EXP_CSCOPE_KILL:
146 { 148 {
147 static char_u connection[2]; 149 static char connection[5];
148 150
149 /* ":cscope kill" accepts connection numbers or partial names of 151 /* ":cscope kill" accepts connection numbers or partial names of
150 * the pathname of the cscope database as argument. Only complete 152 * the pathname of the cscope database as argument. Only complete
151 * with connection numbers. -1 can also be used to kill all 153 * with connection numbers. -1 can also be used to kill all
152 * connections. */ 154 * connections. */
153 for (i = 0, current_idx = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 155 for (i = 0, current_idx = 0; i < csinfo_size; i++)
154 { 156 {
155 if (csinfo[i].fname == NULL) 157 if (csinfo[i].fname == NULL)
156 continue; 158 continue;
157 if (current_idx++ == idx) 159 if (current_idx++ == idx)
158 { 160 {
159 /* Connection number fits in one character since 161 vim_snprintf(connection, sizeof(connection), "%d", i);
160 * CSCOPE_MAX_CONNECTIONS is < 10 */ 162 return (char_u *)connection;
161 connection[0] = i + '0';
162 connection[1] = NUL;
163 return connection;
164 } 163 }
165 } 164 }
166 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL; 165 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
167 } 166 }
168 default: 167 default:
221 exarg_T *eap; 220 exarg_T *eap;
222 int make_split; /* whether to split window */ 221 int make_split; /* whether to split window */
223 { 222 {
224 cscmd_T *cmdp; 223 cscmd_T *cmdp;
225 224
226 cs_init();
227 if ((cmdp = cs_lookup_cmd(eap)) == NULL) 225 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
228 { 226 {
229 cs_help(eap); 227 cs_help(eap);
230 return; 228 return;
231 } 229 }
281 void 279 void
282 do_cstag(eap) 280 do_cstag(eap)
283 exarg_T *eap; 281 exarg_T *eap;
284 { 282 {
285 int ret = FALSE; 283 int ret = FALSE;
286
287 cs_init();
288 284
289 if (*eap->arg == NUL) 285 if (*eap->arg == NUL)
290 { 286 {
291 (void)EMSG(_("E562: Usage: cstag <ident>")); 287 (void)EMSG(_("E562: Usage: cstag <ident>"));
292 return; 288 return;
439 int i; 435 int i;
440 436
441 if (num < 0 || num > 4 || (num > 0 && !dbpath)) 437 if (num < 0 || num > 4 || (num > 0 && !dbpath))
442 return FALSE; 438 return FALSE;
443 439
444 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 440 for (i = 0; i < csinfo_size; i++)
445 { 441 {
446 if (!csinfo[i].fname) 442 if (!csinfo[i].fname)
447 continue; 443 continue;
448 444
449 if (num == 0) 445 if (num == 0)
682 cs_cnt_connections() 678 cs_cnt_connections()
683 { 679 {
684 short i; 680 short i;
685 short cnt = 0; 681 short cnt = 0;
686 682
687 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 683 for (i = 0; i < csinfo_size; i++)
688 { 684 {
689 if (csinfo[i].fname != NULL) 685 if (csinfo[i].fname != NULL)
690 cnt++; 686 cnt++;
691 } 687 }
692 return cnt; 688 return cnt;
1110 int verbose; 1106 int verbose;
1111 int use_ll; 1107 int use_ll;
1112 { 1108 {
1113 int i; 1109 int i;
1114 char *cmd; 1110 char *cmd;
1115 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches; 1111 int *nummatches;
1112 int totmatches;
1116 #ifdef FEAT_QUICKFIX 1113 #ifdef FEAT_QUICKFIX
1117 char cmdletter; 1114 char cmdletter;
1118 char *qfpos; 1115 char *qfpos;
1119 #endif 1116 #endif
1120 1117
1121 /* create the actual command to send to cscope */ 1118 /* create the actual command to send to cscope */
1122 cmd = cs_create_cmd(opt, pat); 1119 cmd = cs_create_cmd(opt, pat);
1123 if (cmd == NULL) 1120 if (cmd == NULL)
1124 return FALSE; 1121 return FALSE;
1125 1122
1123 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1124 if (nummatches == NULL)
1125 return FALSE;
1126
1126 /* send query to all open connections, then count the total number 1127 /* send query to all open connections, then count the total number
1127 * of matches so we can alloc matchesp all in one swell foop 1128 * of matches so we can alloc matchesp all in one swell foop
1128 */ 1129 */
1129 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 1130 for (i = 0; i < csinfo_size; i++)
1130 nummatches[i] = 0; 1131 nummatches[i] = 0;
1131 totmatches = 0; 1132 totmatches = 0;
1132 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 1133 for (i = 0; i < csinfo_size; i++)
1133 { 1134 {
1134 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL) 1135 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
1135 continue; 1136 continue;
1136 1137
1137 /* send cmd to cscope */ 1138 /* send cmd to cscope */
1152 { 1153 {
1153 char *nf = _("E259: no matches found for cscope query %s of %s"); 1154 char *nf = _("E259: no matches found for cscope query %s of %s");
1154 char *buf; 1155 char *buf;
1155 1156
1156 if (!verbose) 1157 if (!verbose)
1158 {
1159 vim_free(nummatches);
1157 return FALSE; 1160 return FALSE;
1161 }
1158 1162
1159 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf))); 1163 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
1160 if (buf == NULL) 1164 if (buf == NULL)
1161 (void)EMSG(nf); 1165 (void)EMSG(nf);
1162 else 1166 else
1163 { 1167 {
1164 sprintf(buf, nf, opt, pat); 1168 sprintf(buf, nf, opt, pat);
1165 (void)EMSG(buf); 1169 (void)EMSG(buf);
1166 vim_free(buf); 1170 vim_free(buf);
1167 } 1171 }
1172 vim_free(nummatches);
1168 return FALSE; 1173 return FALSE;
1169 } 1174 }
1170 1175
1171 #ifdef FEAT_QUICKFIX 1176 #ifdef FEAT_QUICKFIX
1172 /* get cmd letter */ 1177 /* get cmd letter */
1215 { 1220 {
1216 sprintf(buf, nf, *qfpos, *(qfpos-1)); 1221 sprintf(buf, nf, *qfpos, *(qfpos-1));
1217 (void)EMSG(buf); 1222 (void)EMSG(buf);
1218 vim_free(buf); 1223 vim_free(buf);
1219 } 1224 }
1225 vim_free(nummatches);
1220 return FALSE; 1226 return FALSE;
1221 } 1227 }
1222 } 1228 }
1223 if (qfpos != NULL && *qfpos != '0' && totmatches > 0) 1229 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
1224 { 1230 {
1262 qf_jump(qi, 0, 0, forceit); 1268 qf_jump(qi, 0, 0, forceit);
1263 } 1269 }
1264 } 1270 }
1265 mch_remove(tmp); 1271 mch_remove(tmp);
1266 vim_free(tmp); 1272 vim_free(tmp);
1273 vim_free(nummatches);
1267 return TRUE; 1274 return TRUE;
1268 } 1275 }
1269 else 1276 else
1270 #endif /* FEAT_QUICKFIX */ 1277 #endif /* FEAT_QUICKFIX */
1271 { 1278 {
1273 int matched = 0; 1280 int matched = 0;
1274 1281
1275 /* read output */ 1282 /* read output */
1276 cs_fill_results((char *)pat, totmatches, nummatches, &matches, 1283 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1277 &contexts, &matched); 1284 &contexts, &matched);
1285 vim_free(nummatches);
1278 if (matches == NULL) 1286 if (matches == NULL)
1279 return FALSE; 1287 return FALSE;
1280 1288
1281 (void)cs_manage_matches(matches, contexts, matched, Store); 1289 (void)cs_manage_matches(matches, contexts, matched, Store);
1282 1290
1326 wait_return(TRUE); 1334 wait_return(TRUE);
1327 return 0; 1335 return 0;
1328 } /* cs_help */ 1336 } /* cs_help */
1329 1337
1330 1338
1331 /*
1332 * PRIVATE: cs_init
1333 *
1334 * initialize cscope structure if not already
1335 */
1336 static void
1337 cs_init()
1338 {
1339 short i;
1340 static int init_already = FALSE;
1341
1342 if (init_already)
1343 return;
1344
1345 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1346 clear_csinfo(i);
1347
1348 init_already = TRUE;
1349 } /* cs_init */
1350
1351 static void 1339 static void
1352 clear_csinfo(i) 1340 clear_csinfo(i)
1353 int i; 1341 int i;
1354 { 1342 {
1355 csinfo[i].fname = NULL; 1343 csinfo[i].fname = NULL;
1442 CloseHandle(hFile); 1430 CloseHandle(hFile);
1443 } 1431 }
1444 #endif 1432 #endif
1445 1433
1446 i = -1; /* can be set to the index of an empty item in csinfo */ 1434 i = -1; /* can be set to the index of an empty item in csinfo */
1447 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++) 1435 for (j = 0; j < csinfo_size; j++)
1448 { 1436 {
1449 if (csinfo[j].fname != NULL 1437 if (csinfo[j].fname != NULL
1450 #if defined(UNIX) 1438 #if defined(UNIX)
1451 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino 1439 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1452 #else 1440 #else
1469 i = j; /* remember first empty entry */ 1457 i = j; /* remember first empty entry */
1470 } 1458 }
1471 1459
1472 if (i == -1) 1460 if (i == -1)
1473 { 1461 {
1474 if (p_csverbose) 1462 i = csinfo_size;
1475 (void)EMSG(_("E569: maximum number of cscope connections reached")); 1463 if (csinfo_size == 0)
1476 return -1; 1464 {
1465 /* First time allocation: allocate only 1 connection. It should
1466 * be enough for most users. If more is needed, csinfo will be
1467 * reallocated. */
1468 csinfo_size = 1;
1469 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1470 }
1471 else
1472 {
1473 /* Reallocate space for more connections. */
1474 csinfo_size *= 2;
1475 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1476 }
1477 if (csinfo == NULL)
1478 return -1;
1479 for (j = csinfo_size/2; j < csinfo_size; j++)
1480 clear_csinfo(j);
1477 } 1481 }
1478 1482
1479 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL) 1483 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
1480 return -1; 1484 return -1;
1481 1485
1578 else 1582 else
1579 { 1583 {
1580 /* It must be part of a name. We will try to find a match 1584 /* It must be part of a name. We will try to find a match
1581 * within all the names in the csinfo data structure 1585 * within all the names in the csinfo data structure
1582 */ 1586 */
1583 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 1587 for (i = 0; i < csinfo_size; i++)
1584 { 1588 {
1585 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok)) 1589 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1586 break; 1590 break;
1587 } 1591 }
1588 } 1592 }
1589 1593
1590 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL) 1594 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
1591 && i != -1)
1592 { 1595 {
1593 if (p_csverbose) 1596 if (p_csverbose)
1594 (void)EMSG2(_("E261: cscope connection %s not found"), stok); 1597 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1595 } 1598 }
1596 else 1599 else
1597 { 1600 {
1598 if (i == -1) 1601 if (i == -1)
1599 { 1602 {
1600 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 1603 for (i = 0; i < csinfo_size; i++)
1601 { 1604 {
1602 if (csinfo[i].fname) 1605 if (csinfo[i].fname)
1603 cs_kill_execute(i, csinfo[i].fname); 1606 cs_kill_execute(i, csinfo[i].fname);
1604 } 1607 }
1605 } 1608 }
1855 1858
1856 buf = (char *)alloc(CSREAD_BUFSIZE); 1859 buf = (char *)alloc(CSREAD_BUFSIZE);
1857 if (buf == NULL) 1860 if (buf == NULL)
1858 return; 1861 return;
1859 1862
1860 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 1863 for (i = 0; i < csinfo_size; i++)
1861 { 1864 {
1862 if (nummatches_a[i] < 1) 1865 if (nummatches_a[i] < 1)
1863 continue; 1866 continue;
1864 1867
1865 for (j = 0; j < nummatches_a[i]; j++) 1868 for (j = 0; j < nummatches_a[i]; j++)
1927 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL) 1930 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1928 goto parse_out; 1931 goto parse_out;
1929 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL) 1932 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1930 goto parse_out; 1933 goto parse_out;
1931 1934
1932 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 1935 for (i = 0; i < csinfo_size; i++)
1933 { 1936 {
1934 if (nummatches_a[i] < 1) 1937 if (nummatches_a[i] < 1)
1935 continue; 1938 continue;
1936 1939
1937 for (j = 0; j < nummatches_a[i]; j++) 1940 for (j = 0; j < nummatches_a[i]; j++)
2381 { 2384 {
2382 char **dblist = NULL, **pplist = NULL, **fllist = NULL; 2385 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2383 int i; 2386 int i;
2384 char buf[20]; /* for sprintf " (#%d)" */ 2387 char buf[20]; /* for sprintf " (#%d)" */
2385 2388
2389 if (csinfo_size == 0)
2390 return CSCOPE_SUCCESS;
2391
2386 /* malloc our db and ppath list */ 2392 /* malloc our db and ppath list */
2387 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *)); 2393 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2388 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *)); 2394 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2389 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *)); 2395 fllist = (char **)alloc(csinfo_size * sizeof(char *));
2390 if (dblist == NULL || pplist == NULL || fllist == NULL) 2396 if (dblist == NULL || pplist == NULL || fllist == NULL)
2391 { 2397 {
2392 vim_free(dblist); 2398 vim_free(dblist);
2393 vim_free(pplist); 2399 vim_free(pplist);
2394 vim_free(fllist); 2400 vim_free(fllist);
2395 return CSCOPE_FAILURE; 2401 return CSCOPE_FAILURE;
2396 } 2402 }
2397 2403
2398 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 2404 for (i = 0; i < csinfo_size; i++)
2399 { 2405 {
2400 dblist[i] = csinfo[i].fname; 2406 dblist[i] = csinfo[i].fname;
2401 pplist[i] = csinfo[i].ppath; 2407 pplist[i] = csinfo[i].ppath;
2402 fllist[i] = csinfo[i].flags; 2408 fllist[i] = csinfo[i].flags;
2403 if (csinfo[i].fname != NULL) 2409 if (csinfo[i].fname != NULL)
2404 cs_release_csp(i, FALSE); 2410 cs_release_csp(i, FALSE);
2405 } 2411 }
2406 2412
2407 /* rebuild the cscope connection list */ 2413 /* rebuild the cscope connection list */
2408 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 2414 for (i = 0; i < csinfo_size; i++)
2409 { 2415 {
2410 if (dblist[i] != NULL) 2416 if (dblist[i] != NULL)
2411 { 2417 {
2412 cs_add_common(dblist[i], pplist[i], fllist[i]); 2418 cs_add_common(dblist[i], pplist[i], fllist[i]);
2413 if (p_csverbose) 2419 if (p_csverbose)
2500 else 2506 else
2501 { 2507 {
2502 MSG_PUTS_ATTR( 2508 MSG_PUTS_ATTR(
2503 _(" # pid database name prepend path\n"), 2509 _(" # pid database name prepend path\n"),
2504 hl_attr(HLF_T)); 2510 hl_attr(HLF_T));
2505 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 2511 for (i = 0; i < csinfo_size; i++)
2506 { 2512 {
2507 if (csinfo[i].fname == NULL) 2513 if (csinfo[i].fname == NULL)
2508 continue; 2514 continue;
2509 2515
2510 if (csinfo[i].ppath != NULL) 2516 if (csinfo[i].ppath != NULL)
2529 void 2535 void
2530 cs_end() 2536 cs_end()
2531 { 2537 {
2532 int i; 2538 int i;
2533 2539
2534 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++) 2540 for (i = 0; i < csinfo_size; i++)
2535 cs_release_csp(i, TRUE); 2541 cs_release_csp(i, TRUE);
2542 vim_free(csinfo);
2543 csinfo_size = 0;
2536 } 2544 }
2537 2545
2538 #endif /* FEAT_CSCOPE */ 2546 #endif /* FEAT_CSCOPE */
2539 2547
2540 /* the end */ 2548 /* the end */