util.c 5.11 KB
Newer Older
1 2 3 4 5
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "util.h"
6
#include "settings.h"
7

Camil Staps's avatar
Camil Staps committed
8
#if defined(WINDOWS) && defined(LINK_CLEAN_RUNTIME)
9
char print_buffer[PRINT_BUFFER_SIZE];
10 11 12 13 14 15 16 17 18

# ifdef STDERR_TO_FILE
static FILE *stderr_to_file=NULL;
void stderr_print(int n) {
	if (stderr_to_file==NULL)
		stderr_to_file=fopen("stderr","w");
	fwrite(print_buffer,1,n,stderr_to_file);
}
# endif
19 20
#endif

21 22 23
void *safe_malloc(size_t size) {
	void *mem = malloc(size);
	if (!mem) {
24
		EPRINTF("Failed to malloc %d bytes: %s\n",(int)size,strerror(errno));
25 26 27 28 29 30 31 32
		exit(-1);
	}
	return mem;
}

void *safe_calloc(size_t num, size_t size) {
	void *mem = calloc(num, size);
	if (!mem) {
33
		EPRINTF("Failed to calloc %d*%d bytes: %s\n",(int)num,(int)size,strerror(errno));
34 35 36 37
		exit(-1);
	}
	return mem;
}
38

Camil Staps's avatar
Camil Staps committed
39 40 41
void *safe_realloc(void *ptr, size_t size) {
	ptr = realloc(ptr, size);
	if (!ptr) {
42
		EPRINTF("Failed to realloc to %d bytes: %s\n",(int)size,strerror(errno));
Camil Staps's avatar
Camil Staps committed
43 44 45 46 47
		exit(-1);
	}
	return ptr;
}

