diff --git a/.clang-format b/.clang-format index 5ca25b8..67c9a1b 100644 --- a/.clang-format +++ b/.clang-format @@ -1,65 +1,19 @@ ---- -Language: Cpp -# BasedOnStyle: LLVM -AccessModifierOffset: -2 -AlignAfterOpenBracket: true -AlignEscapedNewlinesLeft: false -AlignOperands: true -AlignTrailingComments: false -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: false +AlignAfterOpenBracket: DontAlign +AlignEscapedNewlines: DontAlign +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: true AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false -AllowShortFunctionsOnASingleLine: None -AlwaysBreakAfterDefinitionReturnType: true -AlwaysBreakTemplateDeclarations: false -AlwaysBreakBeforeMultilineStrings: false -BreakBeforeBinaryOperators: None -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BinPackParameters: true -BinPackArguments: true -ColumnLimit: 0 -ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 4 -DerivePointerAlignment: false -ExperimentalAutoDetectBinPacking: false +AlwaysBreakAfterDefinitionReturnType: TopLevel +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: WebKit IndentCaseLabels: false -IndentWrappedFunctionNames: false -IndentFunctionDeclarationAfterType: false -MaxEmptyLinesToKeep: 1 -KeepEmptyLinesAtTheStartOfBlocks: true -NamespaceIndentation: None -ObjCBlockIndentWidth: 2 -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakString: 1000 -PenaltyBreakFirstLessLess: 120 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 60 -PointerAlignment: Left -SpacesBeforeTrailingComments: 1 -Cpp11BracedListStyle: true -Standard: Cpp11 -IndentWidth: 8 -TabWidth: 8 -UseTab: ForIndentation -BreakBeforeBraces: Linux -SortIncludes: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -SpacesInAngles: false -SpaceInEmptyParentheses: false -SpacesInCStyleCastParentheses: false -SpaceAfterCStyleCast: false -SpacesInContainerLiterals: true -SpaceBeforeAssignmentOperators: true +TabWidth: 4 +IndentWidth: 4 ContinuationIndentWidth: 4 -CommentPragmas: '^ IWYU pragma:' -ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] -SpaceBeforeParens: Never -DisableFormat: false -... +UseTab: ForContinuationAndIndentation +ColumnLimit: 0 +ReflowComments: false +SortIncludes: false +SpaceBeforeParens: false \ No newline at end of file diff --git a/build.sh b/build.sh index 46c5b3d..b3f5450 100755 --- a/build.sh +++ b/build.sh @@ -1,9 +1,11 @@ #!/bin/bash -rm ./parade - +# Lint clang-format -i parade.c +# Cleanup +rm -f ./parade + # Linux cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -o parade parade.c diff --git a/parade.c b/parade.c index da568f1..72aeff4 100644 --- a/parade.c +++ b/parade.c @@ -6,8 +6,8 @@ typedef struct Vessel { char id; - struct Vessel* owner; - struct Vessel* parent; + struct Vessel *owner; + struct Vessel *parent; char name[NAMELEN]; char note[TEXTLEN]; char prog[TEXTLEN]; @@ -18,12 +18,10 @@ typedef struct Parade { int len; } Parade; -char* actions[12] = { - "create", "become", "enter", "leave", - "take", "drop", "warp", "transform", - "note", "program", "use", ""}; +char *actions[12] = { + "create", "become", "enter", "leave", "take", "drop", "warp", "transform", "note", "program", "use", ""}; -Vessel* guest; +Vessel *guest; /* helpers */ @@ -50,16 +48,16 @@ chex(char c) } int -slen(char* s) +slen(char *s) { int n = 0; - while(s[n] != '\0' && s[++n]) + while(s[n] && s[++n]) ; return n; } unsigned char -shex(char* s, int len) +shex(char *s, int len) { int i, n = 0; for(i = 0; i < len; ++i) @@ -68,17 +66,17 @@ shex(char* s, int len) } int -cpos(char* s, char c) +cpos(char *s, char c) { - int i; - for(i = 0; i < slen(s); i++) - if(s[i] == c) - return i; + int i = 0; + while(s[i] && s[i]) + if(s[i++] == c) + return i - 1; return -1; } -char* -spor(char* s, int c) +char * +spor(char *s, int c) { int i; for(i = slen(s); i >= 0; --i) @@ -88,21 +86,19 @@ spor(char* s, int c) } int -scmp(char* a, char* b) +scmp(char *a, char *b) { - int i, l = slen(a); - if(l != slen(b)) - return 0; - for(i = 0; i < l; ++i) - if(a[i] != b[i]) - return 0; - return 1; + int i = 0; + while(a[i] == b[i]) + if(!a[i++]) + return 1; + return 0; } -char* -strm(char* s) +char * +strm(char *s) { - char* end; + char *end; while(cisp(*s)) s++; if(*s == 0) @@ -115,7 +111,7 @@ strm(char* s) } int -afnd(char* src[], int len, char* val) +afnd(char *src[], int len, char *val) { int i; for(i = 0; i < len; i++) @@ -124,11 +120,11 @@ afnd(char* src[], int len, char* val) return -1; } -char* -sstr(char* src, char* dest, int from, int to) +char * +sstr(char *src, char *dest, int from, int to) { int i; - char *a = (char*)src + from, *b = (char*)dest; + char *a = (char *)src + from, *b = (char *)dest; for(i = 0; i < to; i++) b[i] = a[i]; dest[to] = '\0'; @@ -138,7 +134,7 @@ sstr(char* src, char* dest, int from, int to) /* Parade */ int -isvisible(Vessel* g, Vessel* v) +isvisible(Vessel *g, Vessel *v) { if(g->parent != v->parent) return 0; @@ -150,13 +146,13 @@ isvisible(Vessel* g, Vessel* v) } int -isparadox(Vessel* v) +isparadox(Vessel *v) { return v->parent == v; } char -rune(Vessel* v) +rune(Vessel *v) { if(isparadox(v)) return '^'; @@ -169,10 +165,10 @@ rune(Vessel* v) return 0; } -Vessel* -addvessel(Parade* p, Vessel* v, char* name) +Vessel * +addvessel(Parade *p, Vessel *v, char *name) { - Vessel* nv = &p->vessels[p->len]; + Vessel *nv = &p->vessels[p->len]; nv->id = p->len; nv->owner = v ? v : nv; nv->parent = v ? v->parent : nv; @@ -181,11 +177,11 @@ addvessel(Parade* p, Vessel* v, char* name) return nv; } -Vessel* -findvisible(Parade* p, Vessel* v, char* name) +Vessel * +findvisible(Parade *p, Vessel *v, char *name) { int i; - char* n = spor(name, ' ') + 1; + char *n = spor(name, ' ') + 1; for(i = 0; i < p->len; ++i) { if(!isvisible(v, &p->vessels[i])) continue; @@ -195,11 +191,11 @@ findvisible(Parade* p, Vessel* v, char* name) return NULL; } -Vessel* -findinventory(Parade* p, Vessel* v, char* name) +Vessel * +findinventory(Parade *p, Vessel *v, char *name) { int i; - char* n = spor(name, ' ') + 1; + char *n = spor(name, ' ') + 1; for(i = 0; i < p->len; ++i) { if(&p->vessels[i] == v) continue; @@ -212,11 +208,11 @@ findinventory(Parade* p, Vessel* v, char* name) return NULL; } -Vessel* -findany(Parade* p, char* name) +Vessel * +findany(Parade *p, char *name) { int i; - char* n = spor(name, ' ') + 1; + char *n = spor(name, ' ') + 1; for(i = 0; i < p->len; ++i) if(scmp(p->vessels[i].name, n)) return &p->vessels[i]; @@ -226,9 +222,9 @@ findany(Parade* p, char* name) /* Actions */ void -createvessel(Parade* p, char* val) +createvessel(Parade *p, char *val) { - Vessel* v; + Vessel *v; if(findany(p, val) || slen(val) < 4 || p->len > 255) printf("You cannot create the %s.\n", val); else { @@ -238,9 +234,9 @@ createvessel(Parade* p, char* val) } void -becomevessel(Parade* p, char* val) +becomevessel(Parade *p, char *val) { - Vessel* v = findany(p, val); + Vessel *v = findany(p, val); if(!v) printf("You do not see the %s.\n", val); else { @@ -250,9 +246,9 @@ becomevessel(Parade* p, char* val) } void -entervessel(Parade* p, char* val) +entervessel(Parade *p, char *val) { - Vessel* v = findvisible(p, guest, val); + Vessel *v = findvisible(p, guest, val); if(!v) printf("You do not see the %s.\n", val); else { @@ -264,7 +260,7 @@ entervessel(Parade* p, char* val) void leavevessel(void) { - Vessel* v = guest->parent; + Vessel *v = guest->parent; if(v == v->parent) printf("You cannot leave the %s%c.\n", v->name, rune(v)); else { @@ -274,9 +270,9 @@ leavevessel(void) } void -takevessel(Parade* p, char* val) +takevessel(Parade *p, char *val) { - Vessel* v = findvisible(p, guest, val); + Vessel *v = findvisible(p, guest, val); if(!v) printf("You do not see the %s.\n", val); else { @@ -286,9 +282,9 @@ takevessel(Parade* p, char* val) } void -dropvessel(Parade* p, char* val) +dropvessel(Parade *p, char *val) { - Vessel* v = findinventory(p, guest, val); + Vessel *v = findinventory(p, guest, val); if(!v) printf("You do not carry the %s.\n", val); else { @@ -298,9 +294,9 @@ dropvessel(Parade* p, char* val) } void -warpvessel(Parade* p, char* val) +warpvessel(Parade *p, char *val) { - Vessel* v = findany(p, val); + Vessel *v = findany(p, val); if(!v) printf("You cannot warp to the %s.\n", val); else { @@ -310,9 +306,9 @@ warpvessel(Parade* p, char* val) } void -transformvessel(Parade* p, char* val) +transformvessel(Parade *p, char *val) { - char* name = spor(val, ' ') + 1; + char *name = spor(val, ' ') + 1; if(findany(p, name) || slen(name) < 3) printf("You cannot transform into the %s.\n", name); else { @@ -322,9 +318,9 @@ transformvessel(Parade* p, char* val) } void -notevessel(char* val) +notevessel(char *val) { - Vessel* v = guest->parent; + Vessel *v = guest->parent; if(slen(val) < 1) printf("You remove the note of the %s%c.\n", v->name, rune(v)); else { @@ -334,9 +330,9 @@ notevessel(char* val) } void -programvessel(char* val) +programvessel(char *val) { - Vessel* v = guest->parent; + Vessel *v = guest->parent; if(slen(val) < 1) printf("You remove the program of the %s%c.\n", v->name, rune(v)); else { @@ -346,15 +342,17 @@ programvessel(char* val) } void -lookvessel(Parade* p) +lookvessel(Parade *p) { int i; if(isparadox(guest)) printf("You are the %s%c.\n", guest->name, rune(guest)); else printf("You are the %s%c in the %s%c.\n", - guest->name, rune(guest), - guest->parent->name, rune(guest->parent)); + guest->name, + rune(guest), + guest->parent->name, + rune(guest->parent)); if(slen(guest->parent->note) > 2) printf("%s\n", guest->parent->note); for(i = 0; i < p->len; ++i) @@ -364,10 +362,10 @@ lookvessel(Parade* p) /* Parade */ -int usevessel(Parade* p, char* val); +int usevessel(Parade *p, char *val); void -act(Parade* p, char* cmd, char* val) +act(Parade *p, char *cmd, char *val) { switch(afnd(actions, 12, cmd)) { case 0x0: @@ -412,8 +410,8 @@ act(Parade* p, char* cmd, char* val) } } -Vessel* -spawn(Parade* p) +Vessel * +spawn(Parade *p) { addvessel(p, NULL, "library"); addvessel(p, &p->vessels[0], "ghost"); @@ -421,11 +419,11 @@ spawn(Parade* p) } int -parse(Parade* p, char* line, int id) +parse(Parade *p, char *line, int id) { int split = cpos(line, '|'); int len = slen(line); - Vessel* nv = &p->vessels[id]; + Vessel *nv = &p->vessels[id]; if(len < 22 || split < 0 || line[0] == ';') return 0; nv->id = id; @@ -440,26 +438,21 @@ parse(Parade* p, char* line, int id) } int -save(Parade* p, char* filename) +save(Parade *p, char *filename) { int i; - FILE* f = fopen(filename, "w"); + FILE *f = fopen(filename, "w"); for(i = 0; i < p->len; ++i) - fprintf(f, "%02x%02x %-15s %s | %s\n", - p->vessels[i].owner->id, - p->vessels[i].parent->id, - p->vessels[i].name, - p->vessels[i].note, - p->vessels[i].prog); + fprintf(f, "%02x%02x %-15s %s | %s\n", p->vessels[i].owner->id, p->vessels[i].parent->id, p->vessels[i].name, p->vessels[i].note, p->vessels[i].prog); fclose(f); return 1; } int -load(Parade* p, char* filename) +load(Parade *p, char *filename) { char line[BUFLEN]; - FILE* f = fopen(filename, "r"); + FILE *f = fopen(filename, "r"); if(f == NULL) return 1; p->len = 0; @@ -471,7 +464,7 @@ load(Parade* p, char* filename) } int -answer(Parade* p, char* input) +answer(Parade *p, char *input) { int split = cpos(input, ' '); char action[NAMELEN], value[TEXTLEN]; @@ -479,14 +472,12 @@ answer(Parade* p, char* input) return 1; if(split >= 0) { sstr(input, action, 0, split); - sstr(input, value, split + 1, - imin(slen(input) - split, TEXTLEN - 1)); + sstr(input, value, split + 1, imin(slen(input) - split, TEXTLEN - 1)); } else if(slen(input) < 2) { action[0] = '\0'; value[0] = '\0'; } else if(slen(input) >= 0) { - sstr(input, action, 0, - imin(slen(input), NAMELEN - 1)); + sstr(input, action, 0, imin(slen(input), NAMELEN - 1)); value[0] = '\0'; } if(scmp(action, "@quit")) @@ -500,9 +491,9 @@ answer(Parade* p, char* input) } int -usevessel(Parade* p, char* val) +usevessel(Parade *p, char *val) { - Vessel* v = findvisible(p, guest, val); + Vessel *v = findvisible(p, guest, val); if(!v) printf("You do not see %s.\n", val); else if(slen(v->prog) < 2) @@ -513,7 +504,7 @@ usevessel(Parade* p, char* val) } int -listen(Parade* p) +listen(Parade *p) { char input[TEXTLEN]; printf("> "); @@ -523,7 +514,7 @@ listen(Parade* p) } int -main(int argc, char** argv) +main(int argc, char **argv) { Parade parade; parade.len = 0; @@ -531,8 +522,10 @@ main(int argc, char** argv) if(argc == 2) load(¶de, argv[1]); printf("A %s%c appeared in the %s%c.\n", - guest->name, rune(guest), - guest->parent->name, rune(guest->parent)); + guest->name, + rune(guest), + guest->parent->name, + rune(guest->parent)); while(listen(¶de)) ; return 0;