summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/=line.c
blob: 1c8eab8e0e8aadb6dbc87b47a920bfe4cda669ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Simple output formatting functions

   Copyright (C) 1995, 1996 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <malloc.h>
#include <string.h>
#include <ctype.h>

#include <line.h>

/* Return a new line structure, which will output to STREAM.  WIDTH is the
   maximum number of characters per line.  If enough memory can't be
   allocated, 0 is returned.  */
struct line *
make_line (FILE *stream, unsigned width)
{
  struct line *line = malloc (sizeof (struct line));
  if (line)
    {
      line->buf = malloc (width + 2);
      if (line->buf)
	{
	  line->max = line->buf + width;
	  line->point = line->buf;
	  line->stream = stream;
	}
      else
	{
	  free (line);
	  line = 0;
	}
    }
  return line;
}

/* Free LINE and any resources it uses.  */
void
line_free (struct line *line)
{
  if (line->point > line->buf)
    line_newline (line, 0);
  free (line->buf);
  free (line);
}

/* Adds the text in STR to LINE, wrapping words as necessary to fit.
   LMARGIN is the left margin used when wrapping; whitespace is deleted at
   wrap-points.  Newlines in STR are honoured by adding a newline and
   indenting to LMARGIN; any following whitespace is kept.  */
void
line_fill (struct line *line, const char *str, unsigned lmargin)
{
  while (*str)
    {
      const char *word_end = str;

      while (*word_end == ' ')
	word_end++;

      if (*word_end == '\n')
	{
	  if (line_column (line) > lmargin)
	    line_newline (line, lmargin);
	  str = word_end + 1;
	}
      else if (*word_end)
	{
	  const char *word_start = word_end;
	  while (*word_end && !isspace (*word_end))
	    word_end++;
	  if (line_left (line, word_end - str) >= 0)
	    {
	      line_write (line, str, word_end - str);
	      str = word_end;
	    }
	  else
	    /* Word won't fit on the current line, move to the next one. */
	    {
	      line_newline (line, lmargin);
	      str = word_start; /* Omit spaces when wrapping.  */
	    }
	}
    }
}

/* Clean up after a printf to LINE, to take care of any newlines that might
   have been added.  ADDED is the amount the printf has added to the line.
   We take care of updating LINE's point.  */
void
_line_cleanup_printf (struct line *line, unsigned added)
{
  char *point = line->point, *new_point = point + added, *last_nl = new_point;

  while (last_nl > point)
    if (*--last_nl == '\n')
      /* There's a newline; deal.  */
      {
	last_nl++;
	fwrite (line->buf, 1, last_nl - line->buf, line->stream);
	if (last_nl < new_point)
	  bcopy (last_nl, line->buf, new_point - last_nl);
	new_point -= (last_nl - line->buf);
	break;
      }

  line->point = new_point;
}

/* Add STR, of length LEN, to LINE.  */
void
line_write (struct line *line, const char *str, unsigned len)
{
  const char *end = memchr (str, '\n', len) ?: str + len;
  unsigned line_len = end - str;
  char *p = line->point, *max = line->max;
  if (line_len > max - p)
    line_len = max - p;
  bcopy (str, p, line_len);
  p += line_len;
  if (line_len == len)
    line->point = p;
  else
    {
      char *buf = line->buf;
      fwrite (buf, 1, p - buf, line->stream);
      line->point = buf;
      line_write (line, end + 1, len - line_len - 1);
    }
}