From f5c803230809f75fce4e2e8af2796137fc1ee2bd Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Sat, 24 Apr 2021 21:20:14 -0400 Subject: Initial --- .clang-format | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lscolors.c | 104 +++++++++++++++++++++++++++++++ 2 files changed, 298 insertions(+) create mode 100644 .clang-format create mode 100644 lscolors.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..06709b7 --- /dev/null +++ b/.clang-format @@ -0,0 +1,194 @@ +# $FreeBSD$ +# Basic .clang-format +--- +BasedOnStyle: WebKit +AlignAfterOpenBracket: DontAlign +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: false +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: TopLevelDefinitions +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: WebKit +BreakBeforeTernaryOperators: false +# TODO: BreakStringLiterals can cause very strange formatting so turn it off? +BreakStringLiterals: false +# Prefer: +# some_var = function(arg1, +# arg2) +# over: +# some_var = +# function(arg1, arg2) +PenaltyBreakAssignment: 100 +# Prefer: +# some_long_function(arg1, arg2 +# arg3) +# over: +# some_long_function( +# arg1, arg2, arg3) +PenaltyBreakBeforeFirstCallParameter: 100 +CompactNamespaces: true +DerivePointerAlignment: false +DisableFormat: false +ForEachMacros: + - ARB_ARRFOREACH + - ARB_ARRFOREACH_REVWCOND + - ARB_ARRFOREACH_REVERSE + - ARB_FOREACH + - ARB_FOREACH_FROM + - ARB_FOREACH_SAFE + - ARB_FOREACH_REVERSE + - ARB_FOREACH_REVERSE_FROM + - ARB_FOREACH_REVERSE_SAFE + - CPU_FOREACH + - FOREACH_THREAD_IN_PROC + - FOREACH_PROC_IN_SYSTEM + - FOREACH_PRISON_CHILD + - FOREACH_PRISON_DESCENDANT + - FOREACH_PRISON_DESCENDANT_LOCKED + - FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL + - MNT_VNODE_FOREACH_ALL + - MNT_VNODE_FOREACH_ACTIVE + - RB_FOREACH + - RB_FOREACH_FROM + - RB_FOREACH_SAFE + - RB_FOREACH_REVERSE + - RB_FOREACH_REVERSE_FROM + - RB_FOREACH_REVERSE_SAFE + - SLIST_FOREACH + - SLIST_FOREACH_FROM + - SLIST_FOREACH_FROM_SAFE + - SLIST_FOREACH_SAFE + - SLIST_FOREACH_PREVPTR + - SPLAY_FOREACH + - LIST_FOREACH + - LIST_FOREACH_FROM + - LIST_FOREACH_FROM_SAFE + - LIST_FOREACH_SAFE + - STAILQ_FOREACH + - STAILQ_FOREACH_FROM + - STAILQ_FOREACH_FROM_SAFE + - STAILQ_FOREACH_SAFE + - TAILQ_FOREACH + - TAILQ_FOREACH_FROM + - TAILQ_FOREACH_FROM_SAFE + - TAILQ_FOREACH_REVERSE + - TAILQ_FOREACH_REVERSE_FROM + - TAILQ_FOREACH_REVERSE_FROM_SAFE + - TAILQ_FOREACH_REVERSE_SAFE + - TAILQ_FOREACH_SAFE + - VM_MAP_ENTRY_FOREACH + - VM_PAGE_DUMP_FOREACH +IndentCaseLabels: false +IndentPPDirectives: None +Language: Cpp +NamespaceIndentation: None +PointerAlignment: Right +ContinuationIndentWidth: 4 +IndentWidth: 8 +TabWidth: 8 +ColumnLimit: 80 +UseTab: Always +SpaceAfterCStyleCast: false +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^\"opt_.*\.h\"' + Priority: 1 + SortPriority: 10 + - Regex: '^' + Priority: 2 + SortPriority: 20 + - Regex: '^' + Priority: 2 + SortPriority: 21 + - Regex: '^' + Priority: 2 + SortPriority: 22 + - Regex: '^' + Priority: 2 + SortPriority: 23 + - Regex: '^' + Priority: 3 + SortPriority: 30 + - Regex: '^ +#include +#include +#include +#include +#include + +const char *types[11] = { "di", "ln", "so", "pi", "ex", "bd", "cd", "su", "sg", + "tw", "ow" }; +const char col[8] = "abcdefgh"; + +int tolscolors(char *); +int tols_colors(char *); +void usage(void); +char numtocol(char, bool); + +int +main(int argc, char **argv) +{ + if (argc != 2) + usage(); + + if (strchr(argv[1], '=')) + return tolscolors(argv[1]); + else + return tols_colors(argv[1]); + + /* NOTREACHED */ + return 0; +} + +int +tolscolors(char *dircolor) +{ + char *color, *ent, *buf, *val, *val2; + char out[22] = "xxxxxxxxxxxxxxxxxxxxxx"; + bool bold = false; + + while ((ent = buf = strsep(&dircolor, ":")) != NULL) { + for (int i = 0; i < 11; i++) { + if (strncmp(ent, types[i], strlen(types[i])) == 0) { + bold = false; + while ((val = strsep(&buf, "=")) != NULL) { + while ((val2 = strsep(&val, ";")) != + NULL) { + if (strcmp(val2, "01") == 0) { + bold = true; + } else if (val2[0] == '3') { + out[2 * i] = numtocol( + val2[1], bold); + } else if (val2[0] == '4') { + out[2 * i + 1] = + numtocol( + val2[1], NULL); + } + } + } + } + } + } + printf("%s\n", out); + return 0; +} + +char +numtocol(char c, bool bold) +{ + char buf = col[strtol(&c, NULL, 10)]; + if (bold) + return toupper(buf); + else + return buf; +} + +int +tols_colors(char *lscolors) +{ + char *ls_out; + strcpy(ls_out, ""); + + for (int i = 0; i < 11; i++) { + if (lscolors[2 * i] == 'x' && lscolors[2 * i + 1] == 'x') + continue; + sprintf(ls_out + strlen(ls_out), "%s=", types[i]); + if (isupper(lscolors[2 * i])) + sprintf(ls_out + strlen(ls_out), "01;"); + if (tolower(lscolors[2 * i]) == 'x') + sprintf(ls_out + strlen(ls_out), "00"); + else if (tolower(lscolors[2 * i] != 'x')) + sprintf(ls_out + strlen(ls_out), "3%i", (int)(strchr(col, tolower(lscolors[2 * i])) - col)); + if (tolower(lscolors[2 * i + 1]) != 'x') + sprintf(ls_out + strlen(ls_out), ";4%i", (int)(strchr(col, tolower(lscolors[2 * i])) - col)); + sprintf(ls_out + strlen(ls_out), ":"); + } + printf("%s\n", ls_out); + return 0; +} + +void +usage(void) +{ + (void)fprintf(stderr, "%s\n", "usage: lscolors format_code "); + exit(EX_USAGE); +} -- cgit v1.2.3-56-ge451