чување 3410bb5ad32b49016627899fd6dba6a7cfde9244
родитељ fc2cecb43b2f6bfc13aabf95e4c4bb11dbecb44c
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Mon, 18 Sep 2023 16:29:22 +0200
* .gitignore: Add cscope.out
* Update TODO
* default.o.do: Add -pedantic
* ftree.c: Remove unused leftover constant
* ftree.c: NUL-terminate u8ch whenever it is converted from u32
* ftree.c: Make some getopt error messages output optopt
* ftree.c: Make error messages consistent
* ftree.1.in: Reorder description of options
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 6, додавања: 31(+), брисања: 19(-)
diff --git a/.gitignore b/.gitignore
@@ -18,6 +18,7 @@ nohup.out
*.xz
*.1
config.redo
+cscope.out
ftree
ftree.1
ftree.html
diff --git a/TODO b/TODO
@@ -4,5 +4,11 @@ TODO
[x] Fix SEGV
- It seems that free(root) after free_node(&root) does this
-[ ] Check if everything can be done without reading the entire input and without
+[/] Check if everything can be done without reading the entire input and without
actual structs; indent seems like it can be enough for that
+ - Unfortunately, I don't see any other way to have a distinction between
+ '`' and '+' besides having two passes, one for reading the input and
+ the other for output, because there needs to be a way to know when a
+ group of node's children ends. (Maybe with seek/rewind, but that can't
+ be done for stdin, so the code would need to include both cases.)
+ - Now looking at tree(1)'s source, it also creates a tree in memory.
diff --git a/default.o.do b/default.o.do
@@ -2,4 +2,4 @@ redo-ifchange $2.c
if [ -r ./config.redo ]; then
. ./config.redo
fi
-${CC:-gcc -g} -Wall -std=c99 -o $3 -c $2.c
+${CC:-gcc -g} -Wall -pedantic -std=c99 -o $3 -c $2.c
diff --git a/ftree.1.in b/ftree.1.in
@@ -34,9 +34,6 @@ will be ignored.
.It Nm Fl h
Prints usage information.
.
-.It Nm Fl v
-Prints version and commit date.
-.
.It Nm Fl s Ar set
Selects the symbol set
.Ar set .
@@ -50,6 +47,9 @@ are:
and
.Cm s
(single\-stroke set, default).
+.
+.It Nm Fl v
+Prints version and commit date.
.El
.Sh EXAMPLES
Format a tree using double-stroke box drawing characters:
diff --git a/ftree.c b/ftree.c
@@ -15,8 +15,7 @@
#include "utf8.h"
#include "version.h"
-#define BUFSIZE 4096
-#define NODE_ALLOC_DELTA 10
+#define BUFSIZE 4096
struct Node {
int level;
@@ -77,7 +76,7 @@ alloc_node(void)
if (!newnode)
{
perror("calloc");
- exit(error(1, "Cannot alloc node"));
+ exit(error(1, "Cannot alloc"));
}
newnode->level = 0;
@@ -94,7 +93,7 @@ void
cleanup(void)
{
free_node(&root);
- //free(root);
+ // free(root);
free(indent);
}
@@ -190,11 +189,13 @@ void
print_indent(void)
{
const u32* ppindent = indent;
- u8 u8ch[6];
+ u8 u8ch[8];
+ size_t len;
while (*ppindent)
{
- u32_char_to_u8(u8ch, *ppindent);
+ len = u32_char_to_u8(u8ch, *ppindent);
+ u8ch[len] = 0;
printf("%s ", u8ch);
ppindent++;
}
@@ -210,13 +211,17 @@ print_node(const struct Node* node)
if (node->level > 0)
{
- u8 u8ch[6];
+ u8 u8ch[8];
+ size_t len;
+
if (!node->next)
- u32_char_to_u8(u8ch, tree_symbols[symbol_set][0]);
+ len = u32_char_to_u8(u8ch, tree_symbols[symbol_set][0]);
else
- u32_char_to_u8(u8ch, tree_symbols[symbol_set][1]);
+ len = u32_char_to_u8(u8ch, tree_symbols[symbol_set][1]);
+ u8ch[len] = 0;
printf("%s", u8ch);
- u32_char_to_u8(u8ch, tree_symbols[symbol_set][2]);
+ len = u32_char_to_u8(u8ch, tree_symbols[symbol_set][2]);
+ u8ch[len] = 0;
printf("%s%s", u8ch, u8ch);
}
printf("%s\n", node->label ? node->label : "<NULL>");
@@ -323,10 +328,10 @@ main(int argc, char** argv)
version();
exit(0);
case ':':
- exit(error(1, "Arguments expected for '-%c'", ch));
+ exit(error(1, "Argument expected for '-%c'", optopt));
case '?':
default:
- exit(error(1, "Invalid parameter '-%c'", ch));
+ exit(error(1, "Invalid parameter '-%c'", optopt));
}
}
@@ -340,7 +345,7 @@ main(int argc, char** argv)
if (!(input = fopen(argv[optind], "r")))
{
perror("fopen");
- exit(error(1, "Couldn't open '%s'", argv[optind]));
+ exit(error(1, "Can't open '%s'", argv[optind]));
}
}
diff --git a/ftree.do b/ftree.do
@@ -8,4 +8,4 @@ for f in *.h *.c; do
echo $f
echo $f | sed -e's/\.c$/.o/g'
done | xargs redo-ifchange
-${CC:-gcc -g} -Wall -std=c99 -o $3 ftree.o utf8.o ${LIBS}
+${CC:-gcc -g} -Wall -pedantic -std=c99 -o $3 ftree.o utf8.o ${LIBS}