From ede12d99fc46ca3fd638d8b47f498497ef28b3df Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 27 Aug 2021 22:44:19 -0300 Subject: 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. --- src/lib.c | 167 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 114 insertions(+), 53 deletions(-) (limited to 'src/lib.c') 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 = "" "\n" "\n" " \n" @@ -186,7 +197,8 @@ static const char *const LOGO_STR = "" " \n" "\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 = "" PROGNAME "."; -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(); -- cgit v1.2.3