summaryrefslogtreecommitdiff
path: root/startmode.c
blob: 988b6fd3603eac22f8ae22fee22a50c801f4050c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "7ed.h"

int NCAT(char filename[]) {

    FILE *file;
    file = fopen(filename,"r");

    if (file == NULL) {
        fprintf(stderr, "Cannot open file. NCAT\n");
        return 1;
    }

    char buffer[BUF_SZ_2];
    size_t bytes_read;
    while ((bytes_read = fread(buffer, 1, BUF_SZ_2, file)) > 0) {
        fwrite(buffer, 1, bytes_read, stdout);
    }
    fclose(file);
    return 0;
}

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) {
                        fprintf(stderr, "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(stderr, "L is either less than 1 or more than %ld\n", Flines);
                } else {
                    focus = Lfocus;
                }

            break;
            case 'P':
            case 'p':
                
                char *line;
                size_t start;
                int ret = GET_LINE(filename, focus, &line, &start);
                if (ret == 1) {
                    return EXIT_FAILURE;
                }
                fprintf(stdout, "%s", line);
                //printf("%ld", start);
                free(line);

            break;
            case 'E':
            case 'e':
                
                editmode(filename, focus);

            break;
            case 'C':
            case 'c':
                
                size_t CFlines;
                int returnval = COUNT_LINES_IN_FILE(filename, &CFlines);
                if (returnval == 1) {
                    return EXIT_FAILURE;
                }
                fprintf(stdout,"%s %ld lines\n", filename, CFlines);

            break;
            case 'Q':
            case 'q':
                return EXIT_SUCCESS;
            break;
            case 'a':
            case 'A':
                NCAT(filename);
            break;
            case 'n':
            case 'N':

                NEW_LINE(filename);

            break;
            default:
                fprintf(stdout, "?\n");
        }



    }
    
    return EXIT_SUCCESS;
}