Skip to content

Commit 3ab7a77

Browse files
committed
Significant refactor to remove use of extern
1 parent 3efde29 commit 3ab7a77

13 files changed

+330
-289
lines changed

.clang-format

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
BasedOnStyle: LLVM
22
IndentWidth: 4
33
UseTab: Never
4-
ColumnLimit: 100
5-
SpacesBeforeTrailingComments: 4
4+
ColumnLimit: 90
5+
SpacesBeforeTrailingComments: 2
66
MaxEmptyLinesToKeep: 4
77
PointerAlignment: Left
8-
ReflowComments: false
9-
SortIncludes: false
108
AllowShortIfStatementsOnASingleLine: false
119
AllowShortFunctionsOnASingleLine: false
1210
BinPackArguments: true
1311
BinPackParameters: true
1412
AlignAfterOpenBracket: Align
15-
16-
BreakBeforeBraces: Allman
13+
BreakBeforeBraces: Allman
14+
PenaltyBreakAssignment: 50

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ SRC := $(subst //,/,$(wildcard $(SRC_DIR)/*.c))
1717
OBJ := $(SRC:$(SRC_DIR)%.c=$(OBJ_DIR)/%.o)
1818
DOT := $(EXE).ltrans0.231t.optimized.dot
1919
LIBS := -lrt -lpthread
20-
CFLAGS := -Wall -Wextra -std=gnu99 -fpie -O2 -flto -gdwarf-4 -D_FORTIFY_SOURCE=2 -DVERSION=\"$(DEB_VERSION)\" -g3
20+
WARNINGS := -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wrestrict -Wnull-dereference -Wshadow -Wformat=2
21+
CFLAGS := $(WARNINGS) -std=gnu99 -fpie -O2 -flto -gdwarf-4 -g3 -D_FORTIFY_SOURCE=2 -DVERSION=\"$(DEB_VERSION)\"
2122

2223
GCC_10 := $(shell expr `cc -dumpversion | cut -f1 -d.` \>= 10)
2324
ifeq ($(GCC_10),1)
@@ -73,7 +74,7 @@ iwyu: $(SRC:%.c=%.iwyu)
7374

7475
tidy: $(SRC) $(HEADER)
7576
$(info clang-tidy: $^)
76-
@clang-tidy --format-style=file -checks=$(TIDY_CHECKS),$(TIDY_IGNORE) $^ -- $(CFLAGS)
77+
@clang-tidy --format-style=file -checks=$(TIDY_CHECKS),$(TIDY_IGNORE) $^ -- $(subst -fanalyzer,,$(CFLAGS))
7778

7879
check-format: $(SRC) $(HEADER)
7980
$(info clang-format: $^)

graphics.c

+76-53
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
#include <ctype.h> // for isalpha
2-
#include <stdbool.h> // for bool, true, false
3-
#include <stdio.h> // for fputs, stdout, printf, putchar, sscanf
4-
#include <string.h> // for memset
5-
#include <sys/ioctl.h> // for winsize
6-
#include "stats.h" // for printStats
1+
#include "stats.h" // for printStats
2+
#include <ctype.h> // for isalpha
3+
#include <stdbool.h> // for bool, true, false
4+
#include <stdio.h> // for fputs, stdout, printf, putchar, sscanf
5+
#include <string.h> // for memset
6+
#include <sys/ioctl.h> // for winsize
7+
8+
#include "graphics.h"
9+
#include "main.h"
710

8-
extern unsigned numCharacters;
9-
extern volatile struct winsize termSize;
10-
extern bool alternateBuffer;
1111

1212
static char csiCommandBuf[16] = {0};
1313
static char* pBuf = csiCommandBuf;
14-
static unsigned char currentTextFormat[8] = {0}; // This should be plenty of simultaneous styles
14+
static unsigned char currentTextFormat[8] = {
15+
0}; // This should be plenty of simultaneous styles
1516

1617

1718

@@ -32,9 +33,10 @@ void unsetTextFormat(void)
3233
}
3334

