<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <termios.h>     //termios, TCSANOW, ECHO, ICANON
#include <unistd.h>     //STDIN_FILENO
#include <stdlib.h>

#define ESC 27
int isInputValid(char c);
void printCommand(char command[], int curPos);
void setCharAt(int cursorPosition, char toChar, char currentCommand[]);
void removeCharAt(int cursorPosition, char currentCommand[]);
void moveCursorToLeftBy(int n);

struct hist_data{
	struct hist_data *next;
	struct hist_data *prev;
	char line[100];
};
struct hist_data *first_hist = NULL;
struct hist_data *currentHistoryAddress = NULL;

typedef void (*sighandler_t)(int);
char c = '\0';
char  line[100];
void fork_exec(int fdi,int fdo,char line[]);
char** parse_line(char line[]);
void checkBuiltInFunctions(char line[]);
void add_history(char* line);
void handle_signal(int signo);
void setPreviousCommandInHistory(char command[]);
void setNextCommandInHistory(char command[]);
void changeCommand(char line[], char current[], char old[], int* cursorPos);

int main(int argc, char *argv[])
{
	int c;
    static struct termios oldt, newt;
    
	signal(SIGINT, SIG_IGN);
	signal(SIGINT, handle_signal);

    /*tcgetattr gets the parameters of the current terminal
     STDIN_FILENO will tell tcgetattr that it should write the settings
     of stdin to oldt*/
    tcgetattr( STDIN_FILENO, &oldt);
    /*now the settings will be copied*/
    newt = oldt;
    
    /*ICANON normally takes care that one line at a time will be processed
     that means it will return if it sees a "\n" or an EOF or an EOL*/
    newt.c_lflag &= ~(ICANON);
    
    /*Those new settings will be set to STDIN
     TCSANOW tells tcsetattr to change attributes immediately. */
    tcsetattr( STDIN_FILENO, TCSANOW, &newt);
    
    /*This is your part:
     I choose 'e' to end input. Notice that EOF is also turned off
     in the non-canonical mode*/
    char currentCommand[100] = "";
    int cursorPosition = 0;
    char line[100]="", oldCommand[100] = "";

char* welcomeMessage = "\
This is a simple open source shell. You may find the source code at https://github.com/onurdev/obdoSH\n\
Licence: MIT Licence\n\
Authors: <NAME>, <NAME>.\n\n";
	printf("%s", welcomeMessage);

    printCommand(currentCommand, cursorPosition);

    while((c=getchar())!= EOF){
        //printf("\n%c - %d\n", c, c);
        if (c == 127 && cursorPosition > 0)
        {
            removeCharAt(cursorPosition - 1, currentCommand);
            cursorPosition--;
            //printf("\n%c - %d\n", c, c);
        }
        if (c == 10)
        {
            line[strlen(line)]='\0';
            add_history(line);
			checkBuiltInFunctions(line);
            fork_exec(0, 1, line);
            //reset current command
            memset(currentCommand, 0, 100 * (sizeof currentCommand[0]) );
            memset(line, 0, 100 * (sizeof line[0]) );
            cursorPosition = 0;
        }
        if (c == '\033') { // check the first char == esc
            //printf("\nIN ESC VALUES\n");
            getchar(); // skip '[' char its not being read anyway...
            switch(getchar()) { // the real value
                case 'A': // up arrow
                	setPreviousCommandInHistory(oldCommand);
                	changeCommand(line, currentCommand, oldCommand, &cursorPosition);
                    break;
                case 'B': // down arrow
                    setNextCommandInHistory(oldCommand);
                    changeCommand(line, currentCommand, oldCommand, &cursorPosition);
                    break;<commit_msg>Backspace bug fix<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <termios.h>     //termios, TCSANOW, ECHO, ICANON
#include <unistd.h>     //STDIN_FILENO
#include <stdlib.h>

#define ESC 27
int isInputValid(char c);
void printCommand(char command[], int curPos);
void setCharAt(int cursorPosition, char toChar, char currentCommand[]);
void removeCharAt(int cursorPosition, char currentCommand[]);
void moveCursorToLeftBy(int n);

struct hist_data{
	struct hist_data *next;
	struct hist_data *prev;
	char line[100];
};
struct hist_data *first_hist = NULL;
struct hist_data *currentHistoryAddress = NULL;

typedef void (*sighandler_t)(int);
char c = '\0';
char  line[100];
void fork_exec(int fdi,int fdo,char line[]);
char** parse_line(char line[]);
void checkBuiltInFunctions(char line[]);
void add_history(char* line);
void handle_signal(int signo);
void setPreviousCommandInHistory(char command[]);
void setNextCommandInHistory(char command[]);
void changeCommand(char line[], char current[], char old[], int* cursorPos);

int main(int argc, char *argv[])
{
	int c;
    static struct termios oldt, newt;
    
	signal(SIGINT, SIG_IGN);
	signal(SIGINT, handle_signal);

    /*tcgetattr gets the parameters of the current terminal
     STDIN_FILENO will tell tcgetattr that it should write the settings
     of stdin to oldt*/
    tcgetattr( STDIN_FILENO, &oldt);
    /*now the settings will be copied*/
    newt = oldt;
    
    /*ICANON normally takes care that one line at a time will be processed
     that means it will return if it sees a "\n" or an EOF or an EOL*/
    newt.c_lflag &= ~(ICANON);
    
    /*Those new settings will be set to STDIN
     TCSANOW tells tcsetattr to change attributes immediately. */
    tcsetattr( STDIN_FILENO, TCSANOW, &newt);
    
    /*This is your part:
     I choose 'e' to end input. Notice that EOF is also turned off
     in the non-canonical mode*/
    char currentCommand[100] = "";
    int cursorPosition = 0;
    char line[100]="", oldCommand[100] = "";

char* welcomeMessage = "\
This is a simple open source shell. You may find the source code at https://github.com/onurdev/obdoSH\n\
Licence: MIT Licence\n\
Authors: <NAME>, <NAME>.\n\n";
	printf("%s", welcomeMessage);

    printCommand(currentCommand, cursorPosition);

    while((c=getchar())!= EOF){
        //printf("\n%c - %d\n", c, c);
        if (c == 127 && cursorPosition > 0)
        {
            removeCharAt(cursorPosition - 1, line);
            memset(currentCommand, 0, 100 * (sizeof currentCommand[0]) );
            strcpy(currentCommand, line);
            cursorPosition--;
            //printf("\n%c - %d\n", c, c);
        }
        if (c == 10)
        {
            line[strlen(line)]='\0';
            add_history(line);
			checkBuiltInFunctions(line);
            fork_exec(0, 1, line);
            //reset current command
            memset(currentCommand, 0, 100 * (sizeof currentCommand[0]) );
            memset(line, 0, 100 * (sizeof line[0]) );
            cursorPosition = 0;
        }
        if (c == '\033') { // check the first char == esc
            //printf("\nIN ESC VALUES\n");
            getchar(); // skip '[' char its not being read anyway...
            switch(getchar()) { // the real value
                case 'A': // up arrow
                	setPreviousCommandInHistory(oldCommand);
                	changeCommand(line, currentCommand, oldCommand, &cursorPosition);
                    break;
                case 'B': // down arrow
                    setNextCommandInHistory(oldCommand);
                    changeCommand(line, currentCommand, oldCommand, &cursorPosition);
                    break;
{'fraction_non_alphanumeric': 0.12387693983120066, 'fraction_numerical': 0.012387693983120064, 'mean_word_length': 2.737029501525941, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6452465', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2225, 'n_words': 700}