Read stdin if no argument supplied

This commit is contained in:
Night Kaly 2024-11-06 14:52:20 +00:00
parent c0877d1a51
commit 652f3fc6a4
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

229
nsh.c
View file

@ -210,132 +210,145 @@ void highlight_include(const char *line, int *i)
} }
} }
/* Main function to process input */ void highlight(char *filename)
int main(int argc, char **argv)
{ {
if (argc < 2) { char *ext;
fprintf(stderr, "nsh <file> <file2> ...\n"); FILE *f;
exit(1); if (filename == NULL) {
} f = stdin;
for (int i = 1; i < argc; i++) { } else {
FILE *f = fopen(argv[i], "r"); f = fopen(filename, "r");
if (!f) { if (!f) {
fprintf(stderr, "Unable to open file: %s\n", argv[i]); fprintf(stderr, "Unable to open file: %s\n", filename);
exit(1); exit(1);
} }
/* Handle file without extension */ /* Handle file without extension */
char *ext = strrchr(argv[i], '.'); ext = strrchr(filename, '.');
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 word[256];
int word_len = 0;
while (fgets(buffer, sizeof(buffer), f) != NULL) { char buffer[1024];
if (ext && strcmp(ext, "c") == 0) { char word[256];
for (int i = 0; buffer[i] != '\0'; i++) { int word_len = 0;
if (buffer[i] == '\t') {
printf(" "); while (fgets(buffer, sizeof(buffer), f) != NULL) {
if (ext && strcmp(ext, "c") == 0) {
for (int i = 0; buffer[i] != '\0'; i++) {
if (buffer[i] == '\t') {
printf(" ");
continue;
}
if (buffer[i] == '\'') {
highlight_char(buffer, &i);
continue;
}
if (buffer[i] == '"') {
highlight_string(buffer, &i);
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; continue;
} }
if (buffer[i] == '\'') {
highlight_char(buffer, &i);
continue;
}
if (buffer[i] == '"') {
highlight_string(buffer, &i);
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] == '/') {
highlight_single_comment(buffer, &i);
break;
}
if (buffer[i] == '/' && buffer[i+1] == '*') {
highlight_multi_comment(buffer, &i);
}
if (isspace(buffer[i]) || (ispunct(buffer[i]) &&
buffer[i] != '_' && buffer[i] != '#')) {
if (word_len > 0) {
word[word_len] = '\0';
if (buffer[i] == '(' && !is_keyword(word) &&
!is_type(word)) {
highlight_function(word);
} else if (is_type(word)) {
highlight_type(word);
} else if (is_keyword(word)) {
highlight_keyword(word);
} else {
highlight_normal(word);
}
word_len = 0;
}
if (buffer[i] == '[' || buffer[i] == ']' ||
buffer[i] == '(' || buffer[i] == ')' ||
buffer[i] == '{' || buffer[i] == '}') {
word[word_len++] = buffer[i];
word[word_len] = '\0';
highlight_brackets(word);
word_len = 0;
continue;
}
if (buffer[i] == '*' || buffer[i] == '&' ||
buffer[i] == '=' || buffer[i] == '+' ||
buffer[i] == '|' || buffer[i] == '!' ||
buffer[i] == '<' || buffer[i] == '>') {
word[word_len++] = buffer[i];
word[word_len] = '\0';
word_len = 0;
highlight_symbol(word);
continue;
}
printf("%c", buffer[i]);
} else if (isdigit(buffer[i]) ) {
word[word_len++] = buffer[i];
if (isdigit(buffer[i+1]) == 0) {
word[word_len] = '\0';
highlight_number(word);
word_len = 0;
}
} else {
word[word_len++] = buffer[i];
}
} }
} else { if (buffer[i] == '/' && buffer[i+1] == '/') {
/* Remove newline from buffer, if present */ highlight_single_comment(buffer, &i);
size_t len = strlen(buffer); break;
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
} }
if (buffer[i] == '/' && buffer[i+1] == '*') {
if (strncmp(buffer, "---", 3) == 0) { highlight_multi_comment(buffer, &i);
printf(COLOR_YELLOW "%s" COLOR_RESET "\n", buffer); }
} else if (strncmp(buffer, "+++", 3) == 0) { if (isspace(buffer[i]) || (ispunct(buffer[i]) &&
printf(COLOR_PEACH "%s" COLOR_RESET "\n", buffer); buffer[i] != '_' && buffer[i] != '#')) {
} else if (strncmp(buffer, "@@", 2) == 0) { if (word_len > 0) {
printf(COLOR_OVERLAY0 "%s" COLOR_RESET "\n", buffer); word[word_len] = '\0';
} else if (strncmp(buffer, "index", 5) == 0) { if (buffer[i] == '(' && !is_keyword(word) &&
printf(COLOR_TEAL "%s" COLOR_RESET "\n", buffer); !is_type(word)) {
} else if (strncmp(buffer, "diff", 4) == 0) { highlight_function(word);
printf(COLOR_BLUE "%s" COLOR_RESET "\n", buffer); } else if (is_type(word)) {
} else if (buffer[0] == '+') { highlight_type(word);
printf(COLOR_GREEN "%s" COLOR_RESET "\n", buffer); } else if (is_keyword(word)) {
} else if (buffer[0] == '-') { highlight_keyword(word);
printf(COLOR_RED "%s" COLOR_RESET "\n", buffer); } else {
highlight_normal(word);
}
word_len = 0;
}
if (buffer[i] == '[' || buffer[i] == ']' ||
buffer[i] == '(' || buffer[i] == ')' ||
buffer[i] == '{' || buffer[i] == '}') {
word[word_len++] = buffer[i];
word[word_len] = '\0';
highlight_brackets(word);
word_len = 0;
continue;
}
if (buffer[i] == '*' || buffer[i] == '&' ||
buffer[i] == '=' || buffer[i] == '+' ||
buffer[i] == '|' || buffer[i] == '!' ||
buffer[i] == '<' || buffer[i] == '>') {
word[word_len++] = buffer[i];
word[word_len] = '\0';
word_len = 0;
highlight_symbol(word);
continue;
}
printf("%c", buffer[i]);
} else if (isdigit(buffer[i]) ) {
word[word_len++] = buffer[i];
if (isdigit(buffer[i+1]) == 0) {
word[word_len] = '\0';
highlight_number(word);
word_len = 0;
}
} else { } else {
printf("%s\n", buffer); word[word_len++] = buffer[i];
} }
} }
} else {
/* Remove newline from buffer, if present */
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
}
if (strncmp(buffer, "---", 3) == 0) {
printf(COLOR_YELLOW "%s" COLOR_RESET "\n", buffer);
} else if (strncmp(buffer, "+++", 3) == 0) {
printf(COLOR_PEACH "%s" COLOR_RESET "\n", buffer);
} else if (strncmp(buffer, "@@", 2) == 0) {
printf(COLOR_OVERLAY0 "%s" COLOR_RESET "\n", buffer);
} else if (strncmp(buffer, "index", 5) == 0) {
printf(COLOR_TEAL "%s" COLOR_RESET "\n", buffer);
} else if (strncmp(buffer, "diff", 4) == 0) {
printf(COLOR_BLUE "%s" COLOR_RESET "\n", buffer);
} else if (buffer[0] == '+') {
printf(COLOR_GREEN "%s" COLOR_RESET "\n", buffer);
} else if (buffer[0] == '-') {
printf(COLOR_RED "%s" COLOR_RESET "\n", buffer);
} else {
printf("%s\n", buffer);
}
} }
} }
}
/* Main function to process input */
int main(int argc, char **argv)
{
if (argc > 1) {
for (int i = 1; i < argc; i++) {
highlight(argv[i]);
}
} else {
highlight(NULL);
}
return 0; return 0;
} }