#include <curses.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "global.h"
#include "library.h"
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 selectionprompt[] = "Selection: ";
static char toprompt[] = "To: ";
static void scrollbar(MOUSEEVENT *p);
BOOL
command(int commandc)
{
char filename[PATHLEN + 1];
MOUSEEVENT *p;
int c, i;
FILE *file;
HISTORY *curritem, *item;
char *s;
switch (commandc) {
case ctrl('C'):
if (caseless == NO) {
caseless = YES;
putmsg2("Caseless mode is now ON");
} else {
caseless = NO;
putmsg2("Caseless mode is now OFF");
}
egrepcaseless(caseless);
return (NO);
case ctrl('R'):
if (isuptodate == YES) {
putmsg("The -d option prevents rebuilding the "
"symbol database");
return (NO);
}
exitcurses();
freefilelist();
makefilelist();
rebuild();
if (errorsfound == YES) {
errorsfound = NO;
askforreturn();
}
entercurses();
putmsg("");
totallines = 0;
topline = nextline = 1;
break;
case ctrl('X'):
if ((p = getmouseevent()) == 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 = mouseselection(p, FLDLINE, FIELDS);
setfield();
resetcmd();
return (NO);
}
break;
case '\t':
case '\n':
case '\r':
case ctrl('N'):
case KEY_DOWN:
case KEY_ENTER:
case KEY_RIGHT:
field = (field + 1) % FIELDS;
setfield();
resetcmd();
return (NO);
case ctrl('P'):
case KEY_UP:
case KEY_LEFT:
field = (field + (FIELDS - 1)) % FIELDS;
setfield();
resetcmd();
return (NO);
case KEY_HOME:
field = 0;
setfield();
resetcmd();
return (NO);
case KEY_LL:
field = FIELDS - 1;
setfield();
resetcmd();
return (NO);
case ' ':
case '+':
case ctrl('V'):
case KEY_NPAGE:
if (totallines == 0) {
return (NO);
}
break;
case '-':
case KEY_PPAGE:
if (totallines == 0) {
return (NO);
}
i = topline;
nextline = topline;
if (nextline > 1 && nextline <= mdisprefs) {
nextline = 1;
} else {
nextline -= mdisprefs;
if (nextline < 1) {
nextline = totallines - mdisprefs + 1;
if (nextline < 1) {
nextline = 1;
}
i = totallines + 1;
}
}
c = nextline;
for (;;) {
seekline(nextline);
display();
if (i - bottomline <= 0) {
break;
}
nextline = ++c;
}
return (NO);
case '>':
if (totallines == 0) {
putmsg("There are no lines to write to a file");
} else {
(void) move(PRLINE, 0);
(void) addstr("Write to file: ");
s = "w";
if ((c = mygetch()) == '>') {
(void) move(PRLINE, 0);
(void) addstr(appendprompt);
c = '\0';
s = "a";
}
if (c != '\r' && c != '\n' && c != KEY_ENTER &&
c != KEY_BREAK &&
getaline(newpat, COLS - sizeof (appendprompt), c,
NO) > 0) {
shellpath(filename, sizeof (filename), newpat);
if ((file = fopen(filename, s)) == NULL) {
cannotopen(filename);
} else {
seekline(1);
while ((c = getc(refsfound)) != EOF) {
(void) putc(c, file);
}
seekline(topline);
(void) fclose(file);
}
}
clearprompt();
}
return (NO);
case '<':
(void) move(PRLINE, 0);
(void) addstr(readprompt);
if (getaline(newpat, COLS - sizeof (readprompt), '\0',
NO) > 0) {
clearprompt();
shellpath(filename, sizeof (filename), newpat);
if (readrefs(filename) == NO) {
putmsg2("Ignoring an empty file");
return (NO);
}
return (YES);
}
clearprompt();
return (NO);
case '^':
case '|':
if (totallines == 0) {
putmsg("There are no lines to pipe to a shell command");
return (NO);
}
(void) move(PRLINE, 0);
(void) addstr(pipeprompt);
if (getaline(newpat,
COLS - sizeof (pipeprompt), '\0', NO) == 0) {
clearprompt();
return (NO);
}
if (commandc == '^') {
(void) strcat(strcat(newpat, " >"), temp2);
}
exitcurses();
if ((file = mypopen(newpat, "w")) == NULL) {
(void) fprintf(stderr,
"cscope: cannot open pipe to shell command: %s\n",
newpat);
} else {
seekline(1);
while ((c = getc(refsfound)) != EOF) {
(void) putc(c, file);
}
seekline(topline);
(void) mypclose(file);
}
if (commandc == '^') {
if (readrefs(temp2) == NO) {
putmsg("Ignoring empty output of ^ command");
}
}
askforreturn();
entercurses();
break;
case ctrl('L'):
case KEY_CLEAR:
(void) clearok(curscr, TRUE);
(void) wrefresh(curscr);
drawscrollbar(topline, bottomline, totallines);
return (NO);
case '!':
(void) execute(shell, shell, (char *)NULL);
seekline(topline);
break;
case '?':
(void) clear();
help();
(void) clear();
seekline(topline);
break;
case ctrl('E'):
editall();
break;
case ctrl('A'):
case ctrl('Y'):
if (*pattern != '\0') {
(void) addstr(pattern);
goto repeat;
}
break;
case ctrl('B'):
case ctrl('F'):
curritem = currentcmd();
item = (commandc == ctrl('F')) ? nextcmd() : prevcmd();
clearmsg2();
if (curritem == item) {
putmsg2(
"End of input field and search pattern history");
}
if (item) {
field = item->field;
setfield();
atfield();
(void) addstr(item->text);
(void) strcpy(pattern, item->text);
switch (c = mygetch()) {
case '\r':
case '\n':
case KEY_ENTER:
goto repeat;
default:
ungetch(c);
atfield();
(void) clrtoeol();
break;
}
}
return (NO);
case '\\':
(void) addch('\\');
if ((commandc = mygetch()) == EOF) {
return (NO);
}
(void) addstr("\b \b");
goto ispat;
case '.':
atfield();
default:
if (isdigit(commandc) && commandc != '0' && !mouse) {
if (returnrequired == NO) {
editref(commandc - '1');
} else {
(void) move(PRLINE, 0);
(void) addstr(selectionprompt);
if (getaline(newpat,
COLS - sizeof (selectionprompt), commandc,
NO) > 0 &&
(i = atoi(newpat)) > 0) {
editref(i - 1);
}
clearprompt();
}
} else if (isprint(commandc)) {
ispat:
if (getaline(newpat, COLS - fldcolumn - 1, commandc,
caseless) > 0) {
(void) strcpy(pattern, newpat);
resetcmd();
repeat:
addcmd(field, pattern);
if (field == CHANGE) {
(void) move(PRLINE, 0);
(void) addstr(toprompt);
(void) getaline(newpat,
COLS - sizeof (toprompt), '\0', NO);
}
if (search() == YES) {
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);
}
void
clearprompt(void)
{
(void) move(PRLINE, 0);
(void) clrtoeol();
}
BOOL
readrefs(char *filename)
{
FILE *file;
int c;
if ((file = fopen(filename, "r")) == NULL) {
cannotopen(filename);
return (NO);
}
if ((c = getc(file)) == EOF) {
return (NO);
}
totallines = 0;
nextline = 1;
if (writerefsfound() == YES) {
(void) putc(c, refsfound);
while ((c = getc(file)) != EOF) {
(void) putc(c, refsfound);
}
(void) fclose(file);
(void) freopen(temp1, "r", refsfound);
countrefs();
}
return (YES);
}
BOOL
changestring(void)
{
char buf[PATLEN + 1];
char newfile[PATHLEN + 1];
char oldfile[PATHLEN + 1];
char linenum[NUMLEN + 1];
char msg[MSGLEN + 1];
FILE *script;
BOOL anymarked = NO;
MOUSEEVENT *p;
int c, i;
char *s;
if ((script = fopen(temp2, "w")) == NULL) {
cannotopen(temp2);
return (NO);
}
change = (BOOL *)mycalloc((unsigned)totallines, sizeof (BOOL));
changing = YES;
initmenu();
for (;;) {
display();
same:
(void) move(PRLINE, 0);
(void) addstr(
"Select lines to change (press the ? key for help): ");
if ((c = mygetch()) == EOF || c == ctrl('D') ||
c == ctrl('Z')) {
break;
}
switch (c) {
case ' ':
case '+':
case ctrl('V'):
case KEY_NPAGE:
case '-':
case KEY_PPAGE:
case '!':
case '?':
(void) command(c);
break;
case ctrl('L'):
case KEY_CLEAR:
(void) command(c);
goto same;
case ESC:
case '\r':
case '\n':
case KEY_ENTER:
case KEY_BREAK:
case ctrl('G'):
clearprompt();
goto nochange;
case '*':
for (i = 0; topline + i < nextline; ++i) {
mark(i);
}
goto same;
case '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 = getmouseevent()) == 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:
if (isdigit(c) && c != '0' && !mouse) {
if (returnrequired == NO) {
mark(c - '1');
} else {
clearprompt();
(void) move(PRLINE, 0);
(void) addstr(selectionprompt);
if (getaline(buf,
COLS - sizeof (selectionprompt), c,
NO) > 0 &&
(i = atoi(buf)) > 0) {
mark(i - 1);
}
}
}
goto same;
}
}
(void) fprintf(script, "ed - <<\\!\nH\n");
*oldfile = '\0';
seekline(1);
for (i = 0; fscanf(refsfound, "%s%*s%s%*[^\n]", newfile, linenum) == 2;
++i) {
if (change[i] == YES) {
anymarked = YES;
if (strcmp(newfile, oldfile) != 0) {
if (access(newfile, WRITE) != 0) {
(void) sprintf(msg,
"Cannot write to file %s",
newfile);
putmsg(msg);
anymarked = NO;
break;
}
if (*oldfile != '\0') {
(void) fprintf(script,
"w\n");
}
(void) strcpy(oldfile, newfile);
(void) fprintf(script, "e %s\n", oldfile);
}
(void) fprintf(script,
"%ss/", linenum);
for (s = pattern; *s != '\0'; ++s) {
if (*s == '/') {
(void) putc('\\', script);
}
(void) putc(*s, script);
}
(void) putc('/', script);
for (s = newpat; *s != '\0'; ++s) {
if (strchr("/\\&", *s) != NULL) {
(void) putc('\\', script);
}
(void) putc(*s, script);
}
(void) fprintf(script, "/gp\n");
}
}
(void) fprintf(script, "w\nq\n!\n");
(void) fclose(script);
clearprompt();
if (anymarked == YES) {
(void) refresh();
(void) fprintf(stderr, "Changed lines:\n\r");
(void) execute(shell, shell, temp2, (char *)NULL);
askforreturn();
}
nochange:
changing = NO;
initmenu();
free(change);
seekline(topline);
return (YES);
}
void
mark(int i)
{
int j;
j = i + topline - 1;
if (j < totallines) {
(void) move(displine[i], selectlen);
if (change[j] == NO) {
change[j] = YES;
(void) addch('>');
} else {
change[j] = NO;
(void) addch(' ');
}
}
}
static void
scrollbar(MOUSEEVENT *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);
}