update from Rodney Ruddock (rodney@snowhite.cis.uoguelph.ca)
[unix-history] / usr / src / contrib / ed / r.c
CommitLineData
efe5b60f
KB
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
05ee7127 12static char sccsid[] = "@(#)r.c 5.8 (Berkeley) %G%";
efe5b60f
KB
13#endif /* not lint */
14
ecbf4ad0
KB
15#include <sys/types.h>
16#include <sys/stat.h>
17
afdef93a 18#include <limits.h>
efe5b60f
KB
19#include <a.out.h>
20#include <errno.h>
ecbf4ad0
KB
21#include <regex.h>
22#include <setjmp.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
e692f66f
KB
27#ifdef DBI
28#include <db.h>
29#endif
30
ecbf4ad0
KB
31#include "ed.h"
32#include "extern.h"
efe5b60f
KB
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 */
efe5b60f
KB
38void
39r(inputt, errnum)
ecbf4ad0
KB
40 FILE *inputt;
41 int *errnum;
efe5b60f 42{
ecbf4ad0
KB
43 FILE *l_fp;
44 long l_num;
afdef93a
KB
45 int l_bang_flag=0;
46 char *l_filename_read=NULL, *l_temp=NULL;
efe5b60f 47
ecbf4ad0 48 if (filename_flag == 1) {
e692f66f 49 sigspecial++;
ecbf4ad0 50 l_filename_read = filename_current;
afdef93a 51 l_temp = filename_current;
e692f66f
KB
52 sigspecial--;
53 if (sigint_flag && (!sigspecial))
54 SIGINT_ACTION;
ecbf4ad0 55 } else {
05ee7127
KB
56 if (End_default) {
57 End = bottom;
58 End_default = 0;
59 }
ecbf4ad0
KB
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
afdef93a
KB
69 if (*errnum < 0) {
70 filename_flag = 0;
ecbf4ad0 71 return;
afdef93a 72 }
ecbf4ad0
KB
73 *errnum = 0;
74 }
efe5b60f 75
ecbf4ad0
KB
76 if (filename_current == NULL) {
77 if (l_filename_read == NULL) {
78 strcpy(help_msg, "no filename given");
79 *errnum = -1;
afdef93a 80 filename_flag = 0;
ecbf4ad0
KB
81 if (ss)
82 ungetc('\n', inputt);
83 return;
84 } else
85 filename_current = l_filename_read;
86 }
efe5b60f 87
ecbf4ad0
KB
88 /*
89 * Determine if the file can be read. If not set the help message to
90 * something descriptive that the user should understand.
a1df51d9 91 * We're now allowing ed to read directory and executable files
afdef93a
KB
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.
ecbf4ad0 96 */
afdef93a
KB
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) {
ecbf4ad0
KB
118 strcpy(help_msg, "permission lacking to read file");
119 printf("?%s\n", l_filename_read);
afdef93a 120 filename_flag = 0;
ecbf4ad0
KB
121 *errnum = 0;
122 return;
123 }
afdef93a
KB
124 filename_flag = 0;
125 if (!l_bang_flag)
126 fseek(l_fp, (off_t)0, 0);
ecbf4ad0
KB
127 if (g_flag == 0)
128 u_clr_stk();
129 l_num = input_lines(l_fp, errnum);
ecbf4ad0
KB
130 if (*errnum < 0)
131 return;
132 *errnum = 0;
efe5b60f 133
672cc1c0 134 if (explain_flag > 0)
ecbf4ad0
KB
135 printf("%ld\n", l_num);
136 if (l_filename_read != filename_current)
137 free(l_filename_read);
efe5b60f 138
afdef93a
KB
139 if (l_bang_flag)
140 pclose(l_fp);
141 else
142 fclose(l_fp);
05ee7127 143 /*change_flag = 1; done in input_lines() already */
ecbf4ad0
KB
144 if (sigint_flag)
145 SIGINT_ACTION;
146 *errnum = 1;
147}