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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/*
* libexplain - Explain errno values returned by libc functions
* Copyright (C) 2009 Peter Miller
* Written by Peter Miller <pmiller@opensource.org.au>
*
* This program 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 3 of the License, or (at
* your option) any later version.
*
* This program 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, see <http://www.gnu.org/licenses/>.
*/
#include <libexplain/ac/ctype.h>
#include <libexplain/ac/string.h>
#include <codegen/elastic_buffer.h>
#include <codegen/wrapper.h>
#define DEFAULT_LINE_WIDTH 75
void
wrapper(FILE *fp, const char *prefix, const char *text)
{
size_t prefix_len;
size_t width;
const char *cp;
static elastic_buffer_t line_buf;
static elastic_buffer_t word_buf;
if (!prefix)
prefix = "";
prefix_len = strlen(prefix);
width = DEFAULT_LINE_WIDTH;
if (prefix_len < DEFAULT_LINE_WIDTH)
width = DEFAULT_LINE_WIDTH - prefix_len;
elastic_buffer_rewind(&line_buf);
cp = text;
for (;;)
{
unsigned char c = *cp++;
if (c == '\0')
{
if (line_buf.data_length)
{
fputs(prefix, fp);
elastic_buffer_fwrite(&line_buf, fp);
putc('\n', fp);
}
return;
}
if (isspace(c))
continue;
/*
* Grab the next word.
*/
elastic_buffer_rewind(&word_buf);
for (;;)
{
elastic_buffer_putc(&word_buf, c);
c = *cp;
if (c == '\0')
break;
if (isspace(c))
break;
++cp;
}
if (line_buf.data_length == 0)
{
/* do nothing */
}
else if (line_buf.data_length + 1 + word_buf.data_length <= width)
{
elastic_buffer_putc(&line_buf, ' ');
}
else
{
fputs(prefix, fp);
elastic_buffer_fwrite(&line_buf, fp);
putc('\n', fp);
elastic_buffer_rewind(&line_buf);
}
elastic_buffer_puts(&line_buf, elastic_buffer_get(&word_buf));
}
}
void
wrapper_hang(FILE *fp, const char *prefix, const char *text)
{
size_t prefix_len;
size_t width;
const char *cp;
static elastic_buffer_t line_buf;
static elastic_buffer_t word_buf;
int extra_indent;
if (!prefix)
prefix = "";
prefix_len = strlen(prefix);
width = 80;
if (prefix_len < DEFAULT_LINE_WIDTH)
width -= prefix_len;
elastic_buffer_rewind(&line_buf);
cp = text;
extra_indent = 0;
for (;;)
{
unsigned char c = *cp++;
if (c == '\0')
{
if (line_buf.data_length)
{
fputs(prefix, fp);
if (extra_indent)
fputs(" ", fp);
elastic_buffer_fwrite(&line_buf, fp);
putc('\n', fp);
}
return;
}
if (c == '\t')
{
for (;;)
{
elastic_buffer_putc(&line_buf, ' ');
if (line_buf.data_length >= 16)
break;
}
continue;
}
if (c == '\n')
{
if (line_buf.data_length)
{
fputs(prefix, fp);
if (extra_indent)
fputs(" ", fp);
elastic_buffer_fwrite(&line_buf, fp);
putc('\n', fp);
}
elastic_buffer_rewind(&line_buf);
extra_indent = 0;
continue;
}
if (isspace(c))
continue;
/*
* Grab the next word.
*/
elastic_buffer_rewind(&word_buf);
for (;;)
{
elastic_buffer_putc(&word_buf, c);
c = *cp;
if (c == '\0')
break;
if (isspace(c))
break;
++cp;
}
if (line_buf.data_length == 0)
{
/* do nothing */
}
else if (line_buf.data[line_buf.data_length - 1] == ' ')
{
/* do nothing */
}
else if (line_buf.data_length + 1 + word_buf.data_length <= width)
{
elastic_buffer_putc(&line_buf, ' ');
}
else
{
fputs(prefix, fp);
if (extra_indent)
fputs(" ", fp);
elastic_buffer_fwrite(&line_buf, fp);
putc('\n', fp);
elastic_buffer_rewind(&line_buf);
if (!extra_indent)
{
extra_indent = 1;
width -= 4;
}
}
elastic_buffer_puts(&line_buf, elastic_buffer_get(&word_buf));
}
}
|