tiny-shell 0.2
A mini shell project aiming to gain knowledge about Win32 and Linux API
Loading...
Searching...
No Matches
io_wrap.c File Reference
#include "io_wrap.h"
#include "../os/operations.h"
#include "config.h"
#include "utils.h"
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
Include dependency graph for io_wrap.c:

Go to the source code of this file.

Macros

#define COLOR_GREEN   "\033[32m"
 
#define COLOR_YELLOW   "\033[33m"
 
#define COLOR_RED   "\033[31m"
 
#define COLOR_BLUE   "\033[34m"
 
#define COLOR_BLACK   "\033[30m"
 
#define COLOR_WHITE   "\033[97m"
 
#define COLOR_DEFAULT   (*io_use_white_text() ? COLOR_WHITE : COLOR_BLACK)
 

Functions

static bool * io_use_white_text ()
 
enum run_resultlast_run_status ()
 
void io_set_last_status (enum run_result result)
 Set the result of last command (RUN_OK/RUN_FAIL/...)
 
void io_set_text_white ()
 Set default text color to white.
 
bool * is_prompt_visible ()
 
void io_set_prompt_visibility (bool visible)
 Config if prompt should be printed.
 
void prompt_input ()
 
void scan_input (struct cmd *obj)
 Ask and convert user's input into command.
 
void format_usage (char *fmt,...)
 Used format_xxx instead of printf and such for uniform output.
 
void format_success (char *fmt,...)
 Used format_xxx instead of printf and such for uniform output.
 
void format_output (char *fmt,...)
 Used format_xxx instead of printf and such for uniform output.
 
void format_error (char *fmt,...)
 Used format_xxx instead of printf and such for uniform output.
 

Macro Definition Documentation

◆ COLOR_BLACK

#define COLOR_BLACK   "\033[30m"

Definition at line 15 of file io_wrap.c.

◆ COLOR_BLUE

#define COLOR_BLUE   "\033[34m"

Definition at line 14 of file io_wrap.c.

Referenced by prompt_input().

◆ COLOR_DEFAULT

#define COLOR_DEFAULT   (*io_use_white_text() ? COLOR_WHITE : COLOR_BLACK)

Definition at line 18 of file io_wrap.c.

Referenced by format_error(), format_output(), format_success(), format_usage(), and prompt_input().

◆ COLOR_GREEN

#define COLOR_GREEN   "\033[32m"

Definition at line 11 of file io_wrap.c.

Referenced by format_success().

◆ COLOR_RED

#define COLOR_RED   "\033[31m"

Definition at line 13 of file io_wrap.c.

Referenced by format_error(), and prompt_input().

◆ COLOR_WHITE

#define COLOR_WHITE   "\033[97m"

Definition at line 16 of file io_wrap.c.

◆ COLOR_YELLOW

#define COLOR_YELLOW   "\033[33m"

Definition at line 12 of file io_wrap.c.

Referenced by format_usage().

Function Documentation

◆ format_error()

void format_error ( char * fmt,
... )

Used format_xxx instead of printf and such for uniform output.

Definition at line 89 of file io_wrap.c.

89 {
90 if(support_color())
91 printf(COLOR_RED);
92 printf("ERR: ");
93 va_list argptr; // NOLINT
94 va_start(argptr, fmt);
95 vfprintf(stdout, fmt, argptr);
96 va_end(argptr);
97 if(support_color())
98 printf(COLOR_DEFAULT);
99}
#define COLOR_DEFAULT
Definition io_wrap.c:18
#define COLOR_RED
Definition io_wrap.c:13
bool support_color()
Definition utils.c:58

References COLOR_DEFAULT, COLOR_RED, and support_color().

Referenced by add_path(), cmd_init_from_str(), main(), split_by_whitespaces(), and transform_quotes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_output()

void format_output ( char * fmt,
... )

Used format_xxx instead of printf and such for uniform output.

Definition at line 80 of file io_wrap.c.

80 {
81 if(support_color())
82 printf(COLOR_DEFAULT);
83 va_list argptr; // NOLINT
84 va_start(argptr, fmt);
85 vfprintf(stdout, fmt, argptr);
86 va_end(argptr);
87}

References COLOR_DEFAULT, and support_color().

Referenced by run_get_all_env(), run_get_env(), and run_help().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_success()

void format_success ( char * fmt,
... )

Used format_xxx instead of printf and such for uniform output.

Definition at line 69 of file io_wrap.c.

69 {
70 if(support_color())
71 printf(COLOR_GREEN);
72 va_list argptr; // NOLINT
73 va_start(argptr, fmt);
74 vfprintf(stdout, fmt, argptr);
75 va_end(argptr);
76 if(support_color())
77 printf(COLOR_DEFAULT);
78}
#define COLOR_GREEN
Definition io_wrap.c:11

References COLOR_DEFAULT, COLOR_GREEN, and support_color().

