xref: /csrg-svn/contrib/ed/t.c (revision 57710)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)t.c	5.2 (Berkeley) 01/23/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <db.h>
18 #include <regex.h>
19 #include <setjmp.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "ed.h"
25 #include "extern.h"
26 
27 /*
28  * The transcribe function. POSIX calls it copy, but 't' for transcribe
29  * is more mneumonic and that's what I've always called it. Transcribes
30  * the spec'd lines into the buffer at the spec'd location.
31  */
32 
33 void
34 t(inputt, errnum)
35 	FILE *inputt;
36 	int *errnum;
37 {
38 	LINE *l_ptr, *l_tb, *l_te, *l_temp1, *l_temp2, *l_dest;
39 
40 	l_tb = NULL;
41 	l_te = NULL;
42 
43 	if (((ss = getc(inputt)) != '\n') && (ss != EOF)) {
44 		for (;;) {
45 			if (ss != ' ') {
46 				ungetc(ss, inputt);
47 				break;
48 			}
49 			ss = getc(inputt);
50 		}
51 		l_dest = address_conv(NULL, inputt, errnum);
52 	} else
53 		(ungetc(ss, inputt), *errnum = -1);
54 
55 	if (sigint_flag)
56 		SIGINT_ACTION;
57 	if (*errnum < 0) {
58 		strcpy(help_msg, "bad destination address");
59 		return;
60 	}
61 	*errnum = 0;
62 	if (rol(inputt, errnum))
63 		return;
64 
65 	if (start_default && End_default)
66 		start = End = current;
67 	else
68 		if (start_default)
69 			start = End;
70 	if (start == NULL) {
71 		strcpy(help_msg, "bad address");
72 		*errnum = -1;
73 		return;
74 	}
75 	start_default = End_default = 0;
76 
77 	if (sigint_flag)
78 		SIGINT_ACTION;
79 
80 	if (g_flag == 0)
81 		u_clr_stk();
82 
83 	for (l_ptr = start; l_ptr != (End->below); l_ptr = (l_ptr->below)) {
84 		get_line(l_ptr->handle, l_ptr->len);
85 		l_temp1 = (LINE *) malloc(sizeof(LINE));
86 		if (l_temp1 == NULL) {
87 			*errnum = -1;
88 			strcpy(help_msg, "out of memory error");
89 			return;
90 		}
91 		if (l_tb == NULL) {
92 			l_tb = l_temp1;
93 			(l_temp1->above) = NULL;
94 			(l_temp1->below) = NULL;
95 		} else {
96 			(l_temp1->above) = l_te;
97 			(l_temp1->below) = NULL;
98 			(l_te->below) = l_temp1;
99 		}
100 		l_te = l_temp1;
101 		(l_temp1->len) = l_ptr->len;
102 		/* add it into the buffer at the spec'd location */
103 		(l_temp1->handle) = add_line(text, l_ptr->len);
104 		if (sigint_flag)
105 			break;
106 	}
107 
108 	if (l_dest == NULL)
109 		l_temp2 = top;
110 	else {
111 		u_add_stk(&(l_dest->below));
112 		l_temp2 = l_dest->below;
113 	}
114 
115 	if (l_dest == NULL) {
116 		u_add_stk(&(top->above));
117 		(top->above) = l_tb;
118 		top = l_tb;
119 		(l_tb->above) = NULL;
120 	} else {
121 		(l_tb->above) = l_dest;
122 		(l_dest->below) = l_tb;
123 	}
124 
125 	if (l_dest == bottom) {
126 		bottom = l_te;
127 		(l_te->below) = NULL;
128 	} else {
129 		(l_te->below) = l_temp2;
130 		u_add_stk(&(l_temp2->above));
131 		(l_temp2->above) = l_te;
132 	}
133 
134 	current = l_te;
135 	change_flag = 1;
136 	*errnum = 1;
137 	return;
138 }
139