xref: /csrg-svn/contrib/ed/m.c (revision 57694)
1*57694Sbostic /*-
2*57694Sbostic  * Copyright (c) 1992 The Regents of the University of California.
3*57694Sbostic  * All rights reserved.
4*57694Sbostic  *
5*57694Sbostic  * This code is derived from software contributed to Berkeley by
6*57694Sbostic  * Rodney Ruddock of the University of Guelph.
7*57694Sbostic  *
8*57694Sbostic  * %sccs.include.redist.c%
9*57694Sbostic  */
10*57694Sbostic 
11*57694Sbostic #ifndef lint
12*57694Sbostic static char sccsid[] = "@(#)m.c	5.1 (Berkeley) 01/23/93";
13*57694Sbostic #endif /* not lint */
14*57694Sbostic 
15*57694Sbostic #include "ed.h"
16*57694Sbostic 
17*57694Sbostic /*
18*57694Sbostic  * Move the specified lines to the new location. It's quick 'cause
19*57694Sbostic  * just a couple of pointers are redirected.
20*57694Sbostic  */
21*57694Sbostic 
22*57694Sbostic void
23*57694Sbostic m(inputt, errnum)
24*57694Sbostic 
25*57694Sbostic FILE *inputt;
26*57694Sbostic int *errnum;
27*57694Sbostic 
28*57694Sbostic {
29*57694Sbostic   LINE *l_dest, *l_old_top, *l_old_bottom;
30*57694Sbostic 
31*57694Sbostic   /* set l_dest here */
32*57694Sbostic   if (((ss=getc(inputt)) != '\n') && (ss != EOF))
33*57694Sbostic     {
34*57694Sbostic       while (1)
35*57694Sbostic            {
36*57694Sbostic              if (ss != ' ')
37*57694Sbostic                {
38*57694Sbostic                  ungetc(ss, inputt);
39*57694Sbostic                  break;
40*57694Sbostic                }
41*57694Sbostic              ss = getc(inputt);
42*57694Sbostic            }
43*57694Sbostic       l_dest = address_conv(NULL, inputt, errnum);
44*57694Sbostic     }
45*57694Sbostic   else
46*57694Sbostic     (ungetc(ss, inputt), *errnum = -1);
47*57694Sbostic   if (sigint_flag)
48*57694Sbostic     SIGINT_ACTION;
49*57694Sbostic   if (*errnum < 0)
50*57694Sbostic     {
51*57694Sbostic       strcpy(help_msg, "bad destination address");
52*57694Sbostic       return;
53*57694Sbostic     } /* end-if */
54*57694Sbostic   *errnum = 0;
55*57694Sbostic   if (rol(inputt, errnum))
56*57694Sbostic     return;
57*57694Sbostic 
58*57694Sbostic   if (start_default && End_default)
59*57694Sbostic     start = End = current;
60*57694Sbostic   else if (start_default)
61*57694Sbostic     start = End;
62*57694Sbostic   if (start == NULL)
63*57694Sbostic     {
64*57694Sbostic       strcpy(help_msg, "bad address");
65*57694Sbostic       *errnum = -1;
66*57694Sbostic       return;
67*57694Sbostic     }
68*57694Sbostic   start_default = End_default = 0;
69*57694Sbostic   if (sigint_flag)
70*57694Sbostic     SIGINT_ACTION;
71*57694Sbostic 
72*57694Sbostic   /* do some address checking */
73*57694Sbostic   if ((l_dest) && ((l_dest == start) || (address_check(l_dest, start) == -1)) && (address_check(End, l_dest) == -1))
74*57694Sbostic     {
75*57694Sbostic       ungetc(ss, inputt);
76*57694Sbostic       *errnum = -1;
77*57694Sbostic       strcpy(help_msg, "destination address in address range");
78*57694Sbostic       return;
79*57694Sbostic     }
80*57694Sbostic 
81*57694Sbostic   change_flag = 1;
82*57694Sbostic   if (g_flag == 0)
83*57694Sbostic     u_clr_stk();
84*57694Sbostic 
85*57694Sbostic   /* some more address checking. These are "legal" command constructions
86*57694Sbostic    * but are kind-a useless since the buffer doesn't change */
87*57694Sbostic   if ((start == l_dest) || (End == l_dest))
88*57694Sbostic     return;
89*57694Sbostic   if ((start == top) && (End == bottom))
90*57694Sbostic     return;
91*57694Sbostic   if ((start == top) && (l_dest == NULL))
92*57694Sbostic     return;
93*57694Sbostic 
94*57694Sbostic   l_old_top = top;
95*57694Sbostic   l_old_bottom = bottom;
96*57694Sbostic 
97*57694Sbostic   if (start == top)
98*57694Sbostic     {
99*57694Sbostic       top = End->below;
100*57694Sbostic       u_add_stk(&(End->below->above));
101*57694Sbostic       top->above = NULL;
102*57694Sbostic     }
103*57694Sbostic   else if (End == bottom)
104*57694Sbostic     {
105*57694Sbostic       bottom = start->above;
106*57694Sbostic       u_add_stk(&(start->above->below));
107*57694Sbostic       bottom->below = NULL;
108*57694Sbostic     }
109*57694Sbostic   else
110*57694Sbostic     {
111*57694Sbostic       u_add_stk(&(start->above->below));
112*57694Sbostic       start->above->below = End->below;
113*57694Sbostic       u_add_stk(&(End->below->above));
114*57694Sbostic       End->below->above = start->above;
115*57694Sbostic     }
116*57694Sbostic 
117*57694Sbostic   if (l_dest == NULL)
118*57694Sbostic     {
119*57694Sbostic       u_add_stk(&(start->above));
120*57694Sbostic       start->above = NULL;
121*57694Sbostic       u_add_stk(&(End->below));
122*57694Sbostic       End->below = l_old_top;
123*57694Sbostic       u_add_stk(&(l_old_top->above));
124*57694Sbostic       l_old_top->above = End;
125*57694Sbostic       top = start;
126*57694Sbostic     }
127*57694Sbostic   else if (l_dest == l_old_bottom)
128*57694Sbostic     {
129*57694Sbostic       u_add_stk(&(End->below));
130*57694Sbostic       End->below = NULL;
131*57694Sbostic       u_add_stk(&(start->above));
132*57694Sbostic       start->above = l_dest;
133*57694Sbostic       u_add_stk(&(l_dest->below));
134*57694Sbostic       l_dest->below = start;
135*57694Sbostic       bottom = End;
136*57694Sbostic     }
137*57694Sbostic   else
138*57694Sbostic     {
139*57694Sbostic       u_add_stk(&(start->above));
140*57694Sbostic       start->above = l_dest;
141*57694Sbostic       u_add_stk(&(End->below));
142*57694Sbostic       End->below = l_dest->below;
143*57694Sbostic       u_add_stk(&(l_dest->below->above));
144*57694Sbostic       l_dest->below->above = End;
145*57694Sbostic       u_add_stk(&(l_dest->below));
146*57694Sbostic       l_dest->below = start;
147*57694Sbostic     }
148*57694Sbostic 
149*57694Sbostic   if (l_dest)
150*57694Sbostic     l_dest->below = start;
151*57694Sbostic   current = start;
152*57694Sbostic 
153*57694Sbostic   *errnum = 1;
154*57694Sbostic } /* end-m */
155