infokey.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /* infokey.c -- compile ~/.infokey to ~/.info.
  2. $Id$
  3. Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
  4. 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Andrew Bettison. */
  16. #include "info.h"
  17. #include "doc.h"
  18. #include "session.h"
  19. #include "funs.h"
  20. #include "getopt.h"
  21. #include "variables.h"
  22. extern char *program_name; /* in info.c */
  23. enum sect_e
  24. {
  25. info = 0,
  26. ea = 1,
  27. var = 2
  28. };
  29. static void syntax_error (const char *filename, unsigned int linenum,
  30. const char *fmt, ...) TEXINFO_PRINTFLIKE(3,4);
  31. /* Compilation - the real work.
  32. Source file syntax
  33. ------------------
  34. The source file is a line-based text file with the following
  35. structure:
  36. # comments
  37. # more comments
  38. #info
  39. u prev-line
  40. d next-line
  41. ^a invalid # just beep
  42. \ku prev-line
  43. #stop
  44. \kd next-line
  45. q quit # of course!
  46. #echo-area
  47. ^a echo-area-beg-of-line
  48. ^e echo-area-end-of-line
  49. \kr echo-area-forward
  50. \kl echo-area-backward
  51. \kh echo-area-beg-of-line
  52. \ke echo-area-end-of-line
  53. #var
  54. scroll-step=1
  55. ISO-Latin=Off
  56. Lines starting with '#' are comments, and are ignored. Blank
  57. lines are ignored. Each section is introduced by one of the
  58. following lines:
  59. #info
  60. #echo-area
  61. #var
  62. The sections may occur in any order. Each section may be
  63. omitted completely. If the 'info' section is the first in the
  64. file, its '#info' line may be omitted.
  65. The 'info' and 'echo-area' sections
  66. -----------------------------------
  67. Each line in the 'info' or 'echo-area' sections has the
  68. following syntax:
  69. key-sequence SPACE action-name [ SPACE [ # comment ] ] \n
  70. Where SPACE is one or more white space characters excluding
  71. newline, "action-name" is the name of a GNU Info command,
  72. "comment" is any sequence of characters excluding newline, and
  73. "key-sequence" is a concatenation of one or more key definitions
  74. using the following syntax:
  75. 1. A carat ^ followed by one character indicates a single
  76. control character;
  77. 2. A backslash \ followed by one, two, or three octal
  78. digits indicates a single character having that ASCII
  79. code;
  80. 3. \n indicates a single NEWLINE;
  81. \e indicates a single ESC;
  82. \r indicates a single CR;
  83. \t indicates a single TAB;
  84. \b indicates a single BACKSPACE;
  85. 4. \ku indicates the Up Arrow key;
  86. \kd indicates the Down Arrow key;
  87. \kl indicates the Left Arrow key;
  88. \kr indicates the Right Arrow key;
  89. \kP indicates the Page Up (PRIOR) key;
  90. \kN indicates the Page Down (NEXT) key;
  91. \kh indicates the Home key;
  92. \ke indicates the End key;
  93. \kx indicates the DEL key;
  94. \k followed by any other character indicates a single
  95. control-K, and the following character is interpreted
  96. as in rules 1, 2, 3, 5 and 6.
  97. 5. \m followed by any sequence defined in rules 1, 2, 3, 4
  98. or 6 indicates the "Meta" modification of that key.
  99. 6. A backslash \ followed by any character not described
  100. above indicates that character itself. In particular:
  101. \\ indicates a single backslash \,
  102. \ (backslash-space) indicates a single space,
  103. \^ indicates a single caret ^,
  104. If the following line:
  105. #stop
  106. occurs anywhere in an 'info' or 'echo-area' section, that
  107. indicates to GNU Info to suppress all of its default key
  108. bindings in that context.
  109. The 'var' section
  110. -----------------
  111. Each line in the 'var' section has the following syntax:
  112. variable-name = value \n
  113. Where "variable-name" is the name of a GNU Info variable and
  114. "value" is the value that GNU Info will assign to that variable
  115. when commencing execution. There must be no white space in the
  116. variable name, nor between the variable name and the '='. All
  117. characters immediately following the '=', up to but not
  118. including the terminating newline, are considered to be the
  119. value that will be assigned. In other words, white space
  120. following the '=' is not ignored.
  121. */
  122. static int lookup_action (const char *actname);
  123. /* Read the init file. Return true if no error was encountered. Set
  124. SUPPRESS_INFO or SUPPRESS_EA to true if the init file specified to ignore
  125. default key bindings. */
  126. int
  127. compile (FILE *fp, const char *filename, int *suppress_info, int *suppress_ea)
  128. {
  129. int error = 0; /* Set if there was a fatal error in reading init file. */
  130. char rescan = 0; /* Whether to reuse the same character when moving onto the
  131. next state. */
  132. unsigned int lnum = 0;
  133. int c = 0;
  134. /* This parser is a true state machine, with no sneaky fetching
  135. of input characters inside the main loop. In other words, all
  136. state is fully represented by the following variables:
  137. */
  138. enum
  139. {
  140. start_of_line,
  141. start_of_comment,
  142. in_line_comment,
  143. in_trailing_comment,
  144. get_keyseq,
  145. got_keyseq,
  146. get_action,
  147. got_action,
  148. get_varname,
  149. got_varname,
  150. get_equals,
  151. got_equals,
  152. get_value
  153. }
  154. state = start_of_line;
  155. enum sect_e section = info;
  156. enum
  157. {
  158. normal,
  159. slosh,
  160. control,
  161. octal,
  162. special_key
  163. }
  164. seqstate = normal; /* used if state == get_keyseq */
  165. char meta = 0;
  166. char ocnt = 0; /* used if state == get_keyseq && seqstate == octal */
  167. /* Data is accumulated in the following variables. The code
  168. avoids overflowing these strings, and throws an error
  169. where appropriate if a string limit is exceeded. These string
  170. lengths are arbitrary (and should be large enough) and their
  171. lengths are not hard-coded anywhere else, so increasing them
  172. here will not break anything. */
  173. char oval = 0;
  174. char comment[10];
  175. unsigned int clen = 0;
  176. int seq[20];
  177. unsigned int slen = 0;
  178. char act[80];
  179. unsigned int alen = 0;
  180. char varn[80];
  181. unsigned int varlen = 0;
  182. char val[80];
  183. unsigned int vallen = 0;
  184. #define To_seq(c) \
  185. do { \
  186. if (slen < sizeof seq/sizeof(int)) \
  187. seq[slen++] = meta ? KEYMAP_META(c) : (c); \
  188. else \
  189. { \
  190. syntax_error(filename, lnum, \
  191. _("key sequence too long")); \
  192. error = 1; \
  193. } \
  194. meta = 0; \
  195. } while (0)
  196. while (!error && (rescan || (c = fgetc (fp)) != EOF))
  197. {
  198. rescan = 0;
  199. switch (state)
  200. {
  201. case start_of_line:
  202. lnum++;
  203. if (c == '#')
  204. state = start_of_comment;
  205. else if (c != '\n')
  206. {
  207. switch (section)
  208. {
  209. case info:
  210. case ea:
  211. state = get_keyseq;
  212. seqstate = normal;
  213. slen = 0;
  214. break;
  215. case var:
  216. state = get_varname;
  217. varlen = 0;
  218. break;
  219. }
  220. rescan = 1;
  221. }
  222. break;
  223. case start_of_comment:
  224. clen = 0;
  225. state = in_line_comment;
  226. /* fall through */
  227. case in_line_comment:
  228. if (c == '\n')
  229. {
  230. state = start_of_line;
  231. comment[clen] = '\0';
  232. if (strcmp (comment, "info") == 0)
  233. section = info;
  234. else if (strcmp (comment, "echo-area") == 0)
  235. section = ea;
  236. else if (strcmp (comment, "var") == 0)
  237. section = var;
  238. else if (strcmp (comment, "stop") == 0
  239. && (section == info || section == ea))
  240. {
  241. if (section == info)
  242. *suppress_info = 1;
  243. else
  244. *suppress_ea = 1;
  245. }
  246. }
  247. else if (clen < sizeof comment - 1)
  248. comment[clen++] = c;
  249. break;
  250. case in_trailing_comment:
  251. if (c == '\n')
  252. state = start_of_line;
  253. break;
  254. case get_keyseq:
  255. switch (seqstate)
  256. {
  257. case normal:
  258. if (c == '\n' || isspace (c))
  259. {
  260. state = got_keyseq;
  261. rescan = 1;
  262. if (slen == 0)
  263. {
  264. syntax_error (filename, lnum, _("missing key sequence"));
  265. error = 1;
  266. }
  267. }
  268. else if (c == '\\')
  269. seqstate = slosh;
  270. else if (c == '^')
  271. seqstate = control;
  272. else
  273. To_seq (c);
  274. break;
  275. case slosh:
  276. switch (c)
  277. {
  278. case '0': case '1': case '2': case '3':
  279. case '4': case '5': case '6': case '7':
  280. seqstate = octal;
  281. oval = c - '0';
  282. ocnt = 1;
  283. break;
  284. case 'b':
  285. To_seq ('\b');
  286. seqstate = normal;
  287. break;
  288. case 'e':
  289. To_seq ('\033');
  290. seqstate = normal;
  291. break;
  292. case 'n':
  293. To_seq ('\n');
  294. seqstate = normal;
  295. break;
  296. case 'r':
  297. To_seq ('\r');
  298. seqstate = normal;
  299. break;
  300. case 't':
  301. To_seq ('\t');
  302. seqstate = normal;
  303. break;
  304. case 'm':
  305. meta = 1;
  306. seqstate = normal;
  307. break;
  308. case 'k':
  309. seqstate = special_key;
  310. break;
  311. default:
  312. /* Backslash followed by any other char
  313. just means that char. */
  314. To_seq (c);
  315. seqstate = normal;
  316. break;
  317. }
  318. break;
  319. case octal:
  320. switch (c)
  321. {
  322. case '0': case '1': case '2': case '3':
  323. case '4': case '5': case '6': case '7':
  324. if (++ocnt <= 3)
  325. oval = oval * 8 + c - '0';
  326. if (ocnt == 3)
  327. seqstate = normal;
  328. break;
  329. default:
  330. ocnt = 4;
  331. seqstate = normal;
  332. rescan = 1;
  333. break;
  334. }
  335. if (seqstate != octal)
  336. {
  337. if (oval)
  338. To_seq (oval);
  339. else
  340. {
  341. syntax_error (filename, lnum,
  342. _("NUL character (\\000) not permitted"));
  343. error = 1;
  344. }
  345. }
  346. break;
  347. case special_key:
  348. switch (c)
  349. {
  350. case 'u': To_seq (KEY_UP_ARROW); break;
  351. case 'd': To_seq (KEY_DOWN_ARROW); break;
  352. case 'r': To_seq (KEY_RIGHT_ARROW); break;
  353. case 'l': To_seq (KEY_LEFT_ARROW); break;
  354. case 'U': To_seq (KEY_PAGE_UP); break;
  355. case 'D': To_seq (KEY_PAGE_DOWN); break;
  356. case 'h': To_seq (KEY_HOME); break;
  357. case 'e': To_seq (KEY_END); break;
  358. case 'x': To_seq (KEY_DELETE); break;
  359. default: To_seq (c); rescan = 1; break;
  360. }
  361. seqstate = normal;
  362. break;
  363. case control:
  364. if (CONTROL (c))
  365. To_seq (CONTROL (c));
  366. else
  367. {
  368. syntax_error (filename, lnum,
  369. _("NUL character (^%c) not permitted"), c);
  370. error = 1;
  371. }
  372. seqstate = normal;
  373. break;
  374. }
  375. break;
  376. case got_keyseq:
  377. if (isspace (c) && c != '\n')
  378. break;
  379. state = get_action;
  380. alen = 0;
  381. /* fall through */
  382. case get_action:
  383. if (c == '\n' || isspace (c))
  384. {
  385. int a;
  386. state = got_action;
  387. rescan = 1;
  388. if (alen == 0)
  389. {
  390. syntax_error (filename, lnum, _("missing action name"));
  391. error = 1;
  392. }
  393. else
  394. {
  395. act[alen] = '\0';
  396. a = lookup_action (act);
  397. if (a == A_info_menu_digit)
  398. {
  399. /* It does not make sense for menu-digit to be anything
  400. other than '0' .. '9'. */
  401. syntax_error (filename, lnum,
  402. _("cannot bind key sequence to menu-digit"));
  403. }
  404. else if (a == -1)
  405. {
  406. /* Print an error message, but keep going (don't set
  407. error = 1) for compatibility with infokey files aimed
  408. at future versions which may have different
  409. actions. */
  410. syntax_error (filename, lnum, _("unknown action `%s'"),
  411. act);
  412. }
  413. else
  414. {
  415. int keymap_bind_keyseq (Keymap, int *, KEYMAP_ENTRY *);
  416. KEYMAP_ENTRY ke;
  417. static InfoCommand invalid_function = { 0 };
  418. ke.type = ISFUNC;
  419. ke.value.function = a != A_INVALID
  420. ? &function_doc_array[a]
  421. : &invalid_function;
  422. To_seq (0);
  423. if (section == info)
  424. keymap_bind_keyseq (info_keymap, seq, &ke);
  425. else /* section == ea */
  426. keymap_bind_keyseq (echo_area_keymap, seq, &ke);
  427. }
  428. }
  429. }
  430. else if (alen < sizeof act - 1)
  431. act[alen++] = c;
  432. else
  433. {
  434. syntax_error (filename, lnum, _("action name too long"));
  435. error = 1;
  436. }
  437. break;
  438. case got_action:
  439. if (c == '#')
  440. state = in_trailing_comment;
  441. else if (c == '\n')
  442. state = start_of_line;
  443. else if (!isspace (c))
  444. {
  445. syntax_error (filename, lnum,
  446. _("extra characters following action `%s'"),
  447. act);
  448. error = 1;
  449. }
  450. break;
  451. case get_varname:
  452. if (c == '=')
  453. {
  454. if (varlen == 0)
  455. {
  456. syntax_error (filename, lnum, _("missing variable name"));
  457. error = 1;
  458. }
  459. state = get_value;
  460. vallen = 0;
  461. }
  462. else if (c == '\n' || isspace (c))
  463. {
  464. syntax_error (filename, lnum,
  465. _("missing `=' immediately after variable name"));
  466. error = 1;
  467. }
  468. else if (varlen < sizeof varn - 1)
  469. varn[varlen++] = c;
  470. else
  471. {
  472. syntax_error (filename, lnum, _("variable name too long"));
  473. error = 1;
  474. }
  475. break;
  476. case get_value:
  477. if (c == '\n')
  478. {
  479. VARIABLE_ALIST *v;
  480. state = start_of_line;
  481. varn[varlen] = '\0';
  482. val[vallen] = '\0';
  483. v = variable_by_name (varn);
  484. if (!v)
  485. info_error (_("%s: no such variable"), varn);
  486. else if (!set_variable_to_value (v, val, SET_IN_CONFIG_FILE))
  487. info_error (_("value %s is not valid for variable %s"),
  488. val, varn);
  489. }
  490. else if (vallen < sizeof val - 1)
  491. val[vallen++] = c;
  492. else
  493. {
  494. syntax_error (filename, lnum, _("value too long"));
  495. error = 1;
  496. }
  497. break;
  498. case get_equals:
  499. case got_equals:
  500. case got_varname:
  501. break;
  502. }
  503. }
  504. #undef To_seq
  505. return !error;
  506. }
  507. /* Return the numeric code of an Info command given its name. If not found,
  508. return -1. This uses the auto-generated array in doc.c. */
  509. static int
  510. lookup_action (const char *name)
  511. {
  512. int i;
  513. if (!strcmp (name, "invalid"))
  514. return A_INVALID;
  515. for (i = 0; function_doc_array[i].func_name; i++)
  516. if (!strcmp (function_doc_array[i].func_name, name))
  517. return i;
  518. return -1;
  519. }
  520. /* Error handling. */
  521. /* Give the user a generic error message in the form
  522. progname: message
  523. */
  524. static void
  525. syntax_error (const char *filename,
  526. unsigned int linenum, const char *fmt, ...)
  527. {
  528. va_list ap;
  529. fprintf (stderr, "%s: ", program_name);
  530. fprintf (stderr, _("\"%s\", line %u: "), filename, linenum);
  531. va_start(ap, fmt);
  532. vfprintf (stderr, fmt, ap);
  533. va_end(ap);
  534. fprintf (stderr, "\n");
  535. }