Referenced by add_path(), invoke_runner(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_usage()

void format_usage ( char * fmt,
... )

Used format_xxx instead of printf and such for uniform output.

Definition at line 57 of file io_wrap.c.

57 {
58 if(support_color())
59 printf(COLOR_YELLOW);
60 printf("USAGE: ");
61 va_list argptr; // NOLINT
62 va_start(argptr, fmt);
63 vfprintf(stdout, fmt, argptr);
64 va_end(argptr);
65 if(support_color())
66 printf(COLOR_DEFAULT);
67}
#define COLOR_YELLOW
Definition io_wrap.c:12

References COLOR_DEFAULT, COLOR_YELLOW, and support_color().

Referenced by cmd_init_from_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ io_set_last_status()

void io_set_last_status ( enum run_result result)

Set the result of last command (RUN_OK/RUN_FAIL/...)

Definition at line 29 of file io_wrap.c.

29 {
30 *last_run_status() = result;
31}
enum run_result * last_run_status()
Definition io_wrap.c:24

References last_run_status().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ io_set_prompt_visibility()

void io_set_prompt_visibility ( bool visible)

Config if prompt should be printed.

Definition at line 42 of file io_wrap.c.

42 {
43 *is_prompt_visible() = visible;
44}
bool * is_prompt_visible()
Definition io_wrap.c:37

References is_prompt_visible().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ io_set_text_white()

void io_set_text_white ( )

Set default text color to white.

Definition at line 33 of file io_wrap.c.

33 {
34 *io_use_white_text() = true;
35}
static bool * io_use_white_text()
Definition io_wrap.c:19

References io_use_white_text().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ io_use_white_text()

static bool * io_use_white_text ( )
static

Definition at line 19 of file io_wrap.c.

19 {
20 static bool res = false;
21 return &res;
22}

Referenced by io_set_text_white().

Here is the caller graph for this function:

◆ is_prompt_visible()

bool * is_prompt_visible ( )

Definition at line 37 of file io_wrap.c.

37 {
38 static bool visible = true;
39 return &visible;
40}

Referenced by io_set_prompt_visibility(), and prompt_input().

Here is the caller graph for this function:

◆ last_run_status()

enum run_result * last_run_status ( )

Definition at line 24 of file io_wrap.c.

24 {
25 static enum run_result res = RUN_OK;
26 return &res;
27}
run_result
Result of the execution of a command line.
Definition res.h:6
@ RUN_OK
Definition res.h:7

References RUN_OK.

Referenced by io_set_last_status(), and prompt_input().

Here is the caller graph for this function:

◆ prompt_input()

void prompt_input ( )

Definition at line 101 of file io_wrap.c.

101 {
102 os_char buffer[CWD_BUFFER_SIZE];
103 get_cwd(CWD_BUFFER_SIZE, buffer);
104 if(!*is_prompt_visible()) {
105 return;
106 }
107 switch(*last_run_status()) {
108 case RUN_OK:
109 if(support_color())
110 printf(COLOR_BLUE);
111 printf("%s\n-> ", buffer);
112 break;
113 case RUN_EXIT:
114 break;
115 case RUN_FAILED:
116 if(support_color())
117 printf(COLOR_BLUE "%s\n" COLOR_RED ":( -> ", buffer);
118 else
119 printf("%s\n:( -> ", buffer);
120 break;
121 default:
122 assert(false && "unimplemented");
123 }
124 if(support_color()) {
125 printf(COLOR_DEFAULT);
126 }
127}
#define CWD_BUFFER_SIZE
Definition config.h:6
#define COLOR_BLUE
Definition io_wrap.c:14
void get_cwd(unsigned int buffer_size, os_char *buffer)
Fetch current working directory and copy them into buffer
@ RUN_EXIT
Definition res.h:8
@ RUN_FAILED
Definition res.h:9

References COLOR_BLUE, COLOR_DEFAULT, COLOR_RED, CWD_BUFFER_SIZE, get_cwd(), is_prompt_visible(), last_run_status(), RUN_EXIT, RUN_FAILED, RUN_OK, and support_color().

Referenced by scan_input().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scan_input()

void scan_input ( struct cmd * obj)

Ask and convert user's input into command.

Definition at line 47 of file io_wrap.c.

47 {
49 char buffer[INPUT_BUFFER_SIZE] = {0};
50 if(!fgets(buffer, INPUT_BUFFER_SIZE, stdin)) {
51 cmd_init_from_str(obj, "exit");
52 } else {
53 cmd_init_from_str(obj, buffer);
54 }
55}
void cmd_init_from_str(struct cmd *res, const char *str)
Build command from raw input.
Definition cmd.c:19
#define INPUT_BUFFER_SIZE
Definition config.h:5
void prompt_input()
Definition io_wrap.c:101

References cmd_init_from_str(), INPUT_BUFFER_SIZE, and prompt_input().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: