選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

556 行
14KB

  1. /*===========================================================================
  2. Copyright (c) 1998-2000, The Santa Cruz Operation
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. *Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. *Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. *Neither name of The Santa Cruz Operation nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  15. IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
  16. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  25. DAMAGE.
  26. =========================================================================*/
  27. /* cscope - interactive C symbol cross-reference
  28. *
  29. * terminal input functions
  30. */
  31. #include "global.h"
  32. #include <ncurses.h>
  33. #include <setjmp.h> /* jmp_buf */
  34. #include <stdlib.h>
  35. #include <errno.h>
  36. #if HAVE_SYS_TERMIOS_H
  37. # include <sys/termios.h>
  38. #endif
  39. #include "keys.h"
  40. extern const void *const winput;
  41. extern const void *const wmode;
  42. extern const void *const wresult;
  43. extern const void *const *const current_window;
  44. bool do_press_any_key = false;
  45. static jmp_buf env; /* setjmp/longjmp buffer */
  46. static int prevchar; /* previous, ungotten character */
  47. /* Internal prototypes: */
  48. static void catchint(int sig);
  49. /* catch the interrupt signal */
  50. static void catchint(int sig) {
  51. UNUSED(sig);
  52. signal(SIGINT, catchint);
  53. longjmp(env, 1);
  54. }
  55. static inline bool rebuild_reference() {
  56. if(isuptodate == true) {
  57. postmsg("The -d option prevents rebuilding the symbol database");
  58. return false;
  59. }
  60. exitcurses();
  61. freefilelist(); /* remake the source file list */
  62. makefilelist();
  63. rebuild();
  64. if(errorsfound == true) {
  65. errorsfound = false;
  66. askforreturn();
  67. }
  68. entercurses();
  69. postmsg(""); /* clear any previous message */
  70. totallines = 0;
  71. disprefs = 0;
  72. return true;
  73. }
  74. /* unget a character */
  75. void myungetch(int c) {
  76. prevchar = c;
  77. }
  78. /* ask user to enter a character after reading the message */
  79. void askforchar(void) {
  80. addstr("Type any character to continue: ");
  81. getch();
  82. }
  83. /* ask user to press the RETURN key after reading the message */
  84. void askforreturn(void) {
  85. fprintf(stderr, "Press the RETURN key to continue: ");
  86. getchar();
  87. /* HBB 20060419: message probably messed up the screen --- redraw */
  88. if(incurses == true) { redrawwin(curscr); }
  89. }
  90. /* expand the ~ and $ shell meta characters in a path */
  91. void shellpath(char *out, int limit, char *in) {
  92. char *lastchar;
  93. char *s, *v;
  94. /* skip leading white space */
  95. while(isspace((unsigned char)*in)) {
  96. ++in;
  97. }
  98. lastchar = out + limit - 1;
  99. /* a tilde (~) by itself represents $HOME; followed by a name it
  100. represents the $LOGDIR of that login name */
  101. if(*in == '~') {
  102. *out++ = *in++; /* copy the ~ because it may not be expanded */
  103. /* get the login name */
  104. s = out;
  105. while(s < lastchar && *in != '/' && *in != '\0' && !isspace((unsigned char)*in)) {
  106. *s++ = *in++;
  107. }
  108. *s = '\0';
  109. /* if the login name is null, then use $HOME */
  110. if(*out == '\0') {
  111. v = getenv("HOME");
  112. } else { /* get the home directory of the login name */
  113. v = logdir(out);
  114. }
  115. /* copy the directory name if it isn't too big */
  116. if(v != NULL && strlen(v) < (lastchar - out)) {
  117. strcpy(out - 1, v);
  118. out += strlen(v) - 1;
  119. } else {
  120. /* login not found, so ~ must be part of the file name */
  121. out += strlen(out);
  122. }
  123. }
  124. /* get the rest of the path */
  125. while(out < lastchar && *in != '\0' && !isspace((unsigned char)*in)) {
  126. /* look for an environment variable */
  127. if(*in == '$') {
  128. *out++ = *in++; /* copy the $ because it may not be expanded */
  129. /* get the variable name */
  130. s = out;
  131. while(s < lastchar && *in != '/' && *in != '\0' &&
  132. !isspace((unsigned char)*in)) {
  133. *s++ = *in++;
  134. }
  135. *s = '\0';
  136. /* get its value, but only it isn't too big */
  137. if((v = getenv(out)) != NULL && strlen(v) < (lastchar - out)) {
  138. strcpy(out - 1, v);
  139. out += strlen(v) - 1;
  140. } else {
  141. /* var not found, or too big, so assume $ must be part of the
  142. * file name */
  143. out += strlen(out);
  144. }
  145. } else { /* ordinary character */
  146. *out++ = *in++;
  147. }
  148. }
  149. *out = '\0';
  150. }
  151. static int wmode_input(const int c) {
  152. switch(c) {
  153. case KEY_ENTER:
  154. case '\r':
  155. case '\n':
  156. case ctrl('N'): /* go to next input field */
  157. case KEY_DOWN:
  158. case KEY_RIGHT:
  159. field = (field + 1) % FIELDS;
  160. break;
  161. case ctrl('P'): /* go to previous input field */
  162. case KEY_UP:
  163. case KEY_LEFT:
  164. field = (field + (FIELDS - 1)) % FIELDS;
  165. break;
  166. case KEY_HOME: /* go to first input field */
  167. field = 0;
  168. break;
  169. case KEY_LL: /* go to last input field */
  170. curdispline = disprefs;
  171. break;
  172. default:
  173. return 0;
  174. }
  175. window_change |= CH_MODE;
  176. return 1;
  177. }
  178. static int wresult_input(const int c) {
  179. switch(c) {
  180. case KEY_ENTER: /* open for editing */
  181. case '\r':
  182. case '\n':
  183. editref(curdispline);
  184. window_change = CH_ALL;
  185. break;
  186. case ctrl('N'):
  187. case KEY_DOWN:
  188. case KEY_RIGHT:
  189. if((curdispline + 1) < disprefs) { ++curdispline; }
  190. break;
  191. case ctrl('P'):
  192. case KEY_UP:
  193. case KEY_LEFT:
  194. if(curdispline) { --curdispline; }
  195. break;
  196. case KEY_HOME:
  197. curdispline = 0;
  198. break;
  199. case KEY_LL:
  200. field = FIELDS - 1;
  201. break;
  202. default:
  203. if(c > mdisprefs) { goto noredisp; }
  204. const int pos = dispchar2int(c);
  205. if(pos > -1) { editref(pos); }
  206. goto noredisp;
  207. }
  208. window_change |= CH_RESULT;
  209. noredisp:
  210. return 1;
  211. }
  212. static int global_input(const int c) {
  213. switch(c) {
  214. case '\t':
  215. horswp_window();
  216. break;
  217. case '%':
  218. verswp_window();
  219. break;
  220. case ctrl('R'):
  221. rebuild_reference();
  222. break;
  223. case ctrl('K'):
  224. field = (field + (FIELDS - 1)) % FIELDS;
  225. window_change |= CH_MODE;
  226. break;
  227. case ctrl('J'):
  228. field = (field + 1) % FIELDS;
  229. window_change |= CH_MODE;
  230. break;
  231. case ctrl('H'): /* display previous page */
  232. case '-':
  233. case KEY_PPAGE:
  234. if(totallines == 0) { return 0; } /* don't redisplay if there are no lines */
  235. curdispline = 0;
  236. if(current_page > 0) {
  237. --current_page;
  238. window_change |= CH_RESULT;
  239. }
  240. break;
  241. case '+':
  242. case KEY_NPAGE:
  243. if(totallines == 0) { return 0; } /* don't redisplay if there are no lines */
  244. curdispline = 0;
  245. ++current_page;
  246. window_change |= CH_RESULT;
  247. break;
  248. case '>': /* write or append the lines to a file */
  249. if (totallines == 0) {
  250. postmsg("There are no lines to write to a file");
  251. break;
  252. }
  253. input_mode = INPUT_APPEND;
  254. window_change |= CH_INPUT;
  255. force_window();
  256. break;
  257. case '<': /* read lines from a file */
  258. input_mode = INPUT_READ;
  259. window_change |= CH_INPUT;
  260. force_window();
  261. break;
  262. case '|': /* pipe the lines to a shell command */
  263. case '^':
  264. break; // XXX fix
  265. if(totallines == 0) {
  266. postmsg("There are no lines to pipe to a shell command");
  267. return 0;
  268. }
  269. /* get the shell command */
  270. // move(PRLINE, 0);
  271. // addstr(pipeprompt);
  272. // if (mygetline("", newpat, COLS - sizeof(pipeprompt), '\0', NO) == 0) {
  273. // clearprompt();
  274. // return(NO);
  275. // }
  276. ///* if the ^ command, redirect output to a temp file */
  277. // if (commandc == '^') {
  278. // strcat(strcat(newpat, " >"), temp2);
  279. // /* HBB 20020708: somebody might have even
  280. // * their non-interactive default shells
  281. // * complain about clobbering
  282. // * redirections... --> delete before
  283. // * overwriting */
  284. // remove(temp2);
  285. // }
  286. // exitcurses();
  287. // if ((file = mypopen(newpat, "w")) == NULL) {
  288. // fprintf(stderr, "cscope: cannot open pipe to shell command: %s\n",
  289. // newpat);
  290. // } else {
  291. // seekline(1);
  292. // while ((c = getc(refsfound)) != EOF) {
  293. // putc(c, file);
  294. // }
  295. // seekline(topline);
  296. // mypclose(file);
  297. // }
  298. // if (commandc == '^') {
  299. // if (readrefs(temp2) == NO) {
  300. // postmsg("Ignoring empty output of ^ command");
  301. // }
  302. // }
  303. // askforreturn();
  304. // entercurses();
  305. break;
  306. case '!': /* shell escape */
  307. execute(shell, shell, NULL);
  308. current_page = 0;
  309. break;
  310. case ctrl('U'): /* redraw screen */
  311. case KEY_CLEAR:
  312. window_change = CH_ALL;
  313. break;
  314. case '?': /* help */
  315. window_change = CH_HELP;
  316. break;
  317. case ctrl('E'): /* edit all lines */
  318. editall();
  319. break;
  320. case ctrl('S'): // toggle caseless
  321. caseless = !caseless;
  322. egrepcaseless(caseless);
  323. window_change |= CH_CASE;
  324. break;
  325. case EOF:
  326. myexit(0);
  327. break;
  328. default:
  329. return 0;
  330. }
  331. return 1;
  332. }
  333. int change_input(const int c) {
  334. MOUSE *p; /* mouse data */
  335. switch(c) {
  336. case '*': /* invert page */
  337. for(unsigned i = 0; i < (nextline-1); i++) {
  338. change[topref + i] = !change[topref + i];
  339. }
  340. window_change |= CH_RESULT;
  341. break;
  342. case ctrl('A'): /* invert all lines */
  343. for(unsigned i = 0; i < totallines; i++) {
  344. change[i] = !change[i];
  345. }
  346. window_change |= CH_RESULT;
  347. break;
  348. case ctrl('X'): /* mouse selection */
  349. if((p = getmouseaction(DUMMYCHAR)) == NULL) {
  350. break; /* unknown control sequence */
  351. }
  352. /* if the button number is a scrollbar tag */
  353. if(p->button == '0') {
  354. // scrollbar(p);
  355. break;
  356. }
  357. /* find the selected line */
  358. /* NOTE: the selection is forced into range */
  359. {
  360. int i;
  361. for(i = disprefs - 1; i > 0; --i) {
  362. if(p->y1 >= displine[i]) { break; }
  363. }
  364. change[i] = !change[i];
  365. }
  366. break;
  367. case ctrl('D'):
  368. changestring(input_line, newpat, change, totallines);
  369. free(change);
  370. change = NULL;
  371. input_mode = INPUT_NORMAL;
  372. horswp_window();
  373. rebuild_reference();
  374. search(newpat);
  375. break;
  376. default:
  377. {
  378. /* if a line was selected */
  379. const int cc = dispchar2int(c);
  380. if(cc != -1 && cc < totallines) {
  381. change[cc] = !change[cc];
  382. window_change |= CH_RESULT;
  383. }
  384. }
  385. }
  386. return 0;
  387. }
  388. // XXX: move this
  389. int changestring(const char *from, const char *to, const bool *const change, const int change_len) {
  390. char newfile[PATHLEN + 1]; /* new file name */
  391. char oldfile[PATHLEN + 1]; /* old file name */
  392. char linenum[NUMLEN + 1]; /* file line number */
  393. char msg[MSGLEN + 1]; /* message */
  394. FILE *script; /* shell script file */
  395. // Return early
  396. bool anymarked = false; /* any line marked */
  397. for(int i = 0; i < change_len; i++) {
  398. if(change[i]) {
  399. anymarked = true;
  400. break;
  401. }
  402. }
  403. if(!anymarked) { return false; }
  404. /* open the temporary file */
  405. if((script = myfopen(temp2, "w")) == NULL) {
  406. cannotopen(temp2);
  407. return (false);
  408. }
  409. /* for each line containing the old text */
  410. fprintf(script, "ed - <<\\!\n");
  411. *oldfile = '\0';
  412. fseek(refsfound, 0, SEEK_SET);
  413. for(int i = 0; fscanf(refsfound,
  414. "%" PATHLEN_STR "s%*s%" NUMLEN_STR "s%*[^\n]",
  415. newfile,
  416. linenum) == 2;
  417. ++i) {
  418. /* see if the line is to be changed */
  419. if(!change[i]) { break; }
  420. /* if this is a new file */
  421. if(strcmp(newfile, oldfile) != 0) {
  422. /* make sure it can be changed */
  423. if(access(newfile, WRITE) != 0) {
  424. snprintf(msg, sizeof(msg), "Cannot write to file %s", newfile);
  425. postmsg(msg);
  426. goto end;
  427. }
  428. /* if there was an old file */
  429. if(*oldfile != '\0') { fprintf(script, "w\n"); /* save it */ }
  430. /* edit the new file */
  431. strcpy(oldfile, newfile);
  432. fprintf(script, "e %s\n", oldfile);
  433. }
  434. /* output substitute command */
  435. fprintf(script, "%ss/", linenum); /* change */
  436. for(const char *s = from; *s != '\0'; ++s) {
  437. /* old text */
  438. if(strchr("/\\[.^*", *s) != NULL) { putc('\\', script); }
  439. if(caseless == true && isalpha((unsigned char)*s)) {
  440. putc('[', script);
  441. if(islower((unsigned char)*s)) {
  442. putc(toupper((unsigned char)*s), script);
  443. putc(*s, script);
  444. } else {
  445. putc(*s, script);
  446. putc(tolower((unsigned char)*s), script);
  447. }
  448. putc(']', script);
  449. } else {
  450. putc(*s, script);
  451. }
  452. }
  453. putc('/', script); /* to */
  454. for(const char *s = to; *s != '\0'; ++s) { /* new text */
  455. if(strchr("/\\&", *s) != NULL) { putc('\\', script); }
  456. putc(*s, script);
  457. }
  458. fprintf(script, "/gp\n"); /* and print */
  459. }
  460. fprintf(script, "w\nq\n!\n"); /* write and quit */
  461. fflush(script);
  462. /* edit the files */
  463. execute("sh", "sh", temp2, NULL); // XXX: this should not echo
  464. end:
  465. fclose(script);
  466. return true;
  467. }
  468. int handle_input(const int c) {
  469. /* - was wating for any input - */
  470. if(do_press_any_key) {
  471. do_press_any_key = false;
  472. return 0;
  473. }
  474. /* - Resize - */
  475. /* it's treated specially because curses treat it specially:
  476. + its valid without keypad()
  477. + as far as i can tell this is the only key that does not
  478. flush after itself
  479. */
  480. if(c == KEY_RESIZE) {
  481. redisplay();
  482. flushinp();
  483. return 0;
  484. }
  485. /* --- global --- */
  486. const int r = global_input(c);
  487. if(r) { return 0; }
  488. /* --- mode specific --- */
  489. switch(input_mode) {
  490. case INPUT_NORMAL:
  491. if(*current_window == winput) {
  492. return interpret(c);
  493. } else if(*current_window == wmode) {
  494. return wmode_input(c);
  495. } else if(*current_window == wresult) {
  496. return wresult_input(c);
  497. }
  498. assert("'current_window' dangling.");
  499. break; /* NOTREACHED */
  500. case INPUT_CHANGE_TO:
  501. case INPUT_APPEND:
  502. case INPUT_READ:
  503. return interpret(c);
  504. case INPUT_CHANGE:
  505. return change_input(c);
  506. }
  507. return 0;
  508. }