Skip to content

Commit

Permalink
Enforce C style (via .clang-format) (see #51)
Browse files Browse the repository at this point in the history
* Unix endings (LF, EOF) and UTF-8 only
* space-only indentation
* braces around single-line statements (always)
* #includes sorted
  • Loading branch information
vhotspur authored Jan 9, 2024
2 parents e3ed86a + f984961 commit 09a423f
Show file tree
Hide file tree
Showing 327 changed files with 18,545 additions and 17,719 deletions.
34 changes: 34 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
Language: Cpp
BasedOnStyle: WebKit
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
AlignEscapedNewlines: DontAlign
ContinuationIndentWidth: 8
MaxEmptyLinesToKeep: 1
SpaceAfterCStyleCast: true
PointerAlignment: Right

# Include files ordering:
# - first put standard includes, place non-nested first (this should
# move library stuff at the end of < > includes)
# - then place local includes
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<.*/'
Priority: -1
SortPriority: -1
CaseSensitive: true
- Regex: '^<'
Priority: -1
SortPriority: -2
CaseSensitive: true
- Regex: '^"'
Priority: 0
CaseSensitive: true

LineEnding: LF
InsertNewlineAtEOF: true
InsertBraces: true
...
15 changes: 15 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Clang format linter
on:
push: {}
pull_request: {}

jobs:
clang-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Check C source formatting with clang-format
uses: DoozyX/[email protected]
with:
clangFormatVersion: 16
13 changes: 13 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: File style
on:
push: {}
pull_request: {}

jobs:
check-encoding:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Check correct encoding etc.
run: ./contrib/check-encoding.sh
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* setup C style (via `.clang-format`) (@vhotspur)
* CI checks C style as well as file encoding and line-endings (@vhotspur)

### Changed

### Deprecated
Expand Down
7 changes: 5 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bindir = @bindir@

BINARY = msim

.PHONY: all install uninstall clean distclean rvtest
.PHONY: all install uninstall clean distclean rvtest cstyle

all:
$(MAKE) -C src
Expand All @@ -33,7 +33,7 @@ distclean: clean
$(MAKE) -C src distclean
$(MAKE) -C tests/rvtests/unit-tests distclean
$(RM) -f Makefile config.log config.status config.h stamp-h

test:
make -C . rvtest
bats tests/system/
Expand All @@ -44,3 +44,6 @@ rvtest:
@echo "\nSystem tests:"
cd tests/rvtests ; python3 run_tests.py
@echo "\n All Tests Passed!"

cstyle:
find src/ tests/ -name '*.[ch]' -exec clang-format -style=file -i {} \;
42 changes: 42 additions & 0 deletions contrib/check-encoding.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Checks that text files are in proper encoding.

set -ueo pipefail

find_sources_0() {
find -type f -and '-(' -name '*.[ch]' -or -name '*.in' -or -name '*.sh' '-)' -print0
}

find_crlf_files() {
find_sources_0 | xargs -0 grep -lUP '\r$'
}

print_non_utf8_files() {
local i
local mime
local rc=0
for i in "$@"; do
mime="$( file --mime-encoding -b "$i" )"
case "$mime" in
us-ascii|utf-8) ;;
*) echo "Error: $i is in bad encoding ($mime)." >&2 ; rc=1 ;;
esac
done
return "$rc"
}
export -f print_non_utf8_files

find_non_utf8_files() {
find_sources_0 | xargs -0 bash -c 'print_non_utf8_files "$@"'
}

echo "Checking line endings..." >&2
if [ "$( find_crlf_files | wc -l )" -ne 0 ]; then
echo "Error: following file(s) have CR-LF line endings." >&2
find_crlf_files >&2
exit 1
fi

echo "Checking file encodings..." >&2
find_non_utf8_files
2 changes: 1 addition & 1 deletion src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ $(DEPEND):
-[ -f $(DEPEND_PREV) ] && $(DIFF) -q $(DEPEND_PREV) $@ && $(MV) -f $(DEPEND_PREV) $@

rvtest: all
$(MAKE) -C ../tests/rvtests/unit-tests test
$(MAKE) -C ../tests/rvtests/unit-tests test
32 changes: 16 additions & 16 deletions src/arch/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@
*
*/

