diff options
author | Oskar <[email protected]> | 2024-04-20 00:03:57 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-04-20 00:03:57 +0200 |
commit | e7e9f3077f3dbbe31b22316a47980c417d915b49 (patch) | |
tree | dc8e1a713a7804c2c0afc6676a07690c06acad19 | |
parent | e0e28a119e7a0f83ef9382413dba603ae342a8fb (diff) |
Choicemode switches
-rw-r--r-- | 7ed.c | 27 | ||||
-rw-r--r-- | 7ed.h | 2 | ||||
-rw-r--r-- | editmode.c | 3 | ||||
-rw-r--r-- | functions.c | 39 | ||||
-rw-r--r-- | process_multiples.c | 20 |
5 files changed, 58 insertions, 33 deletions
@@ -51,6 +51,8 @@ #define PROGRAM_NAME "7ed" +uint8_t g_choicemode = MODE_NORMAL; + int main (int argc, char *argv[]) { if (argc == 1) { @@ -60,11 +62,13 @@ int main (int argc, char *argv[]) { 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 y_used = FALSE_7ED; + int n_used = FALSE_7ED; int opt; int returnval; char *optarg_copy = NULL; // We will copy optarg to this - while ((opt = getopt(argc, argv, "i:v")) != -1) { + while ((opt = getopt(argc, argv, "i:vyn")) != -1) { switch (opt) { case 'i': @@ -76,6 +80,17 @@ int main (int argc, char *argv[]) { case 'v': v_used = TRUE_7ED; + + break; + case 'y': + + y_used = TRUE_7ED; + + break; + case 'n': + + n_used = TRUE_7ED; + break; } @@ -85,8 +100,18 @@ int main (int argc, char *argv[]) { fprintf(stderr, "%s", USAGE); return EXIT_FAILURE; } + + if (n_used == TRUE_7ED && y_used == TRUE_7ED) { // If both YES and NO 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' + + if(n_used == TRUE_7ED) { g_choicemode = MODE_NO; } + if(y_used == TRUE_7ED) { g_choicemode = MODE_YES; } + returnval = startmode(optarg_copy); if (returnval == 1) { free(optarg_copy); @@ -25,7 +25,7 @@ int count_lines_in_file_posix(char filename[], size_t *lines); void confirm(); -int choice(); +int choice(uint8_t mode); void shuffle(char arr[], int n); @@ -7,6 +7,7 @@ #include <stdint.h> extern uint8_t new; +extern uint8_t g_choicemode; int delete_specified_newline(char filename[], long focus) { // special version of write_line that does as the name says @@ -273,7 +274,7 @@ int editmode(char filename[], uint64_t focus) { // the editing interface return 0; } - int yesno = choice(); + int yesno = choice(g_choicemode); if (yesno == 1) { return 0; diff --git a/functions.c b/functions.c index 5b434bc..0b17512 100644 --- a/functions.c +++ b/functions.c @@ -9,36 +9,33 @@ int choice(uint8_t mode) { char choice; + char modechoice; - if (mode == MODE_NORMAL) { - do { + do { - fputs("[Y / N] ? ", stdout); + if (mode == MODE_NORMAL) { fputs("[Y / N] ? ", stdout); } + if (mode == MODE_YES) { fputs("[Y / n] ? ", stdout); } + if (mode == MODE_NO) { fputs("[y / N] ? ", stdout); } - choice = getchar(); - if (choice == '\n') { continue; } + choice = getchar(); + if (choice == '\n' && mode == MODE_YES) { modechoice = 'Y'; choice = modechoice; goto modeskip;} + if (choice == '\n' && mode == MODE_NO) { modechoice = 'N'; choice = modechoice; goto modeskip;} + if (choice == '\n' && mode == MODE_NORMAL) { continue; } - while ('\n' != getchar()); + while ('\n' != getchar()); - } while ( (choice != 'Y') && (choice != 'y') && (choice != 'N') && (choice != 'n') ); + modeskip: - if ( (choice == 'Y') || (choice == 'y') ) - { - return 0; - } + } while ( (choice != 'Y') && (choice != 'y') && (choice != 'N') && (choice != 'n') ); - if ((choice == 'N') || (choice == 'n') ) - { - return 1; - } + if ( (choice == 'Y') || (choice == 'y') ) + { + return 0; } - if (mode == MODE_YES) { - - } - - if (mode == MODE_NO) { - + if ((choice == 'N') || (choice == 'n') ) + { + return 1; } return EXIT_FAILURE; diff --git a/process_multiples.c b/process_multiples.c index 10a565d..f98bad4 100644 --- a/process_multiples.c +++ b/process_multiples.c @@ -8,6 +8,8 @@ #include "i_validation.h" #include <stdint.h> +extern uint8_t g_choicemode; + int check_L_linecount(uint64_t Flines, uint64_t focus, int mode) { // Check if you are trying to modify or move to lines under 1 or lines that are over the total amount of lines in the file. // Mode N allows us to go to line 0 because N needs it. if (mode == MODE_N) { @@ -223,7 +225,7 @@ int call_N_plus_continue(char *multiple, uint64_t focus, char *filename) { } if (newlines_to_add > 1) { - choice_yesno = choice(); + choice_yesno = choice(g_choicemode); } if (choice_yesno == 1) { return -1; } @@ -288,7 +290,7 @@ int call_X_plus_continue(char *multiple, uint64_t focus, char *filename, uint64_ return -1; } - if (choice() == 1) { return -1; } + if (choice(g_choicemode) == 1) { return -1; } for(uint64_t count = 0 ; count < amount_of_lines_to_remove ; count++) { // This is very inefficient remove_line_contents(filename, focus+count); @@ -321,7 +323,7 @@ int call_X_immediate(char *multiple, uint64_t Flines, char *filename) { return -1; } - if (choice() == 1) { return -1; } + if (choice(g_choicemode) == 1) { return -1; } remove_line_contents(filename, remove_line_contents_position); @@ -335,7 +337,7 @@ int call_X(char *multiple, uint64_t focus, uint64_t Flines, char *filename) { if (multiple[1] == '\n') { // X Will remove current line imm = _NA; - if(choice() == 0) { + if(choice(g_choicemode) == 0) { remove_line_contents(filename, focus); } return 0; @@ -344,7 +346,7 @@ int call_X(char *multiple, uint64_t focus, uint64_t Flines, char *filename) { if (multiple[1] == '+') { imm = _NA; if (multiple[2] == '\n') { - if(choice() == 0) { + if(choice(g_choicemode) == 0) { remove_line_contents(filename, focus); } return 0; @@ -386,7 +388,7 @@ uint64_t call_D_plus_continue(char *multiple, uint64_t focus, uint64_t Flines, c return focus; } - if (choice() == 1) { return focus; } + if (choice(g_choicemode) == 1) { return focus; } for(uint64_t count = 0 ; count < lines_and_newlines_to_remove ; count++) { // This is very inefficient remove_line_contents_and_newline(filename, focus); @@ -420,7 +422,7 @@ uint64_t call_D_immediate(char *multiple, uint64_t focus, uint64_t Flines, char return focus; } - if (choice() == 1) { return focus; } + if (choice(g_choicemode) == 1) { return focus; } remove_line_contents_and_newline(filename, remove_line_and_newline_position); if (focus == 1 && remove_line_and_newline_position == 1) { @@ -442,7 +444,7 @@ uint64_t call_D(char *multiple, uint64_t focus, uint64_t Flines, char *filename) if (multiple[1] == '\n') { // D Will remove current line imm = _NA; - if(choice() == 0) { + if(choice(g_choicemode) == 0) { remove_line_contents_and_newline(filename, focus); if (focus == 1) { return 1; } return focus-1; @@ -455,7 +457,7 @@ uint64_t call_D(char *multiple, uint64_t focus, uint64_t Flines, char *filename) if (multiple[1] == '+') { imm = _NA; if (multiple[2] == '\n') { - if(choice() == 0) { + if(choice(g_choicemode) == 0) { remove_line_contents_and_newline(filename, focus); if (focus == 1) { return 1; } return focus-1; |