tiny-shell 0.2
A mini shell project aiming to gain knowledge about Win32 and Linux API
Loading...
Searching...
No Matches
io_wrap.c
Go to the documentation of this file.
1#include "io_wrap.h"
2#include "../os/operations.h"
3#include "config.h"
4#include "utils.h"
5
6#include <assert.h>
7#include <stdarg.h>
8#include <stdbool.h>
9#include <stdio.h>
10
11#define COLOR_GREEN "\033[32m"
12#define COLOR_YELLOW "\033[33m"
13#define COLOR_RED "\033[31m"
14#define COLOR_BLUE "\033[34m"
15#define COLOR_BLACK "\033[30m"
16#define COLOR_WHITE "\033[97m"
17
18#define COLOR_DEFAULT (*io_use_white_text() ? COLOR_WHITE : COLOR_BLACK)
19static bool *io_use_white_text() {
20 static bool res = false;
21 return &res;
22}
23
25 static enum run_result res = RUN_OK;
26 return &res;
27}
28
29void io_set_last_status(enum run_result result) {
30 *last_run_status() = result;
31}
32
34 *io_use_white_text() = true;
35}
36
38 static bool visible = true;
39 return &visible;
40}
41
42void io_set_prompt_visibility(bool visible) {
43 *is_prompt_visible() = visible;
44}
45
46void prompt_input();
47void scan_input(struct cmd *obj) {
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}
56
57void format_usage(char *fmt, ...) {
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}
68
69void format_success(char *fmt, ...) {
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}
79
80void format_output(char *fmt, ...) {
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}
88
89void format_error(char *fmt, ...) {
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}
100
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}
void cmd_init_from_str(struct cmd *res, const char *str)
Build command from raw input.
Definition cmd.c:19
#define CWD_BUFFER_SIZE
Definition config.h:6
#define INPUT_BUFFER_SIZE
Definition config.h:5
void format_output(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:80
#define COLOR_BLUE
Definition io_wrap.c:14
void io_set_text_white()
Set default text color to white.
Definition io_wrap.c:33
void format_usage(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:57
static bool * io_use_white_text()
Definition io_wrap.c:19
void format_success(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:69
#define COLOR_YELLOW
Definition io_wrap.c:12
void scan_input(struct cmd *obj)
Ask and convert user's input into command.
Definition io_wrap.c:47
#define COLOR_DEFAULT
Definition io_wrap.c:18
void format_error(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:89
void prompt_input()
Definition io_wrap.c:101
void io_set_last_status(enum run_result result)
Set the result of last command (RUN_OK/RUN_FAIL/...)
Definition io_wrap.c:29
void io_set_prompt_visibility(bool visible)
Config if prompt should be printed.
Definition io_wrap.c:42
#define COLOR_RED
Definition io_wrap.c:13
bool * is_prompt_visible()
Definition io_wrap.c:37
enum run_result * last_run_status()
Definition io_wrap.c:24
#define COLOR_GREEN
Definition io_wrap.c:11
void get_cwd(unsigned int buffer_size, os_char *buffer)
Fetch current working directory and copy them into buffer
run_result
Result of the execution of a command line.
Definition res.h:6
@ RUN_OK
Definition res.h:7
@ RUN_EXIT
Definition res.h:8
@ RUN_FAILED
Definition res.h:9
Resulting struct after parsing the input.
Definition cmd.h:39
bool support_color()
Definition utils.c:58