<commit_before>#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "linenoise.h"

char* findPrompt();
void executeCommand(const char *text);

int main() {
  int childPid;
  int child_status;
  char *line;
  char *prompt;

  prompt = findPrompt();

  while((line = linenoise(prompt)) != NULL) {
    if (line[0] != '\0') {
      linenoiseHistoryAdd(line);
      childPid = fork();
      if(childPid == 0) {
        executeCommand(line);
      } else {
        wait(&child_status);
      }
    }
    free(line);
  }
  return 0;
}

char*  findPrompt() {
  return "$ ";
}

void executeCommand(const char *text) {
  execlp(text, text, NULL);
}
<commit_msg>Add persistent history to shell<commit_after>#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include "linenoise.h"

#define HISTORY_FILE ".beaksh_history"

char* findPrompt();
char* getHistoryPath();
void executeCommand(const char *text);

int main() {
  int childPid;
  int child_status;
  char *line;
  char *prompt;

  prompt = findPrompt();

  char *historyPath = getHistoryPath();
  linenoiseHistoryLoad(historyPath);

  while((line = linenoise(prompt)) != NULL) {
    if (line[0] != '\0') {
      linenoiseHistoryAdd(line);
      linenoiseHistorySave(historyPath);
      childPid = fork();
      if(childPid == 0) {
        executeCommand(line);
      } else {
        wait(&child_status);
      }
    }
    free(line);
  }

  if(historyPath)
    free(historyPath);

  return EXIT_SUCCESS;
}

char*  findPrompt() {
  return "$ ";
}

/*
  Resolve dotfile path for history. Returns NULL if file can't be resolved.
*/
char* getHistoryPath() {
  char *home = getenv("HOME");

  if(!home)
    return NULL;

  int home_path_len = strnlen(home, MAXPATHLEN);
  int history_path_len = home_path_len + strlen(HISTORY_FILE) + 1;

  char *result;

  if((result = malloc(history_path_len + 1)) == NULL) {
    fprintf(stderr, "Problem resolving path for history file, no history will be recorded\n");
    return NULL;
  }

  strncpy(result, home, home_path_len);
  strncat(result, "/", 1);
  strncat(result, HISTORY_FILE, strlen(HISTORY_FILE));

  return result;
}

void executeCommand(const char *text) {
  execlp(text, text, NULL);
}

{'fraction_non_alphanumeric': 0.14587924195680918, 'fraction_numerical': 0.004407227853680035, 'mean_word_length': 3.5219123505976095, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '3411505', 'n_tokens_mistral': 839, 'n_tokens_neox': 804, 'n_words': 187}