1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc. 28*0Sstevel@tonic-gate * All rights reserved. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * cscope - interactive C symbol or text cross-reference 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * command functions 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include <curses.h> /* KEY_.* */ 40*0Sstevel@tonic-gate #include <fcntl.h> /* O_RDONLY */ 41*0Sstevel@tonic-gate #include <unistd.h> 42*0Sstevel@tonic-gate #include <stdio.h> 43*0Sstevel@tonic-gate #include "global.h" 44*0Sstevel@tonic-gate #include "library.h" 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate BOOL caseless; /* ignore letter case when searching */ 47*0Sstevel@tonic-gate BOOL *change; /* change this line */ 48*0Sstevel@tonic-gate BOOL changing; /* changing text */ 49*0Sstevel@tonic-gate char newpat[PATLEN + 1]; /* new pattern */ 50*0Sstevel@tonic-gate char pattern[PATLEN + 1]; /* symbol or text pattern */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate static char appendprompt[] = "Append to file: "; 53*0Sstevel@tonic-gate static char pipeprompt[] = "Pipe to shell command: "; 54*0Sstevel@tonic-gate static char readprompt[] = "Read from file: "; 55*0Sstevel@tonic-gate static char selectionprompt[] = "Selection: "; 56*0Sstevel@tonic-gate static char toprompt[] = "To: "; 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate static void scrollbar(MOUSEEVENT *p); 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate /* execute the command */ 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate BOOL 63*0Sstevel@tonic-gate command(int commandc) 64*0Sstevel@tonic-gate { 65*0Sstevel@tonic-gate char filename[PATHLEN + 1]; /* file path name */ 66*0Sstevel@tonic-gate MOUSEEVENT *p; /* mouse data */ 67*0Sstevel@tonic-gate int c, i; 68*0Sstevel@tonic-gate FILE *file; 69*0Sstevel@tonic-gate HISTORY *curritem, *item; /* command history */ 70*0Sstevel@tonic-gate char *s; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate switch (commandc) { 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate case ctrl('C'): /* toggle caseless mode */ 75*0Sstevel@tonic-gate if (caseless == NO) { 76*0Sstevel@tonic-gate caseless = YES; 77*0Sstevel@tonic-gate putmsg2("Caseless mode is now ON"); 78*0Sstevel@tonic-gate } else { 79*0Sstevel@tonic-gate caseless = NO; 80*0Sstevel@tonic-gate putmsg2("Caseless mode is now OFF"); 81*0Sstevel@tonic-gate } 82*0Sstevel@tonic-gate egrepcaseless(caseless); /* turn on/off -i flag */ 83*0Sstevel@tonic-gate return (NO); 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate case ctrl('R'): /* rebuild the cross reference */ 86*0Sstevel@tonic-gate if (isuptodate == YES) { 87*0Sstevel@tonic-gate putmsg("The -d option prevents rebuilding the " 88*0Sstevel@tonic-gate "symbol database"); 89*0Sstevel@tonic-gate return (NO); 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate exitcurses(); 92*0Sstevel@tonic-gate freefilelist(); /* remake the source file list */ 93*0Sstevel@tonic-gate makefilelist(); 94*0Sstevel@tonic-gate rebuild(); 95*0Sstevel@tonic-gate if (errorsfound == YES) { 96*0Sstevel@tonic-gate errorsfound = NO; 97*0Sstevel@tonic-gate askforreturn(); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate entercurses(); 100*0Sstevel@tonic-gate putmsg(""); /* clear any previous message */ 101*0Sstevel@tonic-gate totallines = 0; 102*0Sstevel@tonic-gate topline = nextline = 1; 103*0Sstevel@tonic-gate break; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate case ctrl('X'): /* mouse selection */ 106*0Sstevel@tonic-gate if ((p = getmouseevent()) == NULL) { 107*0Sstevel@tonic-gate return (NO); /* unknown control sequence */ 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate /* if the button number is a scrollbar tag */ 110*0Sstevel@tonic-gate if (p->button == '0') { 111*0Sstevel@tonic-gate scrollbar(p); 112*0Sstevel@tonic-gate break; 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate /* ignore a sweep */ 115*0Sstevel@tonic-gate if (p->x2 >= 0) { 116*0Sstevel@tonic-gate return (NO); 117*0Sstevel@tonic-gate } 118*0Sstevel@tonic-gate /* if this is a line selection */ 119*0Sstevel@tonic-gate if (p->y1 < FLDLINE) { 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* find the selected line */ 122*0Sstevel@tonic-gate /* note: the selection is forced into range */ 123*0Sstevel@tonic-gate for (i = disprefs - 1; i > 0; --i) { 124*0Sstevel@tonic-gate if (p->y1 >= displine[i]) { 125*0Sstevel@tonic-gate break; 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate /* display it in the file with the editor */ 129*0Sstevel@tonic-gate editref(i); 130*0Sstevel@tonic-gate } else { /* this is an input field selection */ 131*0Sstevel@tonic-gate field = mouseselection(p, FLDLINE, FIELDS); 132*0Sstevel@tonic-gate setfield(); 133*0Sstevel@tonic-gate resetcmd(); 134*0Sstevel@tonic-gate return (NO); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate break; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate case '\t': /* go to next input field */ 139*0Sstevel@tonic-gate case '\n': 140*0Sstevel@tonic-gate case '\r': 141*0Sstevel@tonic-gate case ctrl('N'): 142*0Sstevel@tonic-gate case KEY_DOWN: 143*0Sstevel@tonic-gate case KEY_ENTER: 144*0Sstevel@tonic-gate case KEY_RIGHT: 145*0Sstevel@tonic-gate field = (field + 1) % FIELDS; 146*0Sstevel@tonic-gate setfield(); 147*0Sstevel@tonic-gate resetcmd(); 148*0Sstevel@tonic-gate return (NO); 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate case ctrl('P'): /* go to previous input field */ 151*0Sstevel@tonic-gate case KEY_UP: 152*0Sstevel@tonic-gate case KEY_LEFT: 153*0Sstevel@tonic-gate field = (field + (FIELDS - 1)) % FIELDS; 154*0Sstevel@tonic-gate setfield(); 155*0Sstevel@tonic-gate resetcmd(); 156*0Sstevel@tonic-gate return (NO); 157*0Sstevel@tonic-gate case KEY_HOME: /* go to first input field */ 158*0Sstevel@tonic-gate field = 0; 159*0Sstevel@tonic-gate setfield(); 160*0Sstevel@tonic-gate resetcmd(); 161*0Sstevel@tonic-gate return (NO); 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate case KEY_LL: /* go to last input field */ 164*0Sstevel@tonic-gate field = FIELDS - 1; 165*0Sstevel@tonic-gate setfield(); 166*0Sstevel@tonic-gate resetcmd(); 167*0Sstevel@tonic-gate return (NO); 168*0Sstevel@tonic-gate case ' ': /* display next page */ 169*0Sstevel@tonic-gate case '+': 170*0Sstevel@tonic-gate case ctrl('V'): 171*0Sstevel@tonic-gate case KEY_NPAGE: 172*0Sstevel@tonic-gate /* don't redisplay if there are no lines */ 173*0Sstevel@tonic-gate if (totallines == 0) { 174*0Sstevel@tonic-gate return (NO); 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate /* 177*0Sstevel@tonic-gate * note: seekline() is not used to move to the next 178*0Sstevel@tonic-gate * page because display() leaves the file pointer at 179*0Sstevel@tonic-gate * the next page to optimize paging forward 180*0Sstevel@tonic-gate */ 181*0Sstevel@tonic-gate break; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate case '-': /* display previous page */ 184*0Sstevel@tonic-gate case KEY_PPAGE: 185*0Sstevel@tonic-gate /* don't redisplay if there are no lines */ 186*0Sstevel@tonic-gate if (totallines == 0) { 187*0Sstevel@tonic-gate return (NO); 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate i = topline; /* save the current top line */ 190*0Sstevel@tonic-gate nextline = topline; /* go back to this page */ 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* if on first page but not at beginning, go to beginning */ 193*0Sstevel@tonic-gate if (nextline > 1 && nextline <= mdisprefs) { 194*0Sstevel@tonic-gate nextline = 1; 195*0Sstevel@tonic-gate } else { /* go back the maximum displayable lines */ 196*0Sstevel@tonic-gate nextline -= mdisprefs; 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate /* if this was the first page, go to the last page */ 199*0Sstevel@tonic-gate if (nextline < 1) { 200*0Sstevel@tonic-gate nextline = totallines - mdisprefs + 1; 201*0Sstevel@tonic-gate if (nextline < 1) { 202*0Sstevel@tonic-gate nextline = 1; 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate /* old top is past last line */ 205*0Sstevel@tonic-gate i = totallines + 1; 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate /* 209*0Sstevel@tonic-gate * move down til the bottom line is just before the 210*0Sstevel@tonic-gate * previous top line 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate c = nextline; 213*0Sstevel@tonic-gate for (;;) { 214*0Sstevel@tonic-gate seekline(nextline); 215*0Sstevel@tonic-gate display(); 216*0Sstevel@tonic-gate if (i - bottomline <= 0) { 217*0Sstevel@tonic-gate break; 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate nextline = ++c; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate return (NO); /* display already up to date */ 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate case '>': /* write or append the lines to a file */ 224*0Sstevel@tonic-gate if (totallines == 0) { 225*0Sstevel@tonic-gate putmsg("There are no lines to write to a file"); 226*0Sstevel@tonic-gate } else { /* get the file name */ 227*0Sstevel@tonic-gate (void) move(PRLINE, 0); 228*0Sstevel@tonic-gate (void) addstr("Write to file: "); 229*0Sstevel@tonic-gate s = "w"; 230*0Sstevel@tonic-gate if ((c = mygetch()) == '>') { 231*0Sstevel@tonic-gate (void) move(PRLINE, 0); 232*0Sstevel@tonic-gate (void) addstr(appendprompt); 233*0Sstevel@tonic-gate c = '\0'; 234*0Sstevel@tonic-gate s = "a"; 235*0Sstevel@tonic-gate } 236*0Sstevel@tonic-gate if (c != '\r' && c != '\n' && c != KEY_ENTER && 237*0Sstevel@tonic-gate c != KEY_BREAK && 238*0Sstevel@tonic-gate getline(newpat, COLS - sizeof (appendprompt), c, 239*0Sstevel@tonic-gate NO) > 0) { 240*0Sstevel@tonic-gate shellpath(filename, sizeof (filename), newpat); 241*0Sstevel@tonic-gate if ((file = fopen(filename, s)) == NULL) { 242*0Sstevel@tonic-gate cannotopen(filename); 243*0Sstevel@tonic-gate } else { 244*0Sstevel@tonic-gate seekline(1); 245*0Sstevel@tonic-gate while ((c = getc(refsfound)) != EOF) { 246*0Sstevel@tonic-gate (void) putc(c, file); 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate seekline(topline); 249*0Sstevel@tonic-gate (void) fclose(file); 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate clearprompt(); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate return (NO); /* return to the previous field */ 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate case '<': /* read lines from a file */ 257*0Sstevel@tonic-gate (void) move(PRLINE, 0); 258*0Sstevel@tonic-gate (void) addstr(readprompt); 259*0Sstevel@tonic-gate if (getline(newpat, COLS - sizeof (readprompt), '\0', 260*0Sstevel@tonic-gate NO) > 0) { 261*0Sstevel@tonic-gate clearprompt(); 262*0Sstevel@tonic-gate shellpath(filename, sizeof (filename), newpat); 263*0Sstevel@tonic-gate if (readrefs(filename) == NO) { 264*0Sstevel@tonic-gate putmsg2("Ignoring an empty file"); 265*0Sstevel@tonic-gate return (NO); 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate return (YES); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate clearprompt(); 270*0Sstevel@tonic-gate return (NO); 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate case '^': /* pipe the lines through a shell command */ 273*0Sstevel@tonic-gate case '|': /* pipe the lines to a shell command */ 274*0Sstevel@tonic-gate if (totallines == 0) { 275*0Sstevel@tonic-gate putmsg("There are no lines to pipe to a shell command"); 276*0Sstevel@tonic-gate return (NO); 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate /* get the shell command */ 279*0Sstevel@tonic-gate (void) move(PRLINE, 0); 280*0Sstevel@tonic-gate (void) addstr(pipeprompt); 281*0Sstevel@tonic-gate if (getline(newpat, 282*0Sstevel@tonic-gate COLS - sizeof (pipeprompt), '\0', NO) == 0) { 283*0Sstevel@tonic-gate clearprompt(); 284*0Sstevel@tonic-gate return (NO); 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate /* if the ^ command, redirect output to a temp file */ 287*0Sstevel@tonic-gate if (commandc == '^') { 288*0Sstevel@tonic-gate (void) strcat(strcat(newpat, " >"), temp2); 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate exitcurses(); 291*0Sstevel@tonic-gate if ((file = mypopen(newpat, "w")) == NULL) { 292*0Sstevel@tonic-gate (void) fprintf(stderr, 293*0Sstevel@tonic-gate "cscope: cannot open pipe to shell command: %s\n", 294*0Sstevel@tonic-gate newpat); 295*0Sstevel@tonic-gate } else { 296*0Sstevel@tonic-gate seekline(1); 297*0Sstevel@tonic-gate while ((c = getc(refsfound)) != EOF) { 298*0Sstevel@tonic-gate (void) putc(c, file); 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate seekline(topline); 301*0Sstevel@tonic-gate (void) mypclose(file); 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate if (commandc == '^') { 304*0Sstevel@tonic-gate if (readrefs(temp2) == NO) { 305*0Sstevel@tonic-gate putmsg("Ignoring empty output of ^ command"); 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate askforreturn(); 309*0Sstevel@tonic-gate entercurses(); 310*0Sstevel@tonic-gate break; 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate case ctrl('L'): /* redraw screen */ 313*0Sstevel@tonic-gate case KEY_CLEAR: 314*0Sstevel@tonic-gate (void) clearok(curscr, TRUE); 315*0Sstevel@tonic-gate (void) wrefresh(curscr); 316*0Sstevel@tonic-gate drawscrollbar(topline, bottomline, totallines); 317*0Sstevel@tonic-gate return (NO); 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate case '!': /* shell escape */ 320*0Sstevel@tonic-gate (void) execute(shell, shell, (char *)NULL); 321*0Sstevel@tonic-gate seekline(topline); 322*0Sstevel@tonic-gate break; 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate case '?': /* help */ 325*0Sstevel@tonic-gate (void) clear(); 326*0Sstevel@tonic-gate help(); 327*0Sstevel@tonic-gate (void) clear(); 328*0Sstevel@tonic-gate seekline(topline); 329*0Sstevel@tonic-gate break; 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate case ctrl('E'): /* edit all lines */ 332*0Sstevel@tonic-gate editall(); 333*0Sstevel@tonic-gate break; 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate case ctrl('A'): /* repeat last pattern */ 336*0Sstevel@tonic-gate case ctrl('Y'): /* (old command) */ 337*0Sstevel@tonic-gate if (*pattern != '\0') { 338*0Sstevel@tonic-gate (void) addstr(pattern); 339*0Sstevel@tonic-gate goto repeat; 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate break; 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate case ctrl('B'): /* cmd history back */ 344*0Sstevel@tonic-gate case ctrl('F'): /* cmd history fwd */ 345*0Sstevel@tonic-gate curritem = currentcmd(); 346*0Sstevel@tonic-gate item = (commandc == ctrl('F')) ? nextcmd() : prevcmd(); 347*0Sstevel@tonic-gate clearmsg2(); 348*0Sstevel@tonic-gate if (curritem == item) { 349*0Sstevel@tonic-gate /* inform user that we're at history end */ 350*0Sstevel@tonic-gate putmsg2( 351*0Sstevel@tonic-gate "End of input field and search pattern history"); 352*0Sstevel@tonic-gate } 353*0Sstevel@tonic-gate if (item) { 354*0Sstevel@tonic-gate field = item->field; 355*0Sstevel@tonic-gate setfield(); 356*0Sstevel@tonic-gate atfield(); 357*0Sstevel@tonic-gate (void) addstr(item->text); 358*0Sstevel@tonic-gate (void) strcpy(pattern, item->text); 359*0Sstevel@tonic-gate switch (c = mygetch()) { 360*0Sstevel@tonic-gate case '\r': 361*0Sstevel@tonic-gate case '\n': 362*0Sstevel@tonic-gate case KEY_ENTER: 363*0Sstevel@tonic-gate goto repeat; 364*0Sstevel@tonic-gate default: 365*0Sstevel@tonic-gate ungetch(c); 366*0Sstevel@tonic-gate atfield(); 367*0Sstevel@tonic-gate (void) clrtoeol(); /* clear current field */ 368*0Sstevel@tonic-gate break; 369*0Sstevel@tonic-gate } 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate return (NO); 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate case '\\': /* next character is not a command */ 374*0Sstevel@tonic-gate (void) addch('\\'); /* display the quote character */ 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate /* get a character from the terminal */ 377*0Sstevel@tonic-gate if ((commandc = mygetch()) == EOF) { 378*0Sstevel@tonic-gate return (NO); /* quit */ 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate (void) addstr("\b \b"); /* erase the quote character */ 381*0Sstevel@tonic-gate goto ispat; 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate case '.': 384*0Sstevel@tonic-gate atfield(); /* move back to the input field */ 385*0Sstevel@tonic-gate /* FALLTHROUGH */ 386*0Sstevel@tonic-gate default: 387*0Sstevel@tonic-gate /* edit a selected line */ 388*0Sstevel@tonic-gate if (isdigit(commandc) && commandc != '0' && !mouse) { 389*0Sstevel@tonic-gate if (returnrequired == NO) { 390*0Sstevel@tonic-gate editref(commandc - '1'); 391*0Sstevel@tonic-gate } else { 392*0Sstevel@tonic-gate (void) move(PRLINE, 0); 393*0Sstevel@tonic-gate (void) addstr(selectionprompt); 394*0Sstevel@tonic-gate if (getline(newpat, 395*0Sstevel@tonic-gate COLS - sizeof (selectionprompt), commandc, 396*0Sstevel@tonic-gate NO) > 0 && 397*0Sstevel@tonic-gate (i = atoi(newpat)) > 0) { 398*0Sstevel@tonic-gate editref(i - 1); 399*0Sstevel@tonic-gate } 400*0Sstevel@tonic-gate clearprompt(); 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate } else if (isprint(commandc)) { 403*0Sstevel@tonic-gate /* this is the start of a pattern */ 404*0Sstevel@tonic-gate ispat: 405*0Sstevel@tonic-gate if (getline(newpat, COLS - fldcolumn - 1, commandc, 406*0Sstevel@tonic-gate caseless) > 0) { 407*0Sstevel@tonic-gate (void) strcpy(pattern, newpat); 408*0Sstevel@tonic-gate resetcmd(); /* reset history */ 409*0Sstevel@tonic-gate repeat: 410*0Sstevel@tonic-gate addcmd(field, pattern); /* add to history */ 411*0Sstevel@tonic-gate if (field == CHANGE) { 412*0Sstevel@tonic-gate /* prompt for the new text */ 413*0Sstevel@tonic-gate (void) move(PRLINE, 0); 414*0Sstevel@tonic-gate (void) addstr(toprompt); 415*0Sstevel@tonic-gate (void) getline(newpat, 416*0Sstevel@tonic-gate COLS - sizeof (toprompt), '\0', NO); 417*0Sstevel@tonic-gate } 418*0Sstevel@tonic-gate /* search for the pattern */ 419*0Sstevel@tonic-gate if (search() == YES) { 420*0Sstevel@tonic-gate switch (field) { 421*0Sstevel@tonic-gate case DEFINITION: 422*0Sstevel@tonic-gate case FILENAME: 423*0Sstevel@tonic-gate if (totallines > 1) { 424*0Sstevel@tonic-gate break; 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate topline = 1; 427*0Sstevel@tonic-gate editref(0); 428*0Sstevel@tonic-gate break; 429*0Sstevel@tonic-gate case CHANGE: 430*0Sstevel@tonic-gate return (changestring()); 431*0Sstevel@tonic-gate } 432*0Sstevel@tonic-gate } else if (field == FILENAME && 433*0Sstevel@tonic-gate access(newpat, READ) == 0) { 434*0Sstevel@tonic-gate /* try to edit the file anyway */ 435*0Sstevel@tonic-gate edit(newpat, "1"); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate } else { /* no pattern--the input was erased */ 438*0Sstevel@tonic-gate return (NO); 439*0Sstevel@tonic-gate } 440*0Sstevel@tonic-gate } else { /* control character */ 441*0Sstevel@tonic-gate return (NO); 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate return (YES); 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate /* clear the prompt line */ 448*0Sstevel@tonic-gate 449*0Sstevel@tonic-gate void 450*0Sstevel@tonic-gate clearprompt(void) 451*0Sstevel@tonic-gate { 452*0Sstevel@tonic-gate (void) move(PRLINE, 0); 453*0Sstevel@tonic-gate (void) clrtoeol(); 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate /* read references from a file */ 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate BOOL 459*0Sstevel@tonic-gate readrefs(char *filename) 460*0Sstevel@tonic-gate { 461*0Sstevel@tonic-gate FILE *file; 462*0Sstevel@tonic-gate int c; 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate if ((file = fopen(filename, "r")) == NULL) { 465*0Sstevel@tonic-gate cannotopen(filename); 466*0Sstevel@tonic-gate return (NO); 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate if ((c = getc(file)) == EOF) { /* if file is empty */ 469*0Sstevel@tonic-gate return (NO); 470*0Sstevel@tonic-gate } 471*0Sstevel@tonic-gate totallines = 0; 472*0Sstevel@tonic-gate nextline = 1; 473*0Sstevel@tonic-gate if (writerefsfound() == YES) { 474*0Sstevel@tonic-gate (void) putc(c, refsfound); 475*0Sstevel@tonic-gate while ((c = getc(file)) != EOF) { 476*0Sstevel@tonic-gate (void) putc(c, refsfound); 477*0Sstevel@tonic-gate } 478*0Sstevel@tonic-gate (void) fclose(file); 479*0Sstevel@tonic-gate (void) freopen(temp1, "r", refsfound); 480*0Sstevel@tonic-gate countrefs(); 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate return (YES); 483*0Sstevel@tonic-gate } 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate /* change one text string to another */ 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gate BOOL 488*0Sstevel@tonic-gate changestring(void) 489*0Sstevel@tonic-gate { 490*0Sstevel@tonic-gate char buf[PATLEN + 1]; /* input buffer */ 491*0Sstevel@tonic-gate char newfile[PATHLEN + 1]; /* new file name */ 492*0Sstevel@tonic-gate char oldfile[PATHLEN + 1]; /* old file name */ 493*0Sstevel@tonic-gate char linenum[NUMLEN + 1]; /* file line number */ 494*0Sstevel@tonic-gate char msg[MSGLEN + 1]; /* message */ 495*0Sstevel@tonic-gate FILE *script; /* shell script file */ 496*0Sstevel@tonic-gate BOOL anymarked = NO; /* any line marked */ 497*0Sstevel@tonic-gate MOUSEEVENT *p; /* mouse data */ 498*0Sstevel@tonic-gate int c, i; 499*0Sstevel@tonic-gate char *s; 500*0Sstevel@tonic-gate 501*0Sstevel@tonic-gate /* open the temporary file */ 502*0Sstevel@tonic-gate if ((script = fopen(temp2, "w")) == NULL) { 503*0Sstevel@tonic-gate cannotopen(temp2); 504*0Sstevel@tonic-gate return (NO); 505*0Sstevel@tonic-gate } 506*0Sstevel@tonic-gate /* create the line change indicators */ 507*0Sstevel@tonic-gate change = (BOOL *)mycalloc((unsigned)totallines, sizeof (BOOL)); 508*0Sstevel@tonic-gate changing = YES; 509*0Sstevel@tonic-gate initmenu(); 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate /* until the quit command is entered */ 512*0Sstevel@tonic-gate for (;;) { 513*0Sstevel@tonic-gate /* display the current page of lines */ 514*0Sstevel@tonic-gate display(); 515*0Sstevel@tonic-gate same: 516*0Sstevel@tonic-gate /* get a character from the terminal */ 517*0Sstevel@tonic-gate (void) move(PRLINE, 0); 518*0Sstevel@tonic-gate (void) addstr( 519*0Sstevel@tonic-gate "Select lines to change (press the ? key for help): "); 520*0Sstevel@tonic-gate if ((c = mygetch()) == EOF || c == ctrl('D') || 521*0Sstevel@tonic-gate c == ctrl('Z')) { 522*0Sstevel@tonic-gate break; /* change lines */ 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate /* see if the input character is a command */ 525*0Sstevel@tonic-gate switch (c) { 526*0Sstevel@tonic-gate case ' ': /* display next page */ 527*0Sstevel@tonic-gate case '+': 528*0Sstevel@tonic-gate case ctrl('V'): 529*0Sstevel@tonic-gate case KEY_NPAGE: 530*0Sstevel@tonic-gate case '-': /* display previous page */ 531*0Sstevel@tonic-gate case KEY_PPAGE: 532*0Sstevel@tonic-gate case '!': /* shell escape */ 533*0Sstevel@tonic-gate case '?': /* help */ 534*0Sstevel@tonic-gate (void) command(c); 535*0Sstevel@tonic-gate break; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate case ctrl('L'): /* redraw screen */ 538*0Sstevel@tonic-gate case KEY_CLEAR: 539*0Sstevel@tonic-gate (void) command(c); 540*0Sstevel@tonic-gate goto same; 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate case ESC: /* kept for backwards compatibility */ 543*0Sstevel@tonic-gate /* FALLTHROUGH */ 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate case '\r': /* don't change lines */ 546*0Sstevel@tonic-gate case '\n': 547*0Sstevel@tonic-gate case KEY_ENTER: 548*0Sstevel@tonic-gate case KEY_BREAK: 549*0Sstevel@tonic-gate case ctrl('G'): 550*0Sstevel@tonic-gate clearprompt(); 551*0Sstevel@tonic-gate goto nochange; 552*0Sstevel@tonic-gate 553*0Sstevel@tonic-gate case '*': /* mark/unmark all displayed lines */ 554*0Sstevel@tonic-gate for (i = 0; topline + i < nextline; ++i) { 555*0Sstevel@tonic-gate mark(i); 556*0Sstevel@tonic-gate } 557*0Sstevel@tonic-gate goto same; 558*0Sstevel@tonic-gate 559*0Sstevel@tonic-gate case 'a': /* mark/unmark all lines */ 560*0Sstevel@tonic-gate for (i = 0; i < totallines; ++i) { 561*0Sstevel@tonic-gate if (change[i] == NO) { 562*0Sstevel@tonic-gate change[i] = YES; 563*0Sstevel@tonic-gate } else { 564*0Sstevel@tonic-gate change[i] = NO; 565*0Sstevel@tonic-gate } 566*0Sstevel@tonic-gate } 567*0Sstevel@tonic-gate /* show that all have been marked */ 568*0Sstevel@tonic-gate seekline(totallines); 569*0Sstevel@tonic-gate break; 570*0Sstevel@tonic-gate case ctrl('X'): /* mouse selection */ 571*0Sstevel@tonic-gate if ((p = getmouseevent()) == NULL) { 572*0Sstevel@tonic-gate goto same; /* unknown control sequence */ 573*0Sstevel@tonic-gate } 574*0Sstevel@tonic-gate /* if the button number is a scrollbar tag */ 575*0Sstevel@tonic-gate if (p->button == '0') { 576*0Sstevel@tonic-gate scrollbar(p); 577*0Sstevel@tonic-gate break; 578*0Sstevel@tonic-gate } 579*0Sstevel@tonic-gate /* find the selected line */ 580*0Sstevel@tonic-gate /* note: the selection is forced into range */ 581*0Sstevel@tonic-gate for (i = disprefs - 1; i > 0; --i) { 582*0Sstevel@tonic-gate if (p->y1 >= displine[i]) { 583*0Sstevel@tonic-gate break; 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate } 586*0Sstevel@tonic-gate mark(i); 587*0Sstevel@tonic-gate goto same; 588*0Sstevel@tonic-gate default: 589*0Sstevel@tonic-gate /* if a line was selected */ 590*0Sstevel@tonic-gate if (isdigit(c) && c != '0' && !mouse) { 591*0Sstevel@tonic-gate if (returnrequired == NO) { 592*0Sstevel@tonic-gate mark(c - '1'); 593*0Sstevel@tonic-gate } else { 594*0Sstevel@tonic-gate clearprompt(); 595*0Sstevel@tonic-gate (void) move(PRLINE, 0); 596*0Sstevel@tonic-gate (void) addstr(selectionprompt); 597*0Sstevel@tonic-gate if (getline(buf, 598*0Sstevel@tonic-gate COLS - sizeof (selectionprompt), c, 599*0Sstevel@tonic-gate NO) > 0 && 600*0Sstevel@tonic-gate (i = atoi(buf)) > 0) { 601*0Sstevel@tonic-gate mark(i - 1); 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate goto same; 606*0Sstevel@tonic-gate } 607*0Sstevel@tonic-gate } 608*0Sstevel@tonic-gate /* for each line containing the old text */ 609*0Sstevel@tonic-gate (void) fprintf(script, "ed - <<\\!\nH\n"); 610*0Sstevel@tonic-gate *oldfile = '\0'; 611*0Sstevel@tonic-gate seekline(1); 612*0Sstevel@tonic-gate for (i = 0; fscanf(refsfound, "%s%*s%s%*[^\n]", newfile, linenum) == 2; 613*0Sstevel@tonic-gate ++i) { 614*0Sstevel@tonic-gate /* see if the line is to be changed */ 615*0Sstevel@tonic-gate if (change[i] == YES) { 616*0Sstevel@tonic-gate anymarked = YES; 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate /* if this is a new file */ 619*0Sstevel@tonic-gate if (strcmp(newfile, oldfile) != 0) { 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gate /* make sure it can be changed */ 622*0Sstevel@tonic-gate if (access(newfile, WRITE) != 0) { 623*0Sstevel@tonic-gate (void) sprintf(msg, 624*0Sstevel@tonic-gate "Cannot write to file %s", 625*0Sstevel@tonic-gate newfile); 626*0Sstevel@tonic-gate putmsg(msg); 627*0Sstevel@tonic-gate anymarked = NO; 628*0Sstevel@tonic-gate break; 629*0Sstevel@tonic-gate } 630*0Sstevel@tonic-gate /* if there was an old file */ 631*0Sstevel@tonic-gate if (*oldfile != '\0') { 632*0Sstevel@tonic-gate (void) fprintf(script, 633*0Sstevel@tonic-gate "w\n"); /* save it */ 634*0Sstevel@tonic-gate } 635*0Sstevel@tonic-gate /* edit the new file */ 636*0Sstevel@tonic-gate (void) strcpy(oldfile, newfile); 637*0Sstevel@tonic-gate (void) fprintf(script, "e %s\n", oldfile); 638*0Sstevel@tonic-gate } 639*0Sstevel@tonic-gate /* output substitute command */ 640*0Sstevel@tonic-gate (void) fprintf(script, 641*0Sstevel@tonic-gate "%ss/", linenum); /* change */ 642*0Sstevel@tonic-gate for (s = pattern; *s != '\0'; ++s) { /* old text */ 643*0Sstevel@tonic-gate if (*s == '/') { 644*0Sstevel@tonic-gate (void) putc('\\', script); 645*0Sstevel@tonic-gate } 646*0Sstevel@tonic-gate (void) putc(*s, script); 647*0Sstevel@tonic-gate } 648*0Sstevel@tonic-gate (void) putc('/', script); /* to */ 649*0Sstevel@tonic-gate for (s = newpat; *s != '\0'; ++s) { /* new text */ 650*0Sstevel@tonic-gate if (strchr("/\\&", *s) != NULL) { 651*0Sstevel@tonic-gate (void) putc('\\', script); 652*0Sstevel@tonic-gate } 653*0Sstevel@tonic-gate (void) putc(*s, script); 654*0Sstevel@tonic-gate } 655*0Sstevel@tonic-gate (void) fprintf(script, "/gp\n"); /* and print */ 656*0Sstevel@tonic-gate } 657*0Sstevel@tonic-gate } 658*0Sstevel@tonic-gate (void) fprintf(script, "w\nq\n!\n"); /* write and quit */ 659*0Sstevel@tonic-gate (void) fclose(script); 660*0Sstevel@tonic-gate clearprompt(); 661*0Sstevel@tonic-gate 662*0Sstevel@tonic-gate /* if any line was marked */ 663*0Sstevel@tonic-gate if (anymarked == YES) { 664*0Sstevel@tonic-gate /* edit the files */ 665*0Sstevel@tonic-gate (void) refresh(); 666*0Sstevel@tonic-gate (void) fprintf(stderr, "Changed lines:\n\r"); 667*0Sstevel@tonic-gate (void) execute(shell, shell, temp2, (char *)NULL); 668*0Sstevel@tonic-gate askforreturn(); 669*0Sstevel@tonic-gate } 670*0Sstevel@tonic-gate nochange: 671*0Sstevel@tonic-gate changing = NO; 672*0Sstevel@tonic-gate initmenu(); 673*0Sstevel@tonic-gate free(change); 674*0Sstevel@tonic-gate seekline(topline); 675*0Sstevel@tonic-gate return (YES); /* clear any marks on exit without change */ 676*0Sstevel@tonic-gate } 677*0Sstevel@tonic-gate 678*0Sstevel@tonic-gate /* mark/unmark this displayed line to be changed */ 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gate void 681*0Sstevel@tonic-gate mark(int i) 682*0Sstevel@tonic-gate { 683*0Sstevel@tonic-gate int j; 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate j = i + topline - 1; 686*0Sstevel@tonic-gate if (j < totallines) { 687*0Sstevel@tonic-gate (void) move(displine[i], selectlen); 688*0Sstevel@tonic-gate if (change[j] == NO) { 689*0Sstevel@tonic-gate change[j] = YES; 690*0Sstevel@tonic-gate (void) addch('>'); 691*0Sstevel@tonic-gate } else { 692*0Sstevel@tonic-gate change[j] = NO; 693*0Sstevel@tonic-gate (void) addch(' '); 694*0Sstevel@tonic-gate } 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate } 697*0Sstevel@tonic-gate 698*0Sstevel@tonic-gate /* scrollbar actions */ 699*0Sstevel@tonic-gate 700*0Sstevel@tonic-gate static void 701*0Sstevel@tonic-gate scrollbar(MOUSEEVENT *p) 702*0Sstevel@tonic-gate { 703*0Sstevel@tonic-gate /* reposition list if it makes sense */ 704*0Sstevel@tonic-gate if (totallines == 0) { 705*0Sstevel@tonic-gate return; 706*0Sstevel@tonic-gate } 707*0Sstevel@tonic-gate switch (p->percent) { 708*0Sstevel@tonic-gate 709*0Sstevel@tonic-gate case 101: /* scroll down one page */ 710*0Sstevel@tonic-gate if (nextline + mdisprefs > totallines) { 711*0Sstevel@tonic-gate nextline = totallines - mdisprefs + 1; 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate break; 714*0Sstevel@tonic-gate 715*0Sstevel@tonic-gate case 102: /* scroll up one page */ 716*0Sstevel@tonic-gate nextline = topline - mdisprefs; 717*0Sstevel@tonic-gate if (nextline < 1) { 718*0Sstevel@tonic-gate nextline = 1; 719*0Sstevel@tonic-gate } 720*0Sstevel@tonic-gate break; 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate case 103: /* scroll down one line */ 723*0Sstevel@tonic-gate nextline = topline + 1; 724*0Sstevel@tonic-gate break; 725*0Sstevel@tonic-gate 726*0Sstevel@tonic-gate case 104: /* scroll up one line */ 727*0Sstevel@tonic-gate if (topline > 1) { 728*0Sstevel@tonic-gate nextline = topline - 1; 729*0Sstevel@tonic-gate } 730*0Sstevel@tonic-gate break; 731*0Sstevel@tonic-gate default: 732*0Sstevel@tonic-gate nextline = p->percent * totallines / 100; 733*0Sstevel@tonic-gate } 734*0Sstevel@tonic-gate seekline(nextline); 735*0Sstevel@tonic-gate } 736