date and time created 93/01/23 11:13:11 by bostic
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sun, 24 Jan 1993 03:13:11 +0000 (19:13 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sun, 24 Jan 1993 03:13:11 +0000 (19:13 -0800)
SCCS-vsn: contrib/ed/get_line.c 5.1
SCCS-vsn: contrib/ed/get_pattern.c 5.1

usr/src/contrib/ed/get_line.c [new file with mode: 0644]
usr/src/contrib/ed/get_pattern.c [new file with mode: 0644]

diff --git a/usr/src/contrib/ed/get_line.c b/usr/src/contrib/ed/get_line.c
new file mode 100644 (file)
index 0000000..af2f4f0
--- /dev/null
@@ -0,0 +1,81 @@
+/*-
+ * Copyright (c) 1992 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Rodney Ruddock of the University of Guelph.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)get_line.c 5.1 (Berkeley) %G%";
+#endif /* not lint */
+
+#include "ed.h"
+
+/*
+ * Get the specified line from the buffer. Note that in the buffer
+ * we stored lengths of text, not strings (NULL terminated, except
+ * under MEMORY). So we want to put a terminating NULL in for
+ * whatever command is going to be handling the line.
+ */
+
+/* these variables are here (instead of main and ed.h) because they
+ * are only used with the buffer when run under STDIO. STDIO is a
+ * resource pig with most of the OS's I've tested this with. The
+ * use of these variables improves performance up to 100% in several
+ * cases. The piggyness is thus: fseek causes the current STDIO buf
+ * to be flushed out and a new one read in...even when it is not necessary!
+ * Read 512 (or 1024) when you don't have to for almost every access
+ * and you'll slow down too. So these variable are used to control unneeded
+ * fseeks.
+ * I've been told the newer BSD STDIO has fixed this, but don't
+ * currently have a copy.
+ */
+
+int file_loc=0;
+extern int file_loc;
+
+/* Get a particular line of length len from ed's buffer and place it in
+ * 'text', the standard repository for the "current" line.
+ */
+
+void
+get_line(loc, len)
+
+#ifdef STDIO
+long loc;
+#endif
+#ifdef DBI
+recno_t loc;
+#endif
+#ifdef MEMORY
+char *loc;
+#endif
+int len;
+
+{
+#ifdef STDIO
+
+  if (file_loc != loc)
+    fseek(fhtmp, loc, 0);
+  file_seek = 1;
+  file_loc = loc + fread(text, sizeof(char), len, fhtmp);
+#endif
+
+#ifdef DBI
+  DBT db_key, db_data;
+
+  (db_key.data) = &loc;
+  (db_key.size) = sizeof(recno_t);
+  (dbhtmp->get)(dbhtmp, &db_key, &db_data, (u_int)0);
+  strcpy(text, db_data.data);
+#endif
+
+#ifdef MEMORY
+  tmp = (char *)loc;
+  bcopy(loc, text, len);
+#endif
+  text[len] = '\0';
+} /* end-get_line */
diff --git a/usr/src/contrib/ed/get_pattern.c b/usr/src/contrib/ed/get_pattern.c
new file mode 100644 (file)
index 0000000..5947d5b
--- /dev/null
@@ -0,0 +1,107 @@
+/*-
+ * Copyright (c) 1992 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Rodney Ruddock of the University of Guelph.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)get_pattern.c      5.1 (Berkeley) %G%";
+#endif /* not lint */
+
+#include "ed.h"
+
+/*
+ * This is for getting RE and replacement patterns for any command
+ * that uses RE's and replacements.
+ */
+
+char
+*get_pattern(delim, inputt, errnum, flag)
+
+int delim;
+FILE *inputt;
+int *errnum, flag;
+
+{
+  int l_cnt=1;
+  static int l_max=510;
+  char *l_pat, *l_pat_tmp;
+
+  /* get a "reasonable amount of space for the RE */
+  l_pat = (char *)calloc(l_max+2, sizeof(char));
+  if (l_pat == NULL)
+    {
+      *errnum = -3;
+      strcpy(help_msg, "out of memory error");
+      return(NULL);
+    }
+  l_pat[0] = delim;
+
+  if ((delim == ' ') || (delim == '\n'))
+    {
+      if (delim == '\n')
+        ungetc(delim, inputt);
+      strcpy(help_msg, "illegal delimiter");
+      *errnum = -2;
+      return(l_pat);
+    }
+  
+  while (1)
+       {
+         ss = getc(inputt);
+         if (ss == '\\')
+           {
+             ss = getc(inputt);
+             if ((ss == delim) || ((flag == 1) && (ss == '\n')))
+                 l_pat[l_cnt] = ss;
+             else
+               {
+                 l_pat[l_cnt] = '\\';
+                 /*ungetc(ss, inputt);*/
+                 l_pat[++l_cnt] = ss;
+               }
+             goto leap;
+           }
+         else if ((ss == '\n') || (ss == EOF))
+           {
+             ungetc(ss, inputt);
+             strcpy(help_msg, "no closing delimiter found");
+             *errnum = -1;
+             l_pat[l_cnt] = '\0'; /* this is done for 's's backward compat. */
+             return(l_pat);
+           }
+         if (ss == delim)
+           break;
+
+         l_pat[l_cnt] = ss;
+
+leap:
+         if (l_cnt > l_max)
+           {
+             /* the RE is really long; get more space for it */
+             l_max = l_max + 256;
+             l_pat_tmp = l_pat;
+             l_pat = (char *)calloc(l_max+2, sizeof(char));
+             if (l_pat == NULL)
+               {
+                 *errnum = -3;
+                 strcpy(help_msg, "out of memory error");
+                 return(NULL);
+               }
+             bcopy(l_pat_tmp, l_pat, l_cnt);
+             free(l_pat_tmp);
+           }
+         l_cnt++;
+       }
+  l_pat[l_cnt] = '\0';
+  *errnum = 0;
+  /* send back the pattern. l_pat[0] has the delimiter in it so the RE
+   * really starts at l_pat[1]. It's done this way for the special forms
+   * of 's' (substitute).
+   */
+  return(l_pat);
+} /* end-get_pattern */