48 49 50 51 52
void interpreter_exit(
#ifdef LINK_CLEAN_RUNTIME
		struct interpretation_environment *ie,
#endif
		int code) {
53 54 55
#ifdef STDERR_TO_FILE
	if (stderr_to_file!=NULL)
		fclose(stderr_to_file);
56 57 58
#endif
#ifdef LINK_CLEAN_RUNTIME
	if (ie==NULL || (ie!=(void*)-1 && !ie->options.hyperstrict))
59 60 61 62
#endif
	exit(code);
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void new_file_char_provider(struct char_provider *cp, FILE *f) {
	cp->type = CPT_FILE;
	cp->arg = f;
}

struct string_char_provider {
	size_t ptr;
	size_t size;
	char *s;
};

void new_string_char_provider(struct char_provider *cp, char *s, size_t size, int copy) {
	cp->type = CPT_STRING;

	char *string = s;
	if (copy) {
		string = safe_malloc(size);
80
		memcpy(string, s, size);
81 82 83 84 85 86 87 88 89
	}

	struct string_char_provider *scp = safe_malloc(sizeof(struct string_char_provider));
	scp->ptr = 0;
	scp->size = size;
	scp->s = string;
	cp->arg = scp;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void free_char_provider(struct char_provider *cp) {
	switch (cp->type) {
		case CPT_STRING:
		{
			struct string_char_provider *scp = (struct string_char_provider*) cp->arg;
			free(scp->s);
			free(scp);
			break;
		}
		case CPT_FILE:
			fclose((FILE*)cp->arg);
			break;
	}
}

105 106 107 108 109 110
int provide_chars(void *ptr, size_t size, size_t nmemb, struct char_provider *stream) {
	switch (stream->type) {
		case CPT_FILE:
			{
				int ret = fread(ptr, size, nmemb, (FILE*) stream->arg);
				if (ret < nmemb) {
111 112
#ifdef WINDOWS
# if (WORD_WIDTH==64)
113
					EPRINTF("Read %d out of %" PRIu64 " items",ret,nmemb);
114
# else
115
					EPRINTF("Read %d out of %" PRIu32 " items",ret,nmemb);
116
# endif
117
#else
118
					EPRINTF("Read %d out of %zu items",ret,nmemb);
119
#endif
120 121 122 123 124 125 126 127 128
					return -1;
				}
				return 0;
			}
		case CPT_STRING:
			{
				struct string_char_provider *scp = (struct string_char_provider*) stream->arg;
				if (scp->ptr + size * nmemb > scp->size)
					return -1;
129
				memcpy(ptr, &scp->s[scp->ptr], size * nmemb);
130 131 132
				scp->ptr += size * nmemb;
				return 0;
			}
133
	}
134

135
	return -1;
136 137
}

Erin van der Veen's avatar
Erin van der Veen committed
138 139 140
int starts_with(const char* prefix, char* str) {
	return strncmp(prefix, str, strlen(prefix)) == 0;
}
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
size_t string_to_size(char *size) {
	size_t res = 0;
	while ('0' <= *size && *size <= '9') {
		res *= 10;
		res += *size - '0';
		size++;
	}

	switch (*size) {
		case 'm':
		case 'M':
			res <<= 10;
		case 'k':
		case 'K':
			res <<= 10;
			break;
		case '\0':
			return res;
		default:
			return -1;
	}

	if (*(size+1) == '\0') {
		return res;
	} else {
		return -1;
	}
}
170

171
static char hex_digits[]="0123456789abcdef";
172
char *escape(char c) {
173 174 175 176 177 178 179 180 181
	static char s[4];

	if (c<' ') {
		s[0]='\\';
		s[1]='x';
		s[2]=hex_digits[c>>4];
		s[3]=hex_digits[c&0xf];
	}

182 183 184 185 186
	switch (c) {
		case '\n': return "\\n";
		case '\r': return "\\r";
		case '\t': return "\\t";
		case '\\': return "\\\\";
187
		case '\'': return "'";
188 189 190 191 192 193 194
		case '"':  return "\\\"";
		default:
		   s[0] = c;
		   s[1] = 0;
		   return s;
	}
}
Camil Staps's avatar
Camil Staps committed
195 196

/* https://stackoverflow.com/a/47229318 */
197
#if defined(BCGEN) || defined(LINKER)
Camil Staps's avatar
Camil Staps committed
198 199 200 201 202 203
# ifdef MICROSOFT_C
SSIZE_T
# else
ssize_t
# endif
	getline(char **lineptr, size_t *n, FILE *stream) {
Camil Staps's avatar
Camil Staps committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    size_t pos;
    int c;

    if (lineptr == NULL || stream == NULL || n == NULL) {
        errno = EINVAL;
        return -1;
    }

    c = fgetc(stream);
    if (c == EOF) {
        return -1;
    }

    if (*lineptr == NULL) {
        *lineptr = malloc(128);
        if (*lineptr == NULL) {
            return -1;
        }
        *n = 128;
    }

    pos = 0;
    while(c != EOF) {
        if (pos + 1 >= *n) {
            size_t new_size = *n + (*n >> 2);
            if (new_size < 128) {
                new_size = 128;
            }
            char *new_ptr = realloc(*lineptr, new_size);
            if (new_ptr == NULL) {
                return -1;
            }
            *n = new_size;
            *lineptr = new_ptr;
        }

        ((unsigned char *)(*lineptr))[pos ++] = c;
        if (c == '\n') {
            break;
        }
        c = fgetc(stream);
    }

    (*lineptr)[pos] = '\0';
    return pos;
}

char *strsep(char **stringp, const char *delim) {
	if (*stringp == NULL)
		return NULL;

	int delimlen=strlen(delim);
	char *token=*stringp;
	char *p=token;
	*stringp=NULL;

	for (; *p; p++) {
		for (int d=0; d<delimlen; d++) {
			if (*p==delim[d]) {
				*p='\0';
				*stringp=p+1;
				return token;
			}
		}
	}

	return token;
}
272
#endif