-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedlin.c
299 lines (276 loc) · 7.39 KB
/
edlin.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* edlin.c -- main file for edlin
AUTHOR: Gregory Pietsch <[email protected]>
DESCRIPTION:
This file is the main file for edlin, an edlin-style line editor.
COPYRIGHT NOTICE AND DISCLAIMER:
Copyright (C) 2003 Gregory Pietsch
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 2
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, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* includes */
#include "config.h"
#include <ctype.h>
#include <stdio.h>
#include "dynstr.h"
#include "edlib.h"
#define EXTERN /* force a declaration */
#include "msgs.h"
#ifdef USE_CATGETS
#include "nl_types.h"
#endif
#ifdef USE_KITTEN
#include "kitten.h"
#endif
/* typedefs */
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
/* static variables */
long current_line = 1L;
unsigned page_size = 23;
int exiting = 0;
char *current_filename = 0;
/* functions */
void
help (void)
{
puts (G00013);
puts (G00014);
puts (G00015);
puts (G00016);
puts (G00017);
puts (G00018);
puts (G00019);
puts (G00020);
puts (G00021);
puts (G00022);
puts (G00023);
}
void
parse_command (char *s)
{
char *ip = s;
long acc = 0;
long lp[4] = { 0UL, 0UL, 0UL, 0UL };
char op = '+';
int verifying = 0;
size_t lpip = 0;
if (*s == '\0')
return;
while (*ip && !isalpha (*ip) && *ip != '?')
{
/* parse the digits */
if (*ip == '.')
{
if (acc)
{
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
acc = current_line;
ip++;
}
else if (*ip == '$')
{
if (acc)
{
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
acc = get_last_line ();
ip++;
}
else
while (isdigit (*ip))
acc = acc * 10 + (*ip++ - '0');
if (lpip >= 4)
{
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
lp[lpip] += (op == '+') ? acc : -acc;
acc = 0;
if (*ip == '+' || *ip == '-')
op = *ip++;
else if (*ip == ',')
{
lpip++;
ip++;
op = '+';
}
else if (*ip && !(isalpha ((unsigned char) *ip)
|| (*ip == '?' && isalpha ((unsigned char) ip[1]))))
{
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
}
if (*ip == '?')
{
ip++;
verifying = 1;
}
/* at this point, *ip should be pointing to '\0' or the command character */
switch (tolower ((unsigned char) (*ip)))
{
case '\0': /* amend a single line or show help */
if (lp[0] == 0 && verifying)
help ();
else if (lp[0])
{
current_line = lp[0];
modify_line (current_line - 1);
}
else
{
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
break;
case 'c': /* copy */
if (lp[0] == 0 || lp[1] == 0)
lp[0] = lp[1] = current_line;
if (lp[2] == 0)
{
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
if (lp[3] == 0)
lp[3] = 1;
copy_block (lp[0] - 1, lp[1] - 1, lp[2] - 1, (size_t) lp[3]);
break;
case 'd': /* delete */
if (lp[1] == 0)
lp[1] = (lp[0]) ? lp[0] : current_line;
if (lp[0] == 0)
lp[0] = current_line;
delete_block (lp[0] - 1, lp[1] - 1);
break;
case 'm': /* move */
if (lp[0] == 0)
lp[0] = current_line;
if (lp[1] == 0 || lp[2] == 0)
{
/* invalid parameters */
/* Error: Invalid user input */
fprintf (stderr, G00037, G00033);
return;
}
move_block (lp[0] - 1, lp[1] - 1, lp[2] - 1);
break;
case 'l': /* list */
case 'p': /* print */
if (lp[0] == 0)
{
lp[0] = (*ip == 'l')
? MAX (current_line - (int) (page_size >> 1), 1) : current_line;
}
if (lp[1] == 0)
lp[1] = lp[0] + page_size - 1;
display_block (lp[0] - 1, lp[1] - 1, current_line - 1, page_size);
break;
case 'q': /* quit */
exiting = 1;
break;
case 't': /* transfer file */
if (lp[0] == 0)
lp[0] = current_line;
ip++;
while (*ip && isspace (*ip))
ip++;
transfer_file (lp[0] - 1, ip);
current_line = lp[0];
break;
case 'e': /* write & exit */
case 'w': /* write file */
exiting = (*ip == 'e');
ip++;
while (*ip && isspace (*ip))
ip++;
if (*ip == 0 && current_filename == 0)
/* No filename */
fprintf (stderr, G00037, G00034);
else
write_file (lp[0] ? lp[0] : NPOS, *ip ? ip : current_filename);
break;
case 'i': /* insert */
if (lp[0] == 0)
lp[0] = current_line;
current_line = insert_block (lp[0] - 1);
break;
case 'a': /* append */
current_line = insert_block (get_last_line ());
break;
case 's': /* search */
if (lp[0] == 0)
lp[0] = MIN (current_line + 1, (long) get_last_line ());
if (lp[1] == 0)
lp[1] = get_last_line ();
current_line = search_buffer (current_line, lp[0] - 1, lp[1] - 1,
verifying, ip + 1);
break;
case 'r': /* replace */
if (lp[0] == 0)
lp[0] = MIN (current_line + 1, (long) get_last_line ());
if (lp[1] == 0)
lp[1] = get_last_line ();
current_line = replace_buffer (current_line, lp[0] - 1, lp[1] - 1,
verifying, ip + 1);
break;
default:
/* Invalid user input */
fprintf (stderr, G00037, G00033);
break;
}
}
/* Main function for edlin. */
int
main (int argc, char **argv)
{
char *s;
#if defined(USE_CATGETS) || defined(USE_KITTEN)
/* get catalog */
the_cat = catopen("edlin", 0);
#endif
/* put out the copyright notice and disclaimer */
fputs (PACKAGE_NAME " " PACKAGE_VERSION, stdout);
puts (G00024);
puts (G00025);
puts (G00026);
puts (G00027);
puts (G00028);
puts (G00029);
create_buffer ();
if (argc >= 2)
transfer_file (0, current_filename = argv[1]);
while (!exiting)
{
s = read_line ("*");
parse_command (s);
}
destroy_buffer ();
#if defined(USE_CATGETS) || defined(USE_KITTEN)
/* close catalog */
catclose(the_cat);
#endif
return 0;
}
/* END OF FILE */