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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2025 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "log.h"
#include "cmdput.h"
#include "cmdbase.h"
#include "utf8.h"
#include "file.h"
#include "datafile.h"
size_t
getConsoleWidth (void) {
size_t width = UINT16_MAX;
getConsoleSize(&width, NULL);
return width;
}
const char *
getTranslatedText (const char *text) {
if (!text) return "";
if (*text) text = gettext(text);
return text;
}
static void
checkForOutputError (void) {
if (ferror(stdout)) {
logSystemError("standard output write");
exit(PROG_EXIT_FATAL);
}
}
void
putFlush (void) {
fflush(stdout);
checkForOutputError();
}
void
putConsole (const char *bytes, size_t count) {
writeWithConsoleEncoding(stdout, bytes, count);
checkForOutputError();
}
void
putString (const char *string) {
fputs(string, stdout);
checkForOutputError();
}
void
putBytes (const void *bytes, size_t count) {
fwrite(bytes, 1, count, stdout);
checkForOutputError();
}
void
putByte (char byte) {
fputc(byte, stdout);
checkForOutputError();
}
void
putNewline (void) {
putByte('\n');
}
void
vputf (const char *format, va_list args) {
vprintf(format, args);
checkForOutputError();
}
void
putf (const char *format, ...) {
va_list args;
va_start(args, format);
vputf(format, args);
va_end(args);
}
void
putWrappedText (
const char *text, char *line,
unsigned int lineIndent, unsigned int lineWidth
) {
unsigned int textWidth = lineWidth - lineIndent;
unsigned int textLeft = strlen(text);
while (1) {
unsigned int textLength = textLeft;
if (textLength > textWidth) {
textLength = textWidth;
while (textLength > 0) {
if (isspace(text[textLength])) break;
textLength -= 1;
}
while (textLength > 0) {
if (!isspace(text[--textLength])) {
textLength += 1;
break;
}
}
}
{
unsigned int lineLength = lineIndent + textLength;
if (textLength > 0) {
memcpy(line+lineIndent, text, textLength);
} else {
while (lineLength > 0) {
if (!isspace(line[--lineLength])) {
lineLength += 1;
break;
}
}
}
if (lineLength > 0) {
putBytes(line, lineLength);
putNewline();
}
while (textLength < textLeft) {
if (!isspace(text[textLength])) break;
textLength += 1;
}
if (!(textLeft -= textLength)) break;
text += textLength;
memset(line, ' ', lineIndent);
}
}
}
void
putFormattedLines (
const char *const *const *blocks,
char *line, int lineWidth
) {
const char *const *const *block = blocks;
char *paragraphText = NULL;
size_t paragraphSize = 0;
size_t paragraphLength = 0;
while (*block) {
const char *const *chunk = *block++;
if (!*chunk) continue;
while (1) {
const char *text = *chunk;
if (!text) break;
text = getTranslatedText(text);
if (*text && !iswspace(*text)) {
size_t textLength = strlen(text);
size_t newLength = paragraphLength + textLength + 1;
int extendingParagraph = !!paragraphLength;
if (extendingParagraph) newLength += 1;
if (newLength > paragraphSize) {
size_t newSize = (newLength | 0XFF) + 1;
char *newText = realloc(paragraphText, newSize);
if (!newText) {
logMallocError();
goto done;
}
paragraphText = newText;
paragraphSize = newSize;
}
if (extendingParagraph) paragraphText[paragraphLength++] = ' ';
memcpy(¶graphText[paragraphLength], text, textLength);
paragraphText[paragraphLength += textLength] = 0;
} else {
if (paragraphLength) {
putWrappedText(paragraphText, line, 0, lineWidth);
paragraphLength = 0;
}
putString(text);
putNewline();
}
chunk += 1;
}
if (paragraphLength) {
putWrappedText(paragraphText, line, 0, lineWidth);
paragraphLength = 0;
}
if (*block) putNewline();
}
done:
if (paragraphText) free(paragraphText);
}
void
putUtf8Character (wchar_t character) {
if (character) {
Utf8Buffer utf8;
size_t utfs = convertWcharToUtf8(character, utf8);
putBytes(utf8, utfs);
} else {
static const unsigned char bytes[] = {0XC0, 0X80};
putBytes(bytes, ARRAY_COUNT(bytes));
}
}
void
putUtf8Characters (const wchar_t *characters, size_t count) {
const wchar_t *character = characters;
const wchar_t *end = character + count;
while (character < end) putUtf8Character(*character++);
}
void
putUtf8String (const wchar_t *string) {
putUtf8Characters(string, wcslen(string));
}
void
putHexadecimalCharacter (wchar_t character) {
writeHexadecimalCharacter(stdout, character);
checkForOutputError();
}
void
putHexadecimalCharacters (const wchar_t *characters, size_t count) {
const wchar_t *character = characters;
const wchar_t *end = character + count;
while (character < end) putHexadecimalCharacter(*character++);
}
void
putHexadecimalString (const wchar_t *string) {
putHexadecimalCharacters(string, wcslen(string));
}
|