xref: /csrg-svn/contrib/ed/m.c (revision 60663)
157694Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457694Sbostic  *
557694Sbostic  * This code is derived from software contributed to Berkeley by
657694Sbostic  * Rodney Ruddock of the University of Guelph.
757694Sbostic  *
857694Sbostic  * %sccs.include.redist.c%
957694Sbostic  */
1057694Sbostic 
1157694Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)m.c	8.1 (Berkeley) 05/31/93";
1357694Sbostic #endif /* not lint */
1457694Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1757710Sbostic #include <regex.h>
1857710Sbostic #include <setjmp.h>
1957710Sbostic #include <stdio.h>
2057710Sbostic #include <string.h>
2157710Sbostic 
2258315Sbostic #ifdef DBI
2358315Sbostic #include <db.h>
2458315Sbostic #endif
2558315Sbostic 
2657694Sbostic #include "ed.h"
2757710Sbostic #include "extern.h"
2857694Sbostic 
2957694Sbostic /*
3057694Sbostic  * Move the specified lines to the new location. It's quick 'cause
3157694Sbostic  * just a couple of pointers are redirected.
3257694Sbostic  */
3357694Sbostic void
m(inputt,errnum)3457694Sbostic m(inputt, errnum)
3557710Sbostic 	FILE *inputt;
3657710Sbostic 	int *errnum;
3757694Sbostic {
3858315Sbostic 	LINE *l_dest=NULL, *l_old_top, *l_old_bottom;
3957694Sbostic 
4057710Sbostic 	/* Set l_dest here. */
4157710Sbostic 	if (((ss = getc(inputt)) != '\n') && (ss != EOF)) {
4257710Sbostic 		for (;;) {
4357710Sbostic 			if (ss != ' ') {
4457710Sbostic 				ungetc(ss, inputt);
4557710Sbostic 				break;
4657710Sbostic 			}
4757710Sbostic 			ss = getc(inputt);
4857710Sbostic 		}
4957710Sbostic 		l_dest = address_conv(NULL, inputt, errnum);
5057710Sbostic 	} else
5157710Sbostic 		(ungetc(ss, inputt), *errnum = -1);
5257710Sbostic 	if (*errnum < 0) {
5357710Sbostic 		strcpy(help_msg, "bad destination address");
5457710Sbostic 		return;
5557710Sbostic 	}
5657710Sbostic 	*errnum = 0;
5757710Sbostic 	if (rol(inputt, errnum))
5857710Sbostic 		return;
5957694Sbostic 
6058564Sralph 	if (Start_default && End_default)
6158564Sralph 		Start = End = current;
6257710Sbostic 	else
6358564Sralph 		if (Start_default)
6458564Sralph 			Start = End;
6558564Sralph 	if (Start == NULL) {
6658315Sbostic 		strcpy(help_msg, "buffer empty");
6757710Sbostic 		*errnum = -1;
6857710Sbostic 		return;
6957710Sbostic 	}
7058564Sralph 	Start_default = End_default = 0;
7157694Sbostic 
7257710Sbostic 	/* Do some address checking. */
7358564Sralph 	if ((l_dest) && ((l_dest == Start) ||
7458564Sralph 	    (address_check(l_dest, Start) == -1)) &&
7557710Sbostic 	    (address_check(End, l_dest) == -1)) {
7657710Sbostic 		ungetc(ss, inputt);
7757710Sbostic 		*errnum = -1;
7857710Sbostic 		strcpy(help_msg, "destination address in address range");
7957710Sbostic 		return;
8057710Sbostic 	}
8157710Sbostic 	change_flag = 1;
8257710Sbostic 	if (g_flag == 0)
8357710Sbostic 		u_clr_stk();
8457694Sbostic 
8557710Sbostic 	/*
8657710Sbostic 	 * Some more address checking. These are "legal" command constructions
8757710Sbostic 	 * but are kind-a useless since the buffer doesn't change.
8857710Sbostic 	 */
8958315Sbostic 	*errnum = 1;
9058564Sralph 	if ((Start == l_dest) || (End == l_dest))
9157710Sbostic 		return;
9258564Sralph 	if ((Start == top) && (End == bottom))
9357710Sbostic 		return;
9458564Sralph 	if ((Start == top) && (l_dest == NULL))
9557710Sbostic 		return;
9658315Sbostic 	*errnum = 0;
9757694Sbostic 
9857710Sbostic 	l_old_top = top;
9957710Sbostic 	l_old_bottom = bottom;
10057694Sbostic 
10158315Sbostic 	sigspecial++;
10258315Sbostic 
10358564Sralph 	if (Start == top) {
10457710Sbostic 		top = End->below;
10557710Sbostic 		u_add_stk(&(End->below->above));
10657710Sbostic 		top->above = NULL;
10757710Sbostic 	} else
10857710Sbostic 		if (End == bottom) {
10958564Sralph 			bottom = Start->above;
11058564Sralph 			u_add_stk(&(Start->above->below));
11157710Sbostic 			bottom->below = NULL;
11257710Sbostic 		} else {
11358564Sralph 			u_add_stk(&(Start->above->below));
11458564Sralph 			Start->above->below = End->below;
11557710Sbostic 			u_add_stk(&(End->below->above));
11658564Sralph 			End->below->above = Start->above;
11757710Sbostic 		}
11857694Sbostic 
11957710Sbostic 	if (l_dest == NULL) {
12058564Sralph 		u_add_stk(&(Start->above));
12158564Sralph 		Start->above = NULL;
12257710Sbostic 		u_add_stk(&(End->below));
12357710Sbostic 		End->below = l_old_top;
12457710Sbostic 		u_add_stk(&(l_old_top->above));
12557710Sbostic 		l_old_top->above = End;
12658564Sralph 		top = Start;
12757710Sbostic 	} else
12857710Sbostic 		if (l_dest == l_old_bottom) {
12957710Sbostic 			u_add_stk(&(End->below));
13057710Sbostic 			End->below = NULL;
13158564Sralph 			u_add_stk(&(Start->above));
13258564Sralph 			Start->above = l_dest;
13357710Sbostic 			u_add_stk(&(l_dest->below));
13458564Sralph 			l_dest->below = Start;
13557710Sbostic 			bottom = End;
13657710Sbostic 		} else {
13758564Sralph 			u_add_stk(&(Start->above));
13858564Sralph 			Start->above = l_dest;
13957710Sbostic 			u_add_stk(&(End->below));
14057710Sbostic 			End->below = l_dest->below;
14157710Sbostic 			u_add_stk(&(l_dest->below->above));
14257710Sbostic 			l_dest->below->above = End;
14357710Sbostic 			u_add_stk(&(l_dest->below));
14458564Sralph 			l_dest->below = Start;
14557710Sbostic 		}
14657694Sbostic 
14757710Sbostic 	if (l_dest)
14858564Sralph 		l_dest->below = Start;
14958564Sralph 	current = Start;
15057694Sbostic 
15158315Sbostic 	sigspecial--;
15258315Sbostic 
15357710Sbostic 	*errnum = 1;
15457710Sbostic }
155