3435

35-
void returnToStartLine(bool clearText)
36+
void returnToStartLine(bool clearText, window_t* window)
3637
{
37-
unsigned numLines = (numCharacters + termSize.ws_col - 1) / termSize.ws_col;
38+
unsigned numLines = (window->numCharacters + window->termSize.ws_col - 1) /
39+
window->termSize.ws_col;
3840

3941
if (clearText)
4042
{
@@ -51,36 +53,36 @@ void returnToStartLine(bool clearText)
5153
}
5254

5355

54-
void gotoStatLine(void)
56+
void gotoStatLine(window_t* window)
5557
{
5658
// Clear screen below cursor, move to bottom of screen
57-
printf("\e[0J\e[%u;1H", termSize.ws_row + 1U);
59+
printf("\e[0J\e[%u;1H", window->termSize.ws_row + 1U);
5860
}
5961

6062

61-
void tidyStats(void)
63+
void tidyStats(window_t* window)
6264
{
6365
unsetTextFormat();
6466
fputs("\e[s", stdout);
65-
gotoStatLine();
67+
gotoStatLine(window);
6668
fputs("\e[u", stdout);
6769
setTextFormat();
6870
}
6971

70-
void clearScreen(void)
72+
void clearScreen(window_t* window)
7173
{
72-
if (alternateBuffer)
74+
if (window->alternateBuffer)
7375
{
7476
// Erase screen + saved lines
7577
fputs("\e[2J\e[3J\e[1;1H", stdout);
7678
}
7779
else
7880
{
79-
returnToStartLine(false);
81+
returnToStartLine(false, window);
8082
// Clears screen from cursor to end, switches to Alternate Screen Buffer
8183
// Erases saved lines, sets cursor to top left
8284
fputs("\e[0J\e[?1049h\e[3J\e[1;1H", stdout);
83-
alternateBuffer = true;
85+
window->alternateBuffer = true;
8486
}
8587
}
8688

@@ -93,23 +95,23 @@ static void resetTextFormat(void)
9395

9496
static void addTextFormat(char* csi_command)
9597
{
96-
unsigned int format;
98+
unsigned format;
99+
100+
if (sscanf(csi_command, "\e[%u", &format) != 1)
101+
return;
97102

98-
if (sscanf(csi_command, "\e[%u", &format) > 0)
103+
if (format == 0)
99104
{
100-
if (format == 0)
101-
{
102-
resetTextFormat();
103-
}
104-
else
105+
resetTextFormat();
106+
}
107+
else
108+
{
109+
for (unsigned i = 0; i < sizeof(currentTextFormat); i++)
105110
{
106-
for (unsigned i = 0; i < sizeof(currentTextFormat); i++)
111+
if (currentTextFormat[i] == 0)
107112
{
108-
if (currentTextFormat[i] == 0)
109-
{
110-
currentTextFormat[i] = format;
111-
break;
112-
}
113+
currentTextFormat[i] = format;
114+
break;
113115
}
114116
}
115117
}
@@ -147,14 +149,14 @@ static bool checkCsiCommand(const unsigned char inputChar, bool* escaped)
147149
{
148150
switch (inputChar)
149151
{
150-
case 'C': // Cursor forward
151-
case 'D': // Cursor back
152-
case 'G': // Cursor horizontal position
153-
case 'K': // Erase in line
154-
case 'n': // Device Status Report
152+
case 'C': // Cursor forward
153+
case 'D': // Cursor back
154+
case 'G': // Cursor horizontal position
155+
case 'K': // Erase in line
156+
case 'n': // Device Status Report
155157
validCommand = true;
156158
break;
157-
case 'm': // Text formatting
159+
case 'm': // Text formatting
158160
validCommand = true;
159161
addTextFormat(csiCommandBuf);
160162
break;
@@ -170,32 +172,35 @@ static bool checkCsiCommand(const unsigned char inputChar, bool* escaped)
170172
}
171173

172174

173-
static void checkStats(void)
175+
static void checkStats(window_t* window)
174176
{
175-
if (numCharacters > termSize.ws_col)
177+
if (window->numCharacters > window->termSize.ws_col)
176178
{
177-
if ((numCharacters % termSize.ws_col) == 0)
178-
printStats(true, true);
179+
if ((window->numCharacters % window->termSize.ws_col) == 0)
180+
printStats(true, true, window);
179181
}
180-
else if (numCharacters == termSize.ws_col)
181-
printStats(true, true);
182+
else if (window->numCharacters == window->termSize.ws_col)
183+
printStats(true, true, window);
182184
}
183185

184186

185-
void printChar(unsigned char character, bool verbose, unsigned char* inputBuffer)
187+
void printChar(unsigned char character, unsigned char* inputBuffer, options_t* options,
188+
window_t* window)
186189
{
187-
if (!verbose)
190+
if (!options->verbose)
188191
{
189-
if ((inputBuffer) && (numCharacters < 2048))
190-
*(inputBuffer + numCharacters) = character;
192+
if ((inputBuffer) && (window->numCharacters < 2048))
193+
*(inputBuffer + window->numCharacters) = character;
191194
}
192-
checkStats();
195+
checkStats(window);
193196
putchar(character);
194-
numCharacters++;
197+
198+
window->numCharacters += 1;
195199
}
196200

197201

198-
void processChar(unsigned char character, bool verbose, unsigned char* inputBuffer)
202+
void processChar(unsigned char character, unsigned char* inputBuffer, options_t* options,
203+
window_t* window)
199204
{
200205
static bool escaped;
201206
if (character == '\e')
@@ -214,6 +219,24 @@ void processChar(unsigned char character, bool verbose, unsigned char* inputBuff
214219
}
215220
else
216221
{
217-
printChar(character, verbose, inputBuffer);
222+
printChar(character, inputBuffer, options, window);
223+
}
224+
}
225+
226+
227+
void tabToSpaces(unsigned char* inputBuffer, options_t* options, window_t* window)
228+
{
229+
printChar(' ', inputBuffer, options, window);
230+
231+
if (window->numCharacters > window->termSize.ws_col)
232+
{
233+
while ((window->numCharacters - window->termSize.ws_col) % 8)
234+
printChar(' ', inputBuffer, options, window);
235+
}
236+
else
237+
{
238+
while ((window->numCharacters % 8) &&
239+
(window->numCharacters < window->termSize.ws_col))
240+
printChar(' ', inputBuffer, options, window);
218241
}
219242
}

graphics.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#include "main.h"
34
#include <stdbool.h>
5+
#include <sys/ioctl.h>
6+
#include <time.h>
47

58

69
// clang-format off
@@ -56,11 +59,17 @@
5659
#define ANSI_BG_WHITE "\e[107m"
5760
// clang-format on
5861

59-
void returnToStartLine(bool clearText);
60-
void gotoStatLine(void);
61-
void tidyStats(void);
62-
void clearScreen(void);
62+
63+
64+
65+
void returnToStartLine(bool clearText, window_t* window);
66+
void gotoStatLine(window_t* window);
67+
void tidyStats(window_t* window);
68+
void clearScreen(window_t* window);
6369
void setTextFormat(void);
6470
void unsetTextFormat(void);
65-
void processChar(unsigned char character, bool verbose, unsigned char* inputBuffer);
66-
void printChar(unsigned char character, bool verbose, unsigned char* inputBuffer);
71+
void processChar(unsigned char character, unsigned char* inputBuffer, options_t* options,
72+
window_t* window);
73+
void printChar(unsigned char character, unsigned char* inputBuffer, options_t* options,
74+
window_t* window);
75+
void tabToSpaces(unsigned char* inputBuffer, options_t* options, window_t* window);

0 commit comments

Comments
 (0)