-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandControl.h
128 lines (111 loc) · 2.94 KB
/
commandControl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* ____ ____ _
* / ___/ ___|| |
* | | _\___ \| |
* | |_| |___) | |___
* \____|____/|_____|
*
* GShellLight. Header file for command operations.
*
* Author: Grindelf P.
* Created on 2023-09-13.
*
*/
#ifndef G_SHELL_LIGHT_COMMAND_CONTROLLER_H
#define G_SHELL_LIGHT_COMMAND_CONTROLLER_H
#include <string.h>
#include <stdlib.h>
#include "mainHeader.h"
#include "nativeCommands.h"
#include "commandControl.h"
#include "fileControl.h"
#include "gls.h"
#define SEPARATOR " "
/**
* Trims the commands array to the first empty element.
*
* <p><strong>Example:</strong> if the commands array is:</p>
* {0: ls
* 1: -a
* 2: -l
* 3: \0
* ...
* 9: \0}
* <p>the trimmedCommands array will be:<p>
* {0: ls
* 1: -a
* 2: -l
* 3: \0}
* @param commands vector of arguments
* @param trimmedCommands trimmed vector of arguments
* @return 0 if success
*/
int trim(char commands[ARG_SIZE][CMD_SIZE], char *trimmedCommands[ARG_SIZE]) {
int i;
for (i = 0; i < 10; ++i) {
if (commands[i][0] == '\0') {
break;
}
trimmedCommands[i] = commands[i];
}
return 0;
}
/**
* Split the command into arguments vector.
* @param buffer commands buffer
* @param commandContainer command container where vector will be stored
* @return 0 if success, -1 if error
*/
int splitCommand(char buffer[INIT_LINE_SIZE], char commands[ARG_SIZE][CMD_SIZE], int *commandsNumber) {
char *token;
int numberOfCommands = *commandsNumber;
token = strtok(buffer, SEPARATOR);
while (token != NULL && numberOfCommands < ARG_SIZE) {
strcpy(commands[numberOfCommands], token);
token = strtok(NULL, SEPARATOR);
numberOfCommands++;
}
*commandsNumber = numberOfCommands;
return 0;
}
/**
* Get the arguments vector from the input line
* @param commands arguments vector
* @return 0 if success, -1 if error
*/
int getArgumentsVector(char commands[ARG_SIZE][CMD_SIZE]) {
char buffer[INIT_LINE_SIZE];
int commandsNumber = 0;
printf("Enter your command ('exit' if you want to stop): ");
fgets(buffer, sizeof(buffer), stdin);
// Trim the unused space
size_t length = strlen(buffer);
if (buffer[length - 1] == '\n') {
buffer[length - 1] = '\0';
}
if (splitCommand(buffer, commands, &commandsNumber) == -1) {
return -1;
}
// Trim the commands array if needed
for (int i = commandsNumber; i < ARG_SIZE; i++) {
commands[i][0] = '\0';
}
return 0;
}
/**
* Check if the argument is in the list of native command
* (the command native to GShellLight)
* @param command vector of arguments
* @return 1 if the argument is in the list, 0 otherwise
*/
int isNative(const char *command) {
int isNative = 0;
for (int i = 0; i < NATIVE_CMDS_SIZE; ++i) {
if (strcmp(command, nativeCommands[i]) == 0) {
isNative = 1;
break;
}
}
return isNative;
}
#endif //G_SHELL_LIGHT_COMMAND_CONTROLLER_H