-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedit.cc
More file actions
797 lines (687 loc) · 16.8 KB
/
edit.cc
File metadata and controls
797 lines (687 loc) · 16.8 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "define.h"
#include "struct.h"
/*
* SUPPORT FUNCTIONS
*/
const char *one_line( const char* argument, char* line )
{
if( !*argument ) {
*line = '\0';
} else {
for( ; *argument && *argument != '\n'; argument++, line++ )
*line = *argument;
*line = '\n';
*(line+1) = '\r';
*(line+2) = '\0';
if( *argument == '\n' ) {
++argument;
if( *argument == '\r' )
++argument;
}
}
return argument;
}
static void display_text( char_data* ch, const char *input )
{
if( !*input ) {
page( ch, "(No text has been entered.)\n\r" );
return;
}
char line [ MAX_STRING_LENGTH ];
for( int i = 1; ; i += 2 ) {
input = one_line( input, line );
if( !*line )
return;
// *** FIX ME: result can be longer than MAX_STRING_LENGTH
// But page() can't handle this.
page( ch, "[%2d] %s", i, line );
}
}
const char *break_line( const char* argument, char* line, int length )
{
const char* input = argument;
char* word = 0;
bool newword = true;
if( !*argument ) {
*line = '\0';
return argument;
}
for( ; *input && *input != '\n' && input-argument < length; ) {
if( *input == ' ' ) {
if( newword ) {
word = line;
newword = false;
}
} else {
newword = true;
}
*line++ = *input++;
}
if( !*input || *input == '\n' || !word )
word = line;
*word = '\0';
argument = input+(word-line);
skip_spaces( argument );
return argument;
}
/*
* SUB_PROCEDURES
*/
int format_tell( char *output, const char *input, const char_data *ch )
{
const unsigned width = ( ch && ch->pcdata ) ? ch->pcdata->columns : 80;
const unsigned limit = width-2;
char *start = output;
skip_spaces( input );
strcpy( output, " \"" );
output += 2;
char *word = 0;
unsigned next = 0;
unsigned count = 3; // 3 is correct... accounts for extra space after return.
int total = 3;
while( *input ) {
if( word && count > limit ) {
const int need = 3;
for( char *line = output-1; line > word; --line )
*(line+need) = *line;
output += need;
memcpy( word, "\n\r ", 4 );
word = 0;
count = next+2;
next = 0;
total += 2;
}
/*
if( ansi ) {
while( *input == '@' ) {
*output++ = *input++;
if( char c = *input ) {
*output++ = *input++;
if( c == '@' ) {
++count;
++next;
++total;
} else if( c == 'I' ) {
count += 2;
next += 2;
total += 2;
}
}
}
} else {
*/
while( *input == '\x1b' ) {
while( char c = *input ) {
*output++ = c;
++input;
if( isalpha( c ) ) {
break;
}
}
}
// }
if( !*input )
break;
if( isspace( *input ) ) {
if( *input == '\n' && *(input+1) == '\r' )
++input;
word = output;
next = 0;
*output++ = ' ';
} else {
*output++ = *input;
}
++input;
++count;
++next;
++total;
}
for( ; isspace( *(output-1) ) && output > start; --output, --total );
strcpy( output, "\"" );
++total;
// Return amount of space left on first line of output.
return limit+2-total;
}
unsigned format( char* output, const char* input,
bool ansi, int indent, const char_data *ch,
bool crlf )
{
const unsigned width = ( ch && ch->pcdata ) ? ch->pcdata->columns : 80;
const unsigned limit = width-2;
skip_spaces( input );
const char *letter;
const char *start = output;
char *word = 0;
unsigned next = 0;
unsigned count = indent;
bool skip = false; // 2 spaces after period can be overwritten with \n\r.
unsigned delim_size = 0;
memset( output, ' ', indent );
output += indent;
while( *input ) {
if( word && count > limit ) {
// if( !word )
// word = output;
const int need = skip ? indent : indent+1+delim_size;
if( need > 0 ) {
// Make room to insert crlf and indent.
for( char *line = output-1; line > word; --line ) {
*(line+need) = *line;
}
output += need;
}
// Catch-22: can't insert default color here, because if the string had a color
// code in it, the next line won't be re-started with the desired color.
*(word+delim_size) = '\n';
*(word+1+delim_size) = '\r';
memset( word+2+delim_size, ' ', indent );
word = 0;
count = indent+next;
next = 0;
skip = false;
delim_size = 0;
}
if( ansi ) {
while( *input == '@' ) {
*output++ = *input++;
if( char c = *input ) {
*output++ = *input++;
if( c == '@' ) {
++count;
++next;
} else if( c == 'I' ) {
count += 2;
next += 2;
}
}
}
} else {
while( *input == '\x1b' ) {
while( char c = *input ) {
*output++ = c;
++input;
if( isalpha( c ) ) {
break;
}
}
}
}
if( !*input )
break;
if( isperiod( *input ) ) {
do {
*output++ = *input++;
++count;
++next;
} while( isperiod( *input ) );
for( letter = input; *letter == ' '; ++letter );
if( *letter == '\n' || !*letter )
input = letter;
if( !*input )
break;
// Add a space after period.
if( *input == ' ' ) {
do {
++input;
} while( isspace( *input ) );
if( count > limit ) {
// Word plus period(s) need to go on next line.
*output++ = ' ';
*output++ = ' ';
// count += 2;
next += 2;
continue;
}
word = output;
*output++ = ' ';
*output++ = ' ';
count += 2;
next = 0;
skip = true;
delim_size = 0;
}
continue;
}
if( isspace( *input ) ) {
word = output;
*output++ = ' ';
++count;
next = 0;
skip = false;
delim_size = 0;
do {
++input;
} while( isspace( *input ) );
continue;
}
if( *input == '-' ) {
while( *input == '-' ) {
*output++ = *input++;
++count;
++next;
}
if( count > limit )
continue;
word = output-1;
next = 0;
skip = false;
delim_size = 1;
continue;
}
*output++ = *input++;
++count;
++next;
}
for( ; isspace( *(output-1) ) && output > start; --output ) {
--count;
}
if( count > 0 ) {
if( ch && !ansi ) {
output += sprintf( output, color_code( ch, COLOR_DEFAULT ) );
}
if( crlf ) {
*output++ = '\n';
*output++ = '\r';
}
}
*output = '\0';
return count;
}
/*
* FORMATS CODE
*/
static void indent( const char *input, char *output, bool partial = false )
{
char line [ MAX_STRING_LENGTH ];
int level = 0;
int spaces = 0;
*output = '\0';
int over = 0;
if( partial ) {
for( ; *input == ' '; ++input, ++over );
}
while( true ) {
for( ; *input == ' '; ++input );
input = one_line( input, line );
if( !*line )
break;
const char *letter = line;
if( *letter == '}' ) {
--level;
while( *++letter == ' ' );
}
for( int i = 0; i < 2*level+spaces+over; ++i ) {
*output++ = ' ';
}
spaces = 0;
strcpy( output, line );
output += strlen( line );
if( *letter == '{' ) {
++level;
} else if( !strncmp( letter, "if(", 3 )
|| !strncmp( letter, "if ", 3 )
|| !strncmp( letter, "loop(", 5 )
|| !strncmp( letter, "loop ", 5 )
|| !strncmp( letter, "else", 4 )
|| !strncmp( letter, "&&", 2 )
|| !strncmp( letter, "||", 2 ) ) {
for( ; *letter && *letter != '{'; ++letter );
if( *letter == '{' ) {
++level; // block
} else {
spaces = 2; // single statement
}
}
}
*output = '\0';
}
/*
* MAIN ROUTINE
*/
const char *edit_string( char_data* ch, const char *argument, const char *input,
int mem_type, bool ansi )
{
if( !*argument ) {
// Prevent empty command from changing string.
// This is necessary to prevent unnecessary softcode compiles.
free_string( ch->pcdata->buffer, MEM_PLAYER );
ch->pcdata->buffer = alloc_string( input, MEM_PLAYER );
display_text( ch, input );
return input;
}
const char *const too_long = "Due to internal limits, files can be no longer then 8k characters.\n\r";
const char *const line_long = "Due to internal limits, lines can be no longer then %d characters.\n\r";
static char paragraph [ 3*MAX_STRING_LENGTH ];
char buf [ 3*MAX_STRING_LENGTH ];
char line [ MAX_STRING_LENGTH ];
const char *pString = input;
char *word;
int i, j, k;
int repl = -1;
bool display = true;
*buf = '\0';
*paragraph = '\0';
const int in_len = strlen( input )+1;
const int buf_len = strlen( ch->pcdata->buffer )+1;
if( !strcasecmp( argument, "undo" ) ) {
if( ch->pcdata->buffer == empty_string ) {
page( ch, "You haven't edited anything since logging in.\n\r" );
return pString;
}
if( in_len > 1 ) {
record_delete( in_len, -mem_type);
record_new( in_len, -MEM_PLAYER);
}
if( buf_len > 1 ) {
record_new( buf_len, -mem_type);
record_delete( buf_len, -MEM_PLAYER);
}
pString = ch->pcdata->buffer;
ch->pcdata->buffer = input;
display_text( ch, pString );
return pString;
}
if( !strcmp( argument, "indent" ) ) {
indent( input, paragraph );
input = paragraph;
} else if( !strcmp( argument, "spell" ) ) {
if( const char *s = spell( ch, input ) )
page( ch, s );
else
page( ch, "No spelling errors found.\n\r" );
return pString;
} else if( *argument == '?' ) {
argument = one_argument( ++argument, buf );
i = strlen( buf ); // String being replaced.
word = paragraph;
j = in_len;
k = strlen( argument ); // Replacement.
repl = 0;
const char *l = 0;
while( *input ) {
if( isprint( *input ) ) {
if( !l )
l = input;
} else if( *input == '\n' ) {
l = 0;
}
if( i > 0 && !strncmp( buf, input, i ) ) {
if( k > i ) {
if( j + k - i >= 2*MAX_STRING_LENGTH ) {
page( ch, too_long );
return pString;
}
if( l && ( input - l ) + k - i > MAX_INPUT_LENGTH ) {
page( ch, line_long, MAX_INPUT_LENGTH );
return pString;
}
}
strcpy( word, argument );
word += k;
++repl;
input += i;
j += k - i;
} else {
*word++ = *input++;
}
}
*word = '\0';
input = paragraph;
} else if( *argument ) {
if( isdigit( *argument ) ) {
for( i = 0; isdigit( *argument ); argument++ )
i = 10*i + *argument - '0';
} else {
i = -1;
}
if( *argument == '-' && i != -1 ) {
for( j = 0, argument++; isdigit( *argument ); argument++ )
j = 10*j + *argument - '0';
if( j < i ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
skip_spaces( argument );
for( k = 1; k < i; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
strcat( paragraph, line );
}
if( matches( argument, "cut", 3 ) ) {
for( ; k <= j; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
if( k <= i+1 ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
break;
}
strcat( buf, line );
}
free_string( ch->pcdata->paste, MEM_PLAYER );
ch->pcdata->paste = alloc_string( buf, MEM_PLAYER );
} else if( matches( argument, "copy", 3 ) ) {
unsigned n = 0;
for( ; k <= j; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
if( k <= i+1 ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
break;
}
strcat( buf, line );
++n;
}
free_string( ch->pcdata->paste, MEM_PLAYER );
ch->pcdata->paste = alloc_string( buf, MEM_PLAYER );
page( ch, "Copied %u lines to paste buffer.\n\r", n );
return pString;
} else if( matches( argument, "indent", 3 ) ) {
for( ; k <= j; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
if( k <= i+1 ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
break;
}
strcat( buf, line );
}
indent( buf, paragraph + strlen( paragraph ), true );
} else if( matches( argument, "delete", 3 ) ) {
for( ; k <= j; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
if( k <= i+1 ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
break;
}
}
} else if( *argument ) {
page( ch, "Bad syntax after line range.\n\r" );
return pString;
} else {
for( ; k <= j; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
if( k <= i+1 ) {
page( ch, "Line number range incorrect.\n\r" );
return pString;
}
break;
}
int l = strlen( line );
if( line[ l - 3 ] == '-' ) {
line[ l - 2 ] = '\0';
} else {
line[ l - 2 ] = ' ';
line[ l - 1 ] = '\0';
}
strcat( buf, line );
}
// Parameter "ansi" should be set if color codes will be interpreted
// when the string is actually printed outside the editor.
format( line, buf, ansi );
strcat( paragraph, line );
}
while( true ) {
input = one_line( input, line );
if( !*line )
break;
strcat( paragraph, line );
}
} else if( *argument == '?' && i != -1 ) {
for( k = 1; k < i; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
break;
}
strcat( paragraph, line );
}
input = one_line( input, line );
if( k != i || !*line ) {
page( ch, "No such line.\n\r" );
return pString;
}
argument = one_argument( ++argument, buf );
i = strlen( buf );
word = paragraph + strlen( paragraph );
j = in_len;
k = strlen( argument ); // Replacement.
repl = 0;
const char *str = line;
while( *str ) {
if( i > 0 && !strncmp( buf, str, i ) ) {
if( k > i ) {
// Replacement is longer than original text.
if( j + k - i >= 2*MAX_STRING_LENGTH ) {
page( ch, too_long );
return pString;
}
if( ( str - line ) + k - i > MAX_INPUT_LENGTH ) {
page( ch, line_long, MAX_INPUT_LENGTH );
return pString;
}
}
strcpy( word, argument );
word += k;
++repl;
str += i;
j += k - i;
} else {
*word++ = *str++;
}
}
*word = '\0';
while( true ) {
input = one_line( input, line );
if( !*line )
break;
strcat( paragraph, line );
}
} else if( *argument == '+' && i != -1 ) {
// Paste.
if( !*ch->pcdata->paste ) {
page( ch, "Paste buffer is empty.\n\r" );
return pString;
}
const int paste_len = strlen( ch->pcdata->paste );
if( in_len + paste_len >= 2*MAX_STRING_LENGTH ) {
page( ch, too_long );
return pString;
}
for( k = 1; k < i; k += 2 ) {
input = one_line( input, line );
if( !*line ) {
page( ch, "Line number %d too large.\n\r", i );
return pString;
}
strcat( paragraph, line );
}
strcat( paragraph, ch->pcdata->paste );
while( true ) {
input = one_line( input, line );
if( !*line )
break;
if( k != i )
strcat( paragraph, line );
k += 2;
}
} else {
if( in_len >= 2*MAX_STRING_LENGTH && *argument ) {
page( ch, too_long );
return pString;
}
display = !is_set( ch->pcdata->pfile->flags, PLR_EDITOR_QUIET );
if( *argument == ' ' && i != -1 ) {
++argument;
}
for( k = 1; ; k += 2 ) {
input = one_line( input, line );
if( !display ) {
if( !*line ) {
if( i == -1 || ( k == i && *argument ) || k == i+1 ) {
page( ch, "Appended line %d.\n\r", k );
}
} else if( k == i ) {
if( *argument ) {
page( ch, "Replaced line %d.\n\r", k );
} else {
page( ch, "Deleted line %d.\n\r", k );
}
} else if( k == i+1 ) {
page( ch, "Inserted line %d.\n\r", k );
}
}
if( !*line ) {
if( i == -1 || ( k == i && *argument ) || k == i+1 ) {
strcat( paragraph, argument );
strcat( paragraph, "\n\r" );
} else if( k <= i ) {
page( ch, "Line number %d too large.\n\r", i );
return pString;
}
break;
}
if( ( k == i && *argument ) || k == i+1 ) {
strcat( paragraph, argument );
strcat( paragraph, "\n\r" );
}
if( k != i ) {
strcat( paragraph, line );
}
}
}
input = paragraph;
}
if( repl == 0 ) {
page( ch, "No occurrences found.\n\r" );
return pString;
}
if( in_len > 1 ) {
record_delete( in_len, -mem_type);
record_new( in_len, -MEM_PLAYER);
}
free_string( ch->pcdata->buffer, MEM_PLAYER );
ch->pcdata->buffer = pString;
if( repl > 0 ) {
page( ch, "\n\rReplaced %d occurrences.\n\r\n\r", repl );
}
if( display ) {
display_text( ch, input );
}
return alloc_string( input, mem_type );
}