Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 560c866

Browse files
authored
Merge pull request #2 from milosz275/cli
CLI
2 parents 66d2b10 + d027c7e commit 560c866

File tree

14 files changed

+409
-23
lines changed

14 files changed

+409
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ logs/
5151

5252
# VSCode
5353
*.vc.*
54+
55+
# BitLab history
56+
.*history.txt

.vscode/settings.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"cSpell.language": "en,pl",
33
"cSpell.words": [
4+
"bitlab",
5+
"Bitlab",
46
"codespaces",
5-
"Doxygen",
67
"Doxyfile",
7-
"bitlab",
8-
"Bitlab"
8+
"Doxygen",
9+
"lineptr"
910
],
1011
"task.allowAutomaticTasks": "on",
1112
"editor.formatOnSave": true,
@@ -17,7 +18,9 @@
1718
"editor.formatOnPaste": true,
1819
"makefile.configureOnOpen": true,
1920
"files.associations": {
20-
"bitlab.h": "c"
21+
"bitlab.h": "c",
22+
"utils.h": "c",
23+
"state.h": "c"
2124
},
2225
"C_Cpp.formatting": "vcFormat",
2326
"C_Cpp.autoAddFileAssociations": true,

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MIT License
1+
# MIT License
22

33
Copyright (c) 2024 Miłosz Maculewicz
44

Makefile

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
SUBDIRS := $(filter-out assets/ build/ docs/, $(wildcard */))
1+
BITLAB = bitlab
22

3-
all: $(SUBDIRS)
4-
$(SUBDIRS):
5-
$(MAKE) -C $@
3+
BITLAB_BIN = $(BITLAB)/build/bin/$(BITLAB)
64

7-
clean:
8-
for dir in $(SUBDIRS); do \
9-
$(MAKE) -C $$dir clean; \
10-
done
5+
.PHONY: all bitlab
6+
all: bitlab
7+
debug: bitlab-debug
8+
9+
bitlab:
10+
$(MAKE) -C $(BITLAB)
1111

12-
.PHONY: all $(SUBDIRS) clean
12+
bitlab-debug:
13+
$(MAKE) -C $(BITLAB) debug
14+
15+
clean:
16+
find $(BITLAB)/build/src -name '*.o' -delete
17+
find $(BITLAB)/build/src -name '*~' -delete
18+
$(RM) $(BITLAB)/build/bin/$(BITLAB)

bitlab/include/bitlab.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ typedef enum
99

1010
/**
1111
* Run the BitLab program.
12-
*
12+
*
1313
* @param argc The number of arguments.
1414
* @param argv The arguments.
1515
* @return The result of the BitLab program.
1616
*/
1717
bitlab_result run_bitlab(int argc, char* argv[]);
1818

19-
#endif
19+
#endif // __BITLAB_H

bitlab/include/cli.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#ifndef __CLI_H
2+
#define __CLI_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
#define MAX_LINE_LEN 256
8+
#define CLI_BUFSIZE 64
9+
#define CLI_DELIM " "
10+
#define CLI_COMMANDS_NUM (int) (sizeof(cli_commands) / sizeof(cli_command))
11+
#define CLI_HISTORY_FILE ".bitlab_history.txt"
12+
13+
/**
14+
* CLI command structure.
15+
*
16+
* @param cli_command The function pointer to executed function.
17+
* @param cli_command_name The name of command by which it is called.
18+
* @param cli_command_description The description of command printed by !help.
19+
*/
20+
typedef struct
21+
{
22+
int (*cli_command)(char**);
23+
char* cli_command_name;
24+
char* cli_command_description;
25+
} cli_command;
26+
27+
/**
28+
* Exits BitLab CLI command.
29+
*
30+
* @param args The arguments passed to the function should be empty.
31+
* @return The exit code.
32+
*/
33+
int cli_exit(char** args);
34+
35+
/**
36+
* Prints CLI command history.
37+
*
38+
* @param args The arguments passed to the function should be empty.
39+
* @return The exit code.
40+
*/
41+
int cli_history(char** args);
42+
43+
/**
44+
* Clears CLI window.
45+
*
46+
* @param args The arguments passed to the function should be empty.
47+
* @return The exit code.
48+
*/
49+
int cli_clear(char** args);
50+
51+
/**
52+
* Prints CLI command help.
53+
*
54+
* @param args The arguments passed to this function should be empty.
55+
* @return The exit code.
56+
*/
57+
int cli_help(char** args);
58+
59+
/**
60+
* Gets the line from the file stream.
61+
*
62+
* @param lineptr The pointer to the line.
63+
* @param n The size of the line.
64+
* @param stream The file stream.
65+
* @return The number of characters read.
66+
*/
67+
int cli_get_line(char** lineptr, size_t* n, FILE* stream);
68+
69+
/**
70+
* Reads input from user.
71+
*
72+
* WARNING: char* returned from this function must be freed.
73+
*
74+
* @return The string containing input from user.
75+
*/
76+
char* cli_read_line(void);
77+
78+
/**
79+
* Execute command. This function executes a command from line.
80+
*
81+
* @param line The string containing input from user.
82+
* @return The exit code.
83+
*/
84+
int cli_exec_line(char* line);
85+
86+
/**
87+
* CLI handler thread.
88+
*
89+
* @param arg Not used. Write dummy code for no warning.
90+
*/
91+
void* handle_cli(void* arg);
92+
93+
#endif // __CLI_H

