get_command refactor: put into own file

This commit is contained in:
Khaïs COLIN 2025-02-19 18:04:34 +01:00
parent 486dec7bb7
commit abea59eba4
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 75 additions and 42 deletions

54
src/get_command.c Normal file
View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 18:03:11 by khais #+# #+# */
/* Updated: 2025/02/19 18:04:06 by khais ### ########.fr */
/* */
/* ************************************************************************** */
// stdio must be included before readline
#include <stdio.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "libft.h"
/*
** remove one '\n' from the end of the string, if it exists
*/
static char *strip_newline(char *str)
{
size_t last_char_idx;
if (str == NULL)
return (NULL);
last_char_idx = ft_strlen(str) - 1;
if (str[last_char_idx] == '\n')
str[last_char_idx] = '\0';
return (str);
}
/*
** get a command line using readline.
**
** returned buffer must be freed by caller.
** will add command to history if appropriate.
*/
char *get_command(void)
{
char *line;
if (isatty(STDIN_FILENO))
line = readline("$ ");
else
line = strip_newline(get_next_line(STDIN_FILENO));
if (line != NULL && line[0] != '\0')
add_history(line);
return (line);
}

18
src/get_command.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 18:02:19 by khais #+# #+# */
/* Updated: 2025/02/19 18:03:06 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_COMMAND_H
# define GET_COMMAND_H
char *get_command(void);
#endif

View file

@ -6,52 +6,12 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
/* Updated: 2025/02/19 17:02:17 by khais ### ########.fr */
/* Updated: 2025/02/19 18:03:54 by khais ### ########.fr */
/* */
/* ************************************************************************** */
// stdio must be included before readline
#include <stdio.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "libft.h"
/*
** remove one '\n' from the end of the string, if it exists
*/
static char *strip_newline(char *str)
{
size_t last_char_idx;
if (str == NULL)
return (NULL);
last_char_idx = ft_strlen(str) - 1;
if (str[last_char_idx] == '\n')
str[last_char_idx] = '\0';
return (str);
}
/*
** get a command line using readline.
**
** returned buffer must be freed by caller.
** will add command to history if appropriate.
*/
static char *get_command(void)
{
char *line;
if (isatty(STDIN_FILENO))
line = readline("$ ");
else
line = strip_newline(get_next_line(STDIN_FILENO));
if (line != NULL && line[0] != '\0')
add_history(line);
return (line);
}
#include "get_command.h"
int main(int argc, char *argv[], char **envp)
{