#define tcgetattr(fd, termios) 0
#define tcsetattr(fd, optional_actions, termios) 0
#define tcgetattr(fd, termios) 0
#define tcsetattr(fd, optional_actions, termios) 0

#define VTIME 5
#define VMIN 6
#define VTIME 5
#define VMIN 6

#define TCSANOW 0
#define TCSANOW 0

#define INLCR 0000100
#define ICANON 0000002
#define INLCR 0000100
#define ICANON 0000002

#define ECHOE 0000020
#define ECHO 0000010
#define ECHOE 0000020
#define ECHO 0000010

#define ONLCR 0000004
#define ONLCR 0000004

#define NCCS 32
#define NCCS 32

typedef unsigned char cc_t;
typedef unsigned int tcflag_t;

struct termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_cc[NCCS];
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_cc[NCCS];
};

#else
Expand Down
16 changes: 8 additions & 8 deletions src/arch/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
#include <stddef.h>
#include <unistd.h>

#define MAP_FAILED ((void *) -1)
#define MAP_FAILED ((void *) -1)

#define PROT_READ 0x01
#define PROT_WRITE 0x02
#define PROT_EXEC 0x04
#define PROT_NONE 0x00
#define PROT_READ 0x01
#define PROT_WRITE 0x02
#define PROT_EXEC 0x04
#define PROT_NONE 0x00

#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02
#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02

extern void *mmap(void *addr, size_t length, int prot, int flags, int fd,
off_t offset);
off_t offset);
extern int munmap(void *addr, size_t length);

#else
Expand Down
2 changes: 1 addition & 1 deletion src/arch/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#else

#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/socket.h>

#endif /* __WIN32__ */

Expand Down
64 changes: 33 additions & 31 deletions src/arch/posix/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,55 @@

#ifndef __WIN32__

#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

#include "../../fault.h"
#include "../../input.h"
#include "../../main.h"

static void machine_user_break(int signo)
{
if ((machine_break) || (machine_interactive)) {
printf("\n");
alert("Quit");
input_back();
exit(ERR_OK);
}

machine_break = true;

if (!machine_interactive)
machine_newline = true;

machine_interactive = true;
if ((machine_break) || (machine_interactive)) {
printf("\n");
alert("Quit");
input_back();
exit(ERR_OK);
}

machine_break = true;

if (!machine_interactive) {
machine_newline = true;
}

machine_interactive = true;
}

static void termination_signals_handler(int signo)
{
printf("\n");
alert("Caught signal %d, terminating.", signo);
input_back();
exit(ERR_OK);
printf("\n");
alert("Caught signal %d, terminating.", signo);
input_back();
exit(ERR_OK);
}

void register_signal_handlers(void)
{
struct sigaction act;
act.sa_handler = machine_user_break;
(void) sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL);

struct sigaction act_term;
act_term.sa_handler = termination_signals_handler;
(void) sigemptyset(&act_term.sa_mask);
act_term.sa_flags = 0;
sigaction(SIGTERM, &act_term, NULL);
sigaction(SIGQUIT, &act_term, NULL);
struct sigaction act;

act.sa_handler = machine_user_break;
(void) sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL);

struct sigaction act_term;
act_term.sa_handler = termination_signals_handler;
(void) sigemptyset(&act_term.sa_mask);
act_term.sa_flags = 0;
sigaction(SIGTERM, &act_term, NULL);
sigaction(SIGQUIT, &act_term, NULL);
}

#endif /* !__WIN32__ */
42 changes: 21 additions & 21 deletions src/arch/posix/stdin.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@

bool stdin_poll(char *key)
{
/* Check new character */
fd_set rfds;
/* Watch stdin */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
int retval = select(1, &rfds, NULL, NULL, &tv);
if (retval == 1) {
/* There is a new character */
if(read(0, key, 1) > 0){
return true;
}
}
return false;
/* Check new character */
fd_set rfds;

/* Watch stdin */
FD_ZERO(&rfds);
FD_SET(0, &rfds);

struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;

int retval = select(1, &rfds, NULL, NULL, &tv);

if (retval == 1) {
/* There is a new character */
if (read(0, key, 1) > 0) {
return true;
}
}

return false;
}

#endif /* !__WIN32__ */
Loading

0 comments on commit 09a423f

Please sign in to comment.