Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: v_ex.c,v 1.7 2014/01/26 21:43:45 christos Exp $ */
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#if 0
#ifndef lint
static const char sccsid[] = "Id: v_ex.c,v 10.60 2003/07/19 21:04:00 skimo Exp  (Berkeley) Date: 2003/07/19 21:04:00 ";
#endif /* not lint */
#else
__RCSID("$NetBSD: v_ex.c,v 1.7 2014/01/26 21:43:45 christos Exp $");
#endif

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../common/common.h"
#include "vi.h"

static int v_ecl __P((SCR *));
static int v_ecl_init __P((SCR *));
static int v_ecl_log __P((SCR *, TEXT *));
static int v_ex_done __P((SCR *, VICMD *));

/*
 * v_again -- &
 *	Repeat the previous substitution.
 *
 * PUBLIC: int v_again __P((SCR *, VICMD *));
 */
int
v_again(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	static CHAR_T nul[] = { 0 };

	ex_cinit(sp, &cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1);
	argv_exp0(sp, &cmd, nul, 1);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_exmode -- Q
 *	Switch the editor into EX mode.
 *
 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
 */
int
v_exmode(SCR *sp, VICMD *vp)
{
	GS *gp;

	gp = sp->gp;

	/* Try and switch screens -- the screen may not permit it. */
	if (gp->scr_screen(sp, SC_EX)) {
		msgq(sp, M_ERR,
		    "207|The Q command requires the ex terminal interface");
		return (1);
	}
	(void)gp->scr_attr(sp, SA_ALTERNATE, 0);

	/* Save the current cursor position. */
	sp->frp->lno = sp->lno;
	sp->frp->cno = sp->cno;
	F_SET(sp->frp, FR_CURSORSET);

	/* Switch to ex mode. */
	F_CLR(sp, SC_VI | SC_SCR_VI);
	F_SET(sp, SC_EX);

	/* Move out of the vi screen. */
	(void)ex_puts(sp, "\n");

	return (0);
}

/*
 * v_join -- [count]J
 *	Join lines together.
 *
 * PUBLIC: int v_join __P((SCR *, VICMD *));
 */
int
v_join(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	int lno;

	/*
	 * YASC.
	 * The general rule is that '#J' joins # lines, counting the current
	 * line.  However, 'J' and '1J' are the same as '2J', i.e. join the
	 * current and next lines.  This doesn't map well into the ex command
	 * (which takes two line numbers), so we handle it here.  Note that
	 * we never test for EOF -- historically going past the end of file
	 * worked just fine.
	 */
	lno = vp->m_start.lno + 1;
	if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
		lno = vp->m_start.lno + (vp->count - 1);

	ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_shiftl -- [count]<motion
 *	Shift lines left.
 *
 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
 */
int
v_shiftl(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	static CHAR_T lt[] = {'<', 0};

	ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
	argv_exp0(sp, &cmd, lt, 2);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_shiftr -- [count]>motion
 *	Shift lines right.
 *
 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
 */
int
v_shiftr(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	static CHAR_T gt[] = {'>', 0};

	ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
	argv_exp0(sp, &cmd, gt, 2);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_suspend -- ^Z
 *	Suspend vi.
 *
 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
 */
int
v_suspend(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	static CHAR_T suspend[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};

	ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
	argv_exp0(sp, &cmd, suspend, sizeof(suspend)/sizeof(CHAR_T));
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_switch -- ^^
 *	Switch to the previous file.
 *
 * PUBLIC: int v_switch __P((SCR *, VICMD *));
 */
int
v_switch(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	char *name;
	const CHAR_T *wp;
	size_t wlen;

	/*
	 * Try the alternate file name, then the previous file
	 * name.  Use the real name, not the user's current name.
	 */
	if ((name = sp->alt_name) == NULL) {
		msgq(sp, M_ERR, "180|No previous file to edit");
		return (1);
	}

	/* If autowrite is set, write out the file. */
	if (file_m1(sp, 0, FS_ALL))
		return (1);

	ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
	CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
	argv_exp0(sp, &cmd, wp, wlen);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_tagpush -- ^[
 *	Do a tag search on the cursor keyword.
 *
 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
 */
int
v_tagpush(SCR *sp, VICMD *vp)
{
	EXCMD cmd;

#ifdef GTAGS
	if (O_ISSET(sp, O_GTAGSMODE) && vp->m_start.cno == 0)
		ex_cinit(sp, &cmd, C_RTAG, 0, OOBLNO, 0, 0);
	else
#endif
	ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
	argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_tagpop -- ^T
 *	Pop the tags stack.
 *
 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
 */
int
v_tagpop(SCR *sp, VICMD *vp)
{
	EXCMD cmd;

	ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_filter -- [count]!motion command(s)
 *	Run range through shell commands, replacing text.
 *
 * PUBLIC: int v_filter __P((SCR *, VICMD *));
 */
int
v_filter(SCR *sp, VICMD *vp)
{
	EXCMD cmd;
	TEXT *tp;

	/*
	 * !!!
	 * Historical vi permitted "!!" in an empty file, and it's handled
	 * as a special case in the ex_bang routine.  Don't modify this setup
	 * without understanding that one.  In particular, note that we're
	 * manipulating the ex argument structures behind ex's back.
	 *
	 * !!!
	 * Historical vi did not permit the '!' command to be associated with
	 * a non-line oriented motion command, in general, although it did
	 * with search commands.  So, !f; and !w would fail, but !/;<CR>
	 * would succeed, even if they all moved to the same location in the
	 * current line.  I don't see any reason to disallow '!' using any of
	 * the possible motion commands.
	 *
	 * !!!
	 * Historical vi ran the last bang command if N or n was used as the
	 * search motion.
	 */
	if (F_ISSET(vp, VC_ISDOT) ||
	    ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
		static CHAR_T bang[] = {'!', 0};
		ex_cinit(sp,
		    &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
		EXP(sp)->argsoff = 0;			/* XXX */

		if (argv_exp1(sp, &cmd, bang, 1, 1))
			return (1);
		cmd.argc = EXP(sp)->argsoff;		/* XXX */
		cmd.argv = EXP(sp)->args;		/* XXX */
		return (v_exec_ex(sp, vp, &cmd));
	}

	/* Get the command from the user. */
	if (v_tcmd(sp, vp,
	    '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
		return (1);

	/*
	 * Check to see if the user changed their mind.
	 *
	 * !!!
	 * Entering <escape> on an empty line was historically an error,
	 * this implementation doesn't bother.
	 */
	tp = TAILQ_FIRST(&sp->tiq);
	if (tp->term != TERM_OK) {
		vp->m_final.lno = sp->lno;
		vp->m_final.cno = sp->cno;
		return (0);
	}

	/* Home the cursor. */
	vs_home(sp);

	ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
	EXP(sp)->argsoff = 0;			/* XXX */

	if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
		return (1);
	cmd.argc = EXP(sp)->argsoff;		/* XXX */
	cmd.argv = EXP(sp)->args;		/* XXX */
	return (v_exec_ex(sp, vp, &cmd));
}

/*
 * v_exec_ex --
 *	Execute an ex command.
 *
 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
 */
int
v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
{
	int rval;

	rval = exp->cmd->fn(sp, exp);
	return (v_ex_done(sp, vp) || rval);
}

/*
 * v_ex -- :
 *	Execute a colon command line.
 *
 * PUBLIC: int v_ex __P((SCR *, VICMD *));
 */
int
v_ex(SCR *sp, VICMD *vp)
{
	WIN *wp;
	TEXT *tp;
	int do_cedit, do_resolution, ifcontinue;

	wp = sp->wp;

	/*
	 * !!!
	 * If we put out more than a single line of messages, or ex trashes
	 * the screen, the user may continue entering ex commands.  We find
	 * this out when we do the screen/message resolution.  We can't enter
	 * completely into ex mode however, because the user can elect to
	 * return into vi mode by entering any key, i.e. we have to be in raw
	 * mode.
	 */
	for (do_cedit = do_resolution = 0;;) {
		/*
		 * !!!
		 * There may already be an ex command waiting to run.  If
		 * so, we continue with it.
		 */
		if (!EXCMD_RUNNING(wp)) {
			/* Get a command. */
			if (v_tcmd(sp, vp, ':',
			    TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
				return (1);
			tp = TAILQ_FIRST(&sp->tiq);

			/*
			 * If the user entered a single <esc>, they want to
			 * edit their colon command history.  If they already
			 * entered some text, move it into the edit history.
			 */
			if (tp->term == TERM_CEDIT) {
				if (tp->len > 1 && v_ecl_log(sp, tp))
					return (1);
				do_cedit = 1;
				break;
			}

			/* If the user didn't enter anything, return. */
			if (tp->term == TERM_BS)
				break;

			/* Log the command. */
			if (O_STR(sp, O_CEDIT) != NULL)
				(void)v_ecl_log(sp, tp);

			/* Push a command on the command stack. */
			if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
				return (1);
		}

		/* Home the cursor. */
		vs_home(sp);

		/*
		 * !!!
		 * If the editor wrote the screen behind curses back, put out
		 * a <newline> so that we don't overwrite the user's command
		 * with its output or the next want-to-continue? message.  This
		 * doesn't belong here, but I can't find another place to put
		 * it.  See, we resolved the output from the last ex command,
		 * and the user entered another one.  This is the only place
		 * where we have control before the ex command writes output.
		 * We could get control in vs_msg(), but we have no way to know
		 * if command didn't put out any output when we try and resolve
		 * this command.  This fixes a bug where combinations of ex
		 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
		 */
		if (F_ISSET(sp, SC_SCR_EXWROTE))
			(void)putchar('\n');

		/* Call the ex parser. */
		(void)ex_cmd(sp);

		/* Flush ex messages. */
		(void)ex_fflush(sp);

		/* Resolve any messages. */
		if (vs_ex_resolve(sp, &ifcontinue))
			return (1);

		/*
		 * Continue or return.  If continuing, make sure that we
		 * eventually do resolution.
		 */
		if (!ifcontinue)
			break;
		do_resolution = 1;

		/* If we're continuing, it's a new command. */
		++sp->ccnt;
	}

	/*
	 * If the user previously continued an ex command, we have to do
	 * resolution to clean up the screen.  Don't wait, we already did
	 * that.
	 */
	if (do_resolution) {
		F_SET(sp, SC_EX_WAIT_NO);
		if (vs_ex_resolve(sp, &ifcontinue))
			return (1);
	}

	/* Cleanup from the ex command. */
	if (v_ex_done(sp, vp))
		return (1);

	/* The user may want to edit their colon command history. */
	if (do_cedit)
		return (v_ecl(sp));

	return (0);
}

/*
 * v_ex_done --
 *	Cleanup from an ex command.
 */
static int
v_ex_done(SCR *sp, VICMD *vp)
{
	size_t len;

	/*
	 * The only cursor modifications are real, however, the underlying
	 * line may have changed; don't trust anything.  This code has been
	 * a remarkably fertile place for bugs.  Do a reality check on a
	 * cursor value, and make sure it's okay.  If necessary, change it.
	 * Ex keeps track of the line number, but it cares less about the
	 * column and it may have disappeared.
	 *
	 * Don't trust ANYTHING.
	 *
	 * XXX
	 * Ex will soon have to start handling the column correctly; see
	 * the POSIX 1003.2 standard.
	 */
	if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
		sp->lno = 1;
		sp->cno = 0;
	} else if (sp->cno >= len)
		sp->cno = len ? len - 1 : 0;

	vp->m_final.lno = sp->lno;
	vp->m_final.cno = sp->cno;

	/*
	 * Don't re-adjust the cursor after executing an ex command,
	 * and ex movements are permanent.
	 */
	F_CLR(vp, VM_RCM_MASK);
	F_SET(vp, VM_RCM_SET);

	return (0);
}

/*
 * v_ecl --
 *	Start an edit window on the colon command-line commands.
 */
static int
v_ecl(SCR *sp)
{
	GS *gp;
	WIN *wp;
	SCR *new;

	/* Initialize the screen, if necessary. */
	gp = sp->gp;
	wp = sp->wp;
	if (wp->ccl_sp == NULL && v_ecl_init(sp))
		return (1);

	/* Get a new screen. */
	if (screen_init(gp, sp, &new))
		return (1);
	if (vs_split(sp, new, 1)) {
		(void)screen_fini(new);
		return (1);
	}

	/* Attach to the screen. */
	new->ep = wp->ccl_sp->ep;
	++new->ep->refcnt;
	TAILQ_INSERT_HEAD(&new->ep->scrq, new, eq);

	new->frp = wp->ccl_sp->frp;
	new->frp->flags = sp->frp->flags;
	new->conv = wp->ccl_sp->conv;

	/* Move the cursor to the end. */
	(void)db_last(new, &new->lno);
	if (new->lno == 0)
		new->lno = 1;

	/* Remember the originating window. */
	sp->ccl_parent = sp;

	/* It's a special window. */
	F_SET(new, SC_COMEDIT);

	/* Don't encode on writing to DB. */
	o_set(new, O_FILEENCODING, OS_STRDUP, "WCHAR_T", 0);

	/* Set up the switch. */
	sp->nextdisp = new;
	F_SET(sp, SC_SSWITCH);
	return (0);
}

/*
 * v_ecl_exec --
 *	Execute a command from a colon command-line window.
 *
 * PUBLIC: int v_ecl_exec __P((SCR *));
 */
int
v_ecl_exec(SCR *sp)
{
	size_t len;
	CHAR_T *p;

	if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
		v_emsg(sp, NULL, VIM_EMPTY);
		return (1);
	}
	if (len == 0) {
		msgq(sp, M_BERR, "307|No ex command to execute");
		return (1);
	}
	
	/* Push the command on the command stack. */
	if (ex_run_str(sp, NULL, p, len, 0, 0))
		return (1);

	/* Set up the switch. */
	sp->nextdisp = sp->ccl_parent;
	F_SET(sp, SC_EXIT);
	return (0);
}

/*
 * v_ecl_log --
 *	Log a command into the colon command-line log file.
 */
static int
v_ecl_log(SCR *sp, TEXT *tp)
{
	db_recno_t lno;
	int rval;
	CHAR_T *p;
	size_t len;
	SCR *ccl_sp;

	/* Initialize the screen, if necessary. */
	if (sp->wp->ccl_sp == NULL && v_ecl_init(sp))
		return (1);

	ccl_sp = sp->wp->ccl_sp;

	/*
	 * Don't log colon command window commands into the colon command
	 * window...
	 */
	if (sp->ep == ccl_sp->ep)
		return (0);

	if (db_last(ccl_sp, &lno)) {
		return (1);
	}
	/* Don't log line that is identical to previous one */
	if (lno > 0 &&
	    !db_get(ccl_sp, lno, 0, &p, &len) &&
	    len == tp->len &&
	    !MEMCMP(tp->lb, p, len))
		rval = 0;
	else {
		rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len);
		/* XXXX end "transaction" on ccl */
		/* Is this still necessary now that we no longer hijack sp ? */
		log_cursor(ccl_sp);
	}

	return (rval);
}

/*
 * v_ecl_init --
 *	Initialize the colon command-line log file.
 */
static int
v_ecl_init(SCR *sp)
{
	FREF *frp;
	GS *gp;
	WIN *wp;

	gp = sp->gp;
	wp = sp->wp;

	/* Get a temporary file. */
	if ((frp = file_add(sp, NULL)) == NULL)
		return (1);

	/*
	 * XXX
	 * Create a screen -- the file initialization code wants one.
	 */
	if (screen_init(gp, sp, &wp->ccl_sp))
		return (1);
	conv_enc(wp->ccl_sp, O_FILEENCODING, "WCHAR_T");
	if (file_init(wp->ccl_sp, frp, NULL, 0)) {
		(void)screen_fini(wp->ccl_sp);
		wp->ccl_sp = 0;
		return (1);
	}

	/* The underlying file isn't recoverable. */
	F_CLR(wp->ccl_sp->ep, F_RCV_ON);

	return (0);
}