#include "global.h"
#include "build.h"
#include "alloc.h"
#include <stdlib.h>
#if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
#include <ncurses.h>
#else
#include <curses.h>
#endif
#include <ctype.h>
static char const rcsid[] = "$Id: command.c,v 1.32 2006/08/20 15:00:33 broeker Exp $";
int selecting;
unsigned int curdispline = 0;
BOOL caseless;
BOOL *change;
BOOL changing;
char newpat[PATLEN + 1];
char Pattern[PATLEN + 1];
static char appendprompt[] = "Append to file: ";
static char pipeprompt[] = "Pipe to shell command: ";
static char readprompt[] = "Read from file: ";
static char toprompt[] = "To: ";
static BOOL changestring(void);
static void clearprompt(void);
static void mark(unsigned int i);
static void scrollbar(MOUSE *p);
BOOL
command(int commandc)
{
char filename[PATHLEN + 1];
MOUSE *p;
int c, i;
FILE *file;
struct cmd *curritem, *item;
char *s;
switch (commandc) {
case ctrl('C'):
if (caseless == NO) {
caseless = YES;
postmsg2("Caseless mode is now ON");
} else {
caseless = NO;
postmsg2("Caseless mode is now OFF");
}
egrepcaseless(caseless);
return(NO);
case ctrl('R'):
if (isuptodate == YES) {
postmsg("The -d option prevents rebuilding the symbol database");
return(NO);
}
exitcurses();
freefilelist();
makefilelist();
rebuild();
if (errorsfound == YES) {
errorsfound = NO;
askforreturn();
}
entercurses();
clearmsg();
totallines = 0;
disprefs = 0;
topline = nextline = 1;
selecting = 0;
break;
#if UNIXPC
case ESC:
#endif
case ctrl('X'):
if ((p = getmouseaction(DUMMYCHAR)) == NULL) {
return(NO);
}
if (p->button == '0') {
scrollbar(p);
break;
}
if (p->x2 >= 0) {
return(NO);
}
if (p->y1 < FLDLINE) {
for (i = disprefs - 1; i > 0; --i) {
if (p->y1 >= displine[i]) {
break;
}
}
editref(i);
} else {
field = p->y1 - FLDLINE;
if (field >= FIELDS) {
field = FIELDS - 1;
}
setfield();
resetcmd();
return(NO);
}
break;
case '\t':
if (disprefs) {
selecting = !selecting;
if (selecting) {
move(displine[curdispline], 0);
refresh();
} else {
atfield();
resetcmd();
}
}
return(NO);
#ifdef KEY_ENTER
case KEY_ENTER:
#endif
case '\r':
case '\n':
if (selecting) {
editref(curdispline);
return(YES);
}
case ctrl('N'):
#ifdef KEY_DOWN
case KEY_DOWN:
#endif
#ifdef KEY_RIGHT
case KEY_RIGHT:
#endif
if (selecting) {
if ((curdispline + 1) < disprefs) {
move(displine[++curdispline], 0);
refresh();
}
} else {
field = (field + 1) % FIELDS;
setfield();
atfield();
resetcmd();
}
return(NO);
case ctrl('P'):
#ifdef KEY_UP
case KEY_UP:
#endif
#ifdef KEY_LEFT
case KEY_LEFT:
#endif
if (selecting) {
if (curdispline) {
move(displine[--curdispline], 0);
refresh();
}
} else {
field = (field + (FIELDS - 1)) % FIELDS;
setfield();
atfield();
resetcmd();
}
return(NO);
#ifdef KEY_HOME
case KEY_HOME:
if (selecting) {
curdispline = 0;
move(REFLINE, 0);
refresh();
} else {
field = 0;
setfield();
atfield();
resetcmd();
}
return(NO);
#endif
#ifdef KEY_LL
case KEY_LL:
if (selecting) {
move(displine[disprefs - 1], 0);
refresh();
} else {
field = FIELDS - 1;
setfield();
atfield();
resetcmd();
}
return(NO);
#endif
case ' ':
case '+':
case ctrl('V'):
#ifdef KEY_NPAGE
case KEY_NPAGE:
#endif
if (totallines == 0) {
return(NO);
}
curdispline = 0;
break;
case ctrl('H'):
case '-':
#ifdef KEY_PPAGE
case KEY_PPAGE:
#endif
if (totallines == 0) {
return(NO);
}
curdispline = 0;
if (totallines <= 2 * mdisprefs) {
break;
}
nextline -= mdisprefs;
if (nextline > 1 && nextline <= mdisprefs) {
nextline = 1;
} else {
nextline -= mdisprefs;
if (nextline < 1) {
nextline = totallines - mdisprefs + 1;
if (nextline < 1) {
nextline = 1;
}
}
}
seekline(nextline);
break;
case '>':
if (totallines == 0) {
postmsg("There are no lines to write to a file");
} else {
move(PRLINE, 0);
addstr("Write to file: ");
s = "w";
if ((c = mygetch()) == '>') {
move(PRLINE, 0);
addstr(appendprompt);
c = '\0';
s = "a";
}
if (c != '\r' &&
mygetline("", newpat,
COLS - sizeof(appendprompt), c, NO) > 0
) {
shellpath(filename, sizeof(filename), newpat);
if ((file = myfopen(filename, s)) == NULL) {
cannotopen(filename);
} else {
seekline(1);
while ((c = getc(refsfound)) != EOF) {
putc(c, file);
}
seekline(topline);
fclose(file);
}
}
clearprompt();
}
return(NO);
case '<':
move(PRLINE, 0);
addstr(readprompt);
if (mygetline("", newpat, COLS - sizeof(readprompt),
'\0', NO) > 0) {
clearprompt();
shellpath(filename, sizeof(filename), newpat);
if (readrefs(filename) == NO) {
postmsg2("Ignoring an empty file");
return(NO);
}
return(YES);
}
clearprompt();
return(NO);
case '^':
case '|':
if (totallines == 0) {
postmsg("There are no lines to pipe to a shell command");
return(NO);
}
move(PRLINE, 0);
addstr(pipeprompt);
if (mygetline("", newpat, COLS - sizeof(pipeprompt), '\0', NO)
== 0) {
clearprompt();
return(NO);
}
if (commandc == '^') {
strcat(strcat(newpat, " >"), temp2);
remove(temp2);
}
exitcurses();
if ((file = mypopen(newpat, "w")) == NULL) {
fprintf(stderr, "\
cscope: cannot open pipe to shell command: %s\n", newpat);
} else {
seekline(1);
while ((c = getc(refsfound)) != EOF) {
putc(c, file);
}
seekline(topline);
mypclose(file);
}
if (commandc == '^') {
if (readrefs(temp2) == NO) {
postmsg("Ignoring empty output of ^ command");
}
}
askforreturn();
entercurses();
break;
#if defined(KEY_RESIZE) && !defined(__DJGPP__)
case KEY_RESIZE:
exitcurses();
initscr();
entercurses();
#if TERMINFO
keypad(stdscr, TRUE);
#ifdef HAVE_FIXKEYPAD
fixkeypad();
#endif
#endif
#if UNIXPC
standend();
#endif
dispinit();
setfield();
postmsg("");
display();
break;
#endif
case ctrl('L'):
#ifdef KEY_CLEAR
case KEY_CLEAR:
#endif
clearmsg2();
clearok(curscr, TRUE);
wrefresh(curscr);
drawscrollbar(topline, bottomline);
return(NO);
case '!':
execute(shell, shell, NULL);
seekline(topline);
break;
case '?':
clear();
help();
clear();
seekline(topline);
break;
case ctrl('E'):
editall();
break;
case ctrl('A'):
case ctrl('Y'):
if (*Pattern != '\0') {
addstr(Pattern);
goto repeat;
}
break;
case ctrl('B'):
case ctrl('F'):
if (selecting)
return(NO);
curritem = currentcmd();
item = (commandc == ctrl('F')) ? nextcmd() : prevcmd();
clearmsg2();
if (curritem == item) {
postmsg2("End of input field and search pattern history");
}
if (item) {
field = item->field;
setfield();
atfield();
addstr(item->text);
strcpy(Pattern, item->text);
switch (c = mygetch()) {
case '\r':
case '\n':
goto repeat;
case ctrl('F'):
case ctrl('B'):
myungetch(c);
atfield();
clrtoeol();
break;
default:
myungetch(c);
if (mygetline(Pattern, newpat, COLS - fldcolumn - 1, '\0', caseless )) {
strcpy (Pattern, newpat);
resetcmd();
}
goto repeat;
break;
}
}
return(NO);
case '\\':
addch('\\');
if ((commandc = mygetch()) == EOF) {
return(NO);
}
addstr("\b \b");
goto ispat;
case '.':
postmsg("The . command has been replaced by ^Y");
atfield();
default:
if (selecting && !mouse) {
char *c;
if ((c = strchr(dispchars, commandc)))
editref(c - dispchars);
} else if (isprint(commandc)) {
ispat:
if (mygetline("", newpat, COLS - fldcolumn - 1,
commandc, caseless) > 0) {
strcpy(Pattern, newpat);
resetcmd();
repeat:
addcmd(field, Pattern);
if (field == CHANGE) {
move(PRLINE, 0);
addstr(toprompt);
mygetline("", newpat,
COLS - sizeof(toprompt),
'\0', NO);
}
if (search() == YES) {
curdispline = 0;
++selecting;
switch (field) {
case DEFINITION:
case FILENAME:
if (totallines > 1) {
break;
}
topline = 1;
editref(0);
break;
case CHANGE:
return(changestring());
}
} else if (field == FILENAME &&
access(newpat, READ) == 0) {
edit(newpat, "1");
}
} else {
return(NO);
}
} else {
return(NO);
}
}
return(YES);
}
static void
clearprompt(void)
{
move(PRLINE, 0);
clrtoeol();
}
BOOL
readrefs(char *filename)
{
FILE *file;
int c;
if ((file = myfopen(filename, "rb")) == NULL) {
cannotopen(filename);
return(NO);
}
if ((c = getc(file)) == EOF) {
return(NO);
}
totallines = 0;
disprefs = 0;
nextline = 1;
if (writerefsfound() == YES) {
putc(c, refsfound);
while ((c = getc(file)) != EOF) {
putc(c, refsfound);
}
fclose(file);
fclose(refsfound);
if ( (refsfound = myfopen(temp1, "rb")) == NULL) {
cannotopen(temp1);
return(NO);
}
countrefs();
}
return(YES);
}
static BOOL
changestring(void)
{
char newfile[PATHLEN + 1];
char oldfile[PATHLEN + 1];
char linenum[NUMLEN + 1];
char msg[MSGLEN + 1];
FILE *script;
BOOL anymarked = NO;
MOUSE *p;
int c;
unsigned int i;
char *s;
if ((script = myfopen(temp2, "w")) == NULL) {
cannotopen(temp2);
return(NO);
}
change = mycalloc(totallines, sizeof(BOOL));
changing = YES;
mousemenu();
for (;;) {
display();
same:
atchange();
if ((c = mygetch()) == EOF
|| c == ctrl('D')
|| c == ctrl('Z')) {
break;
}
switch (c) {
case ' ':
case '+':
case ctrl('V'):
#ifdef KEY_NPAGE
case KEY_NPAGE:
#endif
case '-':
#ifdef KEY_PPAGE
case KEY_PPAGE:
#endif
case '!':
case '?':
command(c);
break;
case ctrl('L'):
#ifdef KEY_CLEAR
case KEY_CLEAR:
#endif
command(c);
goto same;
case ESC:
#if UNIXPC
if((p = getmouseaction(DUMMYCHAR)) == NULL) {
goto nochange;
}
break;
#endif
case ctrl('G'):
goto nochange;
case '*':
for (i = 0; topline + i < nextline; ++i) {
mark(i);
}
goto same;
case ctrl('A'):
for (i = 0; i < totallines; ++i) {
if (change[i] == NO) {
change[i] = YES;
} else {
change[i] = NO;
}
}
seekline(totallines);
break;
case ctrl('X'):
if ((p = getmouseaction(DUMMYCHAR)) == NULL) {
goto same;
}
if (p->button == '0') {
scrollbar(p);
break;
}
for (i = disprefs - 1; i > 0; --i) {
if (p->y1 >= displine[i]) {
break;
}
}
mark(i);
goto same;
default:
{
char *cc;
if ((cc = strchr(dispchars, c)))
mark(cc - dispchars);
goto same;
}
}
}
fprintf(script, "ed - <<\\!\n");
*oldfile = '\0';
seekline(1);
for (i = 0;
fscanf(refsfound, "%" PATHLEN_STR "s%*s%" NUMLEN_STR "s%*[^\n]", newfile, linenum) == 2;
++i) {
if (change[i] == YES) {
anymarked = YES;
if (strcmp(newfile, oldfile) != 0) {
if (access(newfile, WRITE) != 0) {
sprintf(msg, "Cannot write to file %s", newfile);
postmsg(msg);
anymarked = NO;
break;
}
if (*oldfile != '\0') {
fprintf(script, "w\n");
}
strcpy(oldfile, newfile);
fprintf(script, "e %s\n", oldfile);
}
fprintf(script, "%ss/", linenum);
for (s = Pattern; *s != '\0'; ++s) {
if (strchr("/\\[.^*", *s) != NULL) {
putc('\\', script);
}
if (caseless == YES && isalpha((unsigned char)*s)) {
putc('[', script);
if(islower((unsigned char)*s)) {
putc(toupper((unsigned char)*s), script);
putc(*s, script);
} else {
putc(*s, script);
putc(tolower((unsigned char)*s), script);
}
putc(']', script);
} else
putc(*s, script);
}
putc('/', script);
for (s = newpat; *s != '\0'; ++s) {
if (strchr("/\\&", *s) != NULL) {
putc('\\', script);
}
putc(*s, script);
}
fprintf(script, "/gp\n");
}
}
fprintf(script, "w\nq\n!\n");
fclose(script);
if (anymarked == YES) {
clearprompt();
refresh();
fprintf(stderr, "Changed lines:\n\r");
execute("sh", "sh", temp2, NULL);
askforreturn();
seekline(1);
} else {
nochange:
clearprompt();
}
changing = NO;
mousemenu();
free(change);
return(anymarked);
}
static void
mark(unsigned int i)
{
unsigned int j;
j = i + topline - 1;
if (j < totallines) {
move(displine[i], 1);
if (change[j] == NO) {
change[j] = YES;
addch('>');
} else {
change[j] = NO;
addch(' ');
}
}
}
static void
scrollbar(MOUSE *p)
{
if (totallines == 0) {
return;
}
switch (p->percent) {
case 101:
if (nextline + mdisprefs > totallines) {
nextline = totallines - mdisprefs + 1;
}
break;
case 102:
nextline = topline - mdisprefs;
if (nextline < 1) {
nextline = 1;
}
break;
case 103:
nextline = topline + 1;
break;
case 104:
if (topline > 1) {
nextline = topline - 1;
}
break;
default:
nextline = p->percent * totallines / 100;
}
seekline(nextline);
}
void
countrefs(void)
{
char *subsystem;
char *book;
char file[PATHLEN + 1];
char function[PATLEN + 1];
char linenum[NUMLEN + 1];
int i;
subsystemlen = 9;
booklen = 4;
filelen = 4;
fcnlen = 8;
numlen = 0;
while ((i = fscanf(refsfound, "%250s%250s%5s %5000[^\n]", file,
function, linenum, tempstring)) != EOF) {
if (i != 4 ||
!isgraph((unsigned char) *file) ||
!isgraph((unsigned char) *function) ||
!isdigit((unsigned char) *linenum)) {
postmsg("File does not have expected format");
totallines = 0;
disprefs = 0;
return;
}
if ((i = strlen(pathcomponents(file, dispcomponents))) > filelen) {
filelen = i;
}
if (ogs == YES) {
ogsnames(file, &subsystem, &book);
if ((i = strlen(subsystem)) > subsystemlen) {
subsystemlen = i;
}
if ((i = strlen(book)) > booklen) {
booklen = i;
}
}
if ((i = strlen(function)) > fcnlen) {
fcnlen = i;
}
if ((i = strlen(linenum)) > numlen) {
numlen = i;
}
++totallines;
}
rewind(refsfound);
i = (COLS - 5) / 3;
if (ogs == YES) {
i = (COLS - 7) / 5;
}
if (filelen > i && i > 4) {
filelen = i;
}
if (subsystemlen > i && i > 9) {
subsystemlen = i;
}
if (booklen > i && i > 4) {
booklen = i;
}
if (fcnlen > i && i > 8) {
fcnlen = i;
}
}