summaryrefslogtreecommitdiffstats
path: root/main/common/ledit_vi.c
blob: 68f7d01a52dcf4f84724679f3329e5f80a54e9f4 (plain) (blame)
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
/**************************************************************************
 *
 * Copyright (c) 2013 Alcatel-Lucent
 * 
 * Alcatel Lucent licenses this file to You under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  A copy of the License is contained the
 * file LICENSE at the top level of this repository.
 * You may also obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 *
 * lineedit.c:
 *
 * This code supports the monitor's command line editing capability and
 * command history log.  The command line editing is a subset of KSH's
 * vi-mode editing.
 *
 * This code includes a few suggestions/fixed from Martin Carroll.
 *
 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
 *
 */

#include "config.h"
#if INCLUDE_LINEEDIT
#include <ctype.h>
#include "genlib.h"
#include "stddefs.h"
#include "cli.h"

#define HMAX			16
#define ESC				0x1B
#define CTLC			0x03
#define BACKSPACE		0x08
#define CMD				100
#define EDIT			101
#define EDIT1			102
#define INSERT			103
#define NEXT			104
#define PREV			105
#define NEITHER			106

#define EDITFILELINE	1
#define EDITCMDLINE		2

static int		stridx;			/* store index */
static int		shwidx;			/* show index */
static int		srchidx;		/* search index */
static int		lastsize;		/* size of last command */
static char		curChar;		/* latest input character */
static char		*curPos;		/* current position on command line */
static char		*startOfLine;	/* start of command line */
static int		lineLen;		/* length of line */
static int		lMode;			/* present mode of entry */
static char		cmdhistory[HMAX+1][CMDLINESIZE];/* array for command history */

static void		ledit(), lcmd(), showprev(), ldelete(), linsert(), lerase();
static void		gotobegin(), gotoend(), backup(), shownext(), linsertbs();
static void		delete_to_end(), delete_something();
static void		historysearch(void), ledit1(void);
static char 	*lineeditor();

/* line_edit() & file_line_edit():
 * These two functions are simply front-ends to the lineeditor()
 * function.  The line_edit() function is called by the command line
 * interface and file_line_edit() is called by the flash file editor
 * to provide a convenient single line editor when modifying a file.
 */

char *
line_edit(char *line_to_edit)
{
	return(lineeditor(line_to_edit,EDITCMDLINE));
}

char *
file_line_edit(char *line_to_edit)
{
	return(lineeditor(line_to_edit,EDITFILELINE));
}

/* lineeditor():
 * This function is fed a pointer to a command line.
 * It sets up a command line editor for that particular line.
 * The line is modified in place so, if successful, the function
 * returns the same pointer but with its contents modified based
 * on the editor commands executed.
 * If failure, the function returns (char *)0.
 */
static char *
lineeditor(char *line_to_edit,int type)
{
	lMode = CMD;
	startOfLine = line_to_edit;
	curPos = line_to_edit;
	while(*curPos != ESC) 
		curPos++;
	*curPos = 0;	/* Remove the escape character from the line */
	lineLen = (ulong)curPos - (ulong)startOfLine;
	if (lineLen > 0) {
		curPos--;
		putstr(" \b\b");
	}
	else
		putstr(" \b");
	lastsize = 0;
	shwidx = stridx;
	srchidx = stridx;
	while(1) {
		curChar = getchar();
		switch(curChar) {
			case ESC:
				if (lMode != CMD) {
					lMode = CMD;
					continue;
				}
				else {
					putchar('\n');
					return((char *)0);
				}
			case '\r':
			case '\n':
				putchar('\n');
				if (lineLen == 0) 
					return((char *)0);
				*(char *)(startOfLine + lineLen) = '\0';
				return(startOfLine);
			case CTLC:
				putchar('\n');
				*startOfLine = 0;
				lineLen = 0;
				return((char *)0);
		}
		switch(lMode) {
			case CMD:
				lcmd(type);
				if (lMode == NEITHER)
					return((char *)0);
				break;
			case INSERT:
				linsert();
				break;
			case EDIT1:
				ledit1();
				break;
			case EDIT:
				ledit();
				break;
		}
		if (lineLen >= (CMDLINESIZE - 2)) {
			printf("line overflow\n");
				return((char *)0);
		}
	}
}

