Read stdin if no argument supplied
This commit is contained in:
parent
c0877d1a51
commit
652f3fc6a4
1 changed files with 121 additions and 108 deletions
33
nsh.c
33
nsh.c
|
@ -210,25 +210,26 @@ void highlight_include(const char *line, int *i)
|
|||
}
|
||||
}
|
||||
|
||||
/* Main function to process input */
|
||||
int main(int argc, char **argv)
|
||||
void highlight(char *filename)
|
||||
{
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "nsh <file> <file2> ...\n");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = 1; i < argc; i++) {
|
||||
FILE *f = fopen(argv[i], "r");
|
||||
char *ext;
|
||||
FILE *f;
|
||||
if (filename == NULL) {
|
||||
f = stdin;
|
||||
} else {
|
||||
f = fopen(filename, "r");
|
||||
if (!f) {
|
||||
fprintf(stderr, "Unable to open file: %s\n", argv[i]);
|
||||
fprintf(stderr, "Unable to open file: %s\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
/* Handle file without extension */
|
||||
char *ext = strrchr(argv[i], '.');
|
||||
ext = strrchr(filename, '.');
|
||||
if (ext != NULL) {
|
||||
/* How to identify file type if there isn't extension? */
|
||||
ext++;
|
||||
}
|
||||
}
|
||||
|
||||
char buffer[1024];
|
||||
char word[256];
|
||||
int word_len = 0;
|
||||
|
@ -336,6 +337,18 @@ int main(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue