cc_strings.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. int char2hex(int c);
  22. char upcase(char a)
  23. {
  24. if(in_set(a, "abcdefghijklmnopqrstuvwxyz"))
  25. {
  26. a = a - 32;
  27. }
  28. return a;
  29. }
  30. int hexify(int c, int high)
  31. {
  32. int i = char2hex(c);
  33. if(0 > i)
  34. {
  35. file_print("Tried to print non-hex number\n", stderr);
  36. exit(EXIT_FAILURE);
  37. }
  38. if(high)
  39. {
  40. i = i << 4;
  41. }
  42. return i;
  43. }
  44. int escape_lookup(char* c);
  45. int weird(char* string)
  46. {
  47. int c;
  48. string = string + 1;
  49. weird_reset:
  50. c = string[0];
  51. if(0 == c) return FALSE;
  52. if('\\' == c)
  53. {
  54. c = escape_lookup(string);
  55. if('x' == string[1]) string = string + 2;
  56. string = string + 1;
  57. }
  58. if(!in_set(c, "\t\n !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")) return TRUE;
  59. if(in_set(c, " \t\n\r") && (':' == string[1])) return TRUE;
  60. string = string + 1;
  61. goto weird_reset;
  62. }
  63. /* Lookup escape values */
  64. int escape_lookup(char* c)
  65. {
  66. if('\\' != c[0]) return c[0];
  67. if(c[1] == 'x')
  68. {
  69. int t1 = hexify(c[2], TRUE);
  70. int t2 = hexify(c[3], FALSE);
  71. return t1 + t2;
  72. }
  73. else if(c[1] == '0') return 0;
  74. else if(c[1] == 't') return 9;
  75. else if(c[1] == 'n') return 10;
  76. else if(c[1] == 'v') return 11;
  77. else if(c[1] == 'f') return 12;
  78. else if(c[1] == 'r') return 13;
  79. else if(c[1] == 'e') return 27;
  80. else if(c[1] == '"') return 34;
  81. else if(c[1] == '\'') return 39;
  82. else if(c[1] == '\\') return 92;
  83. file_print("Unknown escape recieved: ", stderr);
  84. file_print(c, stderr);
  85. file_print(" Unable to process\n", stderr);
  86. exit(EXIT_FAILURE);
  87. }
  88. /* Deal with human strings */
  89. char* collect_regular_string(char* string)
  90. {
  91. string_index = 0;
  92. collect_regular_string_reset:
  93. if(string[0] == '\\')
  94. {
  95. hold_string[string_index] = escape_lookup(string);
  96. if (string[1] == 'x') string = string + 2;
  97. string = string + 2;
  98. }
  99. else
  100. {
  101. hold_string[string_index] = string[0];
  102. string = string + 1;
  103. }
  104. string_index = string_index + 1;
  105. if(string[0] != 0) goto collect_regular_string_reset;
  106. hold_string[string_index] = '"';
  107. hold_string[string_index + 1] = '\n';
  108. char* message = calloc(string_index + 3, sizeof(char));
  109. copy_string(message, hold_string);
  110. reset_hold_string();
  111. return message;
  112. }
  113. /* Deal with non-human strings */
  114. char* collect_weird_string(char* string)
  115. {
  116. string_index = 1;
  117. int temp;
  118. char* table = "0123456789ABCDEF";
  119. hold_string[0] = '\'';
  120. collect_weird_string_reset:
  121. string = string + 1;
  122. hold_string[string_index] = ' ';
  123. temp = escape_lookup(string);
  124. hold_string[string_index + 1] = table[(temp >> 4)];
  125. hold_string[string_index + 2] = table[(temp & 15)];
  126. if(string[0] == '\\')
  127. {
  128. if(string[1] == 'x') string = string + 2;
  129. string = string + 1;
  130. }
  131. string_index = string_index + 3;
  132. if(string[1] != 0) goto collect_weird_string_reset;
  133. hold_string[string_index] = ' ';
  134. hold_string[string_index + 1] = '0';
  135. hold_string[string_index + 2] = '0';
  136. hold_string[string_index + 3] = '\'';
  137. hold_string[string_index + 4] = '\n';
  138. char* hold = calloc(string_index + 6, sizeof(char));
  139. copy_string(hold, hold_string);
  140. reset_hold_string();
  141. return hold;
  142. }
  143. /* Parse string to deal with hex characters*/
  144. char* parse_string(char* string)
  145. {
  146. /* the string */
  147. if(weird(string)) return collect_weird_string(string);
  148. else return collect_regular_string(string);
  149. }