static void
ledit(void)
{
	int	len;

	if (curChar == '\b') 
		curPos--;
	else
		*curPos++ = curChar;

	len = (curPos - startOfLine);	/* line may get longer than original */
	if (len > lineLen)
		lineLen = len;

	putchar(curChar);
	switch(lMode) {
		case EDIT1:
			lMode = CMD;
			break;
	}
}

static void
ledit1(void)
{
	*curPos = curChar;
	putchar(curChar);
	putchar('\b');
	lMode = CMD;
}

static void
lcmd(int type)
{
	switch(curChar) {
		case '0':
			gotobegin();
			return;
		case '$':
			gotoend();
			return;
		case 'A':
			gotoend();
			putchar(*curPos++);
			lMode = INSERT;
			return;
		case 'a':
			if (curPos != startOfLine)
				putchar(*curPos++);
			lMode = INSERT;
			return;
		case 'i':
			lMode = INSERT;
			return;
		case 'D':
			delete_to_end();
			return;
		case 'd':
			delete_something();
			return;
		case 'c':
			delete_something();
			lMode = INSERT;
			return;
		case 'x':
			ldelete();
			return;
		case ' ':
		case 'l':
			if (curPos < startOfLine+lineLen-1) {
				putchar(*curPos);
				curPos++;
			}
			return;
		case '\b':
		case 'h':
			if (curPos > startOfLine) {
				putchar('\b');
				curPos--;
			}
			return;
		case 'r':
			lMode = EDIT1;
			return;
		case 'R':
			lMode = EDIT;
			return;
	}

	/* The remaining commands should only be processed if we are editing
	 * a command line.  For editing a line in a file, the commands that
	 * relate to command line history are not applicable and should be
	 * blocked.
	 */
	if (type == EDITCMDLINE) {
		switch(curChar) {
			case '/':
				putchar('/');
				historysearch();
				return;
			case '+':
			case 'j':
				shownext();
				return;
			case '-':
			case 'k':
				showprev();
				return;
		}
	}

	/* Beep to indicate an error. */
	putchar(0x07);
}

static void
linsert()
{
	char	string[CMDLINESIZE];

	if (curChar == BACKSPACE) {
		linsertbs();
		return;
	}
	if (!isprint(curChar))
		return;
	putchar(curChar);
	putstr(curPos);
	backup((int)strlen(curPos));
	strncpy(string,curPos,CMDLINESIZE-1);
	*curPos++ = curChar;
	strcpy(curPos,string);
	lineLen++;
}

/* linsertbs():
 * Handle the backspace when in 'INSERT' mode.
 */

static void
linsertbs()
{
	char	*eol, *now;
	int	cnt;

	if (curPos == startOfLine)
		return;
	backup(1);
	curPos--;
	cnt = 0;
	eol = startOfLine + lineLen - 1;
	now = curPos;
	while(curPos <= eol) {
		*curPos = *(curPos+1);
		curPos++;
		cnt++;
	}
	putbytes(now,cnt-1);
	putchar(' ');
	backup((int)cnt);
	curPos = now;
	lineLen--;
}

