xref: /csrg-svn/contrib/ed/r.c (revision 60663)
157697Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457697Sbostic  *
557697Sbostic  * This code is derived from software contributed to Berkeley by
657697Sbostic  * Rodney Ruddock of the University of Guelph.
757697Sbostic  *
857697Sbostic  * %sccs.include.redist.c%
957697Sbostic  */
1057697Sbostic 
1157697Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)r.c	8.1 (Berkeley) 05/31/93";
1357697Sbostic #endif /* not lint */
1457697Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic #include <sys/stat.h>
1757710Sbostic 
1859475Sbostic #include <limits.h>
1957697Sbostic #include <a.out.h>
2057697Sbostic #include <errno.h>
2157710Sbostic #include <regex.h>
2257710Sbostic #include <setjmp.h>
2357710Sbostic #include <stdio.h>
2457710Sbostic #include <stdlib.h>
2557710Sbostic #include <string.h>
2657697Sbostic 
2758315Sbostic #ifdef DBI
2858315Sbostic #include <db.h>
2958315Sbostic #endif
3058315Sbostic 
3157710Sbostic #include "ed.h"
3257710Sbostic #include "extern.h"
3357710Sbostic 
3457697Sbostic /*
3557697Sbostic  * This sets up things for the central input routine to place the
3657697Sbostic  * incoming text at the proper place in the buffer.
3757697Sbostic  */
3857697Sbostic void
r(inputt,errnum)3957697Sbostic r(inputt, errnum)
4057710Sbostic 	FILE *inputt;
4157710Sbostic 	int *errnum;
4257697Sbostic {
4357710Sbostic 	FILE *l_fp;
4457710Sbostic 	long l_num;
4559475Sbostic 	int l_bang_flag=0;
4659475Sbostic 	char *l_filename_read=NULL, *l_temp=NULL;
4757697Sbostic 
4857710Sbostic 	if (filename_flag == 1) {
4958315Sbostic 		sigspecial++;
5057710Sbostic 		l_filename_read = filename_current;
5159475Sbostic 		l_temp = filename_current;
5258315Sbostic 		sigspecial--;
5358315Sbostic 		if (sigint_flag && (!sigspecial))
5458315Sbostic 			SIGINT_ACTION;
5557710Sbostic 	} else {
5659915Sbostic 		if (End_default) {
5759915Sbostic 			End = bottom;
5859915Sbostic 			End_default = 0;
5959915Sbostic 		}
6057710Sbostic 		l_temp = filename(inputt, errnum);
6157710Sbostic 		if (*errnum == 1)
6257710Sbostic 			l_filename_read = l_temp;
6357710Sbostic 		else
6457710Sbostic 			if (*errnum == -2) {
6557710Sbostic 				while (((ss = getc(inputt)) != '\n') ||
6657710Sbostic 				    (ss == EOF));
6757710Sbostic 				l_filename_read = filename_current;
6857710Sbostic 			} else
6959475Sbostic 				if (*errnum < 0) {
7059475Sbostic 					filename_flag = 0;
7157710Sbostic 					return;
7259475Sbostic 				}
7357710Sbostic 		*errnum = 0;
7457710Sbostic 	}
7557697Sbostic 
7657710Sbostic 	if (filename_current == NULL) {
7757710Sbostic 		if (l_filename_read == NULL) {
7857710Sbostic 			strcpy(help_msg, "no filename given");
7957710Sbostic 			*errnum = -1;
8059475Sbostic 			filename_flag = 0;
8157710Sbostic 			if (ss)
8257710Sbostic 				ungetc('\n', inputt);
8357710Sbostic 			return;
8457710Sbostic 		} else
8557710Sbostic 			filename_current = l_filename_read;
8657710Sbostic 	}
8757697Sbostic 
8857710Sbostic 	/*
8957710Sbostic 	 * Determine if the file can be read.  If not set the help message to
9057710Sbostic 	 * something descriptive that the user should understand.
9158353Sbostic 	 * We're now allowing ed to read directory and executable files
9259475Sbostic 	 * for as much as it can, if the last character in the file
9359475Sbostic 	 * isn't a '\n' then one will be added and a warning given - the
9459475Sbostic 	 * user (for now) has to figure out how to remove it if they want.
9559475Sbostic 	 * Ed accepts the NUL character now.
9657710Sbostic 	 */
9759475Sbostic 	if (l_temp && l_temp[FILENAME_LEN+1]) { /* bang flag */
9859475Sbostic 		FILE *popen();
9959475Sbostic 
10059475Sbostic 		if (l_temp[0] == '\0') {
10159475Sbostic 			strcpy(help_msg, "no command given");
10259475Sbostic 			*errnum = -1;
10359475Sbostic 			return;
10459475Sbostic 		}
10559475Sbostic 		if ((l_fp = popen(l_temp, "r")) == NULL) {
10659475Sbostic 			strcpy(help_msg, "error executing command");
10759475Sbostic 			*errnum = -1;
10859475Sbostic 			filename_flag = 0;
10959475Sbostic 			if (l_fp != NULL)
11059475Sbostic 				pclose(l_fp);
11159475Sbostic 			return;
11259475Sbostic 		}
11359475Sbostic 		if (filename_flag == 1)
11459475Sbostic 			filename_current = NULL;
11559475Sbostic 		l_bang_flag = 1;
11659475Sbostic 	}
11759475Sbostic 	else if ((l_fp = fopen(l_filename_read, "r")) == NULL) {
11857710Sbostic 		strcpy(help_msg, "permission lacking to read file");
11957710Sbostic 		printf("?%s\n", l_filename_read);
12059475Sbostic 		filename_flag = 0;
12157710Sbostic 		*errnum = 0;
12257710Sbostic 		return;
12357710Sbostic 	}
12459475Sbostic 	filename_flag = 0;
12559475Sbostic 	if (!l_bang_flag)
12659475Sbostic 		fseek(l_fp, (off_t)0, 0);
12757710Sbostic 	if (g_flag == 0)
12857710Sbostic 		u_clr_stk();
12957710Sbostic 	l_num = input_lines(l_fp, errnum);
13057710Sbostic 	if (*errnum < 0)
13157710Sbostic 		return;
13257710Sbostic 	*errnum = 0;
13357697Sbostic 
13458710Sbostic 	if (explain_flag > 0)
13557710Sbostic 		printf("%ld\n", l_num);
13657710Sbostic 	if (l_filename_read != filename_current)
13757710Sbostic 		free(l_filename_read);
13857697Sbostic 
13959475Sbostic 	if (l_bang_flag)
14059475Sbostic 		pclose(l_fp);
14159475Sbostic 	else
14259475Sbostic 		fclose(l_fp);
14359915Sbostic 	/*change_flag = 1; done in input_lines() already */
14457710Sbostic 	if (sigint_flag)
14557710Sbostic 		SIGINT_ACTION;
14657710Sbostic 	*errnum = 1;
14757710Sbostic }
148