syntax.c 28 KB

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