M2-Planet/cc_strings.c

172 lines
4.0 KiB
C
Raw Normal View History

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>
* This file is part of M2-Planet.
2017-11-05 14:23:17 +00: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.
*
* 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
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
2017-11-05 14:23:17 +00:00
*/
#include "cc.h"
#include <stdint.h>
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);
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);
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)
{
2018-08-12 18:11:02 +01:00
c = escape_lookup(string);
if('x' == string[1]) string = string + 2;
string = string + 1;
}
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;
}
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
file_print("Unknown escape recieved: ", stderr);
file_print(c, stderr);
file_print(" Unable to process\n", stderr);
exit(EXIT_FAILURE);
}
/* Deal with human strings */
char* collect_regular_string(char* string)
{
2018-07-27 00:51:44 +01:00
string_index = 0;
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
{
2018-08-12 18:11:02 +01:00
hold_string[string_index] = string[0];
string = string + 1;
}
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();
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;
int temp;
char* table = "0123456789ABCDEF";
2017-11-05 14:23:17 +00:00
hold_string[0] = '\'';
collect_weird_string_reset:
string = string + 1;
2018-08-12 18:11:02 +01:00
hold_string[string_index] = ' ';
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)];
if(string[0] == '\\')
2017-11-05 14:23:17 +00: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;
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();
return hold;
}
2017-11-05 14:23:17 +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);
else return collect_regular_string(string);
2017-11-05 14:23:17 +00:00
}