/* display an error mesg - stdout or on second msg line */ void posterr(char *msg, ...) { va_list ap; char errbuf[MSGLEN]; va_start(ap, msg); if (linemode == YES || incurses == NO) { (void) vfprintf(stderr, msg, ap); (void) fputc('\n', stderr); } else { vsnprintf(errbuf, sizeof(errbuf), msg, ap); postmsg2(errbuf); } }
/* execute the command */ BOOL command(int commandc) { char filename[PATHLEN + 1]; /* file path name */ MOUSE *p; /* mouse data */ int c, i; FILE *file; struct cmd *curritem, *item; /* command history */ char *s; switch (commandc) { case ctrl('C'): /* toggle caseless mode */ if (caseless == NO) { caseless = YES; postmsg2("Caseless mode is now ON"); } else { caseless = NO; postmsg2("Caseless mode is now OFF"); } egrepcaseless(caseless); /* turn on/off -i flag */ return(NO); case ctrl('R'): /* rebuild the cross reference */ if (isuptodate == YES) { postmsg("The -d option prevents rebuilding the symbol database"); return(NO); } exitcurses(); freefilelist(); /* remake the source file list */ makefilelist(); rebuild(); if (errorsfound == YES) { errorsfound = NO; askforreturn(); } entercurses(); clearmsg(); /* clear any previous message */ totallines = 0; disprefs = 0; topline = nextline = 1; selecting = 0; break; #if UNIXPC case ESC: /* possible unixpc mouse selection */ #endif case ctrl('X'): /* mouse selection */ if ((p = getmouseaction(DUMMYCHAR)) == NULL) { return(NO); /* unknown control sequence */ } /* if the button number is a scrollbar tag */ if (p->button == '0') { scrollbar(p); break; } /* ignore a sweep */ if (p->x2 >= 0) { return(NO); } /* if this is a line selection */ if (p->y1 < FLDLINE) { /* find the selected line */ /* note: the selection is forced into range */ for (i = disprefs - 1; i > 0; --i) { if (p->y1 >= displine[i]) { break; } } /* display it in the file with the editor */ editref(i); } else { /* this is an input field selection */ field = p->y1 - FLDLINE; /* force it into range */ if (field >= FIELDS) { field = FIELDS - 1; } setfield(); resetcmd(); return(NO); } break; case '\t': /* go to next input field */ 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': /* go to reference */ if (selecting) { editref(curdispline); return(YES); } /* FALLTHROUGH */ 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'): /* go to previous input field */ #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: /* go to first input field */ if (selecting) { curdispline = 0; move(REFLINE, 0); refresh(); } else { field = 0; setfield(); atfield(); resetcmd(); } return(NO); #endif #ifdef KEY_LL case KEY_LL: /* go to last input field */ if (selecting) { move(displine[disprefs - 1], 0); refresh(); } else { field = FIELDS - 1; setfield(); atfield(); resetcmd(); } return(NO); #endif /* def(KEY_LL) */ case ' ': /* display next page */ case '+': case ctrl('V'): #ifdef KEY_NPAGE case KEY_NPAGE: #endif /* don't redisplay if there are no lines */ if (totallines == 0) { return(NO); } /* note: seekline() is not used to move to the next * page because display() leaves the file pointer at * the next page to optimize paging forward */ curdispline = 0; break; case ctrl('H'): case '-': /* display previous page */ #ifdef KEY_PPAGE case KEY_PPAGE: #endif /* don't redisplay if there are no lines */ if (totallines == 0) { return(NO); } curdispline = 0; /* if there are only two pages, just go to the other one */ if (totallines <= 2 * mdisprefs) { break; } /* if on first page but not at beginning, go to beginning */ nextline -= mdisprefs; /* already at next page */ 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 '>': /* write or append the lines to a file */ if (totallines == 0) { postmsg("There are no lines to write to a file"); } else { /* get the file name */ 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); /* return to the previous field */ case '<': /* read lines from a file */ 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 '^': /* pipe the lines through a shell command */ case '|': /* pipe the lines to a shell command */ if (totallines == 0) { postmsg("There are no lines to pipe to a shell command"); return(NO); } /* get the shell command */ move(PRLINE, 0); addstr(pipeprompt); if (mygetline("", newpat, COLS - sizeof(pipeprompt), '\0', NO) == 0) { clearprompt(); return(NO); } /* if the ^ command, redirect output to a temp file */ if (commandc == '^') { strcat(strcat(newpat, " >"), temp2); /* HBB 20020708: somebody might have even * their non-interactive default shells * complain about clobbering * redirections... --> delete before * overwriting */ remove(temp2); } exitcurses(); if ((file = mypopen(newpat, "w")) == NULL) { fprintf(stderr, "\ cscope: cannot open pipe to shell command: %s\n", newpat); } else {