aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-08-27 22:44:19 -0300
committerEuAndreh <eu@euandre.org>2021-08-27 22:44:19 -0300
commitede12d99fc46ca3fd638d8b47f498497ef28b3df (patch)
tree0cd627ace07e3917924ad675cd19946ff4c1f5b3
parentTODOs.md: Add comments to the body of #task-a6bdaeb0-7099-c728-2b7c-b080aa2fba33 (diff)
downloadgistatic-ede12d99fc46ca3fd638d8b47f498497ef28b3df.tar.gz
gistatic-ede12d99fc46ca3fd638d8b47f498497ef28b3df.tar.xz
src/*.c: Add newline between function return type and its name
The purpose of this change is two-fold: - make function and variable declarations grep-friendly (one can use a pattern /^fn_name/ to find the definition); - make the lines shorter, so less functions need to have their arguments span over many lines. This is more grep-friendly both for finding the function definition, but also for matching on the return type of the function or the variable. Update tests/c-lint.sh to enforce this.
-rw-r--r--src/lib.c167
-rw-r--r--src/lib.h6
-rw-r--r--src/logerr.c9
-rw-r--r--src/logerr.h9
-rw-r--r--src/main.c6
-rw-r--r--src/tar.c17
-rw-r--r--src/tar.h8
-rw-r--r--src/tests-lib.c9
-rw-r--r--src/tests-lib.h11
-rwxr-xr-xtests/c-lint.sh68
10 files changed, 219 insertions, 91 deletions
diff --git a/src/lib.c b/src/lib.c
index 572d708..044ae3f 100644
--- a/src/lib.c
+++ b/src/lib.c
@@ -23,13 +23,21 @@
#endif
-static const int EXIT_ERROR = 1;
-static const int EXIT_USAGE = 2;
+static const int
+EXIT_ERROR = 1;
+
+static const int
+EXIT_USAGE = 2;
#define PROGNAME "gistatic"
-static const char *const CATALOG_NAME = PROGNAME;
-static nl_catd catalog_descriptor = NULL;
+
+static const char *const
+CATALOG_NAME = PROGNAME;
+
+static nl_catd
+catalog_descriptor = NULL;
+
#define MSG_DEFAULT_TITLE 1
#define MSG_LOGO_ALT_INDEX 2
@@ -56,7 +64,8 @@ static nl_catd catalog_descriptor = NULL;
#define MSG_INCOMPATIBLE_OPTIONS 23
#define MSG_INDEX_DESCRIPTION 24
-static const char *const MSGS[] = {
+static const char *const
+MSGS[] = {
"",
[MSG_DEFAULT_TITLE]="Repositories",
[MSG_LOGO_ALT_INDEX]="Logo image of the repository list",
@@ -101,7 +110,8 @@ static const char *const MSGS[] = {
};
#ifdef TEST
-static void dump_translatable_strings(void) {
+static void
+dump_translatable_strings(void) {
const size_t size =
strlen(__FILE__) - strlen(".c") + strlen(".msg") + sizeof('\0');
char *const catalog_path = malloc(size);
@@ -122,7 +132,8 @@ static void dump_translatable_strings(void) {
}
#endif
-static const char *const LOGO_STR = ""
+static const char *const
+LOGO_STR = ""
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 16 16\" width=\"16\" height=\"16\">\n"
" <path d=\"M 0 8 L 1 8 L 1 9 L 0 9 L 0 8 Z\" />\n"
@@ -186,7 +197,8 @@ static const char *const LOGO_STR = ""
" <path d=\"M 14 12 L 15 12 L 15 13 L 14 13 L 14 12 Z\" />\n"
"</svg>\n";
-static const char *const STYLE_STR = ""
+static const char *const
+STYLE_STR = ""
":root {\n"
" --color: black;\n"
" --background-color: white;\n"
@@ -291,13 +303,20 @@ static const char *const STYLE_STR = ""
#define DESCRIPTION_MAXLENGTH 81
-static bool verbose = false;
-static const char *const GIT_SUFFIX = ".git";
-static const char *const PROJECT_HOMEPAGE_LINK =
+
+static bool
+verbose = false;
+
+static const char *const
+GIT_SUFFIX = ".git";
+
+static const char *const
+PROJECT_HOMEPAGE_LINK =
"<a href=\"https://euandreh.xyz/" PROGNAME "\">" PROGNAME "</a>.";
-static void logerr(
+static void
+logerr(
const char *const s,
const char *const msg,
const int lineno
@@ -305,7 +324,8 @@ static void logerr(
logerr_file(s, msg, __FILE__, lineno);
}
-static void logerrs(
+static void
+logerrs(
const char *const pre,
const char *const mid,
const char *const post,
@@ -315,7 +335,8 @@ static void logerrs(
logerrs_file(pre, mid, post, msg, __FILE__, lineno);
}
-static void logerrl(
+static void
+logerrl(
const char *const pre,
const size_t mid,
const char *const post,
@@ -326,7 +347,8 @@ static void logerrl(
}
-static const char *_(int msg_id) {
+static const char *
+_(int msg_id) {
if (!catalog_descriptor || catalog_descriptor == (nl_catd)-1) {
return MSGS[msg_id];
}
@@ -340,7 +362,8 @@ static const char *_(int msg_id) {
}
#ifdef TEST
-static void test_underscore(void) {
+static void
+test_underscore(void) {
test_start("test_underscore");
const char *const original_locale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "");
@@ -353,7 +376,8 @@ static void test_underscore(void) {
}
#endif
-static int print_msg(FILE *const fd, const char *const msg) {
+static int
+print_msg(FILE *const fd, const char *const msg) {
if (fprintf(fd, "%s", msg) < 0) {
logerr("fprintf()", strerror(errno), __LINE__);
return -1;
@@ -361,20 +385,24 @@ static int print_msg(FILE *const fd, const char *const msg) {
return 0;
}
-static int print_help(FILE *const fd) {
+static int
+print_help(FILE *const fd) {
return print_msg(fd, _(MSG_HELP));
}
-static int print_version(FILE *const fd) {
+static int
+print_version(FILE *const fd) {
return print_msg(fd, PROGNAME "-" VERSION " " DATE "\n");
}
-static int print_usage(FILE *const fd) {
+static int
+print_usage(FILE *const fd) {
return print_msg(fd, _(MSG_USAGE));
}
-static char *remove_suffix(char *const s, const char *const suffix) {
+static char *
+remove_suffix(char *const s, const char *const suffix) {
if (!s || !suffix) {
return NULL;
}
@@ -394,7 +422,8 @@ static char *remove_suffix(char *const s, const char *const suffix) {
}
#ifdef TEST
-static void test_remove_suffix(void) {
+static void
+test_remove_suffix(void) {
test_start("test_remove_suffix");
{
testing("empty string");
@@ -497,7 +526,8 @@ static void test_remove_suffix(void) {
}
#endif
-static char *strjoin(const char *const s1, const char *const s2) {
+static char *
+strjoin(const char *const s1, const char *const s2) {
if (!s1 || !s2) {
return NULL;
}
@@ -523,7 +553,8 @@ static char *strjoin(const char *const s1, const char *const s2) {
}
#ifdef TEST
-static void test_strjoin(void) {
+static void
+test_strjoin(void) {
test_start("test_strjoin");
{
testing("joining empty strings");
@@ -575,7 +606,8 @@ static void test_strjoin(void) {
}
#endif
-static char *strsjoin(const char *const strs[]) {
+static char *
+strsjoin(const char *const strs[]) {
size_t size = sizeof('\0');
for (size_t i = 0; strs[i]; i++) {
const size_t curr_len = strnlen(strs[i], SIZE_MAX);
@@ -608,7 +640,8 @@ static char *strsjoin(const char *const strs[]) {
}
#ifdef TEST
-static void test_strsjoin(void) {
+static void
+test_strsjoin(void) {
test_start("test_strsjoin");
{
testing("joining empty strings");
@@ -678,7 +711,8 @@ static void test_strsjoin(void) {
}
#endif
-static char *formatted_date(const time_t time_sec) {
+static char *
+formatted_date(const time_t time_sec) {
const struct tm *const time_utc = gmtime(&time_sec);
if (!time_utc) {
logerrl("gmtime(", time_sec, ")", strerror(errno), __LINE__);
@@ -698,7 +732,8 @@ static char *formatted_date(const time_t time_sec) {
}
#ifdef TEST
-static void test_formatted_date(void) {
+static void
+test_formatted_date(void) {
test_start("test_formatted_date");
{
testing("when given 0");
@@ -753,12 +788,14 @@ static void test_formatted_date(void) {
}
#endif
-static size_t max(const size_t size1, const size_t size2) {
+static size_t
+max(const size_t size1, const size_t size2) {
return size1 > size2 ? size1 : size2;
}
#ifdef TEST
-static void test_max(void) {
+static void
+test_max(void) {
test_start("max");
{
testing("equal values");
@@ -786,7 +823,8 @@ static void test_max(void) {
}
#endif
-static char *escape_html(const char *s) {
+static char *
+escape_html(const char *s) {
if (!s) {
return NULL;
}
@@ -886,7 +924,8 @@ static char *escape_html(const char *s) {
}
#ifdef TEST
-static void test_escape_html(void) {
+static void
+test_escape_html(void) {
test_start("test_escape_html");
{
testing("string with no escapable chars");
@@ -968,12 +1007,14 @@ static void test_escape_html(void) {
}
#endif
-static bool should_trim(const char c) {
+static bool
+should_trim(const char c) {
return c == '\n' || c == '\t' || c == '\r' || c == ' ';
}
#ifdef TEST
-static void test_should_trim(void) {
+static void
+test_should_trim(void) {
test_start("test_should_trim");
{
testing("the \\0 null character");
@@ -993,7 +1034,8 @@ static void test_should_trim(void) {
}
#endif
-static void strtrim(char *const s) {
+static void
+strtrim(char *const s) {
if (s == NULL) {
return;
}
@@ -1023,7 +1065,8 @@ static void strtrim(char *const s) {
}
#ifdef TEST
-static void test_strtrim(void) {
+static void
+test_strtrim(void) {
test_start("test_strtrim");
{
testing("empty string");
@@ -1119,7 +1162,8 @@ static void test_strtrim(void) {
}
#endif
-static int index_header_write(FILE *const fd, const char *const idx_title) {
+static int
+index_header_write(FILE *const fd, const char *const idx_title) {
const int e = fprintf(
fd,
""
@@ -1175,7 +1219,8 @@ static int index_header_write(FILE *const fd, const char *const idx_title) {
return 0;
}
-static char *footer_signature_string(void) {
+static char *
+footer_signature_string(void) {
const size_t template_size = strnlen(
_(MSG_FOOTER_TEMPLATE),
SIZE_MAX
@@ -1207,7 +1252,8 @@ static char *footer_signature_string(void) {
return signature_text;
}
-static int index_footer_write(FILE *const fd) {
+static int
+index_footer_write(FILE *const fd) {
char *const signature_text = footer_signature_string();
if (!signature_text) {
return -1;
@@ -1238,7 +1284,8 @@ static int index_footer_write(FILE *const fd) {
return 0;
}
-static char *last_commit_date(struct git_repository *const repo) {
+static char *
+last_commit_date(struct git_repository *const repo) {
struct git_commit *commit = NULL;
struct git_revwalk *walker = NULL;
struct git_oid oid;
@@ -1269,7 +1316,8 @@ cleanup:
}
#ifdef TEST
-static void test_last_commit_date(void) {
+static void
+test_last_commit_date(void) {
test_start("test_last_commit_date");
{
testing("embedded Git repository"
@@ -1310,7 +1358,8 @@ static void test_last_commit_date(void) {
}
#endif
-static int logo_write(FILE *const fd) {
+static int
+logo_write(FILE *const fd) {
if (fprintf(fd, "%s", LOGO_STR) < 0) {
logerr("fprintf()", strerror(errno), __LINE__);
return -1;
@@ -1318,7 +1367,8 @@ static int logo_write(FILE *const fd) {
return 0;
}
-static int style_write(FILE *const fd) {
+static int
+style_write(FILE *const fd) {
if (fprintf(fd, "%s", STYLE_STR) < 0) {
logerr("fprintf()", strerror(errno), __LINE__);
return -1;
@@ -1326,7 +1376,8 @@ static int style_write(FILE *const fd) {
return 0;
}
-static int index_row_write(FILE *const fd, char *const repopath) {
+static int
+index_row_write(FILE *const fd, char *const repopath) {
int ret = 0;
int e;
@@ -1423,7 +1474,8 @@ cleanup:
return ret;
}
-static int index_write(
+static int
+index_write(
const char *const outdir,
const char *const title,
const int repoc,
@@ -1511,7 +1563,8 @@ cleanup:
return ret;
}
-static int repo_refs_branches_each(
+static int
+repo_refs_branches_each(
FILE *const refs_fd,
struct git_repository *const repo,
struct git_reference *const ref
@@ -1599,7 +1652,8 @@ cleanup:
return ret;
}
-static int repo_refs_tags_each(
+static int
+repo_refs_tags_each(
FILE *const refs_fd,
struct git_repository *const repo,
struct git_reference *const ref,
@@ -1685,7 +1739,8 @@ cleanup:
return ret;
}
-static int repo_refs_write(
+static int
+repo_refs_write(
const char *const outdir,
struct git_repository *const repo,
const char *const project_name,
@@ -1936,7 +1991,8 @@ cleanup:
return ret;
}
-static int repo_tarballs_refs_each(
+static int
+repo_tarballs_refs_each(
struct git_repository *const repo,
struct git_reference *const ref,
const char *const tarballs_dir,
@@ -2039,7 +2095,8 @@ cleanup:
return ret;
}
-static int repo_tarballs_write(
+static int
+repo_tarballs_write(
const char *const outdir,
struct git_repository *const repo,
const char *const project_name
@@ -2099,7 +2156,8 @@ cleanup:
return ret;
}
-static int repo_write(
+static int
+repo_write(
const char *const outdir,
char *const reporelpath,
const char *const clone_url
@@ -2225,7 +2283,8 @@ cleanup:
return ret;
}
-int gistatic_main(const int argc, char *const argv[]) {
+int
+gistatic_main(const int argc, char *const argv[]) {
int ret = EXIT_SUCCESS;
bool cleanup_libgit = false;
catalog_descriptor = catopen(CATALOG_NAME, NL_CAT_LOCALE);
@@ -2345,8 +2404,10 @@ cleanup:
return ret;
}
+
#ifdef TEST
-void unit_tests_gistatic(void) {
+void
+unit_tests_gistatic(void) {
dump_translatable_strings();
git_libgit2_init();
diff --git a/src/lib.h b/src/lib.h
index 50ff2f2..07e900b 100644
--- a/src/lib.h
+++ b/src/lib.h
@@ -1,10 +1,12 @@
#ifndef GISTATIC_GISTATIC_H
#define GISTATIC_GISTATIC_H
-int gistatic_main(const int argc, char *const argv[]);
+int
+gistatic_main(const int argc, char *const argv[]);
#ifdef TEST
-void unit_tests_gistatic(void);
+void
+unit_tests_gistatic(void);
#endif
#endif
diff --git a/src/logerr.c b/src/logerr.c
index ba27b35..029ed7f 100644
--- a/src/logerr.c
+++ b/src/logerr.c
@@ -4,7 +4,8 @@
#include <stdio.h>
-void logerr_file(
+void
+logerr_file(
const char *const s,
const char *const msg,
const char *const file,
@@ -21,7 +22,8 @@ void logerr_file(
);
}
-void logerrs_file(
+void
+logerrs_file(
const char *const pre,
const char *const mid,
const char *const post,
@@ -42,7 +44,8 @@ void logerrs_file(
);
}
-void logerrl_file(
+void
+logerrl_file(
const char *const pre,
const size_t mid,
const char *const post,
diff --git a/src/logerr.h b/src/logerr.h
index 08ad682..47228ab 100644
--- a/src/logerr.h
+++ b/src/logerr.h
@@ -3,14 +3,16 @@
#include <stddef.h>
-void logerr_file(
+void
+logerr_file(
const char *const s,
const char *const msg,
const char *const file,
const int lineno
);
-void logerrs_file(
+void
+logerrs_file(
const char *const pre,
const char *const mid,
const char *const post,
@@ -19,7 +21,8 @@ void logerrs_file(
const int lineno
);
-void logerrl_file(
+void
+logerrl_file(
const char *const pre,
const size_t mid,
const char *const post,
diff --git a/src/main.c b/src/main.c
index 34214ea..a8bab28 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,13 +4,15 @@
#include <stdlib.h>
#ifdef TEST
-static void unit_tests(void) {
+static void
+unit_tests(void) {
unit_tests_tar();
unit_tests_gistatic();
}
#endif
-int main(int argc, char *argv[]) {
+int
+main(int argc, char *argv[]) {
#ifdef TEST
unit_tests();
return EXIT_SUCCESS;
diff --git a/src/tar.c b/src/tar.c
index cc8eaa0..8272eb4 100644
--- a/src/tar.c
+++ b/src/tar.c
@@ -30,7 +30,8 @@ About the ustar format:
*/
-static void logerr(
+static void
+logerr(
const char *const s,
const char *const msg,
const int lineno
@@ -38,7 +39,8 @@ static void logerr(
logerr_file(s, msg, __FILE__, lineno);
}
-static void logerrs(
+static void
+logerrs(
const char *const pre,
const char *const mid,
const char *const post,
@@ -48,7 +50,8 @@ static void logerrs(
logerrs_file(pre, mid, post, msg, __FILE__, lineno);
}
-static void logerrl(
+static void
+logerrl(
const char *const pre,
const size_t mid,
const char *const post,
@@ -66,8 +69,9 @@ struct TarEntry {
// static int tar_write(FILE *const fd, 
-int tarball_write_from_directory(
- FILE *const fd,
+int
+tarball_write_from_directory(
+ const FILE *const fd,
const char *const directory_path
) {
int ret = 0;
@@ -94,5 +98,6 @@ cleanup:
}
#ifdef TEST
-void unit_tests_tar(void) {}
+void
+unit_tests_tar(void) {}
#endif
diff --git a/src/tar.h b/src/tar.h
index 2f02027..ce1b1fb 100644
--- a/src/tar.h
+++ b/src/tar.h
@@ -3,13 +3,15 @@
#include <stdio.h>
-int tarball_write_from_directory(
- FILE *const fd,
+int
+tarball_write_from_directory(
+ const FILE *const fd,
const char *const directory_path
);
#ifdef TEST
-void unit_tests_tar(void);
+void
+unit_tests_tar(void);
#endif
#endif
diff --git a/src/tests-lib.c b/src/tests-lib.c
index 21e6a8f..a9d229a 100644
--- a/src/tests-lib.c
+++ b/src/tests-lib.c
@@ -7,11 +7,13 @@
#define COLOUR_GREEN "\033[0;32m"
#define COLOUR_YELLOW "\033[0;33m"
-void test_start(const char *const name) {
+void
+test_start(const char *const name) {
assert(fprintf(stderr, "%s():\n", name) > 0);
}
-void testing(const char *const message) {
+void
+testing(const char *const message) {
assert(
fprintf(
stderr,
@@ -21,6 +23,7 @@ void testing(const char *const message) {
);
}
-void test_ok(void) {
+void
+test_ok(void) {
assert(fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n") > 0);
}
diff --git a/src/tests-lib.h b/src/tests-lib.h
index fef72ef..1ceab27 100644
--- a/src/tests-lib.h
+++ b/src/tests-lib.h
@@ -1,8 +1,13 @@
#ifndef GISTATIC_TESTS_LIB_H
#define GISTATIC_TESTS_LIB_H
-void test_start(const char *const name);
-void testing(const char *const message);
-void test_ok(void);
+void
+test_start(const char *const name);
+
+void
+testing(const char *const message);
+
+void
+test_ok(void);
#endif
diff --git a/tests/c-lint.sh b/tests/c-lint.sh
index 42d185c..0fa0b01 100755
--- a/tests/c-lint.sh
+++ b/tests/c-lint.sh
@@ -1,23 +1,65 @@
#!/bin/sh
+set -eu
+
+awk '
+BEGIN {
+ ret = 0
+ msg = "function not on the start of the line:"
+}
+
+/^[a-zA-Z0-9_]+ .+\(/ {
+ if (ret == 0) {
+ print msg
+ }
+ printf "%s:%s:%s\n", FILENAME, FNR, $0
+ ret = 1
+}
+
+END {
+ exit ret
+}
+' "$@"
-RE='[a-z]+\(\) {'
-if grep -Eq "$RE" "$@"; then
- echo 'Functions with no argument without explicit "void" parameter:' >&2
- grep -En "$RE" "$@"
- exit 1
-fi
awk '
-/^[a-zA-Z0-9_]+.+{$/ && !/^static / {
- match($0, /[a-zA-Z0-9_]+\(/)
- fn_name = substr($0, RSTART, RLENGTH - 1)
+BEGIN {
+ ret = 0
+ static = 1
+ msg = "non-static function is not declared in a header:"
+}
+
+/^[a-zA-Z0-9_]+\(.*$/ && static == 0 {
+ split($0, line, /\(/)
+ fn_name = line[1]
if (fn_name != "main") {
- header = substr(FILENAME, 0, length(FILENAME) - 1) "h"
- if (system("grep -qF \"" fn_name "(\" " header)) {
- print "non-static function is not declared in a header:"
+ header = substr(FILENAME, 0, length(FILENAME) - 2) ".h"
+ if (system("grep -q ^\"" fn_name "\" \"" header "\"")) {
+ if (ret == 0) {
+ print msg
+ }
printf "%s:%s:%s\n", FILENAME, FNR, $0
- exit 1
+ ret = 1
}
}
}
+
+/^static / {
+ static = 1
+}
+
+!/^static / {
+ static = 0
+}
+
+END {
+ exit ret
+}
' "$@"
+
+
+RE='[a-z]+\(\) {'
+if grep -Eq "$RE" "$@"; then
+ echo 'Functions with no argument without explicit "void" parameter:' >&2
+ grep -En "$RE" "$@"
+ exit 1
+fi