xref: /original-bsd/contrib/ed/r.c (revision 50ed812d)
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.7 (Berkeley) 04/28/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 		l_temp = filename(inputt, errnum);
57 		if (*errnum == 1)
58 			l_filename_read = l_temp;
59 		else
60 			if (*errnum == -2) {
61 				while (((ss = getc(inputt)) != '\n') ||
62 				    (ss == EOF));
63 				l_filename_read = filename_current;
64 			} else
65 				if (*errnum < 0) {
66 					filename_flag = 0;
67 					return;
68 				}
69 		*errnum = 0;
70 	}
71 
72 	if (filename_current == NULL) {
73 		if (l_filename_read == NULL) {
74 			strcpy(help_msg, "no filename given");
75 			*errnum = -1;
76 			filename_flag = 0;
77 			if (ss)
78 				ungetc('\n', inputt);
79 			return;
80 		} else
81 			filename_current = l_filename_read;
82 	}
83 
84 	/*
85 	 * Determine if the file can be read.  If not set the help message to
86 	 * something descriptive that the user should understand.
87 	 * We're now allowing ed to read directory and executable files
88 	 * for as much as it can, if the last character in the file
89 	 * isn't a '\n' then one will be added and a warning given - the
90 	 * user (for now) has to figure out how to remove it if they want.
91 	 * Ed accepts the NUL character now.
92 	 */
93 	if (l_temp && l_temp[FILENAME_LEN+1]) { /* bang flag */
94 		FILE *popen();
95 
96 		if (l_temp[0] == '\0') {
97 			strcpy(help_msg, "no command given");
98 			*errnum = -1;
99 			return;
100 		}
101 		if ((l_fp = popen(l_temp, "r")) == NULL) {
102 			strcpy(help_msg, "error executing command");
103 			*errnum = -1;
104 			filename_flag = 0;
105 			if (l_fp != NULL)
106 				pclose(l_fp);
107 			return;
108 		}
109 		if (filename_flag == 1)
110 			filename_current = NULL;
111 		l_bang_flag = 1;
112 	}
113 	else if ((l_fp = fopen(l_filename_read, "r")) == NULL) {
114 		strcpy(help_msg, "permission lacking to read file");
115 		printf("?%s\n", l_filename_read);
116 		filename_flag = 0;
117 		*errnum = 0;
118 		return;
119 	}
120 	filename_flag = 0;
121 	if (!l_bang_flag)
122 		fseek(l_fp, (off_t)0, 0);
123 	if (g_flag == 0)
124 		u_clr_stk();
125 	l_num = input_lines(l_fp, errnum);
126 	if (*errnum < 0)
127 		return;
128 	*errnum = 0;
129 
130 	if (explain_flag > 0)
131 		printf("%ld\n", l_num);
132 	if (l_filename_read != filename_current)
133 		free(l_filename_read);
134 
135 	if (l_bang_flag)
136 		pclose(l_fp);
137 	else
138 		fclose(l_fp);
139 	change_flag = 1;
140 	if (sigint_flag)
141 		SIGINT_ACTION;
142 	*errnum = 1;
143 }
144