Compare commits
No commits in common. "abee9ec8fdf86bb5c81231ebd6faec411d43ae47" and "0f722e126bb133019a5980d5b47dd18684302b90" have entirely different histories.
abee9ec8fd
...
0f722e126b
1 changed files with 30 additions and 87 deletions
117
nsh.c
117
nsh.c
|
@ -13,17 +13,14 @@
|
||||||
#define COLOR_YELLOW "\033[38;2;249;226;175m"
|
#define COLOR_YELLOW "\033[38;2;249;226;175m"
|
||||||
#define COLOR_SKY "\033[38;2;137;220;235m"
|
#define COLOR_SKY "\033[38;2;137;220;235m"
|
||||||
#define COLOR_RED "\033[38;2;243;139;168m"
|
#define COLOR_RED "\033[38;2;243;139;168m"
|
||||||
#define COLOR_PEACH "\033[38;2;250;179;135m"
|
#define COLOR_PEACH "\033[38;2;250;179;135m"
|
||||||
#define COLOR_PINK "\033[38;2;245;194;231m"
|
|
||||||
|
|
||||||
const char *keywords[] = {
|
const char *keywords[] = {
|
||||||
"if", "else", "while", "for", "return", "switch", "case", "default",
|
"if", "else", "while", "for", "return", "switch", "case", "default", "break", "continue", "const", "#define", "#include", NULL
|
||||||
"break", "continue", "const", "#define", "#include", NULL
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *types[] = {
|
const char *types[] = {
|
||||||
"void", "int", "char", "float", "double", "long", "short", "unsigned",
|
"void", "int", "char", "float", "double", "long", "short", "unsigned", "signed", "struct", "enum", "union", NULL
|
||||||
"signed", "struct", "enum", "union", NULL
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Function declarations for different token handling */
|
/* Function declarations for different token handling */
|
||||||
|
@ -140,73 +137,35 @@ void highlight_char(const char *line, int *i)
|
||||||
printf(COLOR_TEAL "'");
|
printf(COLOR_TEAL "'");
|
||||||
(*i)++;
|
(*i)++;
|
||||||
while (line[*i] != '\'' && line[*i] != '\0') {
|
while (line[*i] != '\'' && line[*i] != '\0') {
|
||||||
/* Handle escape sequence */
|
if (line[*i] == '\\' && (line[*i + 1] == '\'' || line[*i + 1] == '\\')) {
|
||||||
if (line[*i] == '\\' && line[*i+1] > 32 && line[*i+1] < 127) {
|
printf("%c%c", line[*i], line[*i + 1]); // Handle escape sequences
|
||||||
printf(COLOR_PINK "%c%c" COLOR_RESET, line[*i], line[*i + 1]);
|
(*i) += 2;
|
||||||
/* Check if escape sequence is octal */
|
|
||||||
int cur_idx = (*i) + 2;
|
|
||||||
while (line[cur_idx] > 47 && line[cur_idx] < 58) {
|
|
||||||
printf(COLOR_PINK "%c" COLOR_RESET, line[cur_idx]);
|
|
||||||
cur_idx++;
|
|
||||||
}
|
|
||||||
if (cur_idx == *i + 2) {
|
|
||||||
/* Not octal escape code */
|
|
||||||
(*i) += 2;
|
|
||||||
} else {
|
|
||||||
(*i) += cur_idx - *i;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
printf(COLOR_TEAL "%c", line[*i]);
|
printf("%c", line[*i]);
|
||||||
(*i)++;
|
(*i)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (line[*i] == '\'') {
|
if (line[*i] == '\'') {
|
||||||
printf("'" COLOR_RESET);
|
printf("'" COLOR_RESET); // Print closing single quote and reset color
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function to handle and highlight string literals */
|
/* Function to handle and highlight string literals */
|
||||||
void highlight_string(const char *line, int *i)
|
void highlight_string(const char *line, int *i)
|
||||||
{
|
{
|
||||||
printf(COLOR_GREEN "\"");
|
printf(COLOR_GREEN "\""); // Print opening quote in green
|
||||||
(*i)++;
|
(*i)++;
|
||||||
while (line[*i] != '"' && line[*i] != '\0') {
|
while (line[*i] != '"' && line[*i] != '\0') {
|
||||||
/* Handle escape sequence */
|
if (line[*i] == '\\' && (line[*i + 1] == '"' || line[*i + 1] == '\\')) {
|
||||||
if (line[*i] == '\\' && line[*i+1] > 32 && line[*i+1] < 127) {
|
printf("%c%c", line[*i], line[*i + 1]); // Print the escape character and the next character
|
||||||
printf(COLOR_PINK "%c%c" COLOR_RESET, line[*i], line[*i + 1]);
|
(*i) += 2; // Move past the escape character and the next character
|
||||||
/* Check if escape sequence is octal */
|
|
||||||
int cur_idx = (*i) + 2;
|
|
||||||
while (line[cur_idx] > 47 && line[cur_idx] < 58) {
|
|
||||||
printf(COLOR_PINK "%c" COLOR_RESET, line[cur_idx]);
|
|
||||||
cur_idx++;
|
|
||||||
}
|
|
||||||
if (cur_idx == *i + 2) {
|
|
||||||
/* Not octal escape code */
|
|
||||||
(*i) += 2;
|
|
||||||
} else {
|
|
||||||
(*i) += cur_idx - *i;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
printf(COLOR_GREEN "%c", line[*i]);
|
printf("%c", line[*i]);
|
||||||
(*i)++;
|
(*i)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (line[*i] == '"') {
|
if (line[*i] == '"') {
|
||||||
printf("\"" COLOR_RESET);
|
printf("\"" COLOR_RESET); // Print the closing quote and reset color
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Function to handle and highlight include strings */
|
|
||||||
void highlight_include(const char *line, int *i)
|
|
||||||
{
|
|
||||||
printf(COLOR_GREEN "<");
|
|
||||||
(*i)++;
|
|
||||||
while (line[*i] != '>' && line[*i] != '\0') {
|
|
||||||
printf(COLOR_GREEN "%c", line[*i]);
|
|
||||||
(*i)++;
|
|
||||||
}
|
|
||||||
if (line[*i] == '>') {
|
|
||||||
printf(">" COLOR_RESET);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,10 +182,11 @@ int main(int argc, char **argv)
|
||||||
fprintf(stderr, "Unable to open file: %s\n", argv[i]);
|
fprintf(stderr, "Unable to open file: %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
/* Handle file without extension */
|
/* handle file without extension
|
||||||
|
* ext is the extension if . exist in filename, otherwise nothing */
|
||||||
char *ext = strrchr(argv[i], '.');
|
char *ext = strrchr(argv[i], '.');
|
||||||
if (ext != NULL) {
|
if (ext != NULL) {
|
||||||
/* How to identify file type if there isn't extension? */
|
/* how to identify file type if there isn't extension? */
|
||||||
ext++;
|
ext++;
|
||||||
}
|
}
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
@ -236,10 +196,6 @@ int main(int argc, char **argv)
|
||||||
while (fgets(buffer, sizeof(buffer), f) != NULL) {
|
while (fgets(buffer, sizeof(buffer), f) != NULL) {
|
||||||
if (ext && strcmp(ext, "c") == 0) {
|
if (ext && strcmp(ext, "c") == 0) {
|
||||||
for (int i = 0; buffer[i] != '\0'; i++) {
|
for (int i = 0; buffer[i] != '\0'; i++) {
|
||||||
if (buffer[i] == '\t') {
|
|
||||||
printf(" ");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (buffer[i] == '\'') {
|
if (buffer[i] == '\'') {
|
||||||
highlight_char(buffer, &i);
|
highlight_char(buffer, &i);
|
||||||
continue;
|
continue;
|
||||||
|
@ -248,14 +204,6 @@ int main(int argc, char **argv)
|
||||||
highlight_string(buffer, &i);
|
highlight_string(buffer, &i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (buffer[i] == '<') {
|
|
||||||
if ((buffer[i-1] != '\0' && buffer[i-1] == 'e') ||
|
|
||||||
(buffer[i-1] != '\0' && buffer[i-1] == ' ' &&
|
|
||||||
buffer[i-2] != '\0' && buffer[i-2] == 'e')) {
|
|
||||||
highlight_include(buffer, &i);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (buffer[i] == '/' && buffer[i+1] == '/') {
|
if (buffer[i] == '/' && buffer[i+1] == '/') {
|
||||||
highlight_single_comment(buffer, &i);
|
highlight_single_comment(buffer, &i);
|
||||||
break;
|
break;
|
||||||
|
@ -263,39 +211,34 @@ int main(int argc, char **argv)
|
||||||
if (buffer[i] == '/' && buffer[i+1] == '*') {
|
if (buffer[i] == '/' && buffer[i+1] == '*') {
|
||||||
highlight_multi_comment(buffer, &i);
|
highlight_multi_comment(buffer, &i);
|
||||||
}
|
}
|
||||||
if (isspace(buffer[i]) || (ispunct(buffer[i]) &&
|
if (isspace(buffer[i]) || (ispunct(buffer[i]) && buffer[i] != '_' && buffer[i] != '#')) {
|
||||||
buffer[i] != '_' && buffer[i] != '#')) {
|
if (word_len > 0 && buffer[i] != '*' && buffer[i] != '&' && buffer[i] != '=' && buffer[i] != '+' && buffer[i] != '|') {
|
||||||
if (word_len > 0) {
|
|
||||||
word[word_len] = '\0';
|
word[word_len] = '\0';
|
||||||
if (buffer[i] == '(' && !is_keyword(word) &&
|
if (buffer[i] == '(' && !is_keyword(word) && !is_type(word)) {
|
||||||
!is_type(word)) {
|
highlight_function(word); // Highlight function if it’s not a keyword or type
|
||||||
highlight_function(word);
|
|
||||||
} else if (is_type(word)) {
|
} else if (is_type(word)) {
|
||||||
highlight_type(word);
|
highlight_type(word); // Highlight types correctly
|
||||||
} else if (is_keyword(word)) {
|
} else if (is_keyword(word)) {
|
||||||
highlight_keyword(word);
|
highlight_keyword(word); // Highlight keywords or other words
|
||||||
} else {
|
} else {
|
||||||
highlight_normal(word);
|
highlight_normal(word);
|
||||||
}
|
}
|
||||||
word_len = 0;
|
word_len = 0;
|
||||||
}
|
}
|
||||||
if (buffer[i] == '[' || buffer[i] == ']' ||
|
if (buffer[i] == '[' || buffer[i] == ']' || buffer[i] == '(' || buffer[i] == ')' || buffer[i] == '{' || buffer[i] == '}') {
|
||||||
buffer[i] == '(' || buffer[i] == ')' ||
|
|
||||||
buffer[i] == '{' || buffer[i] == '}') {
|
|
||||||
word[word_len++] = buffer[i];
|
word[word_len++] = buffer[i];
|
||||||
word[word_len] = '\0';
|
word[word_len] = '\0';
|
||||||
highlight_brackets(word);
|
highlight_brackets(word);
|
||||||
word_len = 0;
|
word_len = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (buffer[i] == '*' || buffer[i] == '&' ||
|
if (buffer[i] == '*' || buffer[i] == '&' || buffer[i] == '=' || buffer[i] == '+' || buffer[i] == '|') {
|
||||||
buffer[i] == '=' || buffer[i] == '+' ||
|
|
||||||
buffer[i] == '|' || buffer[i] == '!' ||
|
|
||||||
buffer[i] == '<' || buffer[i] == '>') {
|
|
||||||
word[word_len++] = buffer[i];
|
word[word_len++] = buffer[i];
|
||||||
word[word_len] = '\0';
|
if (buffer[i+1] != '*' && buffer[i+1] != '&' && buffer[i+1] != '=' && buffer[i+1] != '+' && buffer[i+1] != '|') {
|
||||||
word_len = 0;
|
word[word_len] = '\0';
|
||||||
highlight_symbol(word);
|
highlight_symbol(word);
|
||||||
|
word_len = 0;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
printf("%c", buffer[i]);
|
printf("%c", buffer[i]);
|
||||||
|
|
Loading…
Reference in a new issue