summaryrefslogtreecommitdiff
path: root/startmode.c
diff options
context:
space:
mode:
authorOskar <[email protected]>2023-10-26 15:59:28 +0200
committerOskar <[email protected]>2023-10-26 15:59:28 +0200
commit45795ba6e39027982e215d1ea4c37ad67c987c04 (patch)
tree6b36b9d8fe5d0708df7096ef1a2f4e31cb0f7c75 /startmode.c
First commit
Diffstat (limited to 'startmode.c')
-rw-r--r--startmode.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/startmode.c b/startmode.c
new file mode 100644
index 0000000..7c1e5fd
--- /dev/null
+++ b/startmode.c
@@ -0,0 +1,94 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include "7ed.h"
+
+int startmode(char filename[]) {
+
+ size_t Flines;
+ int returnval = COUNT_LINES_IN_FILE(filename, &Flines);
+ if (returnval == 1) {
+ return EXIT_FAILURE;
+ }
+ fprintf(stdout,"%s %ld lines\n", filename, Flines);
+
+ long focus = 1;
+
+ while(1) {
+
+ fprintf(stdout, "(%ld): ", focus);
+ char command = getchar();
+ if (command == '\n') {
+ continue;
+ }
+ while ('\n' != getchar());
+
+ switch (command) {
+ case 'L':
+ case 'l':
+ long Lfocus;
+ char buf[1024];
+ int success;
+
+ do {
+ fprintf(stdout, "(L): ");
+ if (!fgets(buf, 1024, stdin)) {
+ fprintf(stderr, "Too many characters\n");
+ break;
+ }
+ char *endptr;
+
+ Lfocus = strtol(buf, &endptr, 10);
+ errno = 0;
+ if (errno == ERANGE) {
+ printf("Sorry, this number is too small or too large.\n");
+ success = 0;
+ }
+ else if (endptr == buf) {
+ // no character was read
+ success = 0;
+ }
+ else if (*endptr && *endptr != '\n') {
+ // *endptr is neither end of string nor newline,
+ // so we didn't convert the *whole* input
+ success = 0;
+ }
+
+ else {
+ success = 1;
+ }
+
+ } while (!success);
+
+ if (Lfocus < 1 || Lfocus > (long)Flines) {
+ fprintf(stdout, "L is either less than 1 or more than %ld\n", Flines);
+ } else {
+ focus = Lfocus;
+ }
+
+ break;
+ case 'P':
+ case 'p':
+ print_7ed();
+
+ break;
+ case 'E':
+ case 'e':
+ printf("EDIT MODE\n");
+ break;
+ case 'Q':
+ case 'q':
+ return EXIT_SUCCESS;
+ break;
+ default:
+ printf("?\n");
+ }
+
+
+
+ }
+
+ return EXIT_SUCCESS;
+} \ No newline at end of file