Added a way to print out instead of stdout but to a file (everything about the process but errors (those still go to stderr) and made it so now someone can actually use echo -e "" | ./bin/stergios (prior this was impossible to do due to readline quirks)
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 7s
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 7s
This commit is contained in:
10 files changed
+191
-63
No files matched your search
@@ -1,5 +1,67 @@
|
||||
#include "io.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
|
||||
static bool read_line_raw(FILE* stream, char** msgptr, size_t* n) {
|
||||
if (msgptr == NULL || n == NULL) return 1;
|
||||
|
||||
if (*msgptr == NULL) {
|
||||
*n = 128;
|
||||
*msgptr = malloc(*n);
|
||||
if (*msgptr == NULL) return 1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
size_t pos = 0;
|
||||
int32_t c;
|
||||
|
||||
while ((c = fgetc(stream)) != EOF) {
|
||||
/* If the string dosent fit, double the size */
|
||||
if (pos + 1 >= *n) {
|
||||
size_t new_size = *n * 2;
|
||||
char* new_ptr = realloc(*msgptr, new_size);
|
||||
if (new_ptr == NULL) return 1;
|
||||
*msgptr = new_ptr;
|
||||
*n = new_size;
|
||||
}
|
||||
if (c == '\n' || c == '#') {
|
||||
/* Skip the info after comments */
|
||||
if (c == '#') {
|
||||
/* Read and discard the rest of the line until a newline or EOF */
|
||||
while ((c = fgetc(stream)) != EOF && c != '\n');
|
||||
}
|
||||
break; /* Stop reading for this line. */
|
||||
}
|
||||
(*msgptr)[pos++] = (char)c; /* Add the corresponding characters */
|
||||
}
|
||||
|
||||
if (pos == 0 && c == EOF) return 1;
|
||||
|
||||
(*msgptr)[pos] = '\0'; /* Add null byte */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
char* prompt_line(const char* prompt) {
|
||||
if (isatty(fileno(stdin))) {
|
||||
char* line = readline(prompt);
|
||||
if (line && *line) add_history(line);
|
||||
return line;
|
||||
} else {
|
||||
printf("%s", prompt);
|
||||
fflush(stdout);
|
||||
char* buf = NULL;
|
||||
size_t size = 0;
|
||||
if (read_line_raw(stdin, &buf, &size)) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
bool read_line(FILE* stream, char** msgptr, size_t* n) {
|
||||
if (msgptr == NULL || n == NULL) return 1;
|
||||
|
||||
@@ -64,6 +126,20 @@ int128_t str_to_int128(const char* s) {
|
||||
return sign * v;
|
||||
}
|
||||
|
||||
void fprint_int128(FILE* output, int128_t n) {
|
||||
if (n == 0) { fprintf(output, "0"); return; }
|
||||
char buf[40];
|
||||
int i = 0;
|
||||
int neg = n < 0;
|
||||
if (neg) n = -n;
|
||||
while (n > 0) {
|
||||
buf[i++] = '0' + n % 10;
|
||||
n /= 10;
|
||||
}
|
||||
if (neg) fputc('-', output);
|
||||
while (i--) fputc(buf[i], output);
|
||||
}
|
||||
|
||||
void print_int128(int128_t n) {
|
||||
if (n == 0) { printf("0"); return; }
|
||||
char buf[40];
|
||||
|
||||
Reference in new issue
Block a user