static void
delete_something()
{
	char	*eol, *now, C, *new;
	int	cnt;

	C = getchar();
	if (C == 'w') {		/* word */
		now = curPos;
		eol = startOfLine + lineLen -1;
		while (curPos <= eol) {
			if (*curPos == ' ')
				break;
			curPos++;
		}
		if (curPos < eol) {
			new = curPos;
			strcpy(now,new);
			cnt = strlen(now);
			putbytes(now,cnt);
			curPos = now + cnt;
			while(curPos <= eol) {
				putchar(' ');
				curPos++;
				cnt++;
			}
			backup(cnt);
		}
		else {
			curPos = now;
			delete_to_end();
			return;
		}
		curPos = now;
		lineLen = strlen(startOfLine);
	}
	else if (C == 'f') {		/* find */
		C = getchar();
		now = curPos;
		eol = startOfLine + lineLen -1;
		while (curPos <= eol) {
			if (*curPos == C)
				break;
			curPos++;
		}
		if (curPos < eol) {
			new = curPos+1;
			strcpy(now,new);
			cnt = strlen(now);
			putbytes(now,cnt);
			curPos = now + cnt;
			while(curPos <= eol) {
				putchar(' ');
				curPos++;
				cnt++;
			}
			backup(cnt);
		}
		else {
			curPos = now;
			delete_to_end();
			return;
		}
		curPos = now;
		lineLen = strlen(startOfLine);
	}
}

static void
delete_to_end()
{
	char	*eol, *now, *sol;

	eol = startOfLine + lineLen;
	now = curPos;
	if (curPos == eol)
		return;
	while(curPos < eol) {
		putchar(' ');
		curPos++;
	}
	if (now == startOfLine)
		sol = now+1;
	else
		sol = now;
	while(curPos >= sol) {
		putchar('\b');
		curPos--;
	}
	lineLen = now - startOfLine;
	if (lineLen == 0) {
		curPos = startOfLine;
		*curPos = 0;
	}
	else
		*(curPos+1) = '\0';
}

static void
ldelete()
{
	char	*eol, *now;
	int	cnt;

	if (lineLen == 0)
		return;
	cnt = 0;
	eol = startOfLine + lineLen - 1;
	now = curPos;
	if (curPos != eol) {
		while(curPos <= eol) {
			*curPos = *(curPos+1);
			curPos++;
			cnt++;
		}
		putbytes(now,cnt-1);
		putchar(' ');
		backup((int)cnt);
	}
	else {
		putstr(" \b\b");
		*eol = '\0';
		now--;
	}
	curPos = now;
	lineLen--;
	if (lineLen == 0)
		curPos = startOfLine;
}

/* showdone():
 * Used as common completion code for the showprev() and
 * shownext() functions below.
 */
static void
showdone(int idx)
{
	if (idx == HMAX) {
		printf("History buffer empty.\007\n");
		lMode = NEITHER;
		return;
	}

	backup((int)(curPos - startOfLine));
	lineLen = strlen(cmdhistory[shwidx]);
	putbytes(cmdhistory[shwidx],lineLen);
	lerase((int)(lastsize-lineLen));
	backup((int)(lineLen));
	strcpy(startOfLine,cmdhistory[shwidx]);
	curPos = startOfLine;
	lastsize = lineLen;
}

/* showprev() & shownext():
 * Show previous or next command in history list based on 
 * the current position in the list being established by
 * the shwidx variable.
 */
static void
showprev()
{
	int	i;

	if (shwidx == 0)
		shwidx = HMAX-1;
	else
		shwidx--;

	for(i=0;i<HMAX;i++) {
		if (*cmdhistory[shwidx])
			break;
		if (shwidx == 0)
			shwidx = HMAX-1;
		else
			shwidx--;
	}
	showdone(i);
}


static void
shownext()
{
	int	i;

	if (shwidx == HMAX-1)
		shwidx = 0;
	else
		shwidx++;

	for(i=0;i<HMAX;i++) {
		if (*cmdhistory[shwidx])
			break;
		if (shwidx == HMAX)
			shwidx = 0;
		else
			shwidx++;
	}
	showdone(i);
}

static void
backup(count)
int	count;
{
	char	string[CMDLINESIZE];
	int	i;

	if (count <= 0)
		return;
	*string = '\0';
	for(i=0;i<count;i++)
		strcat(string,"\b");
	putbytes(string,count);
}

