1*57687Sbostic /*- 2*57687Sbostic * Copyright (c) 1992 The Regents of the University of California. 3*57687Sbostic * All rights reserved. 4*57687Sbostic * 5*57687Sbostic * This code is derived from software contributed to Berkeley by 6*57687Sbostic * Rodney Ruddock of the University of Guelph. 7*57687Sbostic * 8*57687Sbostic * %sccs.include.redist.c% 9*57687Sbostic */ 10*57687Sbostic 11*57687Sbostic #ifndef lint 12*57687Sbostic static char sccsid[] = "@(#)get_pattern.c 5.1 (Berkeley) 01/23/93"; 13*57687Sbostic #endif /* not lint */ 14*57687Sbostic 15*57687Sbostic #include "ed.h" 16*57687Sbostic 17*57687Sbostic /* 18*57687Sbostic * This is for getting RE and replacement patterns for any command 19*57687Sbostic * that uses RE's and replacements. 20*57687Sbostic */ 21*57687Sbostic 22*57687Sbostic char 23*57687Sbostic *get_pattern(delim, inputt, errnum, flag) 24*57687Sbostic 25*57687Sbostic int delim; 26*57687Sbostic FILE *inputt; 27*57687Sbostic int *errnum, flag; 28*57687Sbostic 29*57687Sbostic { 30*57687Sbostic int l_cnt=1; 31*57687Sbostic static int l_max=510; 32*57687Sbostic char *l_pat, *l_pat_tmp; 33*57687Sbostic 34*57687Sbostic /* get a "reasonable amount of space for the RE */ 35*57687Sbostic l_pat = (char *)calloc(l_max+2, sizeof(char)); 36*57687Sbostic if (l_pat == NULL) 37*57687Sbostic { 38*57687Sbostic *errnum = -3; 39*57687Sbostic strcpy(help_msg, "out of memory error"); 40*57687Sbostic return(NULL); 41*57687Sbostic } 42*57687Sbostic l_pat[0] = delim; 43*57687Sbostic 44*57687Sbostic if ((delim == ' ') || (delim == '\n')) 45*57687Sbostic { 46*57687Sbostic if (delim == '\n') 47*57687Sbostic ungetc(delim, inputt); 48*57687Sbostic strcpy(help_msg, "illegal delimiter"); 49*57687Sbostic *errnum = -2; 50*57687Sbostic return(l_pat); 51*57687Sbostic } 52*57687Sbostic 53*57687Sbostic while (1) 54*57687Sbostic { 55*57687Sbostic ss = getc(inputt); 56*57687Sbostic if (ss == '\\') 57*57687Sbostic { 58*57687Sbostic ss = getc(inputt); 59*57687Sbostic if ((ss == delim) || ((flag == 1) && (ss == '\n'))) 60*57687Sbostic l_pat[l_cnt] = ss; 61*57687Sbostic else 62*57687Sbostic { 63*57687Sbostic l_pat[l_cnt] = '\\'; 64*57687Sbostic /*ungetc(ss, inputt);*/ 65*57687Sbostic l_pat[++l_cnt] = ss; 66*57687Sbostic } 67*57687Sbostic goto leap; 68*57687Sbostic } 69*57687Sbostic else if ((ss == '\n') || (ss == EOF)) 70*57687Sbostic { 71*57687Sbostic ungetc(ss, inputt); 72*57687Sbostic strcpy(help_msg, "no closing delimiter found"); 73*57687Sbostic *errnum = -1; 74*57687Sbostic l_pat[l_cnt] = '\0'; /* this is done for 's's backward compat. */ 75*57687Sbostic return(l_pat); 76*57687Sbostic } 77*57687Sbostic if (ss == delim) 78*57687Sbostic break; 79*57687Sbostic 80*57687Sbostic l_pat[l_cnt] = ss; 81*57687Sbostic 82*57687Sbostic leap: 83*57687Sbostic if (l_cnt > l_max) 84*57687Sbostic { 85*57687Sbostic /* the RE is really long; get more space for it */ 86*57687Sbostic l_max = l_max + 256; 87*57687Sbostic l_pat_tmp = l_pat; 88*57687Sbostic l_pat = (char *)calloc(l_max+2, sizeof(char)); 89*57687Sbostic if (l_pat == NULL) 90*57687Sbostic { 91*57687Sbostic *errnum = -3; 92*57687Sbostic strcpy(help_msg, "out of memory error"); 93*57687Sbostic return(NULL); 94*57687Sbostic } 95*57687Sbostic bcopy(l_pat_tmp, l_pat, l_cnt); 96*57687Sbostic free(l_pat_tmp); 97*57687Sbostic } 98*57687Sbostic l_cnt++; 99*57687Sbostic } 100*57687Sbostic l_pat[l_cnt] = '\0'; 101*57687Sbostic *errnum = 0; 102*57687Sbostic /* send back the pattern. l_pat[0] has the delimiter in it so the RE 103*57687Sbostic * really starts at l_pat[1]. It's done this way for the special forms 104*57687Sbostic * of 's' (substitute). 105*57687Sbostic */ 106*57687Sbostic return(l_pat); 107*57687Sbostic } /* end-get_pattern */ 108