xref: /original-bsd/contrib/ed/t.c (revision bf410196)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 05/31/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <regex.h>
18 #include <setjmp.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #ifdef DBI
24 #include <db.h>
25 #endif
26 
27 #include "ed.h"
28 #include "extern.h"
29 
30 /*
31  * The transcribe function. POSIX calls it copy, but 't' for transcribe
32  * is more mneumonic and that's what I've always called it. Transcribes
33  * the spec'd lines into the buffer at the spec'd location.
34  */
35 
36 void
37 t(inputt, errnum)
38 	FILE *inputt;
39 	int *errnum;
40 {
41 	LINE *l_ptr, *l_tb, *l_te, *l_temp1, *l_temp2, *l_dest=NULL;
42 
43 	l_tb = NULL;
44 	l_te = NULL;
45 
46 	if (((ss = getc(inputt)) != '\n') && (ss != EOF)) {
47 		for (;;) {
48 			if (ss != ' ') {
49 				ungetc(ss, inputt);
50 				break;
51 			}
52 			ss = getc(inputt);
53 		}
54 		l_dest = address_conv(NULL, inputt, errnum);
55 	} else
56 		(ungetc(ss, inputt), *errnum = -1);
57 
58 	if (*errnum < 0) {
59 		strcpy(help_msg, "bad destination address");
60 		return;
61 	}
62 	*errnum = 0;
63 	if (rol(inputt, errnum))
64 		return;
65 
66 	if (Start_default && End_default)
67 		Start = End = current;
68 	else
69 		if (Start_default)
70 			Start = End;
71 	if (Start == NULL) {
72 		strcpy(help_msg, "empty buffer");
73 		*errnum = -1;
74 		return;
75 	}
76 	Start_default = End_default = 0;
77 
78 	if (g_flag == 0)
79 		u_clr_stk();
80 
81 	sigspecial++;
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 		if (sigint_flag && (!sigspecial))
86 			break;
87 		l_temp1 = (LINE *) malloc(sizeof(LINE));
88 		if (l_temp1 == NULL) {
89 			*errnum = -1;
90 			strcpy(help_msg, "out of memory error");
91 			return;
92 		}
93 		if (l_tb == NULL) {
94 			l_tb = l_temp1;
95 			(l_temp1->above) = NULL;
96 			(l_temp1->below) = NULL;
97 		} else {
98 			(l_temp1->above) = l_te;
99 			(l_temp1->below) = NULL;
100 			(l_te->below) = l_temp1;
101 		}
102 		l_te = l_temp1;
103 		(l_temp1->len) = l_ptr->len;
104 		/* add it into the buffer at the spec'd location */
105 		(l_temp1->handle) = add_line(text, l_ptr->len);
106 		if (sigint_flag && (!sigspecial))
107 			break;
108 	}
109 
110 	if (l_dest == NULL)
111 		l_temp2 = top;
112 	else {
113 		u_add_stk(&(l_dest->below));
114 		l_temp2 = l_dest->below;
115 	}
116 
117 	if (l_dest == NULL) {
118 		u_add_stk(&(top->above));
119 		(top->above) = l_tb;
120 		top = l_tb;
121 		(l_tb->above) = NULL;
122 	} else {
123 		(l_tb->above) = l_dest;
124 		(l_dest->below) = l_tb;
125 	}
126 
127 	if (l_dest == bottom) {
128 		bottom = l_te;
129 		(l_te->below) = NULL;
130 	} else {
131 		(l_te->below) = l_temp2;
132 		u_add_stk(&(l_temp2->above));
133 		(l_temp2->above) = l_te;
134 	}
135 
136 	current = l_te;
137 	change_flag = 1;
138 	sigspecial--;
139 	*errnum = 1;
140 	return;
141 }
142