xref: /original-bsd/contrib/ed/r.c (revision 48611f03)
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.6 (Berkeley) 03/18/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <a.out.h>
19 #include <errno.h>
20 #include <regex.h>
21 #include <setjmp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #ifdef DBI
27 #include <db.h>
28 #endif
29 
30 #include "ed.h"
31 #include "extern.h"
32 
33 /*
34  * This sets up things for the central input routine to place the
35  * incoming text at the proper place in the buffer.
36  */
37 void
38 r(inputt, errnum)
39 	FILE *inputt;
40 	int *errnum;
41 {
42 	FILE *l_fp;
43 	long l_num;
44 	char *l_filename_read=NULL, *l_temp;
45 
46 	if (filename_flag == 1) {
47 		sigspecial++;
48 		l_filename_read = filename_current;
49 		filename_flag = 0;
50 		sigspecial--;
51 		if (sigint_flag && (!sigspecial))
52 			SIGINT_ACTION;
53 	} else {
54 		l_temp = filename(inputt, errnum);
55 		if (*errnum == 1)
56 			l_filename_read = l_temp;
57 		else
58 			if (*errnum == -2) {
59 				while (((ss = getc(inputt)) != '\n') ||
60 				    (ss == EOF));
61 				l_filename_read = filename_current;
62 			} else
63 				if (*errnum < 0)
64 					return;
65 		*errnum = 0;
66 	}
67 
68 	if (filename_current == NULL) {
69 		if (l_filename_read == NULL) {
70 			strcpy(help_msg, "no filename given");
71 			*errnum = -1;
72 			if (ss)
73 				ungetc('\n', inputt);
74 			return;
75 		} else
76 			filename_current = l_filename_read;
77 	}
78 
79 	/*
80 	 * Determine if the file can be read.  If not set the help message to
81 	 * something descriptive that the user should understand.
82 	 * We're now allowing ed to read directory and executable files
83 	 * for as much as it can, if there are NULL's in the file it
84 	 * is guaranteed to be different since ed doesn't do NULL's.
85 	 */
86 	if ((l_fp = fopen(l_filename_read, "r")) == NULL) {
87 		strcpy(help_msg, "permission lacking to read file");
88 		printf("?%s\n", l_filename_read);
89 		*errnum = 0;
90 		return;
91 	}
92 	fseek(l_fp, (off_t)0, 0);
93 	if (g_flag == 0)
94 		u_clr_stk();
95 	l_num = input_lines(l_fp, errnum);
96 	if (*errnum < 0)
97 		return;
98 	*errnum = 0;
99 
100 	if (explain_flag > 0)
101 		printf("%ld\n", l_num);
102 	if (l_filename_read != filename_current)
103 		free(l_filename_read);
104 
105 	fclose(l_fp);
106 	change_flag = 1;
107 	if (sigint_flag)
108 		SIGINT_ACTION;
109 	*errnum = 1;
110 }
111