
Путь: Toys/Other, команды версии: Ver.4 Ver.9 fmt Комментарии в файле fmt.c :
Исходный текст в файле fmt.c #define FOR_fmt
#include "toys.h"
GLOBALS(
long width;
int level, pos;
)
static void newline(void)
{
if (TT.pos) xputc('\n');
TT.pos = 0;
}
// Process lines of input, with (0,0) flush between files
static void fmt_line(char **pline, long len)
{
char *line;
int idx, indent, count;
// Flush line on EOF
if (!pline) return newline();
// Measure indentation
for (line = *pline, idx = count = 0; isspace(line[idx]); idx++) {
if (line[idx]=='\t') count += 8-(count&7);
else if (line[idx]==' ') count++;
}
indent = idx;
// Blank lines (even with same indentation) flush line
if (idx==len) {
xputc('\n');
TT.level = 0;
return newline();
}
// Did indentation change?
if (count!=TT.level) newline();
TT.level = count;
// Loop through words
while (idx<len) {
char *word = line+idx;
// Measure this word (unicode width) and end
while (idx<len && !isspace(line[idx])) idx++;
line[idx++] = 0;
count = utf8len(word);
if (TT.pos+count+!!TT.pos>=TT.width) newline();
// When indenting a new line, preserve tab/space mixture of input
if (!TT.pos) {
TT.pos = TT.level;
if (indent) printf("%.*s", indent, line);
} else count++;
printf(" %s"+!(TT.pos!=TT.level), word);
TT.pos += count;
while (isspace(line[idx])) idx++;
}
}
void fmt_main(void)
{
loopfiles_lines(toys.optargs, fmt_line);
} |
![]() |