From 256bf3acc9c21c55c955824f1a00bc7cba6b85a8 Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 7 Mar 2024 21:41:07 +0100 Subject: improved 7ed.c to make using the current flags a little smarter --- 7ed.c | 54 +++++++++++++++++++++++++++++++++++----------- 7ed.h | 4 ++++ input.c | 11 +--------- input.h | 18 ++++++++++++++++ startmode.c | 2 +- test/testgrounds/file2.txt | 2 +- 6 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 input.h diff --git a/7ed.c b/7ed.c index 25f254f..cd31f34 100644 --- a/7ed.c +++ b/7ed.c @@ -3,35 +3,63 @@ #include #include #include "7ed.h" +#include "input.h" #include -#define USAGE "" +#define VERSION 1 +#define USAGE "7ed [OPTION] [FILENAME]\n" \ + "\nVALID OPTIONS:\n\n"\ + " -v Prints out the version number\n"\ + " -i Input file\n" + #define PROGRAM_NAME "7ed" int main (int argc, char *argv[]) { + if (argc == 1) { + fprintf(stderr, "%s: Please provide a file.\n%s\n", argv[0], USAGE); + return EXIT_FAILURE; + } + + int i_used = FALSE_7ED; // These flags are to make sure -v and -i dont get used together. It makes the program feel a bit nicer. + int v_used = FALSE_7ED; + int opt; int returnval; - while ((opt = getopt(argc, argv, "i:")) != -1) { + char *optarg_copy = NULL; // We will copy optarg to this + while ((opt = getopt(argc, argv, "i:v")) != -1) { switch (opt) { case 'i': - returnval = startmode(optarg); - if (returnval == 1) { - return EXIT_FAILURE; - } - break; + optarg_copy = malloc(strlen(optarg) + 1); // malloc memory for optarg_copy + strcpy(optarg_copy, optarg); // Copy optarg to optarg_copy + i_used = TRUE_7ED; + + break; + case 'v': + + v_used = TRUE_7ED; + break; - default: - fprintf(stderr, "%s", USAGE); - return EXIT_FAILURE; } } - - if (argc == 1) { - fprintf(stderr, "%s: Please provide a file.\n%s", argv[0], USAGE); + if (i_used == TRUE_7ED && v_used == TRUE_7ED) { // If both flags are used print usage and exit + free(optarg_copy); + fprintf(stderr, "%s", USAGE); return EXIT_FAILURE; } + + if (i_used == TRUE_7ED) { // enter startmode if we used 'i' + returnval = startmode(optarg_copy); + if (returnval == 1) { + free(optarg_copy); + return EXIT_FAILURE; + } + free(optarg_copy); + } + if (v_used == TRUE_7ED) { // print version + fprintf(stdout, "7Editor Version %d\n", VERSION); + } return EXIT_SUCCESS; diff --git a/7ed.h b/7ed.h index ece0aab..ab6a239 100644 --- a/7ed.h +++ b/7ed.h @@ -1,5 +1,6 @@ #include #include +#include #define BUF_SZ_256 256 #define BUF_SZ_512 512 @@ -8,6 +9,9 @@ #define BUF_SZ_4 4096 #define BUF_SZ_8 8192 +#define TRUE_7ED 0 +#define FALSE_7ED 1 + int count_lines_in_file(char filename[], uint64_t *lines); int count_lines_in_file_posix(char filename[], size_t *lines); diff --git a/input.c b/input.c index 28c6587..fdb45ce 100644 --- a/input.c +++ b/input.c @@ -4,22 +4,13 @@ #include #include #include "7ed.h" +#include "input.h" #include -#define _ONE 1 -#define _SINGLE 1 -#define _MULTIPLE 2 -#define _FAIL '?' // final return value from smode_input to indicate an invalid -#define _VALID 0 // this may only be used to mark as valid or invalid from the validate functions -#define _INVALID -1 // this may only be used to mark as valid or invalid from the validate functions - // _FAIL and _INVALID are sorta tied to eachother // This will be the new input system for combining commands with line numbers // Work in progress and far from finished. This is not included when compiling normally. -#define SMODE_MAX_SIZE 33 -#define SMODE_MAX_INPUT_SIZE 32 - int validate_L(char *smode_buf) { /* diff --git a/input.h b/input.h new file mode 100644 index 0000000..94c746d --- /dev/null +++ b/input.h @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include +#include "7ed.h" +#include + +#define SMODE_MAX_SIZE 33 +#define SMODE_MAX_INPUT_SIZE 32 + +#define _ONE 1 +#define _SINGLE 1 +#define _MULTIPLE 2 +#define _FAIL '?' // final return value from smode_input to indicate an invalid +#define _VALID 0 // this may only be used to mark as valid or invalid from the validate functions +#define _INVALID -1 // this may only be used to mark as valid or invalid from the validate functions + // _FAIL and _INVALID are sorta tied to eachother \ No newline at end of file diff --git a/startmode.c b/startmode.c index 0ad7155..8b89b93 100644 --- a/startmode.c +++ b/startmode.c @@ -30,7 +30,7 @@ int startmode(char filename[]) { uint64_t Flines; int returnval = count_lines_in_file(filename, &Flines); if (returnval == 1) { - return EXIT_FAILURE; + return 1; } fprintf(stdout,"%s %lu lines\n", filename, Flines); diff --git a/test/testgrounds/file2.txt b/test/testgrounds/file2.txt index 73ee54a..d205e16 100644 --- a/test/testgrounds/file2.txt +++ b/test/testgrounds/file2.txt @@ -1,6 +1,6 @@ asldaskdlalksjd 11111111 -222222 +WHAT!!!! öölaölskdöaslkölaksöd +10230123203293 ()))&%#"#¤%&/&%¤ -- cgit v1.2.3