157697Sbostic /*- 257697Sbostic * Copyright (c) 1992 The Regents of the University of California. 357697Sbostic * 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*57710Sbostic static char sccsid[] = "@(#)r.c 5.2 (Berkeley) 01/23/93"; 1357697Sbostic #endif /* not lint */ 1457697Sbostic 15*57710Sbostic #include <sys/types.h> 16*57710Sbostic #include <sys/stat.h> 17*57710Sbostic 1857697Sbostic #include <a.out.h> 19*57710Sbostic #include <db.h> 2057697Sbostic #include <errno.h> 21*57710Sbostic #include <regex.h> 22*57710Sbostic #include <setjmp.h> 23*57710Sbostic #include <stdio.h> 24*57710Sbostic #include <stdlib.h> 25*57710Sbostic #include <string.h> 2657697Sbostic 27*57710Sbostic #include "ed.h" 28*57710Sbostic #include "extern.h" 29*57710Sbostic 3057697Sbostic /* 3157697Sbostic * This sets up things for the central input routine to place the 3257697Sbostic * incoming text at the proper place in the buffer. 3357697Sbostic */ 3457697Sbostic void 3557697Sbostic r(inputt, errnum) 36*57710Sbostic FILE *inputt; 37*57710Sbostic int *errnum; 3857697Sbostic { 39*57710Sbostic struct exec l_magic; 40*57710Sbostic struct stat l_s_buf; 41*57710Sbostic FILE *l_fp; 42*57710Sbostic long l_num; 43*57710Sbostic char *l_filename_read, *l_temp; 44*57710Sbostic int l_srv; 4557697Sbostic 46*57710Sbostic if (filename_flag == 1) { 47*57710Sbostic l_filename_read = filename_current; 48*57710Sbostic filename_flag = 0; 49*57710Sbostic } else { 50*57710Sbostic l_temp = filename(inputt, errnum); 51*57710Sbostic if (*errnum == 1) 52*57710Sbostic l_filename_read = l_temp; 53*57710Sbostic else 54*57710Sbostic if (*errnum == -2) { 55*57710Sbostic while (((ss = getc(inputt)) != '\n') || 56*57710Sbostic (ss == EOF)); 57*57710Sbostic l_filename_read = filename_current; 58*57710Sbostic } else 59*57710Sbostic if (*errnum < 0) 60*57710Sbostic return; 61*57710Sbostic *errnum = 0; 62*57710Sbostic } 6357697Sbostic 64*57710Sbostic if (filename_current == NULL) { 65*57710Sbostic if (l_filename_read == NULL) { 66*57710Sbostic strcpy(help_msg, "no filename given"); 67*57710Sbostic *errnum = -1; 68*57710Sbostic if (ss) 69*57710Sbostic ungetc('\n', inputt); 70*57710Sbostic return; 71*57710Sbostic } else 72*57710Sbostic filename_current = l_filename_read; 73*57710Sbostic } 74*57710Sbostic if (sigint_flag) 75*57710Sbostic SIGINT_ACTION; 7657697Sbostic 77*57710Sbostic /* 78*57710Sbostic * Determine if the file can be read. If not set the help message to 79*57710Sbostic * something descriptive that the user should understand. 80*57710Sbostic */ 81*57710Sbostic if (((l_srv = stat(l_filename_read, &l_s_buf)) == -1) || 82*57710Sbostic (l_s_buf.st_mode & S_IFDIR)) { 83*57710Sbostic if (l_srv == -1) 84*57710Sbostic strcpy(help_msg, strerror(errno)); 85*57710Sbostic else 86*57710Sbostic strcpy(help_msg, 87*57710Sbostic "filename is directory, not a text file"); 88*57710Sbostic printf("?%s\n", l_filename_read); 89*57710Sbostic *errnum = 0; 90*57710Sbostic return; 91*57710Sbostic } 92*57710Sbostic if ((l_fp = fopen(l_filename_read, "r")) == NULL) { 93*57710Sbostic strcpy(help_msg, "permission lacking to read file"); 94*57710Sbostic printf("?%s\n", l_filename_read); 95*57710Sbostic *errnum = 0; 96*57710Sbostic return; 97*57710Sbostic } 98*57710Sbostic /* 99*57710Sbostic * There is permission to read the file, but if it's an executable 100*57710Sbostic * file of the object code and linked type, we really don't want to 101*57710Sbostic * look at it (according to ed spec's). 102*57710Sbostic */ 103*57710Sbostic if (fread(&l_magic, sizeof(struct exec), 1, l_fp) != 0) { 104*57710Sbostic if (!(N_BADMAG(l_magic))) { 105*57710Sbostic strcpy(help_msg, "unable to read executable file"); 106*57710Sbostic printf("?%s\n", l_filename_read); 107*57710Sbostic *errnum = 0; 108*57710Sbostic return; 109*57710Sbostic } 110*57710Sbostic } 111*57710Sbostic fseek(l_fp, (off_t)0, 0); 112*57710Sbostic if (g_flag == 0) 113*57710Sbostic u_clr_stk(); 114*57710Sbostic l_num = input_lines(l_fp, errnum); 115*57710Sbostic if (sigint_flag == 1) 116*57710Sbostic goto point; 117*57710Sbostic if (*errnum < 0) 118*57710Sbostic return; 119*57710Sbostic *errnum = 0; 12057697Sbostic 121*57710Sbostic if (explain_flag) /* !=0 */ 122*57710Sbostic printf("%ld\n", l_num); 123*57710Sbostic if (l_filename_read != filename_current) 124*57710Sbostic free(l_filename_read); 12557697Sbostic 126*57710Sbostic point: fclose(l_fp); 127*57710Sbostic change_flag = 1; 128*57710Sbostic if (sigint_flag) 129*57710Sbostic SIGINT_ACTION; 130*57710Sbostic *errnum = 1; 131*57710Sbostic } 132