Enable -Wconversion: if_cscope.c #3068

Helped-by: Gustaf Lindstedt <gustaflindstedt@gmail.com>
Helped-by: Wander Nauta <info@wandernauta.nl>
This commit is contained in:
cztchoice 2015-07-21 21:40:35 +08:00 committed by Justin M. Keyes
parent ba6c7a6f5b
commit ef5ee31452
2 changed files with 135 additions and 133 deletions

View File

@ -82,7 +82,6 @@ set(CONV_SOURCES
ex_getln.c ex_getln.c
fileio.c fileio.c
getchar.c getchar.c
if_cscope.c
mbyte.c mbyte.c
memline.c memline.c
message.c message.c

View File

@ -45,11 +45,9 @@
#endif #endif
static csinfo_T * csinfo = NULL; static csinfo_T * csinfo = NULL;
static int csinfo_size = 0; /* number of items allocated in static size_t csinfo_size = 0; // number of items allocated in csinfo[]
csinfo[] */
static int eap_arg_len; /* length of eap->arg, set in static int eap_arg_len; // length of eap->arg, set in cs_lookup_cmd()
cs_lookup_cmd() */
static cscmd_T cs_cmds[] = static cscmd_T cs_cmds[] =
{ {
{ "add", cs_add, { "add", cs_add,
@ -87,7 +85,6 @@ static enum {
char_u *get_cscope_name(expand_T *xp, int idx) char_u *get_cscope_name(expand_T *xp, int idx)
{ {
int current_idx; int current_idx;
int i;
switch (expand_what) { switch (expand_what) {
case EXP_CSCOPE_SUBCMD: case EXP_CSCOPE_SUBCMD:
@ -95,13 +92,16 @@ char_u *get_cscope_name(expand_T *xp, int idx)
* add, find, help, kill, reset, show */ * add, find, help, kill, reset, show */
return (char_u *)cs_cmds[idx].name; return (char_u *)cs_cmds[idx].name;
case EXP_SCSCOPE_SUBCMD: case EXP_SCSCOPE_SUBCMD:
{
/* Complete with sub-commands of ":scscope": same sub-commands as /* Complete with sub-commands of ":scscope": same sub-commands as
* ":cscope" but skip commands which don't support split windows */ * ":cscope" but skip commands which don't support split windows */
int i;
for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++) for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
if (cs_cmds[i].cansplit) if (cs_cmds[i].cansplit)
if (current_idx++ == idx) if (current_idx++ == idx)
break; break;
return (char_u *)cs_cmds[i].name; return (char_u *)cs_cmds[i].name;
}
case EXP_CSCOPE_FIND: case EXP_CSCOPE_FIND:
{ {
const char *query_type[] = const char *query_type[] =
@ -123,11 +123,12 @@ char_u *get_cscope_name(expand_T *xp, int idx)
* the pathname of the cscope database as argument. Only complete * the pathname of the cscope database as argument. Only complete
* with connection numbers. -1 can also be used to kill all * with connection numbers. -1 can also be used to kill all
* connections. */ * connections. */
size_t i;
for (i = 0, current_idx = 0; i < csinfo_size; i++) { for (i = 0, current_idx = 0; i < csinfo_size; i++) {
if (csinfo[i].fname == NULL) if (csinfo[i].fname == NULL)
continue; continue;
if (current_idx++ == idx) { if (current_idx++ == idx) {
vim_snprintf(connection, sizeof(connection), "%d", i); vim_snprintf(connection, sizeof(connection), "%zu", i);
return (char_u *)connection; return (char_u *)connection;
} }
} }
@ -300,8 +301,8 @@ int cs_fgets(char_u *buf, int size)
{ {
char *p; char *p;
if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL) if ((p = cs_manage_matches(NULL, NULL, 0, Get)) == NULL)
return TRUE; return true;
STRLCPY(buf, p, size); STRLCPY(buf, p, size);
return FALSE; return FALSE;
@ -315,7 +316,7 @@ int cs_fgets(char_u *buf, int size)
*/ */
void cs_free_tags(void) void cs_free_tags(void)
{ {
cs_manage_matches(NULL, NULL, -1, Free); cs_manage_matches(NULL, NULL, 0, Free);
} }
/* /*
@ -325,7 +326,7 @@ void cs_free_tags(void)
*/ */
void cs_print_tags(void) void cs_print_tags(void)
{ {
cs_manage_matches(NULL, NULL, -1, Print); cs_manage_matches(NULL, NULL, 0, Print);
} }
/* /*
@ -357,12 +358,10 @@ void cs_print_tags(void)
*/ */
int cs_connection(int num, char_u *dbpath, char_u *ppath) int cs_connection(int num, char_u *dbpath, char_u *ppath)
{ {
int i;
if (num < 0 || num > 4 || (num > 0 && !dbpath)) if (num < 0 || num > 4 || (num > 0 && !dbpath))
return FALSE; return false;
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (!csinfo[i].fname) if (!csinfo[i].fname)
continue; continue;
@ -455,7 +454,6 @@ cs_add_common (
char *fname = NULL; char *fname = NULL;
char *fname2 = NULL; char *fname2 = NULL;
char *ppath = NULL; char *ppath = NULL;
int i;
size_t usedlen = 0; size_t usedlen = 0;
char_u *fbuf = NULL; char_u *fbuf = NULL;
@ -487,6 +485,7 @@ staterr:
goto staterr; goto staterr;
} }
int i;
/* if filename is a directory, append the cscope database name to it */ /* if filename is a directory, append the cscope database name to it */
if ((file_info.stat.st_mode & S_IFMT) == S_IFDIR) { if ((file_info.stat.st_mode & S_IFMT) == S_IFDIR) {
fname2 = (char *)xmalloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2); fname2 = (char *)xmalloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
@ -523,9 +522,10 @@ staterr:
} }
if (i != -1) { if (i != -1) {
if (cs_create_connection(i) == CSCOPE_FAILURE assert(i >= 0);
|| cs_read_prompt(i) == CSCOPE_FAILURE) { if (cs_create_connection((size_t)i) == CSCOPE_FAILURE
cs_release_csp(i, TRUE); || cs_read_prompt((size_t)i) == CSCOPE_FAILURE) {
cs_release_csp((size_t)i, true);
goto add_err; goto add_err;
} }
@ -565,24 +565,22 @@ static int cs_check_for_tags(void)
* *
* count the number of cscope connections * count the number of cscope connections
*/ */
static int cs_cnt_connections(void) static size_t cs_cnt_connections(void)
{ {
short i; size_t cnt = 0;
short cnt = 0;
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (csinfo[i].fname != NULL) if (csinfo[i].fname != NULL)
cnt++; cnt++;
} }
return cnt; return cnt;
} /* cs_cnt_connections */ } /* cs_cnt_connections */
static void static void cs_reading_emsg(
cs_reading_emsg ( size_t idx /* connection index */
int idx /* connection index */
) )
{ {
EMSGN(_("E262: error reading cscope connection %" PRId64), idx); EMSGU(_("E262: error reading cscope connection %" PRIu64), idx);
} }
#define CSREAD_BUFSIZE 2048 #define CSREAD_BUFSIZE 2048
@ -591,7 +589,7 @@ cs_reading_emsg (
* *
* count the number of matches for a given cscope connection. * count the number of matches for a given cscope connection.
*/ */
static int cs_cnt_matches(int idx) static int cs_cnt_matches(size_t idx)
{ {
char *stok; char *stok;
int nlines; int nlines;
@ -605,7 +603,7 @@ static int cs_cnt_matches(int idx)
cs_reading_emsg(idx); cs_reading_emsg(idx);
xfree(buf); xfree(buf);
return -1; return CSCOPE_FAILURE;
} }
/* /*
@ -703,12 +701,11 @@ static char *cs_create_cmd(char *csoption, char *pattern)
* This piece of code was taken/adapted from nvi. do we need to add * This piece of code was taken/adapted from nvi. do we need to add
* the BSD license notice? * the BSD license notice?
*/ */
static int cs_create_connection(int i) static int cs_create_connection(size_t i)
{ {
#ifdef UNIX #ifdef UNIX
int to_cs[2], from_cs[2]; int to_cs[2], from_cs[2];
#endif #endif
int len;
char *prog, *cmd, *ppath = NULL; char *prog, *cmd, *ppath = NULL;
#if defined(UNIX) #if defined(UNIX)
@ -773,17 +770,17 @@ err_closing:
expand_env(p_csprg, (char_u *)prog, MAXPATHL); expand_env(p_csprg, (char_u *)prog, MAXPATHL);
/* alloc space to hold the cscope command */ /* alloc space to hold the cscope command */
len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32); size_t len = strlen(prog) + strlen(csinfo[i].fname) + 32;
if (csinfo[i].ppath) { if (csinfo[i].ppath) {
/* expand the prepend path for env var's */ /* expand the prepend path for env var's */
ppath = xmalloc(MAXPATHL + 1); ppath = xmalloc(MAXPATHL + 1);
expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL); expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
len += (int)strlen(ppath); len += strlen(ppath);
} }
if (csinfo[i].flags) if (csinfo[i].flags)
len += (int)strlen(csinfo[i].flags); len += strlen(csinfo[i].flags);
cmd = xmalloc(len); cmd = xmalloc(len);
@ -896,7 +893,6 @@ err_closing:
static int cs_find(exarg_T *eap) static int cs_find(exarg_T *eap)
{ {
char *opt, *pat; char *opt, *pat;
int i;
if (cs_check_for_connections() == FALSE) { if (cs_check_for_connections() == FALSE) {
(void)EMSG(_("E567: no cscope connections")); (void)EMSG(_("E567: no cscope connections"));
@ -918,7 +914,7 @@ static int cs_find(exarg_T *eap)
* Let's replace the NULs written by strtok() with spaces - we need the * Let's replace the NULs written by strtok() with spaces - we need the
* spaces to correctly display the quickfix/location list window's title. * spaces to correctly display the quickfix/location list window's title.
*/ */
for (i = 0; i < eap_arg_len; ++i) for (int i = 0; i < eap_arg_len; ++i)
if (NUL == eap->arg[i]) if (NUL == eap->arg[i])
eap->arg[i] = ' '; eap->arg[i] = ' ';
@ -932,12 +928,12 @@ static int cs_find(exarg_T *eap)
* *
* common code for cscope find, shared by cs_find() and do_cstag() * common code for cscope find, shared by cs_find() and do_cstag()
*/ */
static int cs_find_common(char *opt, char *pat, int forceit, int verbose, int use_ll, char_u *cmdline) static int cs_find_common(char *opt, char *pat, int forceit, int verbose,
int use_ll, char_u *cmdline)
{ {
int i;
char *cmd; char *cmd;
int *nummatches; int *nummatches;
int totmatches; size_t totmatches;
char cmdletter; char cmdletter;
char *qfpos; char *qfpos;
@ -1003,10 +999,10 @@ static int cs_find_common(char *opt, char *pat, int forceit, int verbose, int us
/* Send query to all open connections, then count the total number /* Send query to all open connections, then count the total number
* of matches so we can alloc all in one swell foop. */ * of matches so we can alloc all in one swell foop. */
for (i = 0; i < csinfo_size; i++) for (size_t i = 0; i < csinfo_size; i++)
nummatches[i] = 0; nummatches[i] = 0;
totmatches = 0; totmatches = 0;
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL) if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
continue; continue;
@ -1017,7 +1013,7 @@ static int cs_find_common(char *opt, char *pat, int forceit, int verbose, int us
nummatches[i] = cs_cnt_matches(i); nummatches[i] = cs_cnt_matches(i);
if (nummatches[i] > -1) if (nummatches[i] > -1)
totmatches += nummatches[i]; totmatches += (size_t)nummatches[i];
if (nummatches[i] == 0) if (nummatches[i] == 0)
(void)cs_read_prompt(i); (void)cs_read_prompt(i);
@ -1084,7 +1080,7 @@ static int cs_find_common(char *opt, char *pat, int forceit, int verbose, int us
return TRUE; return TRUE;
} else { } else {
char **matches = NULL, **contexts = NULL; char **matches = NULL, **contexts = NULL;
int matched = 0; size_t matched = 0;
/* read output */ /* read output */
cs_fill_results((char *)pat, totmatches, nummatches, &matches, cs_fill_results((char *)pat, totmatches, nummatches, &matches,
@ -1136,11 +1132,11 @@ static int cs_help(exarg_T *eap)
} }
wait_return(TRUE); wait_return(TRUE);
return 0; return CSCOPE_SUCCESS;
} /* cs_help */ } /* cs_help */
static void clear_csinfo(int i) static void clear_csinfo(size_t i)
{ {
csinfo[i].fname = NULL; csinfo[i].fname = NULL;
csinfo[i].ppath = NULL; csinfo[i].ppath = NULL;
@ -1175,22 +1171,24 @@ static char *GetWin32Error(void)
static int cs_insert_filelist(char *fname, char *ppath, char *flags, static int cs_insert_filelist(char *fname, char *ppath, char *flags,
FileInfo *file_info) FileInfo *file_info)
{ {
short i, j; size_t i = 0;
bool empty_found = false;
i = -1; /* can be set to the index of an empty item in csinfo */ for (size_t j = 0; j < csinfo_size; j++) {
for (j = 0; j < csinfo_size; j++) {
if (csinfo[j].fname != NULL if (csinfo[j].fname != NULL
&& os_fileid_equal_fileinfo(&(csinfo[j].file_id), file_info)) { && os_fileid_equal_fileinfo(&(csinfo[j].file_id), file_info)) {
if (p_csverbose) if (p_csverbose)
(void)EMSG(_("E568: duplicate cscope database not added")); (void)EMSG(_("E568: duplicate cscope database not added"));
return -1; return CSCOPE_FAILURE;
} }
if (csinfo[j].fname == NULL && i == -1) if (csinfo[j].fname == NULL && !empty_found) {
i = j; /* remember first empty entry */ i = j; /* remember first empty entry */
empty_found = true;
}
} }
if (i == -1) { if (!empty_found) {
i = csinfo_size; i = csinfo_size;
if (csinfo_size == 0) { if (csinfo_size == 0) {
/* First time allocation: allocate only 1 connection. It should /* First time allocation: allocate only 1 connection. It should
@ -1203,7 +1201,7 @@ static int cs_insert_filelist(char *fname, char *ppath, char *flags,
csinfo_size *= 2; csinfo_size *= 2;
csinfo = xrealloc(csinfo, sizeof(csinfo_T)*csinfo_size); csinfo = xrealloc(csinfo, sizeof(csinfo_T)*csinfo_size);
} }
for (j = csinfo_size/2; j < csinfo_size; j++) for (size_t j = csinfo_size/2; j < csinfo_size; j++)
clear_csinfo(j); clear_csinfo(j);
} }
@ -1224,7 +1222,8 @@ static int cs_insert_filelist(char *fname, char *ppath, char *flags,
csinfo[i].flags = NULL; csinfo[i].flags = NULL;
os_fileinfo_id(file_info, &(csinfo[i].file_id)); os_fileinfo_id(file_info, &(csinfo[i].file_id));
return i; assert(i <= INT_MAX);
return (int)i;
} /* cs_insert_filelist */ } /* cs_insert_filelist */
@ -1265,42 +1264,55 @@ static cscmd_T * cs_lookup_cmd(exarg_T *eap)
static int cs_kill(exarg_T *eap) static int cs_kill(exarg_T *eap)
{ {
char *stok; char *stok;
short i; int num;
size_t i = 0;
bool killall = false;
if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL) { if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL) {
cs_usage_msg(Kill); cs_usage_msg(Kill);
return CSCOPE_FAILURE; return CSCOPE_FAILURE;
} }
/* only single digit positive and negative integers are allowed */ // Check if string is a number, only single digit
// positive and negative integers are allowed
if ((strlen(stok) < 2 && ascii_isdigit((int)(stok[0]))) if ((strlen(stok) < 2 && ascii_isdigit((int)(stok[0])))
|| (strlen(stok) < 3 && stok[0] == '-' || (strlen(stok) < 3 && stok[0] == '-'
&& ascii_isdigit((int)(stok[1])))) && ascii_isdigit((int)(stok[1])))) {
i = atoi(stok); num = atoi(stok);
else { if (num == -1)
/* It must be part of a name. We will try to find a match killall = true;
* within all the names in the csinfo data structure else if (num >= 0) {
*/ i = (size_t)num;
} else { // All negative values besides -1 are invalid.
if (p_csverbose)
(void)EMSG2(_("E261: cscope connection %s not found"), stok);
return CSCOPE_FAILURE;
}
} else {
// Else it must be part of a name. We will try to find a match
// within all the names in the csinfo data structure
for (i = 0; i < csinfo_size; i++) { for (i = 0; i < csinfo_size; i++) {
if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok)) if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
break; break;
} }
} }
if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL)) { if (i >= csinfo_size || csinfo[i].fname == NULL) {
if (p_csverbose) if (p_csverbose)
(void)EMSG2(_("E261: cscope connection %s not found"), stok); (void)EMSG2(_("E261: cscope connection %s not found"), stok);
return CSCOPE_FAILURE;
} else { } else {
if (i == -1) { if (killall) {
for (i = 0; i < csinfo_size; i++) { for (i = 0; i < csinfo_size; i++) {
if (csinfo[i].fname) if (csinfo[i].fname)
cs_kill_execute(i, csinfo[i].fname); cs_kill_execute(i, csinfo[i].fname);
} }
} else } else {
cs_kill_execute(i, stok); cs_kill_execute((size_t)i, stok);
}
} }
return 0; return CSCOPE_SUCCESS;
} /* cs_kill */ } /* cs_kill */
@ -1309,9 +1321,8 @@ static int cs_kill(exarg_T *eap)
* *
* Actually kills a specific cscope connection. * Actually kills a specific cscope connection.
*/ */
static void static void cs_kill_execute(
cs_kill_execute ( size_t i, /* cscope table index */
int i, /* cscope table index */
char *cname /* cscope database name */ char *cname /* cscope database name */
) )
{ {
@ -1344,7 +1355,8 @@ cs_kill_execute (
* would still have to be modified to escape all the special regular expression * would still have to be modified to escape all the special regular expression
* characters to comply with ctags formatting. * characters to comply with ctags formatting.
*/ */
static char *cs_make_vim_style_matches(char *fname, char *slno, char *search, char *tagstr) static char *cs_make_vim_style_matches(char *fname, char *slno, char *search,
char *tagstr)
{ {
/* vim style is ctags: /* vim style is ctags:
* *
@ -1394,12 +1406,13 @@ static char *cs_make_vim_style_matches(char *fname, char *slno, char *search, ch
* *
* Print: prints the tags * Print: prints the tags
*/ */
static char *cs_manage_matches(char **matches, char **contexts, int totmatches, mcmd_e cmd) static char *cs_manage_matches(char **matches, char **contexts,
size_t totmatches, mcmd_e cmd)
{ {
static char **mp = NULL; static char **mp = NULL;
static char **cp = NULL; static char **cp = NULL;
static int cnt = -1; static size_t cnt = 0;
static int next = -1; static size_t next = 0;
char *p = NULL; char *p = NULL;
switch (cmd) { switch (cmd) {
@ -1407,7 +1420,7 @@ static char *cs_manage_matches(char **matches, char **contexts, int totmatches,
assert(matches != NULL); assert(matches != NULL);
assert(totmatches > 0); assert(totmatches > 0);
if (mp != NULL || cp != NULL) if (mp != NULL || cp != NULL)
(void)cs_manage_matches(NULL, NULL, -1, Free); (void)cs_manage_matches(NULL, NULL, 0, Free);
mp = matches; mp = matches;
cp = contexts; cp = contexts;
cnt = totmatches; cnt = totmatches;
@ -1422,12 +1435,11 @@ static char *cs_manage_matches(char **matches, char **contexts, int totmatches,
break; break;
case Free: case Free:
if (mp != NULL) { if (mp != NULL) {
if (cnt > 0) while (cnt--) {
while (cnt--) { xfree(mp[cnt]);
xfree(mp[cnt]); if (cp != NULL)
if (cp != NULL) xfree(cp[cnt]);
xfree(cp[cnt]); }
}
xfree(mp); xfree(mp);
xfree(cp); xfree(cp);
} }
@ -1453,7 +1465,8 @@ static char *cs_manage_matches(char **matches, char **contexts, int totmatches,
* *
* parse cscope output * parse cscope output
*/ */
static char *cs_parse_results(int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search) static char *cs_parse_results(size_t cnumber, char *buf, int bufsize,
char **context, char **linenumber, char **search)
{ {
int ch; int ch;
char *p; char *p;
@ -1508,7 +1521,6 @@ static char *cs_parse_results(int cnumber, char *buf, int bufsize, char **contex
*/ */
static void cs_file_results(FILE *f, int *nummatches_a) static void cs_file_results(FILE *f, int *nummatches_a)
{ {
int i, j;
char *search, *slno; char *search, *slno;
char *fullname; char *fullname;
char *cntx; char *cntx;
@ -1516,11 +1528,11 @@ static void cs_file_results(FILE *f, int *nummatches_a)
char *buf = xmalloc(CSREAD_BUFSIZE); char *buf = xmalloc(CSREAD_BUFSIZE);
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (nummatches_a[i] < 1) if (nummatches_a[i] < 1)
continue; continue;
for (j = 0; j < nummatches_a[i]; j++) { for (int j = 0; j < nummatches_a[i]; j++) {
if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx, if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
&slno, &search)) == NULL) &slno, &search)) == NULL)
continue; continue;
@ -1554,12 +1566,13 @@ static void cs_file_results(FILE *f, int *nummatches_a)
* into ctags format * into ctags format
* When there are no matches sets "*matches_p" to NULL. * When there are no matches sets "*matches_p" to NULL.
*/ */
static void cs_fill_results(char *tagstr, int totmatches, int *nummatches_a, char ***matches_p, char ***cntxts_p, int *matched) static void cs_fill_results(char *tagstr, size_t totmatches, int *nummatches_a,
char ***matches_p, char ***cntxts_p,
size_t *matched)
{ {
int i, j;
char *buf; char *buf;
char *search, *slno; char *search, *slno;
int totsofar = 0; size_t totsofar = 0;
char **matches = NULL; char **matches = NULL;
char **cntxts = NULL; char **cntxts = NULL;
char *fullname; char *fullname;
@ -1568,14 +1581,14 @@ static void cs_fill_results(char *tagstr, int totmatches, int *nummatches_a, cha
assert(totmatches > 0); assert(totmatches > 0);
buf = xmalloc(CSREAD_BUFSIZE); buf = xmalloc(CSREAD_BUFSIZE);
matches = xmalloc(sizeof(char *) * totmatches); matches = xmalloc(sizeof(char *) * (size_t)totmatches);
cntxts = xmalloc(sizeof(char *) * totmatches); cntxts = xmalloc(sizeof(char *) * (size_t)totmatches);
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (nummatches_a[i] < 1) if (nummatches_a[i] < 1)
continue; continue;
for (j = 0; j < nummatches_a[i]; j++) { for (int j = 0; j < nummatches_a[i]; j++) {
if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx, if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
&slno, &search)) == NULL) &slno, &search)) == NULL)
continue; continue;
@ -1617,19 +1630,13 @@ static void cs_fill_results(char *tagstr, int totmatches, int *nummatches_a, cha
/* get the requested path components */ /* get the requested path components */
static char *cs_pathcomponents(char *path) static char *cs_pathcomponents(char *path)
{ {
int i;
char *s;
if (p_cspc == 0) if (p_cspc == 0)
return path; return path;
s = path + strlen(path) - 1; char *s = path + strlen(path) - 1;
for (i = 0; i < p_cspc; ++i) for (int i = 0; i < p_cspc; ++i)
while (s > path && *--s != '/' while (s > path && *--s != '/') continue;
) if ((s > path && *s == '/'))
;
if ((s > path && *s == '/')
)
++s; ++s;
return s; return s;
} }
@ -1639,11 +1646,12 @@ static char *cs_pathcomponents(char *path)
* *
* called from cs_manage_matches() * called from cs_manage_matches()
*/ */
static void cs_print_tags_priv(char **matches, char **cntxts, int num_matches) static void cs_print_tags_priv(char **matches, char **cntxts,
size_t num_matches)
{ {
char *ptag; char *ptag;
char *fname, *lno, *extra, *tbuf; char *fname, *lno, *extra, *tbuf;
int i, idx, num; size_t num;
char *globalcntx = "GLOBAL"; char *globalcntx = "GLOBAL";
char *context; char *context;
char *cstag_msg = _("Cscope tag: %s"); char *cstag_msg = _("Cscope tag: %s");
@ -1668,8 +1676,8 @@ static void cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T)); MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
num = 1; num = 1;
for (i = 0; i < num_matches; i++) { for (size_t i = 0; i < num_matches; i++) {
idx = i; size_t idx = i;
/* if we really wanted to, we could avoid this malloc and strcpy /* if we really wanted to, we could avoid this malloc and strcpy
* by parsing matches[i] on the fly and placing stuff into buf * by parsing matches[i] on the fly and placing stuff into buf
@ -1688,7 +1696,7 @@ static void cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */ lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
const char *csfmt_str = "%4d %6s "; const char *csfmt_str = "%4zu %6s ";
/* hopefully 'num' (num of matches) will be less than 10^16 */ /* hopefully 'num' (num of matches) will be less than 10^16 */
newsize = strlen(csfmt_str) + 16 + strlen(lno); newsize = strlen(csfmt_str) + 16 + strlen(lno);
if (bufsize < newsize) { if (bufsize < newsize) {
@ -1750,23 +1758,22 @@ static void cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
* *
* read a cscope prompt (basically, skip over the ">> ") * read a cscope prompt (basically, skip over the ">> ")
*/ */
static int cs_read_prompt(int i) static int cs_read_prompt(size_t i)
{ {
int ch; char ch;
char *buf = NULL; /* buffer for possible error message from cscope */ char *buf = NULL; /* buffer for possible error message from cscope */
int bufpos = 0; size_t bufpos = 0;
char *cs_emsg; char *cs_emsg = _("E609: Cscope error: %s");
int maxlen; size_t cs_emsg_len = strlen(cs_emsg);
static char *eprompt = "Press the RETURN key to continue:"; static char *eprompt = "Press the RETURN key to continue:";
int epromptlen = (int)strlen(eprompt); size_t epromptlen = strlen(eprompt);
int n;
cs_emsg = _("E609: Cscope error: %s");
/* compute maximum allowed len for Cscope error message */ /* compute maximum allowed len for Cscope error message */
maxlen = (int)(IOSIZE - strlen(cs_emsg)); assert(IOSIZE >= cs_emsg_len);
size_t maxlen = IOSIZE - cs_emsg_len;
for (;; ) { for (;; ) {
while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0]) while ((ch = (char)getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
/* if there is room and char is printable */ /* if there is room and char is printable */
if (bufpos < maxlen - 1 && vim_isprintc(ch)) { if (bufpos < maxlen - 1 && vim_isprintc(ch)) {
// lazy buffer allocation // lazy buffer allocation
@ -1796,9 +1803,9 @@ static int cs_read_prompt(int i)
} }
} }
for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n) { for (size_t n = 0; n < strlen(CSCOPE_PROMPT); ++n) {
if (n > 0) if (n > 0)
ch = getc(csinfo[i].fr_fp); ch = (char)getc(csinfo[i].fr_fp);
if (ch == EOF) { if (ch == EOF) {
PERROR("cs_read_prompt EOF"); PERROR("cs_read_prompt EOF");
if (buf != NULL && buf[0] != NUL) if (buf != NULL && buf[0] != NUL)
@ -1842,7 +1849,7 @@ static void sig_handler(int s) {
* Does the actual free'ing for the cs ptr with an optional flag of whether * Does the actual free'ing for the cs ptr with an optional flag of whether
* or not to free the filename. Called by cs_kill and cs_reset. * or not to free the filename. Called by cs_kill and cs_reset.
*/ */
static void cs_release_csp(int i, int freefnpp) static void cs_release_csp(size_t i, int freefnpp)
{ {
/* /*
* Trying to exit normally (not sure whether it is fit to UNIX cscope * Trying to exit normally (not sure whether it is fit to UNIX cscope
@ -1963,8 +1970,7 @@ static void cs_release_csp(int i, int freefnpp)
static int cs_reset(exarg_T *eap) static int cs_reset(exarg_T *eap)
{ {
char **dblist = NULL, **pplist = NULL, **fllist = NULL; char **dblist = NULL, **pplist = NULL, **fllist = NULL;
int i; char buf[20]; /* for snprintf " (#%zu)" */
char buf[20]; /* for sprintf " (#%d)" */
if (csinfo_size == 0) if (csinfo_size == 0)
return CSCOPE_SUCCESS; return CSCOPE_SUCCESS;
@ -1974,7 +1980,7 @@ static int cs_reset(exarg_T *eap)
pplist = xmalloc(csinfo_size * sizeof(char *)); pplist = xmalloc(csinfo_size * sizeof(char *));
fllist = xmalloc(csinfo_size * sizeof(char *)); fllist = xmalloc(csinfo_size * sizeof(char *));
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
dblist[i] = csinfo[i].fname; dblist[i] = csinfo[i].fname;
pplist[i] = csinfo[i].ppath; pplist[i] = csinfo[i].ppath;
fllist[i] = csinfo[i].flags; fllist[i] = csinfo[i].flags;
@ -1983,7 +1989,7 @@ static int cs_reset(exarg_T *eap)
} }
/* rebuild the cscope connection list */ /* rebuild the cscope connection list */
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (dblist[i] != NULL) { if (dblist[i] != NULL) {
cs_add_common(dblist[i], pplist[i], fllist[i]); cs_add_common(dblist[i], pplist[i], fllist[i]);
if (p_csverbose) { if (p_csverbose) {
@ -1991,7 +1997,7 @@ static int cs_reset(exarg_T *eap)
* connection number in the same line as * connection number in the same line as
* "Added cscope database..." * "Added cscope database..."
*/ */
sprintf(buf, " (#%d)", i); snprintf(buf, ARRAY_SIZE(buf), " (#%zu)", i);
MSG_PUTS_ATTR(buf, hl_attr(HLF_R)); MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
} }
} }
@ -2020,7 +2026,7 @@ static int cs_reset(exarg_T *eap)
* ships with Solaris 2.6), the output never has the prefix prepended. * ships with Solaris 2.6), the output never has the prefix prepended.
* Contrast this with my development system (Digital Unix), which does. * Contrast this with my development system (Digital Unix), which does.
*/ */
static char *cs_resolve_file(int i, char *name) static char *cs_resolve_file(size_t i, char *name)
{ {
char *fullname; char *fullname;
char_u *csdir = NULL; char_u *csdir = NULL;
@ -2072,22 +2078,21 @@ static char *cs_resolve_file(int i, char *name)
*/ */
static int cs_show(exarg_T *eap) static int cs_show(exarg_T *eap)
{ {
short i;
if (cs_cnt_connections() == 0) if (cs_cnt_connections() == 0)
MSG_PUTS(_("no cscope connections\n")); MSG_PUTS(_("no cscope connections\n"));
else { else {
MSG_PUTS_ATTR( MSG_PUTS_ATTR(
_(" # pid database name prepend path\n"), _(" # pid database name prepend path\n"),
hl_attr(HLF_T)); hl_attr(HLF_T));
for (i = 0; i < csinfo_size; i++) { for (size_t i = 0; i < csinfo_size; i++) {
if (csinfo[i].fname == NULL) if (csinfo[i].fname == NULL)
continue; continue;
if (csinfo[i].ppath != NULL) if (csinfo[i].ppath != NULL)
(void)smsg("%2d %-5ld %-34s %-32s", (void)smsg("%2zu %-5" PRId64 " %-34s %-32s",
i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath); i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
else else
(void)smsg("%2d %-5ld %-34s <none>", (void)smsg("%2zu %-5" PRId64 " %-34s <none>",
i, (long)csinfo[i].pid, csinfo[i].fname); i, (long)csinfo[i].pid, csinfo[i].fname);
} }
} }
@ -2104,10 +2109,8 @@ static int cs_show(exarg_T *eap)
*/ */
void cs_end(void) void cs_end(void)
{ {
int i; for (size_t i = 0; i < csinfo_size; i++)
cs_release_csp(i, true);
for (i = 0; i < csinfo_size; i++)
cs_release_csp(i, TRUE);
xfree(csinfo); xfree(csinfo);
csinfo_size = 0; csinfo_size = 0;
} }