2023-12-15 18:32:59 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define COMMAND "source-highlight --failsafe -f esc --style-file=esc.style -o STDOUT -i "
|
|
|
|
#define READ "r"
|
|
|
|
#define CHARS_TO_WRITE 10
|
|
|
|
|
2024-01-10 17:12:21 +01:00
|
|
|
void reset_term(){
|
|
|
|
struct termios terminal_settings;
|
|
|
|
tcgetattr(STDIN_FILENO, &terminal_settings);
|
|
|
|
terminal_settings.c_lflag |= (ICANON | ECHO);
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_settings);
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:32:59 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc < 2) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
|
|
|
|
struct termios terminal_settings;
|
|
|
|
char current_char;
|
|
|
|
char *command = malloc(sizeof(char) * (strlen(COMMAND) + strlen(argv[1])));
|
|
|
|
|
|
|
|
tcgetattr(STDIN_FILENO, &terminal_settings);
|
|
|
|
terminal_settings.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_settings);
|
|
|
|
sprintf(command, "%s%s", COMMAND, argv[1]);
|
|
|
|
|
|
|
|
FILE *command_output_fptr = popen(command, READ);
|
|
|
|
|
|
|
|
free(command);
|
|
|
|
|
|
|
|
while (!feof(command_output_fptr)) {
|
|
|
|
getchar();
|
|
|
|
|
2024-01-10 17:12:21 +01:00
|
|
|
for (unsigned int i = 0; i < CHARS_TO_WRITE; i++) {
|
2023-12-15 18:32:59 +01:00
|
|
|
current_char = fgetc(command_output_fptr);
|
2024-01-10 17:12:21 +01:00
|
|
|
if(feof(command_output_fptr)){
|
|
|
|
pclose(command_output_fptr);
|
|
|
|
reset_term();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2023-12-15 18:32:59 +01:00
|
|
|
printf("%c", current_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pclose(command_output_fptr);
|
2024-01-10 17:12:21 +01:00
|
|
|
reset_term();
|
2023-12-15 18:32:59 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|