bitlab/include/log.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LOGS_DIR "logs"
1111
#define BITLAB_LOG "bitlab.log"
1212
#define LOG_BITLAB_STARTED "BitLab started ----------------------------------------------------------------------------------------"
13+
#define LOG_BITLAB_FINISHED "BitLab finished successfully"
1314

1415
#define LOCKED_FILE_RETRY_TIME 1000 // in microseconds (1 millisecond)
1516
#define LOCKED_FILE_TIMEOUT 5000000 // in microseconds (5 seconds)
@@ -81,4 +82,4 @@ void log_message(log_level level, const char* filename, const char* source_file,
8182
*/
8283
void finish_logging();
8384

84-
#endif
85+
#endif // __LOG_H

bitlab/include/state.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __STATE_H
2+
#define __STATE_H
3+
4+
#include <signal.h>
5+
#include <unistd.h>
6+
#include <time.h>
7+
#include <fcntl.h>
8+
9+
/**
10+
* The program state structure used to store the state of the program.
11+
*
12+
* @param pid The process ID of the program.
13+
* @param start_time The start time of the program.
14+
* @param exit_flag The exit flag of the program.
15+
*/
16+
typedef struct
17+
{
18+
pid_t pid;
19+
time_t start_time;
20+
volatile sig_atomic_t exit_flag;
21+
} program_state;
22+
23+
extern program_state state;
24+
25+
void init_program_state(program_state* state);
26+
27+
void set_exit_flag(program_state* state, sig_atomic_t flag);
28+
29+
sig_atomic_t get_exit_flag(program_state* state);
30+
31+
#endif

bitlab/include/utils.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#ifndef __UTILS_H
22
#define __UTILS_H
33

4+
#include <stdio.h>
45
#include <stddef.h>
56

67
#define TIMESTAMP_LENGTH 20
78

9+
void usleep(unsigned int usec);
10+
811
/**
912
* Get the timestamp. This function is used to get the timestamp in a YYYYMMDDHHMMSS format.
1013
*
@@ -21,4 +24,9 @@ void get_timestamp(char* buffer, size_t buffer_size);
2124
*/
2225
void get_formatted_timestamp(char* buffer, size_t buffer_size);
2326

24-
#endif
27+
/**
28+
* Clear the CLI window.
29+
*/
30+
void clear_cli();
31+
32+
#endif // __UTILS_H

bitlab/src/bitlab.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
#include "bitlab.h"
22

33
#include <stdio.h>
4+
#include <pthread.h>
5+
#include <errno.h>
6+
#include <string.h>
47

8+
#include "state.h"
59
#include "log.h"
10+
#include "cli.h"
11+
#include "utils.h"
612

713
bitlab_result run_bitlab(int argc, char* argv[])
814
{
9-
// [ ] Utilize the argc and argv parameters.
15+
// [ ] utilize the argc and argv parameters.
1016
if (argc != 1 && argv != NULL) {}
1117

18+
// init
1219
init_logging(BITLAB_LOG);
1320
log_message(LOG_INFO, BITLAB_LOG, __FILE__, LOG_BITLAB_STARTED);
21+
init_program_state(&state);
1422

23+
pthread_t cli_thread;
24+
if (pthread_create(&cli_thread, NULL, handle_cli, (void*)NULL) != 0)
25+
log_message(LOG_WARN, BITLAB_LOG, __FILE__, "CLI thread creation failed: %s", strerror(errno));
26+
log_message(LOG_INFO, BITLAB_LOG, __FILE__, "CLI thread started");
1527

16-
printf("BitLab is running!\n");
28+
// main loop
29+
while (!get_exit_flag(&state))
30+
usleep(100000); // 100 ms
1731

32+
// cleanup
33+
pthread_join(cli_thread, NULL);
34+
log_message(LOG_INFO, BITLAB_LOG, __FILE__, LOG_BITLAB_FINISHED);
1835
finish_logging();
1936
return BITLAB_RESULT_SUCCESS;
2037
}

0 commit comments

Comments
 (0)