static void
lerase(int count)
{
	char	string[CMDLINESIZE];
	int	i;

	if (count <= 0)
		return;
	*string = '\0';
	for(i=0;i<count;i++)
		strcat(string," ");
	for(i=0;i<count;i++)
		strcat(string,"\b");
	putbytes(string,count*2);
}

static void
gotoend()
{
	char	string[CMDLINESIZE], *eol;
	int	i;

	eol = startOfLine + lineLen -1;
	for (i=0;i<CMDLINESIZE-1;i++) {
		if (curPos == eol)
			break;
		string[i] = *curPos++;
	}
	if (i)
		putbytes(string,i);
}

static void
gotobegin()
{
	char	string[CMDLINESIZE];
	int	i;

	i = 0;
	while(curPos != startOfLine) {
		string[i++] = '\b';
		curPos--;
	}
	putbytes(string,i);
}

/* History():
 * Command used at the CLI to allow the user to dump the content
 * of the history buffer.
 */
char * HistoryHelp[] = {
		"Display command history",
		"",
		0,
};
	
int
History(int argc,char *argv[])
{
	int	i;

	for(i=stridx;i<HMAX;i++) {
		if (cmdhistory[i][0])
			printf("%s\n",cmdhistory[i]);
	}
	if (stridx) {
		for(i=0;i<stridx;i++) {
			if (cmdhistory[i][0])
				printf("%s\n",cmdhistory[i]);
		}
	}
	return(CMD_SUCCESS);
}

/* historyinit():
 * Initialize the command history...
 */
void
historyinit()
{
	int	i;

	shwidx = stridx = 0;
	for (i=0;i<HMAX;i++)
		cmdhistory[i][0] = 0;
}

/* historylog():
 * This function is called by the CLI retrieval code to store away
 * the command in the CLI history list, a circular queue
 * (size HMAX) of most recent commands.
 */
void
historylog(char *cmdline)
{
	int	idx;

	if (strlen(cmdline) >= CMDLINESIZE)
		return;

	if (stridx == 0)
		idx = HMAX-1;
	else
		idx = stridx -1;

	/* don't store if this command is same as last command */
	if (strcmp(cmdhistory[idx],cmdline) == 0)
		return;

	if (stridx == HMAX)
		stridx = 0;

	strcpy(cmdhistory[stridx++],cmdline);
}


static void
historysearch(void)
{
	static char	string[100];
	char	*s, *ptr, *last, C;
	int	size, len, count;

	lerase((int)lastsize);
	s = string;
	size = 0;
	while(1) {
		C = getchar();
		if (C == '\b') {
			if (size == 0)
				continue;
			putstr("\b \b");
			s--;
			size--;
			continue;
		}
		if ((C == '\r') || (C == '\n'))
			break;
		putchar(C);
		size++;
		*s++ = C;
	}
	backup((int)(size+1));
	if (size != 0)
		*s = '\0';
	else 
		size = strlen(string);

	count = 0;

	while(1) {
		ptr = cmdhistory[srchidx];
		len = strlen(ptr);
		last = ptr + len - size + 1;
		while(ptr < last) {
			if (strncmp(ptr,string,size) == 0) 
				goto gotmatch;
			ptr++;
		}
		if (srchidx == 0)
			srchidx = HMAX-1;
		else
			srchidx--;
		if (++count == HMAX) {
			backup((int)(curPos - startOfLine));
			lineLen = 0;
			lerase((int)(size+1));
			backup((int)(lineLen));
			*startOfLine = '\0';
			curPos = startOfLine;
			lastsize = lineLen;
			return;
		}
	}
gotmatch:
	backup((int)(curPos - startOfLine));
	lineLen = strlen(cmdhistory[srchidx]);
	putbytes(cmdhistory[srchidx],lineLen);
	lerase((int)(lastsize-lineLen));
	backup((int)(lineLen));
	strcpy(startOfLine,cmdhistory[srchidx]);
	curPos = startOfLine;
	lastsize = lineLen;
	if (srchidx == 0)
		srchidx = HMAX-1;
	else
		srchidx--;
	return;
}

#endif