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

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 11 of file main.c.

11 {
12 // Version report
13 if(argc >= 2 && (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0)) {
14 format_success("%s, version %s\n", APP_NAME, APP_VERSION);
15 return EXIT_SUCCESS;
16 }
17 // No prompt
18 if(argc >= 2 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--silent") == 0)) {
20 }
21 // Exec a file
22 if(argc >= 3 && (strcmp(argv[1], "-f") == 0 || strcmp(argv[1], "--file") == 0)) {
23 if(!freopen(argv[2], "r", stdin)) {
24 format_error("Cannot open file \"%s\"\n", argv[2]);
25 return EXIT_FAILURE;
26 }
28 }
29
30 // Use white text if `TINY_SHELL_WHITE` environment variable is set
31 if(has_shell_env("TINY_SHELL_WHITE")) {
33 }
34
35 while(true) {
36 // Scan the input line and convert it to `cmd`
37 struct cmd cmd;
39 // Invoke the corresponding function
40 enum run_result res = invoke_runner(&cmd);
41 // Set status indicator
43 // Deallocate the command
45 format_success("\n");
46 if(res == RUN_EXIT) {
47 return EXIT_SUCCESS;
48 }
49 }
50}
void cmd_destroy(struct cmd *obj)
Definition cmd.c:194
#define APP_VERSION
Definition config.h:3
#define APP_NAME
Definition config.h:4
enum run_result invoke_runner(const struct cmd *cmd)
Invoke corresponding functions based on cmd's type and value.
Definition invoke.c:22
void io_set_text_white()
Set default text color to white.
Definition io_wrap.c:33
void format_success(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:69
void scan_input(struct cmd *obj)
Ask and convert user's input into command.
Definition io_wrap.c:47
void format_error(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:89
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
bool has_shell_env(const os_char *var)
Check if a variable environment exists.
run_result
Result of the execution of a command line.
Definition res.h:6
@ RUN_EXIT
Definition res.h:8
Resulting struct after parsing the input.
Definition cmd.h:39

References APP_NAME, APP_VERSION, cmd_destroy(), format_error(), format_success(), has_shell_env(), invoke_runner(), io_set_last_status(), io_set_prompt_visibility(), io_set_text_white(), RUN_EXIT, and scan_input().

Here is the call graph for this function: