xref: /original-bsd/contrib/ed/r.c (revision 92a0c623)
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[] = "@(#)r.c	5.8 (Berkeley) 05/11/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <limits.h>
19 #include <a.out.h>
20 #include <errno.h>
21 #include <regex.h>
22 #include <setjmp.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #ifdef DBI
28 #include <db.h>
29 #endif
30 
31 #include "ed.h"
32 #include "extern.h"
33 
34 /*
35  * This sets up things for the central input routine to place the
36  * incoming text at the proper place in the buffer.
37  */
38 void
39 r(inputt, errnum)
40 	FILE *inputt;
41 	int *errnum;
42 {
43 	FILE *l_fp;
44 	long l_num;
45 	int l_bang_flag=0;
46 	char *l_filename_read=NULL, *l_temp=NULL;
47 
48 	if (filename_flag == 1) {
49 		sigspecial++;
50 		l_filename_read = filename_current;
51 		l_temp = filename_current;
52 		sigspecial--;
53 		if (sigint_flag && (!sigspecial))
54 			SIGINT_ACTION;
55 	} else {
56 		if (End_default) {
57 			End = bottom;
58 			End_default = 0;
59 		}
60 		l_temp = filename(inputt, errnum);
61 		if (*errnum == 1)
62 			l_filename_read = l_temp;
63 		else
64 			if (*errnum == -2) {
65 				while (((ss = getc(inputt)) != '\n') ||
66 				    (ss == EOF));
67 				l_filename_read = filename_current;
68 			} else
69 				if (*errnum < 0) {
70 					filename_flag = 0;
71 					return;
72 				}
73 		*errnum = 0;
74 	}
75 
76 	if (filename_current == NULL) {
77 		if (l_filename_read == NULL) {
78 			strcpy(help_msg, "no filename given");
79 			*errnum = -1;
80 			filename_flag = 0;
81 			if (ss)
82 				ungetc('\n', inputt);
83 			return;
84 		} else
85 			filename_current = l_filename_read;
86 	}
87 
88 	/*
89 	 * Determine if the file can be read.  If not set the help message to
90 	 * something descriptive that the user should understand.
91 	 * We're now allowing ed to read directory and executable files
92 	 * for as much as it can, if the last character in the file
93 	 * isn't a '\n' then one will be added and a warning given - the
94 	 * user (for now) has to figure out how to remove it if they want.
95 	 * Ed accepts the NUL character now.
96 	 */
97 	if (l_temp && l_temp[FILENAME_LEN+1]) { /* bang flag */
98 		FILE *popen();
99 
100 		if (l_temp[0] == '\0') {
101 			strcpy(help_msg, "no command given");
102 			*errnum = -1;
103 			return;
104 		}
105 		if ((l_fp = popen(l_temp, "r")) == NULL) {
106 			strcpy(help_msg, "error executing command");
107 			*errnum = -1;
108 			filename_flag = 0;
109 			if (l_fp != NULL)
110 				pclose(l_fp);
111 			return;
112 		}
113 		if (filename_flag == 1)
114 			filename_current = NULL;
115 		l_bang_flag = 1;
116 	}
117 	else if ((l_fp = fopen(l_filename_read, "r")) == NULL) {
118 		strcpy(help_msg, "permission lacking to read file");
119 		printf("?%s\n", l_filename_read);
120 		filename_flag = 0;
121 		*errnum = 0;
122 		return;
123 	}
124 	filename_flag = 0;
125 	if (!l_bang_flag)
126 		fseek(l_fp, (off_t)0, 0);
127 	if (g_flag == 0)
128 		u_clr_stk();
129 	l_num = input_lines(l_fp, errnum);
130 	if (*errnum < 0)
131 		return;
132 	*errnum = 0;
133 
134 	if (explain_flag > 0)
135 		printf("%ld\n", l_num);
136 	if (l_filename_read != filename_current)
137 		free(l_filename_read);
138 
139 	if (l_bang_flag)
140 		pclose(l_fp);
141 	else
142 		fclose(l_fp);
143 	/*change_flag = 1; done in input_lines() already */
144 	if (sigint_flag)
145 		SIGINT_ACTION;
146 	*errnum = 1;
147 }
148