syntax.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /* editor syntax highlighting.
  2. Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
  3. 2007 Free Software Foundation, Inc.
  4. Authors: 1998 Paul Sheer
  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 2 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, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. */
  18. /** \file
  19. * \brief Source: editor syntax highlighting
  20. * \author Paul Sheer
  21. * \date 1996, 1997
  22. *
  23. * Mispelled words are flushed from the syntax highlighting rules
  24. * when they have been around longer than
  25. * TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
  26. * chars per second and say 3 chars + a space per word, we can
  27. * accumulate 450 words absolute max with a value of 60. This is
  28. * below this limit of 1024 words in a context.
  29. */
  30. #include <config.h>
  31. #include <stdio.h>
  32. #include <stdarg.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #include <errno.h>
  38. #include <sys/stat.h>
  39. #include <stdlib.h>
  40. #include "../src/global.h"
  41. #include "edit-impl.h"
  42. #include "edit-widget.h"
  43. #include "../src/search/search.h" /* search engine */
  44. #include "../src/skin/skin.h"
  45. #include "edit-impl.h"
  46. #include "edit-widget.h"
  47. #include "../src/main.h" /* mc_home */
  48. #include "../src/wtools.h" /* message() */
  49. #include "../src/strutil.h" /* utf string functions */
  50. /* bytes */
  51. #define SYNTAX_MARKER_DENSITY 512
  52. #define TRANSIENT_WORD_TIME_OUT 60
  53. #define UNKNOWN_FORMAT "unknown"
  54. #define MAX_WORDS_PER_CONTEXT 1024
  55. #define MAX_CONTEXTS 128
  56. #define RULE_ON_LEFT_BORDER 1
  57. #define RULE_ON_RIGHT_BORDER 2
  58. #define SYNTAX_TOKEN_STAR '\001'
  59. #define SYNTAX_TOKEN_PLUS '\002'
  60. #define SYNTAX_TOKEN_BRACKET '\003'
  61. #define SYNTAX_TOKEN_BRACE '\004'
  62. struct key_word {
  63. char *keyword;
  64. unsigned char first;
  65. char *whole_word_chars_left;
  66. char *whole_word_chars_right;
  67. int line_start;
  68. int color;
  69. };
  70. struct context_rule {
  71. char *left;
  72. unsigned char first_left;
  73. char *right;
  74. unsigned char first_right;
  75. char line_start_left;
  76. char line_start_right;
  77. int between_delimiters;
  78. char *whole_word_chars_left;
  79. char *whole_word_chars_right;
  80. char *keyword_first_chars;
  81. int spelling;
  82. /* first word is word[1] */
  83. struct key_word **keyword;
  84. };
  85. struct _syntax_marker {
  86. long offset;
  87. struct syntax_rule rule;
  88. struct _syntax_marker *next;
  89. };
  90. int option_syntax_highlighting = 1;
  91. int option_auto_syntax = 1;
  92. char *option_syntax_type = NULL;
  93. static gint
  94. mc_defines_destroy (gpointer key, gpointer value, gpointer data)
  95. {
  96. char **values = value;
  97. (void) data;
  98. g_free (key);
  99. while (*values)
  100. g_free (*values++);
  101. g_free (value);
  102. return FALSE;
  103. }
  104. /* Completely destroys the defines tree */
  105. static void
  106. destroy_defines (GTree **defines)
  107. {
  108. g_tree_traverse (*defines, mc_defines_destroy, G_POST_ORDER, NULL);
  109. g_tree_destroy (*defines);
  110. *defines = 0;
  111. }
  112. static void
  113. subst_defines (GTree *defines, char **argv, char **argv_end)
  114. {
  115. char **t, **p;
  116. int argc;
  117. while (*argv && argv < argv_end) {
  118. if ((t = g_tree_lookup (defines, *argv))) {
  119. int count = 0;
  120. /* Count argv array members */
  121. argc = 0;
  122. for (p = &argv[1]; *p; p++)
  123. argc++;
  124. /* Count members of definition array */
  125. for (p = t; *p; p++)
  126. count++;
  127. p = &argv[count + argc];
  128. /* Buffer overflow or infinitive loop in define */
  129. if (p >= argv_end)
  130. break;
  131. /* Move rest of argv after definition members */
  132. while (argc >= 0)
  133. *p-- = argv[argc-- + 1];
  134. /* Copy definition members to argv */
  135. for (p = argv; *t; *p++ = *t++);
  136. }
  137. argv++;
  138. }
  139. }
  140. static long
  141. compare_word_to_right (WEdit *edit, long i, const char *text,
  142. const char *whole_left, const char *whole_right,
  143. int line_start)
  144. {
  145. const unsigned char *p, *q;
  146. int c, d, j;
  147. if (!*text)
  148. return -1;
  149. c = edit_get_byte (edit, i - 1);
  150. if (line_start)
  151. if (c != '\n')
  152. return -1;
  153. if (whole_left)
  154. if (strchr (whole_left, c))
  155. return -1;
  156. for (p = (unsigned char *) text, q = p + str_term_width1 ((char *) p); p < q; p++, i++) {
  157. switch (*p) {
  158. case SYNTAX_TOKEN_STAR:
  159. if (++p > q)
  160. return -1;
  161. for (;;) {
  162. c = edit_get_byte (edit, i);
  163. if (!*p)
  164. if (whole_right)
  165. if (!strchr (whole_right, c))
  166. break;
  167. if (c == *p)
  168. break;
  169. if (c == '\n')
  170. return -1;
  171. i++;
  172. }
  173. break;
  174. case SYNTAX_TOKEN_PLUS:
  175. if (++p > q)
  176. return -1;
  177. j = 0;
  178. for (;;) {
  179. c = edit_get_byte (edit, i);
  180. if (c == *p) {
  181. j = i;
  182. if (*p == *text && !p[1]) /* handle eg '+' and @+@ keywords properly */
  183. break;
  184. }
  185. if (j && strchr ((char *) p + 1, c)) /* c exists further down, so it will get matched later */
  186. break;
  187. if (c == '\n' || c == '\t' || c == ' ') {
  188. if (!*p) {
  189. i--;
  190. break;
  191. }
  192. if (!j)
  193. return -1;
  194. i = j;
  195. break;
  196. }
  197. if (whole_right)
  198. if (!strchr (whole_right, c)) {
  199. if (!*p) {
  200. i--;
  201. break;
  202. }
  203. if (!j)
  204. return -1;
  205. i = j;
  206. break;
  207. }
  208. i++;
  209. }
  210. break;
  211. case SYNTAX_TOKEN_BRACKET:
  212. if (++p > q)
  213. return -1;
  214. c = -1;
  215. for (;; i++) {
  216. d = c;
  217. c = edit_get_byte (edit, i);
  218. for (j = 0; p[j] != SYNTAX_TOKEN_BRACKET && p[j]; j++)
  219. if (c == p[j])
  220. goto found_char2;
  221. break;
  222. found_char2:
  223. ; /* dummy command */
  224. }
  225. i--;
  226. while (*p != SYNTAX_TOKEN_BRACKET && p <= q)
  227. p++;
  228. if (p > q)
  229. return -1;
  230. if (p[1] == d)
  231. i--;
  232. break;
  233. case SYNTAX_TOKEN_BRACE:
  234. if (++p > q)
  235. return -1;
  236. c = edit_get_byte (edit, i);
  237. for (; *p != SYNTAX_TOKEN_BRACE && *p; p++)
  238. if (c == *p)
  239. goto found_char3;
  240. return -1;
  241. found_char3:
  242. while (*p != SYNTAX_TOKEN_BRACE && p < q)
  243. p++;
  244. break;
  245. default:
  246. if (*p != edit_get_byte (edit, i))
  247. return -1;
  248. }
  249. }
  250. if (whole_right)
  251. if (strchr (whole_right, edit_get_byte (edit, i)))
  252. return -1;
  253. return i;
  254. }
  255. static const char *xx_strchr (const unsigned char *s, int c)
  256. {
  257. while (*s >= '\005' && *s != (unsigned char) c) {
  258. s++;
  259. }
  260. return (const char *) s;
  261. }
  262. static struct syntax_rule apply_rules_going_right (WEdit * edit, long i, struct syntax_rule rule)
  263. {
  264. struct context_rule *r;
  265. int contextchanged = 0, c;
  266. int found_right = 0, found_left = 0, keyword_foundleft = 0, keyword_foundright = 0;
  267. int is_end;
  268. long end = 0;
  269. struct syntax_rule _rule = rule;
  270. if (!(c = edit_get_byte (edit, i)))
  271. return rule;
  272. is_end = (rule.end == (unsigned char) i);
  273. /* check to turn off a keyword */
  274. if (_rule.keyword) {
  275. if (edit_get_byte (edit, i - 1) == '\n')
  276. _rule.keyword = 0;
  277. if (is_end) {
  278. _rule.keyword = 0;
  279. keyword_foundleft = 1;
  280. }
  281. }
  282. /* check to turn off a context */
  283. if (_rule.context && !_rule.keyword) {
  284. long e;
  285. r = edit->rules[_rule.context];
  286. if (r->first_right == c && !(rule.border & RULE_ON_RIGHT_BORDER) && (e = compare_word_to_right (edit, i, r->right, r->whole_word_chars_left, r->whole_word_chars_right, r->line_start_right)) > 0) {
  287. _rule.end = e;
  288. found_right = 1;
  289. _rule.border = RULE_ON_RIGHT_BORDER;
  290. if (r->between_delimiters)
  291. _rule.context = 0;
  292. } else if (is_end && rule.border & RULE_ON_RIGHT_BORDER) {
  293. /* always turn off a context at 4 */
  294. found_left = 1;
  295. _rule.border = 0;
  296. if (!keyword_foundleft)
  297. _rule.context = 0;
  298. } else if (is_end && rule.border & RULE_ON_LEFT_BORDER) {
  299. /* never turn off a context at 2 */
  300. found_left = 1;
  301. _rule.border = 0;
  302. }
  303. }
  304. /* check to turn on a keyword */
  305. if (!_rule.keyword) {
  306. const char *p;
  307. p = (r = edit->rules[_rule.context])->keyword_first_chars;
  308. if (p)
  309. while (*(p = xx_strchr ((unsigned char *) p + 1, c))) {
  310. struct key_word *k;
  311. int count;
  312. long e;
  313. count = p - r->keyword_first_chars;
  314. k = r->keyword[count];
  315. e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left, k->whole_word_chars_right, k->line_start);
  316. if (e > 0) {
  317. end = e;
  318. _rule.end = e;
  319. _rule.keyword = count;
  320. keyword_foundright = 1;
  321. break;
  322. }
  323. }
  324. }
  325. /* check to turn on a context */
  326. if (!_rule.context) {
  327. if (!found_left && is_end) {
  328. if (rule.border & RULE_ON_RIGHT_BORDER) {
  329. _rule.border = 0;
  330. _rule.context = 0;
  331. contextchanged = 1;
  332. _rule.keyword = 0;
  333. } else if (rule.border & RULE_ON_LEFT_BORDER) {
  334. r = edit->rules[_rule._context];
  335. _rule.border = 0;
  336. if (r->between_delimiters) {
  337. long e;
  338. _rule.context = _rule._context;
  339. contextchanged = 1;
  340. _rule.keyword = 0;
  341. if (r->first_right == c && (e = compare_word_to_right (edit, i, r->right, r->whole_word_chars_left, r->whole_word_chars_right, r->line_start_right)) >= end) {
  342. _rule.end = e;
  343. found_right = 1;
  344. _rule.border = RULE_ON_RIGHT_BORDER;
  345. _rule.context = 0;
  346. }
  347. }
  348. }
  349. }
  350. if (!found_right) {
  351. int count;
  352. struct context_rule **rules = edit->rules;
  353. for (count = 1; rules[count]; count++) {
  354. r = rules[count];
  355. if (r->first_left == c) {
  356. long e;
  357. e = compare_word_to_right (edit, i, r->left, r->whole_word_chars_left, r->whole_word_chars_right, r->line_start_left);
  358. if (e >= end && (!_rule.keyword || keyword_foundright)) {
  359. _rule.end = e;
  360. found_right = 1;
  361. _rule.border = RULE_ON_LEFT_BORDER;
  362. _rule._context = count;
  363. if (!r->between_delimiters)
  364. if (!_rule.keyword) {
  365. _rule.context = count;
  366. contextchanged = 1;
  367. }
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. }
  374. /* check again to turn on a keyword if the context switched */
  375. if (contextchanged && !_rule.keyword) {
  376. const char *p;
  377. p = (r = edit->rules[_rule.context])->keyword_first_chars;
  378. while (*(p = xx_strchr ((unsigned char *) p + 1, c))) {
  379. struct key_word *k;
  380. int count;
  381. long e;
  382. count = p - r->keyword_first_chars;
  383. k = r->keyword[count];
  384. e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left, k->whole_word_chars_right, k->line_start);
  385. if (e > 0) {
  386. _rule.end = e;
  387. _rule.keyword = count;
  388. break;
  389. }
  390. }
  391. }
  392. return _rule;
  393. }
  394. static struct syntax_rule edit_get_rule (WEdit * edit, long byte_index)
  395. {
  396. long i;
  397. if (byte_index > edit->last_get_rule) {
  398. for (i = edit->last_get_rule + 1; i <= byte_index; i++) {
  399. edit->rule = apply_rules_going_right (edit, i, edit->rule);
  400. if (i > (edit->syntax_marker ? edit->syntax_marker->offset + SYNTAX_MARKER_DENSITY : SYNTAX_MARKER_DENSITY)) {
  401. struct _syntax_marker *s;
  402. s = edit->syntax_marker;
  403. edit->syntax_marker = g_malloc0 (sizeof (struct _syntax_marker));
  404. edit->syntax_marker->next = s;
  405. edit->syntax_marker->offset = i;
  406. edit->syntax_marker->rule = edit->rule;
  407. }
  408. }
  409. } else if (byte_index < edit->last_get_rule) {
  410. struct _syntax_marker *s;
  411. for (;;) {
  412. if (!edit->syntax_marker) {
  413. memset (&edit->rule, 0, sizeof (edit->rule));
  414. for (i = -1; i <= byte_index; i++)
  415. edit->rule = apply_rules_going_right (edit, i, edit->rule);
  416. break;
  417. }
  418. if (byte_index >= edit->syntax_marker->offset) {
  419. edit->rule = edit->syntax_marker->rule;
  420. for (i = edit->syntax_marker->offset + 1; i <= byte_index; i++)
  421. edit->rule = apply_rules_going_right (edit, i, edit->rule);
  422. break;
  423. }
  424. s = edit->syntax_marker->next;
  425. MC_PTR_FREE (edit->syntax_marker);
  426. edit->syntax_marker = s;
  427. }
  428. }
  429. edit->last_get_rule = byte_index;
  430. return edit->rule;
  431. }
  432. static void translate_rule_to_color (WEdit * edit, struct syntax_rule rule, int *color)
  433. {
  434. struct key_word *k;
  435. k = edit->rules[rule.context]->keyword[rule.keyword];
  436. *color = k->color;
  437. }
  438. void edit_get_syntax_color (WEdit * edit, long byte_index, int *color)
  439. {
  440. if (edit->rules && byte_index < edit->last_byte &&
  441. option_syntax_highlighting && tty_use_colors ()) {
  442. translate_rule_to_color (edit, edit_get_rule (edit, byte_index), color);
  443. } else {
  444. *color = tty_use_colors () ? mc_skin_color_get("editor", "_default_") : 0;
  445. }
  446. }
  447. /*
  448. Returns 0 on error/eof or a count of the number of bytes read
  449. including the newline. Result must be free'd.
  450. In case of an error, *line will not be modified.
  451. */
  452. static int read_one_line (char **line, FILE * f)
  453. {
  454. GString *p = g_string_new ("");
  455. int c, r = 0;
  456. for (;;) {
  457. c = fgetc (f);
  458. if (c == EOF) {
  459. if (ferror (f)) {
  460. if (errno == EINTR)
  461. continue;
  462. r = 0;
  463. }
  464. break;
  465. }
  466. r++;
  467. /* handle all of \r\n, \r, \n correctly. */
  468. if (c == '\r') {
  469. if ( (c = fgetc (f)) == '\n')
  470. r++;
  471. else
  472. ungetc (c, f);
  473. break;
  474. }
  475. if (c == '\n')
  476. break;
  477. g_string_append_c (p, c);
  478. }
  479. if (r != 0) {
  480. *line = p->str;
  481. g_string_free (p, FALSE);
  482. } else {
  483. g_string_free (p, TRUE);
  484. }
  485. return r;
  486. }
  487. static char *convert (char *s)
  488. {
  489. char *r, *p;
  490. p = r = s;
  491. while (*s) {
  492. switch (*s) {
  493. case '\\':
  494. s++;
  495. switch (*s) {
  496. case ' ':
  497. *p = ' ';
  498. s--;
  499. break;
  500. case 'n':
  501. *p = '\n';
  502. break;
  503. case 'r':
  504. *p = '\r';
  505. break;
  506. case 't':
  507. *p = '\t';
  508. break;
  509. case 's':
  510. *p = ' ';
  511. break;
  512. case '*':
  513. *p = '*';
  514. break;
  515. case '\\':
  516. *p = '\\';
  517. break;
  518. case '[':
  519. case ']':
  520. *p = SYNTAX_TOKEN_BRACKET;
  521. break;
  522. case '{':
  523. case '}':
  524. *p = SYNTAX_TOKEN_BRACE;
  525. break;
  526. case 0:
  527. *p = *s;
  528. return r;
  529. default:
  530. *p = *s;
  531. break;
  532. }
  533. break;
  534. case '*':
  535. *p = SYNTAX_TOKEN_STAR;
  536. break;
  537. case '+':
  538. *p = SYNTAX_TOKEN_PLUS;
  539. break;
  540. default:
  541. *p = *s;
  542. break;
  543. }
  544. s++;
  545. p++;
  546. }
  547. *p = '\0';
  548. return r;
  549. }
  550. #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
  551. static int get_args (char *l, char **args, int args_size)
  552. {
  553. int argc = 0;
  554. while (argc < args_size) {
  555. char *p = l;
  556. while (*p && whiteness (*p))
  557. p++;
  558. if (!*p)
  559. break;
  560. for (l = p + 1; *l && !whiteness (*l); l++);
  561. if (*l)
  562. *l++ = '\0';
  563. args[argc++] = convert (p);
  564. }
  565. args[argc] = (char *) NULL;
  566. return argc;
  567. }
  568. #define free_args(x)
  569. #define break_a {result=line;break;}
  570. #define check_a {if(!*a){result=line;break;}}
  571. #define check_not_a {if(*a){result=line;break;}}
  572. static int
  573. this_try_alloc_color_pair (const char *fg, const char *bg)
  574. {
  575. char f[80], b[80], *p;
  576. if (bg)
  577. if (!*bg)
  578. bg = 0;
  579. if (fg)
  580. if (!*fg)
  581. fg = 0;
  582. if (fg) {
  583. g_strlcpy (f, fg, sizeof (f));
  584. p = strchr (f, '/');
  585. if (p)
  586. *p = '\0';
  587. fg = f;
  588. }
  589. if (bg) {
  590. g_strlcpy (b, bg, sizeof (b));
  591. p = strchr (b, '/');
  592. if (p)
  593. *p = '\0';
  594. bg = b;
  595. }
  596. return tty_try_alloc_color_pair (fg, bg);
  597. }
  598. static char *error_file_name = 0;
  599. static FILE *open_include_file (const char *filename)
  600. {
  601. FILE *f;
  602. MC_PTR_FREE (error_file_name);
  603. error_file_name = g_strdup (filename);
  604. if (*filename == PATH_SEP)
  605. return fopen (filename, "r");
  606. g_free (error_file_name);
  607. error_file_name = g_strconcat (home_dir, PATH_SEP_STR EDIT_DIR PATH_SEP_STR,
  608. filename, (char *) NULL);
  609. f = fopen (error_file_name, "r");
  610. if (f)
  611. return f;
  612. g_free (error_file_name);
  613. error_file_name = g_strconcat (mc_home, PATH_SEP_STR, "syntax", PATH_SEP_STR,
  614. filename, (char *) NULL);
  615. if ((f = fopen (error_file_name, "r"))) {
  616. g_free (error_file_name);
  617. return f;
  618. }
  619. g_free (error_file_name);
  620. error_file_name = g_strconcat (mc_home_alt, PATH_SEP_STR "syntax" PATH_SEP_STR,
  621. filename, (char *) NULL);
  622. return fopen (error_file_name, "r");
  623. }
  624. /* returns line number on error */
  625. static int
  626. edit_read_syntax_rules (WEdit *edit, FILE *f, char **args, int args_size)
  627. {
  628. FILE *g = 0;
  629. char *fg, *bg;
  630. char last_fg[32] = "", last_bg[32] = "";
  631. char whole_right[512];
  632. char whole_left[512];
  633. char *l = 0;
  634. int save_line = 0, line = 0;
  635. struct context_rule **r, *c = 0;
  636. int num_words = -1, num_contexts = -1;
  637. int result = 0;
  638. int argc;
  639. int i, j;
  640. int alloc_contexts = MAX_CONTEXTS,
  641. alloc_words_per_context = MAX_WORDS_PER_CONTEXT,
  642. max_alloc_words_per_context = MAX_WORDS_PER_CONTEXT;
  643. args[0] = 0;
  644. strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
  645. strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
  646. r = edit->rules = g_malloc0 (alloc_contexts * sizeof (struct context_rule *));
  647. if (!edit->defines)
  648. edit->defines = g_tree_new ((GCompareFunc) strcmp);
  649. for (;;) {
  650. char **a;
  651. line++;
  652. l = 0;
  653. if (!read_one_line (&l, f)) {
  654. if (g) {
  655. fclose (f);
  656. f = g;
  657. g = 0;
  658. line = save_line + 1;
  659. MC_PTR_FREE (error_file_name);
  660. MC_PTR_FREE (l);
  661. if (!read_one_line (&l, f))
  662. break;
  663. } else {
  664. break;
  665. }
  666. }
  667. argc = get_args (l, args, args_size);
  668. a = args + 1;
  669. if (!args[0]) {
  670. /* do nothing */
  671. } else if (!strcmp (args[0], "include")) {
  672. if (g || argc != 2) {
  673. result = line;
  674. break;
  675. }
  676. g = f;
  677. f = open_include_file (args[1]);
  678. if (!f) {
  679. MC_PTR_FREE (error_file_name);
  680. result = line;
  681. break;
  682. }
  683. save_line = line;
  684. line = 0;
  685. } else if (!strcmp (args[0], "wholechars")) {
  686. check_a;
  687. if (!strcmp (*a, "left")) {
  688. a++;
  689. g_strlcpy (whole_left, *a, sizeof (whole_left));
  690. } else if (!strcmp (*a, "right")) {
  691. a++;
  692. g_strlcpy (whole_right, *a, sizeof (whole_right));
  693. } else {
  694. g_strlcpy (whole_left, *a, sizeof (whole_left));
  695. g_strlcpy (whole_right, *a, sizeof (whole_right));
  696. }
  697. a++;
  698. check_not_a;
  699. } else if (!strcmp (args[0], "context")) {
  700. check_a;
  701. if (num_contexts == -1) {
  702. if (strcmp (*a, "default")) { /* first context is the default */
  703. break_a;
  704. }
  705. a++;
  706. c = r[0] = g_malloc0 (sizeof (struct context_rule));
  707. c->left = g_strdup (" ");
  708. c->right = g_strdup (" ");
  709. num_contexts = 0;
  710. } else {
  711. /* Terminate previous context. */
  712. r[num_contexts - 1]->keyword[num_words] = NULL;
  713. c = r[num_contexts] = g_malloc0 (sizeof (struct context_rule));
  714. if (!strcmp (*a, "exclusive")) {
  715. a++;
  716. c->between_delimiters = 1;
  717. }
  718. check_a;
  719. if (!strcmp (*a, "whole")) {
  720. a++;
  721. c->whole_word_chars_left = g_strdup (whole_left);
  722. c->whole_word_chars_right = g_strdup (whole_right);
  723. } else if (!strcmp (*a, "wholeleft")) {
  724. a++;
  725. c->whole_word_chars_left = g_strdup (whole_left);
  726. } else if (!strcmp (*a, "wholeright")) {
  727. a++;
  728. c->whole_word_chars_right = g_strdup (whole_right);
  729. }
  730. check_a;
  731. if (!strcmp (*a, "linestart")) {
  732. a++;
  733. c->line_start_left = 1;
  734. }
  735. check_a;
  736. c->left = g_strdup (*a++);
  737. check_a;
  738. if (!strcmp (*a, "linestart")) {
  739. a++;
  740. c->line_start_right = 1;
  741. }
  742. check_a;
  743. c->right = g_strdup (*a++);
  744. c->first_left = *c->left;
  745. c->first_right = *c->right;
  746. }
  747. c->keyword = g_malloc (alloc_words_per_context * sizeof (struct key_word *));
  748. num_words = 1;
  749. c->keyword[0] = g_malloc0 (sizeof (struct key_word));
  750. subst_defines (edit->defines, a, &args[1024]);
  751. fg = *a;
  752. if (*a)
  753. a++;
  754. bg = *a;
  755. if (*a)
  756. a++;
  757. g_strlcpy (last_fg, fg ? fg : "", sizeof (last_fg));
  758. g_strlcpy (last_bg, bg ? bg : "", sizeof (last_bg));
  759. c->keyword[0]->color = this_try_alloc_color_pair (fg, bg);
  760. c->keyword[0]->keyword = g_strdup (" ");
  761. check_not_a;
  762. alloc_words_per_context = MAX_WORDS_PER_CONTEXT;
  763. if (++num_contexts >= alloc_contexts) {
  764. struct context_rule **tmp;
  765. alloc_contexts += 128;
  766. tmp = g_realloc (r, alloc_contexts * sizeof (struct context_rule *));
  767. r = tmp;
  768. }
  769. } else if (!strcmp (args[0], "spellcheck")) {
  770. if (!c) {
  771. result = line;
  772. break;
  773. }
  774. c->spelling = 1;
  775. } else if (!strcmp (args[0], "keyword")) {
  776. struct key_word *k;
  777. if (num_words == -1)
  778. break_a;
  779. check_a;
  780. k = r[num_contexts - 1]->keyword[num_words] = g_malloc0 (sizeof (struct key_word));
  781. if (!strcmp (*a, "whole")) {
  782. a++;
  783. k->whole_word_chars_left = g_strdup (whole_left);
  784. k->whole_word_chars_right = g_strdup (whole_right);
  785. } else if (!strcmp (*a, "wholeleft")) {
  786. a++;
  787. k->whole_word_chars_left = g_strdup (whole_left);
  788. } else if (!strcmp (*a, "wholeright")) {
  789. a++;
  790. k->whole_word_chars_right = g_strdup (whole_right);
  791. }
  792. check_a;
  793. if (!strcmp (*a, "linestart")) {
  794. a++;
  795. k->line_start = 1;
  796. }
  797. check_a;
  798. if (!strcmp (*a, "whole")) {
  799. break_a;
  800. }
  801. k->keyword = g_strdup (*a++);
  802. k->first = *k->keyword;
  803. subst_defines (edit->defines, a, &args[1024]);
  804. fg = *a;
  805. if (*a)
  806. a++;
  807. bg = *a;
  808. if (*a)
  809. a++;
  810. if (!fg)
  811. fg = last_fg;
  812. if (!bg)
  813. bg = last_bg;
  814. k->color = this_try_alloc_color_pair (fg, bg);
  815. check_not_a;
  816. if (++num_words >= alloc_words_per_context) {
  817. struct key_word **tmp;
  818. alloc_words_per_context += 1024;
  819. if (alloc_words_per_context > max_alloc_words_per_context)
  820. max_alloc_words_per_context = alloc_words_per_context;
  821. tmp = g_realloc (c->keyword, alloc_words_per_context * sizeof (struct key_word *));
  822. c->keyword = tmp;
  823. }
  824. } else if (*(args[0]) == '#') {
  825. /* do nothing for comment */
  826. } else if (!strcmp (args[0], "file")) {
  827. break;
  828. } else if (!strcmp (args[0], "define")) {
  829. char *key = *a++;
  830. char **argv;
  831. if (argc < 3)
  832. break_a;
  833. if ((argv = g_tree_lookup (edit->defines, key))) {
  834. mc_defines_destroy (NULL, argv, NULL);
  835. } else {
  836. key = g_strdup (key);
  837. }
  838. argv = g_new (char *, argc - 1);
  839. g_tree_insert (edit->defines, key, argv);
  840. while (*a) {
  841. *argv++ = g_strdup (*a++);
  842. };
  843. *argv = NULL;
  844. } else { /* anything else is an error */
  845. break_a;
  846. }
  847. free_args (args);
  848. MC_PTR_FREE (l);
  849. }
  850. free_args (args);
  851. MC_PTR_FREE (l);
  852. /* Terminate context array. */
  853. if (num_contexts > 0) {
  854. r[num_contexts - 1]->keyword[num_words] = NULL;
  855. r[num_contexts] = NULL;
  856. }
  857. if (!edit->rules[0])
  858. MC_PTR_FREE (edit->rules);
  859. if (result)
  860. return result;
  861. if (num_contexts == -1) {
  862. return line;
  863. }
  864. {
  865. char *first_chars, *p;
  866. first_chars = g_malloc (max_alloc_words_per_context + 2);
  867. for (i = 0; edit->rules[i]; i++) {
  868. c = edit->rules[i];
  869. p = first_chars;
  870. *p++ = (char) 1;
  871. for (j = 1; c->keyword[j]; j++)
  872. *p++ = c->keyword[j]->first;
  873. *p = '\0';
  874. c->keyword_first_chars = g_strdup (first_chars);
  875. }
  876. g_free (first_chars);
  877. }
  878. return result;
  879. }
  880. void edit_free_syntax_rules (WEdit * edit)
  881. {
  882. int i, j;
  883. if (!edit)
  884. return;
  885. if (edit->defines)
  886. destroy_defines (&edit->defines);
  887. if (!edit->rules)
  888. return;
  889. edit_get_rule (edit, -1);
  890. MC_PTR_FREE (edit->syntax_type);
  891. edit->syntax_type = 0;
  892. for (i = 0; edit->rules[i]; i++) {
  893. if (edit->rules[i]->keyword) {
  894. for (j = 0; edit->rules[i]->keyword[j]; j++) {
  895. MC_PTR_FREE (edit->rules[i]->keyword[j]->keyword);
  896. MC_PTR_FREE (edit->rules[i]->keyword[j]->whole_word_chars_left);
  897. MC_PTR_FREE (edit->rules[i]->keyword[j]->whole_word_chars_right);
  898. MC_PTR_FREE (edit->rules[i]->keyword[j]);
  899. }
  900. }
  901. MC_PTR_FREE (edit->rules[i]->left);
  902. MC_PTR_FREE (edit->rules[i]->right);
  903. MC_PTR_FREE (edit->rules[i]->whole_word_chars_left);
  904. MC_PTR_FREE (edit->rules[i]->whole_word_chars_right);
  905. MC_PTR_FREE (edit->rules[i]->keyword);
  906. MC_PTR_FREE (edit->rules[i]->keyword_first_chars);
  907. MC_PTR_FREE (edit->rules[i]);
  908. }
  909. while (edit->syntax_marker) {
  910. struct _syntax_marker *s = edit->syntax_marker->next;
  911. MC_PTR_FREE (edit->syntax_marker);
  912. edit->syntax_marker = s;
  913. }
  914. MC_PTR_FREE (edit->rules);
  915. tty_color_free_all_tmp();
  916. }
  917. /* returns -1 on file error, line number on error in file syntax */
  918. static int
  919. edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file,
  920. const char *editor_file, const char *first_line,
  921. const char *type)
  922. {
  923. #define NENTRIES 30
  924. FILE *f, *g = NULL;
  925. char *args[1024], *l = 0;
  926. int line = 0;
  927. int result = 0;
  928. int count = 0;
  929. char *lib_file;
  930. int found = 0;
  931. char **tmpnames = NULL;
  932. f = fopen (syntax_file, "r");
  933. if (!f){
  934. lib_file = concat_dir_and_file (mc_home, "Syntax");
  935. f = fopen (lib_file, "r");
  936. g_free (lib_file);
  937. if (!f)
  938. return -1;
  939. }
  940. args[0] = 0;
  941. for (;;) {
  942. line++;
  943. MC_PTR_FREE (l);
  944. if (!read_one_line (&l, f))
  945. break;
  946. (void)get_args (l, args, 1023); /* Final NULL */
  947. if (!args[0])
  948. continue;
  949. /* Looking for `include ...` lines before first `file ...` ones */
  950. if (!found && !strcmp (args[0], "include")) {
  951. if (g)
  952. continue;
  953. if (!args[1] || !(g = open_include_file (args[1]))) {
  954. result = line;
  955. break;
  956. }
  957. goto found_type;
  958. }
  959. /* looking for `file ...' lines only */
  960. if (strcmp (args[0], "file")) {
  961. continue;
  962. }
  963. found = 1;
  964. /* must have two args or report error */
  965. if (!args[1] || !args[2]) {
  966. result = line;
  967. break;
  968. }
  969. if (pnames && *pnames) {
  970. /* 1: just collecting a list of names of rule sets */
  971. /* Reallocate the list if required */
  972. if (count % NENTRIES == 0) {
  973. if ((tmpnames = (char**) g_realloc (*pnames, (count + NENTRIES
  974. + 1) * sizeof (char*))) != NULL)
  975. *pnames = tmpnames;
  976. else
  977. abort ();
  978. }
  979. (*pnames)[count++] = g_strdup (args[2]);
  980. (*pnames)[count] = NULL;
  981. } else if (type) {
  982. /* 2: rule set was explicitly specified by the caller */
  983. if (!strcmp (type, args[2]))
  984. goto found_type;
  985. } else if (editor_file && edit) {
  986. /* 3: auto-detect rule set from regular expressions */
  987. int q;
  988. q = mc_search(args[1], editor_file, MC_SEARCH_T_REGEX);
  989. /* does filename match arg 1 ? */
  990. if (!q && args[3]) {
  991. /* does first line match arg 3 ? */
  992. q = mc_search(args[3], first_line, MC_SEARCH_T_REGEX);
  993. }
  994. if (q) {
  995. int line_error;
  996. char *syntax_type;
  997. found_type:
  998. syntax_type = args[2];
  999. line_error = edit_read_syntax_rules (edit, g ? g : f, args, 1023);
  1000. if (line_error) {
  1001. if (!error_file_name) /* an included file */
  1002. result = line + line_error;
  1003. else
  1004. result = line_error;
  1005. } else {
  1006. MC_PTR_FREE (edit->syntax_type);
  1007. edit->syntax_type = g_strdup (syntax_type);
  1008. /* if there are no rules then turn off syntax highlighting for speed */
  1009. if (!g && !edit->rules[1])
  1010. if (!edit->rules[0]->keyword[1] && !edit->rules[0]->spelling) {
  1011. edit_free_syntax_rules (edit);
  1012. break;
  1013. }
  1014. }
  1015. if (g) {
  1016. fclose (g);
  1017. g = NULL;
  1018. } else {
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. MC_PTR_FREE (l);
  1025. fclose (f);
  1026. return result;
  1027. }
  1028. static char *get_first_editor_line (WEdit * edit)
  1029. {
  1030. int i;
  1031. static char s[256];
  1032. s[0] = '\0';
  1033. if (!edit)
  1034. return s;
  1035. for (i = 0; i < 255; i++) {
  1036. s[i] = edit_get_byte (edit, i);
  1037. if (s[i] == '\n') {
  1038. s[i] = '\0';
  1039. break;
  1040. }
  1041. }
  1042. s[255] = '\0';
  1043. return s;
  1044. }
  1045. /*
  1046. * Load rules into edit struct. Either edit or *pnames must be NULL. If
  1047. * edit is NULL, a list of types will be stored into names. If type is
  1048. * NULL, then the type will be selected according to the filename.
  1049. */
  1050. void
  1051. edit_load_syntax (WEdit *edit, char ***pnames, const char *type)
  1052. {
  1053. int r;
  1054. char *f = NULL;
  1055. if (option_auto_syntax)
  1056. type = NULL;
  1057. edit_free_syntax_rules (edit);
  1058. if (!tty_use_colors ())
  1059. return;
  1060. if (!option_syntax_highlighting && (!pnames || !*pnames))
  1061. return;
  1062. if (edit) {
  1063. if (!edit->filename)
  1064. return;
  1065. if (!*edit->filename && !type)
  1066. return;
  1067. }
  1068. f = concat_dir_and_file (home_dir, EDIT_SYNTAX_FILE);
  1069. r = edit_read_syntax_file (edit, pnames, f, edit ? edit->filename : 0,
  1070. get_first_editor_line (edit), type);
  1071. if (r == -1) {
  1072. edit_free_syntax_rules (edit);
  1073. message (D_ERROR, _(" Load syntax file "),
  1074. _(" Cannot open file %s \n %s "), f,
  1075. unix_error_string (errno));
  1076. } else if (r) {
  1077. edit_free_syntax_rules (edit);
  1078. message (D_ERROR, _(" Load syntax file "),
  1079. _(" Error in file %s on line %d "),
  1080. error_file_name ? error_file_name : f, r);
  1081. MC_PTR_FREE (error_file_name);
  1082. } else {
  1083. /* succeeded */
  1084. }
  1085. g_free (f);
  1086. }
  1087. const char *
  1088. edit_get_syntax_type (const WEdit *edit)
  1089. {
  1090. return edit->syntax_type;
  1091. }