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