2017-11-05 14:23:17 +00:00
|
|
|
/* Copyright (C) 2016 Jeremiah Orians
|
2018-08-27 02:15:32 +01:00
|
|
|
* Copyright (C) 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2018-10-18 00:27:06 +01:00
|
|
|
* This file is part of M2-Planet.
|
2017-11-05 14:23:17 +00:00
|
|
|
*
|
2018-10-18 00:27:06 +01:00
|
|
|
* M2-Planet is free software: you can redistribute it and/or modify
|
2017-11-05 14:23:17 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2018-10-18 00:27:06 +01:00
|
|
|
* M2-Planet is distributed in the hope that it will be useful,
|
2017-11-05 14:23:17 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-10-18 00:27:06 +01:00
|
|
|
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
|
2017-11-05 14:23:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cc.h"
|
2017-11-05 22:13:02 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2017-12-15 23:17:29 +00:00
|
|
|
struct token_list* emit(char *s, struct token_list* head);
|
2018-06-02 01:05:27 +01:00
|
|
|
int char2hex(int c);
|
2017-11-05 14:23:17 +00:00
|
|
|
|
|
|
|
char upcase(char a)
|
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
if(in_set(a, "abcdefghijklmnopqrstuvwxyz"))
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
|
|
|
a = a - 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
int hexify(int c, int high)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-06-02 01:05:27 +01:00
|
|
|
int i = char2hex(c);
|
|
|
|
|
|
|
|
if(0 > i)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-06-02 01:05:27 +01:00
|
|
|
file_print("Tried to print non-hex number\n", stderr);
|
2017-11-05 22:13:02 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(high)
|
|
|
|
{
|
2018-06-02 01:05:27 +01:00
|
|
|
i = i << 4;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
2018-06-02 01:05:27 +01:00
|
|
|
return i;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 18:11:02 +01:00
|
|
|
int escape_lookup(char* c);
|
2018-01-25 01:00:22 +00:00
|
|
|
int weird(char* string)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
int c;
|
|
|
|
string = string + 1;
|
|
|
|
weird_reset:
|
|
|
|
c = string[0];
|
|
|
|
if(0 == c) return FALSE;
|
|
|
|
if('\\' == c)
|
2017-12-15 23:17:29 +00:00
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
c = escape_lookup(string);
|
|
|
|
if('x' == string[1]) string = string + 2;
|
|
|
|
string = string + 1;
|
2017-12-15 23:17:29 +00:00
|
|
|
}
|
2018-08-12 18:11:02 +01:00
|
|
|
|
|
|
|
if(!in_set(c, "\t\n !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")) return TRUE;
|
|
|
|
if(in_set(c, " \t\n\r") && (':' == string[1])) return TRUE;
|
|
|
|
string = string + 1;
|
|
|
|
goto weird_reset;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
/* Lookup escape values */
|
|
|
|
int escape_lookup(char* c)
|
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
if('\\' != c[0]) return c[0];
|
|
|
|
|
|
|
|
if(c[1] == 'x')
|
2018-06-02 01:05:27 +01:00
|
|
|
{
|
|
|
|
int t1 = hexify(c[2], TRUE);
|
|
|
|
int t2 = hexify(c[3], FALSE);
|
|
|
|
return t1 + t2;
|
|
|
|
}
|
2019-02-23 16:05:20 +00:00
|
|
|
else if(c[1] == '0') return 0;
|
2019-10-19 19:25:51 +01:00
|
|
|
else if(c[1] == 'a') return 7;
|
|
|
|
else if(c[1] == 'b') return 8;
|
2018-08-12 18:11:02 +01:00
|
|
|
else if(c[1] == 't') return 9;
|
2018-08-27 02:15:32 +01:00
|
|
|
else if(c[1] == 'n') return 10;
|
|
|
|
else if(c[1] == 'v') return 11;
|
|
|
|
else if(c[1] == 'f') return 12;
|
2018-08-12 18:11:02 +01:00
|
|
|
else if(c[1] == 'r') return 13;
|
2018-08-27 02:15:32 +01:00
|
|
|
else if(c[1] == 'e') return 27;
|
|
|
|
else if(c[1] == '"') return 34;
|
|
|
|
else if(c[1] == '\'') return 39;
|
|
|
|
else if(c[1] == '\\') return 92;
|
2018-06-02 01:05:27 +01:00
|
|
|
|
2019-10-23 20:04:14 +01:00
|
|
|
file_print("Unknown escape received: ", stderr);
|
2018-06-02 01:05:27 +01:00
|
|
|
file_print(c, stderr);
|
|
|
|
file_print(" Unable to process\n", stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-12-15 23:17:29 +00:00
|
|
|
/* Deal with human strings */
|
|
|
|
char* collect_regular_string(char* string)
|
|
|
|
{
|
2018-07-27 00:51:44 +01:00
|
|
|
string_index = 0;
|
2018-01-25 01:00:22 +00:00
|
|
|
|
2018-08-12 18:11:02 +01:00
|
|
|
collect_regular_string_reset:
|
|
|
|
if(string[0] == '\\')
|
|
|
|
{
|
|
|
|
hold_string[string_index] = escape_lookup(string);
|
|
|
|
if (string[1] == 'x') string = string + 2;
|
|
|
|
string = string + 2;
|
|
|
|
}
|
|
|
|
else
|
2017-12-15 23:17:29 +00:00
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
hold_string[string_index] = string[0];
|
|
|
|
string = string + 1;
|
2017-12-15 23:17:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 18:11:02 +01:00
|
|
|
string_index = string_index + 1;
|
|
|
|
if(string[0] != 0) goto collect_regular_string_reset;
|
|
|
|
|
|
|
|
hold_string[string_index] = '"';
|
|
|
|
hold_string[string_index + 1] = '\n';
|
2018-07-27 00:51:44 +01:00
|
|
|
char* message = calloc(string_index + 3, sizeof(char));
|
|
|
|
copy_string(message, hold_string);
|
|
|
|
reset_hold_string();
|
2017-12-15 23:17:29 +00:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with non-human strings */
|
|
|
|
char* collect_weird_string(char* string)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-07-27 00:51:44 +01:00
|
|
|
string_index = 1;
|
2018-06-26 22:03:17 +01:00
|
|
|
int temp;
|
2018-01-25 01:00:22 +00:00
|
|
|
char* table = "0123456789ABCDEF";
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-08-10 22:30:48 +01:00
|
|
|
hold_string[0] = '\'';
|
2018-08-16 17:01:55 +01:00
|
|
|
collect_weird_string_reset:
|
|
|
|
string = string + 1;
|
2018-08-12 18:11:02 +01:00
|
|
|
hold_string[string_index] = ' ';
|
2018-08-16 17:01:55 +01:00
|
|
|
temp = escape_lookup(string);
|
2018-08-12 18:11:02 +01:00
|
|
|
hold_string[string_index + 1] = table[(temp >> 4)];
|
|
|
|
hold_string[string_index + 2] = table[(temp & 15)];
|
|
|
|
|
2018-08-16 17:01:55 +01:00
|
|
|
if(string[0] == '\\')
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-08-16 17:01:55 +01:00
|
|
|
if(string[1] == 'x') string = string + 2;
|
|
|
|
string = string + 1;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
2018-08-12 18:11:02 +01:00
|
|
|
|
|
|
|
string_index = string_index + 3;
|
2018-08-16 17:01:55 +01:00
|
|
|
if(string[1] != 0) goto collect_weird_string_reset;
|
2018-08-12 18:11:02 +01:00
|
|
|
|
|
|
|
hold_string[string_index] = ' ';
|
|
|
|
hold_string[string_index + 1] = '0';
|
|
|
|
hold_string[string_index + 2] = '0';
|
|
|
|
hold_string[string_index + 3] = '\'';
|
|
|
|
hold_string[string_index + 4] = '\n';
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-07-27 00:51:44 +01:00
|
|
|
char* hold = calloc(string_index + 6, sizeof(char));
|
|
|
|
copy_string(hold, hold_string);
|
|
|
|
reset_hold_string();
|
2017-12-15 23:17:29 +00:00
|
|
|
return hold;
|
|
|
|
}
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2017-12-15 23:17:29 +00:00
|
|
|
/* Parse string to deal with hex characters*/
|
|
|
|
char* parse_string(char* string)
|
|
|
|
{
|
2017-11-05 14:23:17 +00:00
|
|
|
/* the string */
|
2018-08-12 18:11:02 +01:00
|
|
|
if(weird(string)) return collect_weird_string(string);
|
2017-12-15 23:17:29 +00:00
|
|
|
else return collect_regular_string(string);
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|