<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://raychev.org/w/index.php?action=history&amp;feed=atom&amp;title=Ed_source%2Fbuf.c</id>
	<title>Ed source/buf.c - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://raychev.org/w/index.php?action=history&amp;feed=atom&amp;title=Ed_source%2Fbuf.c"/>
	<link rel="alternate" type="text/html" href="https://raychev.org/w/index.php?title=Ed_source/buf.c&amp;action=history"/>
	<updated>2026-11-30T04:24:09Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.12</generator>
	<entry>
		<id>https://raychev.org/w/index.php?title=Ed_source/buf.c&amp;diff=60&amp;oldid=prev</id>
		<title>Delyan: Import public page from https://delyan.org/ed/html/buf.c.html</title>
		<link rel="alternate" type="text/html" href="https://raychev.org/w/index.php?title=Ed_source/buf.c&amp;diff=60&amp;oldid=prev"/>
		<updated>2026-11-29T03:26:56Z</updated>

		<summary type="html">&lt;p&gt;Import public page from https://delyan.org/ed/html/buf.c.html&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;span id=&amp;quot;top&amp;quot;&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;span id=&amp;quot;file1&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;     1  /* buf.c: This file contains the scratch-file buffer routines for the&lt;br /&gt;
     2     ed line editor. */&lt;br /&gt;
     3  /*-&lt;br /&gt;
     4   * Copyright (c) 1993 Andrew Moore, Talke Studio.&lt;br /&gt;
     5   * All rights reserved.&lt;br /&gt;
     6   *&lt;br /&gt;
     7   * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
     8   * modification, are permitted provided that the following conditions&lt;br /&gt;
     9   * are met:&lt;br /&gt;
    10   * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
    11   *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
    12   * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
    13   *    notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
    14   *    documentation and/or other materials provided with the distribution.&lt;br /&gt;
    15   *&lt;br /&gt;
    16   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS&amp;#039;&amp;#039; AND&lt;br /&gt;
    17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE&lt;br /&gt;
    18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE&lt;br /&gt;
    19   * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE&lt;br /&gt;
    20   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL&lt;br /&gt;
    21   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS&lt;br /&gt;
    22   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)&lt;br /&gt;
    23   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT&lt;br /&gt;
    24   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY&lt;br /&gt;
    25   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF&lt;br /&gt;
    26   * SUCH DAMAGE.&lt;br /&gt;
    27   */&lt;br /&gt;
    28  &lt;br /&gt;
    29  #include &amp;amp;lt;sys/cdefs.h&amp;amp;gt;&lt;br /&gt;
    30  __FBSDID(&amp;amp;quot;$FreeBSD: src/bin/ed/buf.c,v 1.22 2002/06/30 05:13:53 obrien Exp $&amp;amp;quot;);&lt;br /&gt;
    31  &lt;br /&gt;
    32  #include &amp;amp;lt;sys/file.h&amp;amp;gt;&lt;br /&gt;
    33  #include &amp;amp;lt;sys/stat.h&amp;amp;gt;&lt;br /&gt;
    34  &lt;br /&gt;
    35  #include &amp;amp;quot;ed.h&amp;amp;quot;&lt;br /&gt;
    36  &lt;br /&gt;
    37  &lt;br /&gt;
    38  FILE *sfp;              /* scratch file pointer */&lt;br /&gt;
    39  off_t sfseek;               /* scratch file position */&lt;br /&gt;
    40  int seek_write;             /* seek before writing */&lt;br /&gt;
    41  line_t buffer_head;         /* incore buffer */&lt;br /&gt;
    42  &lt;br /&gt;
    43  /* get_sbuf_line: get a line of text from the scratch file; return pointer&lt;br /&gt;
    44     to the text */&lt;br /&gt;
    45  char *&lt;br /&gt;
    46  get_sbuf_line(line_t *lp)&lt;br /&gt;
    47  {&lt;br /&gt;
    48      static char *sfbuf = NULL;  /* buffer */&lt;br /&gt;
    49      static int sfbufsz = 0;     /* buffer size */&lt;br /&gt;
    50  &lt;br /&gt;
    51      int len, ct;&lt;br /&gt;
    52  &lt;br /&gt;
    53      if (lp == &amp;amp;amp;buffer_head)&lt;br /&gt;
    54          return NULL;&lt;br /&gt;
    55      seek_write = 1;             /* force seek on write */&lt;br /&gt;
    56      /* out of position */&lt;br /&gt;
    57      if (sfseek != lp-&amp;amp;gt;seek) {&lt;br /&gt;
    58          sfseek = lp-&amp;amp;gt;seek;&lt;br /&gt;
    59          if (fseeko(sfp, sfseek, SEEK_SET) &amp;amp;lt; 0) {&lt;br /&gt;
    60              fprintf(stderr, &amp;amp;quot;%s\n&amp;amp;quot;, strerror(errno));&lt;br /&gt;
    61              errmsg = &amp;amp;quot;cannot seek temp file&amp;amp;quot;;&lt;br /&gt;
    62              return NULL;&lt;br /&gt;
    63          }&lt;br /&gt;
    64      }&lt;br /&gt;
    65      len = lp-&amp;amp;gt;len;&lt;br /&gt;
    66      REALLOC(sfbuf, sfbufsz, len + 1, NULL);&lt;br /&gt;
    67      if ((ct = fread(sfbuf, sizeof(char), len, sfp)) &amp;amp;lt;  0 || ct != len) {&lt;br /&gt;
    68          fprintf(stderr, &amp;amp;quot;%s\n&amp;amp;quot;, strerror(errno));&lt;br /&gt;
    69          errmsg = &amp;amp;quot;cannot read temp file&amp;amp;quot;;&lt;br /&gt;
    70          return NULL;&lt;br /&gt;
    71      }&lt;br /&gt;
    72      sfseek += len;              /* update file position */&lt;br /&gt;
    73      sfbuf[len] = &amp;#039;\0&amp;#039;;&lt;br /&gt;
    74      return sfbuf;&lt;br /&gt;
    75  }&lt;br /&gt;
    76  &lt;br /&gt;
    77  &lt;br /&gt;
    78  /* put_sbuf_line: write a line of text to the scratch file and add a line node&lt;br /&gt;
    79     to the editor buffer;  return a pointer to the end of the text */&lt;br /&gt;
    80  const char *&lt;br /&gt;
    81  put_sbuf_line(const char *cs)&lt;br /&gt;
    82  {&lt;br /&gt;
    83      line_t *lp;&lt;br /&gt;
    84      int len, ct;&lt;br /&gt;
    85      const char *s;&lt;br /&gt;
    86  &lt;br /&gt;
    87      if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {&lt;br /&gt;
    88          fprintf(stderr, &amp;amp;quot;%s\n&amp;amp;quot;, strerror(errno));&lt;br /&gt;
    89          errmsg = &amp;amp;quot;out of memory&amp;amp;quot;;&lt;br /&gt;
    90          return NULL;&lt;br /&gt;
    91      }&lt;br /&gt;
    92      /* assert: cs is &amp;#039;\n&amp;#039; terminated */&lt;br /&gt;
    93      for (s = cs; *s != &amp;#039;\n&amp;#039;; s++)&lt;br /&gt;
    94          ;&lt;br /&gt;
    95      if (s - cs &amp;amp;gt;= LINECHARS) {&lt;br /&gt;
    96          errmsg = &amp;amp;quot;line too long&amp;amp;quot;;&lt;br /&gt;
    97          return NULL;&lt;br /&gt;
    98      }&lt;br /&gt;
    99      len = s - cs;&lt;br /&gt;
   100      /* out of position */&lt;br /&gt;
   101      if (seek_write) {&lt;br /&gt;
   102          if (fseeko(sfp, (off_t)0, SEEK_END) &amp;amp;lt; 0) {&lt;br /&gt;
   103              fprintf(stderr, &amp;amp;quot;%s\n&amp;amp;quot;, strerror(errno));&lt;br /&gt;
   104              errmsg = &amp;amp;quot;cannot seek temp file&amp;amp;quot;;&lt;br /&gt;
   105              return NULL;&lt;br /&gt;
   106          }&lt;br /&gt;
   107          sfseek = ftello(sfp);&lt;br /&gt;
   108          seek_write = 0;&lt;br /&gt;
   109      }&lt;br /&gt;
   110      /* assert: SPL1() */&lt;br /&gt;
   111      if ((ct = fwrite(cs, sizeof(char), len, sfp)) &amp;amp;lt; 0 || ct != len) {&lt;br /&gt;
   112          sfseek = -1;&lt;br /&gt;
   113          fprintf(stderr, &amp;amp;quot;%s\n&amp;amp;quot;, strerror(errno));&lt;br /&gt;
   114          errmsg = &amp;amp;quot;cannot write temp file&amp;amp;quot;;&lt;br /&gt;
   115          return NULL;&lt;br /&gt;
   116      }&lt;br /&gt;
   117      lp-&amp;amp;gt;len = len;&lt;br /&gt;
   118      lp-&amp;amp;gt;seek  = sfseek;&lt;br /&gt;
   119      add_line_node(lp);&lt;br /&gt;
   120      sfseek += len;          /* update file position */&lt;br /&gt;
   121      return ++s;&lt;br /&gt;
   122  }&lt;br /&gt;
   123  &lt;br /&gt;
   124  &lt;br /&gt;
   125  /* add_line_node: add a line node in the editor buffer after the current line */&lt;br /&gt;
   126  void&lt;br /&gt;
   127  add_line_node(line_t *lp)&lt;br /&gt;
   128  {&lt;br /&gt;
   129      line_t *cp;&lt;br /&gt;
   130  &lt;br /&gt;
   131      cp = get_addressed_line_node(current_addr);             /* this get_addressed_line_node last! */&lt;br /&gt;
   132      INSQUE(lp, cp);&lt;br /&gt;
   133      addr_last++;&lt;br /&gt;
   134      current_addr++;&lt;br /&gt;
   135  }&lt;br /&gt;
   136  &lt;br /&gt;
   137  &lt;br /&gt;
   138  /* get_line_node_addr: return line number of pointer */&lt;br /&gt;
   139  long&lt;br /&gt;
   140  get_line_node_addr(line_t *lp)&lt;br /&gt;
   141  {&lt;br /&gt;
   142      line_t *cp = &amp;amp;amp;buffer_head;&lt;br /&gt;
   143      long n = 0;&lt;br /&gt;
   144  &lt;br /&gt;
   145      while (cp != lp &amp;amp;amp;&amp;amp;amp; (cp = cp-&amp;amp;gt;q_forw) != &amp;amp;amp;buffer_head)&lt;br /&gt;
   146          n++;&lt;br /&gt;
   147      if (n &amp;amp;amp;&amp;amp;amp; cp == &amp;amp;amp;buffer_head) {&lt;br /&gt;
   148          errmsg = &amp;amp;quot;invalid address&amp;amp;quot;;&lt;br /&gt;
   149          return ERR;&lt;br /&gt;
   150       }&lt;br /&gt;
   151       return n;&lt;br /&gt;
   152  }&lt;br /&gt;
   153  &lt;br /&gt;
   154  &lt;br /&gt;
   155  /* get_addressed_line_node: return pointer to a line node in the editor buffer */&lt;br /&gt;
   156  line_t *&lt;br /&gt;
   157  get_addressed_line_node(long n)&lt;br /&gt;
   158  {&lt;br /&gt;
   159      static line_t *lp = &amp;amp;amp;buffer_head;&lt;br /&gt;
   160      static long on = 0;&lt;br /&gt;
   161  &lt;br /&gt;
   162      SPL1();&lt;br /&gt;
   163      if (n &amp;amp;gt; on)&lt;br /&gt;
   164          if (n &amp;amp;lt;= (on + addr_last) &amp;amp;gt;&amp;amp;gt; 1)&lt;br /&gt;
   165              for (; on &amp;amp;lt; n; on++)&lt;br /&gt;
   166                  lp = lp-&amp;amp;gt;q_forw;&lt;br /&gt;
   167          else {&lt;br /&gt;
   168              lp = buffer_head.q_back;&lt;br /&gt;
   169              for (on = addr_last; on &amp;amp;gt; n; on--)&lt;br /&gt;
   170                  lp = lp-&amp;amp;gt;q_back;&lt;br /&gt;
   171          }&lt;br /&gt;
   172      else&lt;br /&gt;
   173          if (n &amp;amp;gt;= on &amp;amp;gt;&amp;amp;gt; 1)&lt;br /&gt;
   174              for (; on &amp;amp;gt; n; on--)&lt;br /&gt;
   175                  lp = lp-&amp;amp;gt;q_back;&lt;br /&gt;
   176          else {&lt;br /&gt;
   177              lp = &amp;amp;amp;buffer_head;&lt;br /&gt;
   178              for (on = 0; on &amp;amp;lt; n; on++)&lt;br /&gt;
   179                  lp = lp-&amp;amp;gt;q_forw;&lt;br /&gt;
   180          }&lt;br /&gt;
   181      SPL0();&lt;br /&gt;
   182      return lp;&lt;br /&gt;
   183  }&lt;br /&gt;
   184  &lt;br /&gt;
   185  &lt;br /&gt;
   186  extern int newline_added;&lt;br /&gt;
   187  &lt;br /&gt;
   188  char sfn[15] = &amp;amp;quot;&amp;amp;quot;;              /* scratch file name */&lt;br /&gt;
   189  &lt;br /&gt;
   190  /* open_sbuf: open scratch file */&lt;br /&gt;
   191  int&lt;br /&gt;
   192  open_sbuf(void)&lt;br /&gt;
   193  {&lt;br /&gt;
   194      int fd;&lt;br /&gt;
   195      int u;&lt;br /&gt;
   196  &lt;br /&gt;
   197      isbinary = newline_added = 0;&lt;br /&gt;
   198      u = umask(077);&lt;br /&gt;
   199      strcpy(sfn, &amp;amp;quot;/tmp/ed.XXXXXX&amp;amp;quot;);&lt;br /&gt;
   200      if ((fd = mkstemp(sfn)) == -1 ||&lt;br /&gt;
   201          (sfp = fdopen(fd, &amp;amp;quot;w+&amp;amp;quot;)) == NULL) {&lt;br /&gt;
   202          if (fd != -1)&lt;br /&gt;
   203              close(fd);&lt;br /&gt;
   204          perror(sfn);&lt;br /&gt;
   205          errmsg = &amp;amp;quot;cannot open temp file&amp;amp;quot;;&lt;br /&gt;
   206          umask(u);&lt;br /&gt;
   207          return ERR;&lt;br /&gt;
   208      }&lt;br /&gt;
   209      umask(u);&lt;br /&gt;
   210      return 0;&lt;br /&gt;
   211  }&lt;br /&gt;
   212  &lt;br /&gt;
   213  &lt;br /&gt;
   214  /* close_sbuf: close scratch file */&lt;br /&gt;
   215  int&lt;br /&gt;
   216  close_sbuf(void)&lt;br /&gt;
   217  {&lt;br /&gt;
   218      if (sfp) {&lt;br /&gt;
   219          if (fclose(sfp) &amp;amp;lt; 0) {&lt;br /&gt;
   220              fprintf(stderr, &amp;amp;quot;%s: %s\n&amp;amp;quot;, sfn, strerror(errno));&lt;br /&gt;
   221              errmsg = &amp;amp;quot;cannot close temp file&amp;amp;quot;;&lt;br /&gt;
   222              return ERR;&lt;br /&gt;
   223          }&lt;br /&gt;
   224          sfp = NULL;&lt;br /&gt;
   225          unlink(sfn);&lt;br /&gt;
   226      }&lt;br /&gt;
   227      sfseek = seek_write = 0;&lt;br /&gt;
   228      return 0;&lt;br /&gt;
   229  }&lt;br /&gt;
   230  &lt;br /&gt;
   231  &lt;br /&gt;
   232  /* quit: remove_lines scratch file and exit */&lt;br /&gt;
   233  void&lt;br /&gt;
   234  quit(int n)&lt;br /&gt;
   235  {&lt;br /&gt;
   236      if (sfp) {&lt;br /&gt;
   237          fclose(sfp);&lt;br /&gt;
   238          unlink(sfn);&lt;br /&gt;
   239      }&lt;br /&gt;
   240      exit(n);&lt;br /&gt;
   241  }&lt;br /&gt;
   242  &lt;br /&gt;
   243  &lt;br /&gt;
   244  unsigned char ctab[256];        /* character translation table */&lt;br /&gt;
   245  &lt;br /&gt;
   246  /* init_buffers: open scratch buffer; initialize line queue */&lt;br /&gt;
   247  void&lt;br /&gt;
   248  init_buffers(void)&lt;br /&gt;
   249  {&lt;br /&gt;
   250      int i = 0;&lt;br /&gt;
   251  &lt;br /&gt;
   252      /* Read stdin one character at a time to avoid i/o contention&lt;br /&gt;
   253         with shell escapes invoked by nonterminal input, e.g.,&lt;br /&gt;
   254         ed - &amp;amp;lt;&amp;amp;lt;EOF&lt;br /&gt;
   255         !cat&lt;br /&gt;
   256         hello, world&lt;br /&gt;
   257         EOF */&lt;br /&gt;
   258      setbuffer(stdin, stdinbuf, 1);&lt;br /&gt;
   259  &lt;br /&gt;
   260      /* Ensure stdout is line buffered. This avoids bogus delays&lt;br /&gt;
   261         of output if stdout is piped through utilities to a terminal. */&lt;br /&gt;
   262      setvbuf(stdout, NULL, _IOLBF, 0);&lt;br /&gt;
   263      if (open_sbuf() &amp;amp;lt; 0)&lt;br /&gt;
   264          quit(2);&lt;br /&gt;
   265      REQUE(&amp;amp;amp;buffer_head, &amp;amp;amp;buffer_head);&lt;br /&gt;
   266      for (i = 0; i &amp;amp;lt; 256; i++)&lt;br /&gt;
   267          ctab[i] = i;&lt;br /&gt;
   268  }&lt;br /&gt;
   269  &lt;br /&gt;
   270  &lt;br /&gt;
   271  /* translit_text: translate characters in a string */&lt;br /&gt;
   272  char *&lt;br /&gt;
   273  translit_text(char *s, int len, int from, int to)&lt;br /&gt;
   274  {&lt;br /&gt;
   275      static int i = 0;&lt;br /&gt;
   276  &lt;br /&gt;
   277      unsigned char *us;&lt;br /&gt;
   278  &lt;br /&gt;
   279      ctab[i] = i;            /* restore table to initial state */&lt;br /&gt;
   280      ctab[i = from] = to;&lt;br /&gt;
   281      for (us = (unsigned char *) s; len-- &amp;amp;gt; 0; us++)&lt;br /&gt;
   282          *us = ctab[*us];&lt;br /&gt;
   283      return s;&lt;br /&gt;
   284  }&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
Generated by [http://www.iki.fi/~mtr/genscript/ GNU Enscript 1.6.6].&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;[https://delyan.org/ed/html/buf.c.html Original page on delyan.org]&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Delyan</name></author>
	</entry>
</feed>