cc_strings.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. * This file is part of M2-Planet.
  4. *
  5. * M2-Planet 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. *
  10. * M2-Planet is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "cc.h"
  19. #include <stdint.h>
  20. struct token_list* emit(char *s, struct token_list* head);
  21. void require(int bool, char* error);
  22. char upcase(char a)
  23. {
  24. if(in_set(a, "abcdefghijklmnopqrstuvwxyz"))
  25. {
  26. a = a - 32;
  27. }
  28. return a;
  29. }
  30. int char2hex(int c)
  31. {
  32. if (c >= '0' && c <= '9') return (c - 48);
  33. else if (c >= 'a' && c <= 'f') return (c - 87);
  34. else if (c >= 'A' && c <= 'F') return (c - 55);
  35. else return -1;
  36. }
  37. int hexify(int c, int high)
  38. {
  39. int i = char2hex(c);
  40. if(0 > i)
  41. {
  42. fputs("Tried to print non-hex number\n", stderr);
  43. exit(EXIT_FAILURE);
  44. }
  45. if(high)
  46. {
  47. i = i << 4;
  48. }
  49. return i;
  50. }
  51. int escape_lookup(char* c);
  52. int weird(char* string)
  53. {
  54. int c;
  55. string = string + 1;
  56. weird_reset:
  57. c = string[0];
  58. if(0 == c) return FALSE;
  59. if('\\' == c)
  60. {
  61. c = escape_lookup(string);
  62. if('x' == string[1]) string = string + 2;
  63. string = string + 1;
  64. }
  65. if(!in_set(c, "\t\n !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")) return TRUE;
  66. if(in_set(c, " \t\n\r") && (':' == string[1])) return TRUE;
  67. string = string + 1;
  68. goto weird_reset;
  69. }
  70. /* Lookup escape values */
  71. int escape_lookup(char* c)
  72. {
  73. if('\\' != c[0]) return c[0];
  74. if(c[1] == 'x')
  75. {
  76. int t1 = hexify(c[2], TRUE);
  77. int t2 = hexify(c[3], FALSE);
  78. return t1 + t2;
  79. }
  80. else if(c[1] == '0') return 0;
  81. else if(c[1] == 'a') return 7;
  82. else if(c[1] == 'b') return 8;
  83. else if(c[1] == 't') return 9;
  84. else if(c[1] == 'n') return 10;
  85. else if(c[1] == 'v') return 11;
  86. else if(c[1] == 'f') return 12;
  87. else if(c[1] == 'r') return 13;
  88. else if(c[1] == 'e') return 27;
  89. else if(c[1] == '"') return 34;
  90. else if(c[1] == '\'') return 39;
  91. else if(c[1] == '\\') return 92;
  92. fputs("Unknown escape received: ", stderr);
  93. fputs(c, stderr);
  94. fputs(" Unable to process\n", stderr);
  95. exit(EXIT_FAILURE);
  96. }
  97. /* Deal with human strings */
  98. char* collect_regular_string(char* string)
  99. {
  100. string_index = 0;
  101. collect_regular_string_reset:
  102. require((MAX_STRING - 3) > string_index, "Attempt at parsing regular string exceeds max length\n");
  103. if(string[0] == '\\')
  104. {
  105. hold_string[string_index] = escape_lookup(string);
  106. if (string[1] == 'x') string = string + 2;
  107. string = string + 2;
  108. }
  109. else
  110. {
  111. hold_string[string_index] = string[0];
  112. string = string + 1;
  113. }
  114. string_index = string_index + 1;
  115. if(string[0] != 0) goto collect_regular_string_reset;
  116. hold_string[string_index] = '"';
  117. hold_string[string_index + 1] = '\n';
  118. char* message = calloc(string_index + 3, sizeof(char));
  119. require(NULL != message, "Exhausted memory while storing regular string\n");
  120. copy_string(message, hold_string, string_index + 2);
  121. reset_hold_string();
  122. return message;
  123. }
  124. /* Deal with non-human strings */
  125. char* collect_weird_string(char* string)
  126. {
  127. string_index = 1;
  128. int temp;
  129. char* table = "0123456789ABCDEF";
  130. hold_string[0] = '\'';
  131. collect_weird_string_reset:
  132. require((MAX_STRING - 6) > string_index, "Attempt at parsing weird string exceeds max length\n");
  133. string = string + 1;
  134. hold_string[string_index] = ' ';
  135. temp = escape_lookup(string) & 0xFF;
  136. hold_string[string_index + 1] = table[(temp >> 4)];
  137. hold_string[string_index + 2] = table[(temp & 15)];
  138. if(string[0] == '\\')
  139. {
  140. if(string[1] == 'x') string = string + 2;
  141. string = string + 1;
  142. }
  143. string_index = string_index + 3;
  144. if(string[1] != 0) goto collect_weird_string_reset;
  145. hold_string[string_index] = ' ';
  146. hold_string[string_index + 1] = '0';
  147. hold_string[string_index + 2] = '0';
  148. hold_string[string_index + 3] = '\'';
  149. hold_string[string_index + 4] = '\n';
  150. char* hold = calloc(string_index + 6, sizeof(char));
  151. require(NULL != hold, "Exhausted available memory while attempting to collect a weird string\n");
  152. copy_string(hold, hold_string, string_index + 5);
  153. reset_hold_string();
  154. return hold;
  155. }
  156. /* Parse string to deal with hex characters*/
  157. char* parse_string(char* string)
  158. {
  159. /* the string */
  160. if(weird(string)) return collect_weird_string(string);
  161. else return collect_regular_string(string);
  162. }