-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed.c
239 lines (221 loc) · 6.02 KB
/
ed.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (C) 2021 Benjamin Stürz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#define PROG_NAME "ed"
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "errprintf.h"
#include "common.h"
#include "buf.h"
// Simple ED clone
// TODO: finish this program
static char* skip_ws(char* s) {
while (isspace(*s)) ++s;
return s;
}
enum ed_mode {
ED_NORMAL,
ED_INSERT,
ED_EXIT,
};
struct ed_data {
const char* filename;
const char* prompt;
int suppress;
size_t current_line;
};
static bool write_file(const char* filename, char** buffer, int suppress) {
FILE* file = fopen(filename, "w");
if (!file) {
if (!suppress)
errprintf("%s", filename);
return false;
}
int num = 0;
for (size_t i = 0; i < buf_len(buffer); ++i) {
num += fprintf(file, "%s\n", buffer[i]);
}
fclose(file);
if (!suppress)
fprintf(stderr, "%d\n", num);
return true;
}
static bool read_file(const char* filename, char*** buffer, int suppress) {
FILE* file = fopen(filename, "r");
if (!file) {
if (!suppress)
errprintf("%s", filename);
return false;
}
size_t num = 0;
char* line = NULL;
char ch;
while ((ch = fgetc(file)) != EOF) {
if (ch == '\n') buf_push(line, '\0'), buf_push(*buffer, line), line = NULL;
else buf_push(line, ch);
++num;
}
fclose(file);
if (line)
buf_push(line, '\0'), buf_push(*buffer, line);
if (!suppress)
printf("%zu\n", num);
return true;
}
static enum ed_mode ed_insert(struct ed_data* data, char*** buffer) {
char* line = readline(stdin);
if (strcmp(line, ".") == 0) {
buf_free(line);
return ED_NORMAL;
}
buf_insert(*buffer, data->current_line, line);
++data->current_line;
return ED_INSERT;
}
static enum ed_mode ed_normal(struct ed_data* data, char*** buffer) {
printf("%s", data->prompt);
fflush(stdout);
char* line = readline(stdin);
enum ed_mode mode = ED_NORMAL;
char* s = line;
if (feof(stdin) || strcmp(line, "q") == 0) {
buf_free(line);
return ED_EXIT;
}
s = skip_ws(s);
if (*s == '#') goto end;
else if (*s == 'w') {
++s;
if (*s == 'q') mode = ED_EXIT, ++s;
s = skip_ws(s);
if (*s) {
if (!write_file(s, *buffer, data->suppress)) puts("?"), mode = ED_NORMAL;
} else {
if (!data->filename || !write_file(data->filename, *buffer, data->suppress))
puts("?"), mode = ED_NORMAL;
}
goto end;
}
if (!*s) {
if (data->current_line <= buf_len(*buffer)) {
puts((*buffer)[data->current_line - 1]);
++data->current_line;
}
else puts("?");
goto end;
}
// parse numbers
size_t first_num = buf_len(*buffer), second_num = buf_len(*buffer);
if (*s == '$') {
first_num = buf_len(*buffer);
++s;
} else if (isdigit(*s)) {
first_num = 0;
while (isdigit(*s)) first_num = first_num * 10 + (*s++ - '0');
if (first_num == 0) { puts("?"); goto end; }
}
s = skip_ws(s);
if (*s == ',') {
++s;
if (*s == '$') {
++s;
second_num = buf_len(*buffer);
} else if (isdigit(*s)) {
second_num = 0;
while (isdigit(*s)) second_num = second_num * 10 + (*s++ - '0');
} else second_num = first_num;
if (second_num == 0) { puts("?"); goto end; }
} else second_num = first_num;
s = skip_ws(s);
if (first_num > second_num || second_num > buf_len(*buffer)) { puts("?"); goto end; }
switch (*s) {
case '\0':
if (first_num <= buf_len(*buffer)) {
data->current_line = first_num + 1;
puts((*buffer)[first_num - 1]);
} else puts("?");
break;
case 'p':
if (!buf_len(*buffer)) { puts("?"); goto end; }
for (size_t i = first_num; i <= second_num; ++i) {
puts((*buffer)[i - 1]);
}
break;
case 'a':
data->current_line = first_num;
mode = ED_INSERT;
break;
case 'i':
data->current_line = first_num ? first_num - 1 : 0;
mode = ED_INSERT;
break;
case 'd':
if (first_num) buf_remove(*buffer, first_num - 1, second_num - first_num + 1);
else puts("?");
break;
case 'c':
if (first_num) {
buf_remove(*buffer, first_num - 1, 1);
data->current_line = first_num - 1;
mode = ED_INSERT;
} else puts("?");
break;
default:
puts("?");
break;
}
end:
buf_free(line);
return mode;
}
int main(int argc, char* argv[]) {
struct ed_data data;
data.filename = NULL;
data.prompt = "";
data.suppress = 0;
data.current_line = 1;
int option;
while ((option = getopt(argc, argv, "sp:")) != -1) {
switch (option) {
case 's': data.suppress = 1; break;
case 'p': data.prompt = optarg; break;
default: return 1;
}
}
const int narg = argc - optind;
if (narg > 1) {
fputs("Usage: ed [-p string] [-s] [file]\n", stderr);
return 1;
}
else if (narg == 1) data.filename = argv[optind];
char** buffer = NULL;
if (data.filename) read_file(data.filename, &buffer, data.suppress);
enum ed_mode mode = ED_NORMAL;
while (1) {
switch (mode) {
case ED_NORMAL:
mode = ed_normal(&data, &buffer);
break;
case ED_INSERT:
mode = ed_insert(&data, &buffer);
break;
case ED_EXIT: goto end;
}
}
end:
return 0;
}