util.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "header.h"
  2. #include "util.h"
  3. #include "unicode.h"
  4. int power(int base, int exp)
  5. {
  6. int product = 1;
  7. for(int i = exp; i > 0; i--)
  8. product *= base;
  9. return product;
  10. }
  11. static char_t symbols[] = "{}[]()!'£$%^&*-+=:;@~#<>,.?/\\|_\"`";
  12. char_t is_bracket(char_t ch, int quote, int *is_start) {
  13. switch (ch) {
  14. case '[': {
  15. if(is_start != NULL) {
  16. *is_start = TRUE;
  17. }
  18. return ']';
  19. }
  20. case '(': {
  21. if(is_start != NULL) {
  22. *is_start = TRUE;
  23. }
  24. return ')';
  25. }
  26. case '{': {
  27. if(is_start != NULL) {
  28. *is_start = TRUE;
  29. }
  30. return '}';
  31. }
  32. case '<': {
  33. if(is_start != NULL) {
  34. *is_start = TRUE;
  35. }
  36. return '>';
  37. }
  38. case ']': return '[';
  39. case ')': return '(';
  40. case '}': return '{';
  41. case '>': return '<';
  42. case '"': return quote ? '"' : 0;
  43. case '\'': return quote ? '\'' : 0;
  44. case '`': return quote ? '`' : 0;
  45. default: return 0;
  46. }
  47. return 0;
  48. }
  49. int is_symbol(char_t c)
  50. {
  51. register char_t *p = symbols;
  52. for (p = symbols; *p != '\0'; p++)
  53. if (*p == c) return 1;
  54. return 0;
  55. }
  56. // is_symbol ignore
  57. int is_symboli(char_t c, char_t ignore)
  58. {
  59. register char_t *p = symbols;
  60. for (p = symbols; *p != '\0'; p++)
  61. if (*p == c && *p != ignore) return 1;
  62. return 0;
  63. }
  64. // is_symbol ignore string
  65. int is_symbolis(char_t c, const char *ignore)
  66. {
  67. register char_t *p = symbols;
  68. int found = FALSE;
  69. for (p = symbols; *p != '\0'; p++)
  70. if (*p == c) {
  71. for(int i = 0; ignore[i] != '\0'; i++) {
  72. if(ignore[i] == c) {
  73. found = TRUE;
  74. break;
  75. }
  76. }
  77. return !found;
  78. }
  79. return 0;
  80. }
  81. void replace_all(char * str, char oldChar, char newChar)
  82. {
  83. int i = 0;
  84. /* Run till end of string */
  85. while(str[i] != '\0')
  86. {
  87. /* If occurrence of character is found */
  88. if(str[i] == oldChar) {
  89. str[i] = newChar;
  90. }
  91. i++;
  92. }
  93. }
  94. void cleanup_path(char *path, char *output)
  95. {
  96. char *dir, final_path[PATH_MAX+1] = "\0";
  97. const char *list_dirs[20];
  98. int i = 0;
  99. final_path[0] = '/';
  100. dir = strtok(path, "/");
  101. while( dir != NULL ) {
  102. if(dir[0] == '.' && dir[1] == '.') {
  103. i--;
  104. } else {
  105. list_dirs[i] = dir;
  106. i++;
  107. }
  108. dir = strtok(NULL, "/");
  109. }
  110. for(int z = 0 ; i > z; z++) {
  111. strcat(final_path, list_dirs[z]);
  112. if(z != i-1)
  113. strcat(final_path, "/");
  114. }
  115. final_path[PATH_MAX] = '\0';
  116. strcpy(output, final_path);
  117. return;
  118. }
  119. int is_quote(buffer_t *bp, char_t c)
  120. {
  121. int as = TRUE, ab = TRUE;
  122. if(bp != NULL && bp->b_mode != NULL) {
  123. if(!bp->b_mode->sqas) {
  124. as = FALSE;
  125. }
  126. if(!bp->b_mode->bqas) {
  127. ab = FALSE;
  128. }
  129. }
  130. return (c == '\'' && as) || c == '"' || (c == '`' && ab);
  131. }
  132. point_t find_matching_bracket(buffer_t *bp, window_t *wp, int dir, int isrender)
  133. {
  134. char_t *p, z, op, *tp;
  135. point_t cp = bp->b_point;
  136. int depth = 0, newlines = 0, instring = FALSE,
  137. /* only search for matches that are on the current page
  138. wp doesn't update until after render, so we can't use w_row and
  139. should use bp's b_row instead
  140. */
  141. lun = dir == -1 ? wp->w_rows - (bp->b_row - wp->w_rows) : wp->w_rows - bp->b_row;
  142. int isquote = FALSE, skip = FALSE;
  143. p = ptr(bp, cp);
  144. op = *p;
  145. isquote = is_quote(bp, *p);
  146. if((z = is_bracket(*p, TRUE, NULL)) == 0) {
  147. // TODO: jump over whitespace to get to bracket
  148. return -1;
  149. }
  150. if(dir == -1) {
  151. cp--;
  152. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip || instring) && cp >= 0) {
  153. if(*p == '\n')
  154. newlines++;
  155. /* Skip over stuff like '}' or '(' */
  156. if(!skip && is_quote(bp, *p)) {
  157. if(instring) {
  158. if(*p == *tp)
  159. instring = !instring;
  160. } else {
  161. int ii = cp-1, found = FALSE;
  162. tp = ptr(bp, ii);
  163. for(; ((bp->b_mode != NULL && !bp->b_mode->bmls) || *tp != '\n') && ii >= 0; ii--) {
  164. tp = ptr(bp, ii);
  165. if(*tp == *p) {
  166. found = TRUE;
  167. break;
  168. }
  169. if(ii >= 2 && *(tp = ptr(bp, ii-2)) == '\\')
  170. ii--;
  171. }
  172. if(found)
  173. instring = !instring;
  174. }
  175. }
  176. if(*p == op && !skip && !instring) {
  177. depth++;
  178. } else if(*p == z && !instring) {
  179. depth--;
  180. }
  181. skip = FALSE;
  182. /* Imagine this case: "this is \"a\" test."
  183. We want to skip the inner quotes that are escaped with the \.
  184. To detect this, we have to look 2 chars behind to find the \.
  185. */
  186. if(isquote) {
  187. char_t *s;
  188. if(*(s = ptr(bp, cp-2)) == '\\')
  189. skip = TRUE;
  190. }
  191. cp--;
  192. if(newlines > lun && isrender)
  193. break;
  194. }
  195. if(cp >= 0) {
  196. if(*ptr(bp, cp) == z) {
  197. // if(cp < bp->b_page && !isrender)
  198. // bp->b_reframe = 1;
  199. return cp;
  200. }
  201. }
  202. } else {
  203. cp++;
  204. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip || instring) && p <= bp->b_ebuf) {
  205. if(*p == '\n')
  206. newlines++;
  207. /* Skip over stuff like '}' or '(' */
  208. if(!skip && is_quote(bp, *p)) {
  209. if(instring) {
  210. if(*p == *tp)
  211. instring = !instring;
  212. } else {
  213. int ii = 1+cp, found = FALSE;
  214. point_t ep = pos(bp, bp->b_ebuf);
  215. tp = ptr(bp, ii);
  216. for(; ((bp->b_mode != NULL && !bp->b_mode->bmls) || *tp != '\n') && ii <= ep; ii++) {
  217. tp = ptr(bp, ii);
  218. if(*tp == *p) {
  219. found = TRUE;
  220. break;
  221. }
  222. if(*tp == '\\')
  223. ii++;
  224. }
  225. if(found)
  226. instring = !instring;
  227. }
  228. }
  229. if(*p == op && !skip && !instring) {
  230. depth++;
  231. } else if(*p == z && !instring) {
  232. depth--;
  233. }
  234. skip = FALSE;
  235. if(*p == '\\' && isquote)
  236. skip = TRUE;
  237. cp++;
  238. if(newlines > lun && isrender)
  239. break;
  240. }
  241. if(p < bp->b_ebuf) {
  242. if(*ptr(bp, cp) == z) {
  243. if(cp > bp->b_epage && !isrender)
  244. bp->b_reframe = 1;
  245. return cp;
  246. }
  247. }
  248. }
  249. return -1;
  250. }
  251. const char unctrl(char_t p)
  252. {
  253. return p + (char)64;
  254. }
  255. point_t shift_pmark(int dir, point_t opoint)
  256. {
  257. point_t mark = curbp->b_pmark[0];
  258. if(dir) {
  259. if(mark == opoint) {
  260. return NOMARK;
  261. }
  262. for(int i = PMARK_SIZE-1; i > 0; i--) {
  263. curbp->b_pmark[i] = curbp->b_pmark[i-1];
  264. }
  265. curbp->b_pmark[0] = opoint != NOMARK ? opoint : curbp->b_point;
  266. return NOMARK;
  267. }
  268. for(int i = 0; i < PMARK_SIZE ; i++) {
  269. if(i+1 > PMARK_SIZE-1)
  270. curbp->b_pmark[i] = NOMARK;
  271. else
  272. curbp->b_pmark[i] = curbp->b_pmark[i+1];
  273. }
  274. return mark;
  275. }
  276. int is_combining_unicode(uint32_t result)
  277. {
  278. struct combine_t range;
  279. int match = FALSE;
  280. for(int b = 0; b < RANGES_MAX; b++) {
  281. range = unicode_combine_ranges[b];
  282. if(range.end == 0) {
  283. if(range.start == result) {
  284. match = TRUE;
  285. break;
  286. }
  287. } else if(range.start <= result && range.end >= result) {
  288. match = TRUE;
  289. break;
  290. }
  291. }
  292. return match;
  293. }
  294. static const unsigned char utf8_mask[6] = {0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  295. uint32_t char_to_unicode(char_t *p, int nch)
  296. {
  297. /* The following 12 lines come from mle(1) */
  298. char_t mask = utf8_mask[nch - 1];
  299. uint32_t result = p[0] & mask;
  300. int i;
  301. for (i = 1; i < nch /*&& (p + i) < stop*/; ++i) {
  302. result <<= 6;
  303. result |= p[i] & 0x3f;
  304. }
  305. // replace incomplete code point with replacement char
  306. if (i != nch) {
  307. result = 0xfffd;
  308. nch = i;
  309. }
  310. return result;
  311. }
  312. void adjust_bline()
  313. {
  314. if(curbp->b_opoint > curbp->b_point) {
  315. curbp->b_opoint--;
  316. while(curbp->b_opoint >= curbp->b_point) {
  317. if(*ptr(curbp, curbp->b_opoint) == '\n')
  318. curbp->b_line--;
  319. curbp->b_opoint--;
  320. }
  321. } else if(curbp->b_opoint < curbp->b_point) {
  322. while(curbp->b_opoint < curbp->b_point) {
  323. if(*ptr(curbp, curbp->b_opoint) == '\n')
  324. curbp->b_line++;
  325. curbp->b_opoint++;
  326. }
  327. }
  328. }