├── .gitignore ├── LICENSE.md ├── Makefile ├── README.md ├── cansid.c ├── cansid.h ├── minunit.h └── tests.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | tests 3 | 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 64 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all test clean 2 | 3 | all: cansid.o 4 | 5 | clean: 6 | rm -vf *.o 7 | rm -f tests 8 | 9 | test: tests 10 | ./tests 11 | 12 | cansid.o: cansid.c cansid.h 13 | $(CC) -c cansid.c -o cansid.o $(CFLAGS) 14 | 15 | test.o: tests.c cansid.h minunit.h 16 | $(CC) -c tests.c -o test.o $(CFLAGS) 17 | 18 | tests: test.o cansid.o 19 | $(CC) -o tests test.o cansid.o $(LDFLAGS) 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CANSID (C ANSI Driver) 2 | 3 | This repository contains a simple C ANSI escape sequence parser. It is intended for use in my own hobby operating system, but can be adapted for different uses. 4 | 5 | ## Usage 6 | 7 | First, call the function `cansid_init(void)` which returns a `struct cansid_state`: 8 | ```c 9 | struct cansid_state state = cansid_init(); 10 | ``` 11 | Then, whenever you receive a char that you want to run through the parser, hand it to `cansid_process(struct cansid_state *, char)`. This returns a `struct color_char`. 12 | ```c 13 | char c = 'x'; // Whatever you want to parse 14 | struct color_char ch = cansid_process(&state, c); 15 | ``` 16 | The returned struct indicates how you should print the character. It contains two fields: `style`, and `ascii`. The `style` field is arranged in [this format](http://wiki.osdev.org/Text_UI#Colours). The `ascii` field is simply the character which should be printed. If `ascii` is the NUL byte (i.e `0x00` or `\0`), then the character should not be outputted to the screen (and therefore the `style` field should be ignored too). 17 | 18 | ## Building 19 | 20 | To add CANSID to your repository, simply place the files `cansid.c` and `cansid.h` in the appropriate locations. There are no dependencies and can even be compiled in a freestanding environment. 21 | 22 | ## Tests 23 | 24 | Running tests can be done with `make test`. 25 | 26 | ## Contributing 27 | 28 | Feel free to open an issue or a pull request if you would like to contribute or ask a question. 29 | -------------------------------------------------------------------------------- /cansid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cansid.h" 5 | 6 | #define ESC '\x1B' 7 | 8 | struct cansid_state cansid_init(void) 9 | { 10 | struct cansid_state rv = { 11 | .state = CANSID_ESC, 12 | .style = 0x0F, 13 | .next_style = 0x0F 14 | }; 15 | return rv; 16 | } 17 | 18 | static inline unsigned char cansid_convert_color(unsigned char color) 19 | { 20 | const unsigned char lookup_table[8] = { 0, 4, 2, 6, 1, 5, 3, 7 }; 21 | return lookup_table[(int)color]; 22 | } 23 | 24 | struct color_char cansid_process(struct cansid_state *state, char x) 25 | { 26 | struct color_char rv = { 27 | .style = state->style, 28 | .ascii = '\0' 29 | }; 30 | switch (state->state) { 31 | case CANSID_ESC: 32 | if (x == ESC) 33 | state->state = CANSID_BRACKET; 34 | else { 35 | rv.ascii = x; 36 | } 37 | break; 38 | case CANSID_BRACKET: 39 | if (x == '[') 40 | state->state = CANSID_PARSE; 41 | else { 42 | state->state = CANSID_ESC; 43 | rv.ascii = x; 44 | } 45 | break; 46 | case CANSID_PARSE: 47 | if (x == '3') { 48 | state->state = CANSID_FGCOLOR; 49 | } else if (x == '4') { 50 | state->state = CANSID_BGCOLOR; 51 | } else if (x == '0') { 52 | state->state = CANSID_ENDVAL; 53 | state->next_style = 0x0F; 54 | } else if (x == '1') { 55 | state->state = CANSID_ENDVAL; 56 | state->next_style |= (1 << 3); 57 | } else if (x == '=') { 58 | state->state = CANSID_EQUALS; 59 | } else { 60 | state->state = CANSID_ESC; 61 | state->next_style = state->style; 62 | rv.ascii = x; 63 | } 64 | break; 65 | case CANSID_BGCOLOR: 66 | if (x >= '0' && x <= '7') { 67 | state->state = CANSID_ENDVAL; 68 | state->next_style &= 0x1F; 69 | state->next_style |= cansid_convert_color(x - '0') << 4; 70 | } else { 71 | state->state = CANSID_ESC; 72 | state->next_style = state->style; 73 | rv.ascii = x; 74 | } 75 | break; 76 | case CANSID_FGCOLOR: 77 | if (x >= '0' && x <= '7') { 78 | state->state = CANSID_ENDVAL; 79 | state->next_style &= 0xF8; 80 | state->next_style |= cansid_convert_color(x - '0'); 81 | } else { 82 | state->state = CANSID_ESC; 83 | state->next_style = state->style; 84 | rv.ascii = x; 85 | } 86 | break; 87 | case CANSID_EQUALS: 88 | if (x == '1') { 89 | state->state = CANSID_ENDVAL; 90 | state->next_style &= ~(1 << 3); 91 | } else { 92 | state->state = CANSID_ESC; 93 | state->next_style = state->style; 94 | rv.ascii = x; 95 | } 96 | break; 97 | case CANSID_ENDVAL: 98 | if (x == ';') { 99 | state->state = CANSID_PARSE; 100 | } else if (x == 'm') { 101 | // Finish and apply styles 102 | state->state = CANSID_ESC; 103 | state->style = state->next_style; 104 | } else { 105 | state->state = CANSID_ESC; 106 | state->next_style = state->style; 107 | rv.ascii = x; 108 | } 109 | break; 110 | default: 111 | break; 112 | } 113 | return rv; 114 | } 115 | -------------------------------------------------------------------------------- /cansid.h: -------------------------------------------------------------------------------- 1 | #ifndef CANSID_H 2 | #define CANSID_H 3 | 4 | struct cansid_state { 5 | enum { 6 | CANSID_ESC, 7 | CANSID_BRACKET, 8 | CANSID_PARSE, 9 | CANSID_BGCOLOR, 10 | CANSID_FGCOLOR, 11 | CANSID_EQUALS, 12 | CANSID_ENDVAL, 13 | } state; 14 | unsigned char style; 15 | unsigned char next_style; 16 | }; 17 | 18 | struct color_char { 19 | unsigned char style; 20 | unsigned char ascii; 21 | }; 22 | 23 | struct cansid_state cansid_init(void); 24 | struct color_char cansid_process(struct cansid_state *state, char x); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /minunit.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 David Siñuela Pastor, siu.4coders@gmail.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | #ifndef __MINUNIT_H__ 24 | #define __MINUNIT_H__ 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | #if defined(_WIN32) 31 | #include 32 | #if defined(_MSC_VER) && _MSC_VER < 1900 33 | #define snprintf _snprintf 34 | #define __func__ __FUNCTION__ 35 | #endif 36 | 37 | #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) 38 | 39 | /* Change POSIX C SOURCE version for pure c99 compilers */ 40 | #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L 41 | #undef _POSIX_C_SOURCE 42 | #define _POSIX_C_SOURCE 200112L 43 | #endif 44 | 45 | #include /* POSIX flags */ 46 | #include /* clock_gettime(), time() */ 47 | #include /* gethrtime(), gettimeofday() */ 48 | #include 49 | #include 50 | #include 51 | 52 | #if defined(__MACH__) && defined(__APPLE__) 53 | #include 54 | #include 55 | #endif 56 | 57 | #else 58 | #error "Unable to define timers for an unknown OS." 59 | #endif 60 | 61 | #include 62 | #include 63 | 64 | /* Maximum length of last message */ 65 | #define MINUNIT_MESSAGE_LEN 1024 66 | /* Accuracy with which floats are compared */ 67 | #define MINUNIT_EPSILON 1E-12 68 | 69 | /* Misc. counters */ 70 | static int minunit_run = 0; 71 | static int minunit_assert = 0; 72 | static int minunit_fail = 0; 73 | static int minunit_status = 0; 74 | 75 | /* Timers */ 76 | static double minunit_real_timer = 0; 77 | static double minunit_proc_timer = 0; 78 | 79 | /* Last message */ 80 | static char minunit_last_message[MINUNIT_MESSAGE_LEN]; 81 | 82 | /* Test setup and teardown function pointers */ 83 | static void (*minunit_setup)(void) = NULL; 84 | static void (*minunit_teardown)(void) = NULL; 85 | 86 | /* Definitions */ 87 | #define MU_TEST(method_name) static void method_name(void) 88 | #define MU_TEST_SUITE(suite_name) static void suite_name(void) 89 | 90 | #define MU__SAFE_BLOCK(block) do {\ 91 | block\ 92 | } while(0) 93 | 94 | /* Run test suite and unset setup and teardown functions */ 95 | #define MU_RUN_SUITE(suite_name) MU__SAFE_BLOCK(\ 96 | suite_name();\ 97 | minunit_setup = NULL;\ 98 | minunit_teardown = NULL;\ 99 | ) 100 | 101 | /* Configure setup and teardown functions */ 102 | #define MU_SUITE_CONFIGURE(setup_fun, teardown_fun) MU__SAFE_BLOCK(\ 103 | minunit_setup = setup_fun;\ 104 | minunit_teardown = teardown_fun;\ 105 | ) 106 | 107 | /* Test runner */ 108 | #define MU_RUN_TEST(test) MU__SAFE_BLOCK(\ 109 | if (minunit_real_timer==0 && minunit_proc_timer==0) {\ 110 | minunit_real_timer = mu_timer_real();\ 111 | minunit_proc_timer = mu_timer_cpu();\ 112 | }\ 113 | if (minunit_setup) (*minunit_setup)();\ 114 | minunit_status = 0;\ 115 | test();\ 116 | minunit_run++;\ 117 | if (minunit_status) {\ 118 | minunit_fail++;\ 119 | printf("F");\ 120 | printf("\n%s\n", minunit_last_message);\ 121 | }\ 122 | fflush(stdout);\ 123 | if (minunit_teardown) (*minunit_teardown)();\ 124 | ) 125 | 126 | /* Report */ 127 | #define MU_REPORT() MU__SAFE_BLOCK(\ 128 | double minunit_end_real_timer;\ 129 | double minunit_end_proc_timer;\ 130 | printf("\n\n%d tests, %d assertions, %d failures\n", minunit_run, minunit_assert, minunit_fail);\ 131 | minunit_end_real_timer = mu_timer_real();\ 132 | minunit_end_proc_timer = mu_timer_cpu();\ 133 | printf("\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n",\ 134 | minunit_end_real_timer - minunit_real_timer,\ 135 | minunit_end_proc_timer - minunit_proc_timer);\ 136 | ) 137 | 138 | /* Assertions */ 139 | #define mu_check(test) MU__SAFE_BLOCK(\ 140 | minunit_assert++;\ 141 | if (!(test)) {\ 142 | snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, #test);\ 143 | minunit_status = 1;\ 144 | return;\ 145 | } else {\ 146 | printf(".");\ 147 | }\ 148 | ) 149 | 150 | #define mu_fail(message) MU__SAFE_BLOCK(\ 151 | minunit_assert++;\ 152 | snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, message);\ 153 | minunit_status = 1;\ 154 | return;\ 155 | ) 156 | 157 | #define mu_assert(test, message) MU__SAFE_BLOCK(\ 158 | minunit_assert++;\ 159 | if (!(test)) {\ 160 | snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, message);\ 161 | minunit_status = 1;\ 162 | return;\ 163 | } else {\ 164 | printf(".");\ 165 | }\ 166 | ) 167 | 168 | #define mu_assert_int_eq(expected, result) MU__SAFE_BLOCK(\ 169 | int minunit_tmp_e;\ 170 | int minunit_tmp_r;\ 171 | minunit_assert++;\ 172 | minunit_tmp_e = (expected);\ 173 | minunit_tmp_r = (result);\ 174 | if (minunit_tmp_e != minunit_tmp_r) {\ 175 | snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %d expected but was %d", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\ 176 | minunit_status = 1;\ 177 | return;\ 178 | } else {\ 179 | printf(".");\ 180 | }\ 181 | ) 182 | 183 | #define mu_assert_double_eq(expected, result) MU__SAFE_BLOCK(\ 184 | double minunit_tmp_e;\ 185 | double minunit_tmp_r;\ 186 | minunit_assert++;\ 187 | minunit_tmp_e = (expected);\ 188 | minunit_tmp_r = (result);\ 189 | if (fabs(minunit_tmp_e-minunit_tmp_r) > MINUNIT_EPSILON) {\ 190 | int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON);\ 191 | snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %.*g expected but was %.*g", __func__, __FILE__, __LINE__, minunit_significant_figures, minunit_tmp_e, minunit_significant_figures, minunit_tmp_r);\ 192 | minunit_status = 1;\ 193 | return;\ 194 | } else {\ 195 | printf(".");\ 196 | }\ 197 | ) 198 | 199 | #define mu_assert_string_eq(expected, result) MU__SAFE_BLOCK(\ 200 | const char* minunit_tmp_e = expected;\ 201 | const char* minunit_tmp_r = result;\ 202 | minunit_assert++;\ 203 | if (!minunit_tmp_e) {\ 204 | minunit_tmp_e = "";\ 205 | }\ 206 | if (!minunit_tmp_r) {\ 207 | minunit_tmp_r = "";\ 208 | }\ 209 | if(strcmp(minunit_tmp_e, minunit_tmp_r)) {\ 210 | snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: '%s' expected but was '%s'", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\ 211 | minunit_status = 1;\ 212 | return;\ 213 | } else {\ 214 | printf(".");\ 215 | }\ 216 | ) 217 | 218 | /* 219 | * The following two functions were written by David Robert Nadeau 220 | * from http://NadeauSoftware.com/ and distributed under the 221 | * Creative Commons Attribution 3.0 Unported License 222 | */ 223 | 224 | /** 225 | * Returns the real time, in seconds, or -1.0 if an error occurred. 226 | * 227 | * Time is measured since an arbitrary and OS-dependent start time. 228 | * The returned real time is only useful for computing an elapsed time 229 | * between two calls to this function. 230 | */ 231 | static double mu_timer_real(void) 232 | { 233 | #if defined(_WIN32) 234 | /* Windows 2000 and later. ---------------------------------- */ 235 | LARGE_INTEGER Time; 236 | LARGE_INTEGER Frequency; 237 | 238 | QueryPerformanceFrequency(&Frequency); 239 | QueryPerformanceCounter(&Time); 240 | 241 | Time.QuadPart *= 1000000; 242 | Time.QuadPart /= Frequency.QuadPart; 243 | 244 | return (double)Time.QuadPart / 1000000.0; 245 | 246 | #elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__))) 247 | /* HP-UX, Solaris. ------------------------------------------ */ 248 | return (double)gethrtime( ) / 1000000000.0; 249 | 250 | #elif defined(__MACH__) && defined(__APPLE__) 251 | /* OSX. ----------------------------------------------------- */ 252 | static double timeConvert = 0.0; 253 | if ( timeConvert == 0.0 ) 254 | { 255 | mach_timebase_info_data_t timeBase; 256 | (void)mach_timebase_info( &timeBase ); 257 | timeConvert = (double)timeBase.numer / 258 | (double)timeBase.denom / 259 | 1000000000.0; 260 | } 261 | return (double)mach_absolute_time( ) * timeConvert; 262 | 263 | #elif defined(_POSIX_VERSION) 264 | /* POSIX. --------------------------------------------------- */ 265 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) 266 | { 267 | struct timespec ts; 268 | #if defined(CLOCK_MONOTONIC_PRECISE) 269 | /* BSD. --------------------------------------------- */ 270 | const clockid_t id = CLOCK_MONOTONIC_PRECISE; 271 | #elif defined(CLOCK_MONOTONIC_RAW) 272 | /* Linux. ------------------------------------------- */ 273 | const clockid_t id = CLOCK_MONOTONIC_RAW; 274 | #elif defined(CLOCK_HIGHRES) 275 | /* Solaris. ----------------------------------------- */ 276 | const clockid_t id = CLOCK_HIGHRES; 277 | #elif defined(CLOCK_MONOTONIC) 278 | /* AIX, BSD, Linux, POSIX, Solaris. ----------------- */ 279 | const clockid_t id = CLOCK_MONOTONIC; 280 | #elif defined(CLOCK_REALTIME) 281 | /* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */ 282 | const clockid_t id = CLOCK_REALTIME; 283 | #else 284 | const clockid_t id = (clockid_t)-1; /* Unknown. */ 285 | #endif /* CLOCK_* */ 286 | if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 ) 287 | return (double)ts.tv_sec + 288 | (double)ts.tv_nsec / 1000000000.0; 289 | /* Fall thru. */ 290 | } 291 | #endif /* _POSIX_TIMERS */ 292 | 293 | /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */ 294 | struct timeval tm; 295 | gettimeofday( &tm, NULL ); 296 | return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0; 297 | #else 298 | return -1.0; /* Failed. */ 299 | #endif 300 | } 301 | 302 | /** 303 | * Returns the amount of CPU time used by the current process, 304 | * in seconds, or -1.0 if an error occurred. 305 | */ 306 | static double mu_timer_cpu(void) 307 | { 308 | #if defined(_WIN32) 309 | /* Windows -------------------------------------------------- */ 310 | FILETIME createTime; 311 | FILETIME exitTime; 312 | FILETIME kernelTime; 313 | FILETIME userTime; 314 | 315 | /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API does not offer better */ 316 | if ( GetProcessTimes( GetCurrentProcess( ), 317 | &createTime, &exitTime, &kernelTime, &userTime ) != 0 ) 318 | { 319 | ULARGE_INTEGER userSystemTime; 320 | memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER)); 321 | return (double)userSystemTime.QuadPart / 10000000.0; 322 | } 323 | 324 | #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) 325 | /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */ 326 | 327 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) 328 | /* Prefer high-res POSIX timers, when available. */ 329 | { 330 | clockid_t id; 331 | struct timespec ts; 332 | #if _POSIX_CPUTIME > 0 333 | /* Clock ids vary by OS. Query the id, if possible. */ 334 | if ( clock_getcpuclockid( 0, &id ) == -1 ) 335 | #endif 336 | #if defined(CLOCK_PROCESS_CPUTIME_ID) 337 | /* Use known clock id for AIX, Linux, or Solaris. */ 338 | id = CLOCK_PROCESS_CPUTIME_ID; 339 | #elif defined(CLOCK_VIRTUAL) 340 | /* Use known clock id for BSD or HP-UX. */ 341 | id = CLOCK_VIRTUAL; 342 | #else 343 | id = (clockid_t)-1; 344 | #endif 345 | if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 ) 346 | return (double)ts.tv_sec + 347 | (double)ts.tv_nsec / 1000000000.0; 348 | } 349 | #endif 350 | 351 | #if defined(RUSAGE_SELF) 352 | { 353 | struct rusage rusage; 354 | if ( getrusage( RUSAGE_SELF, &rusage ) != -1 ) 355 | return (double)rusage.ru_utime.tv_sec + 356 | (double)rusage.ru_utime.tv_usec / 1000000.0; 357 | } 358 | #endif 359 | 360 | #if defined(_SC_CLK_TCK) 361 | { 362 | const double ticks = (double)sysconf( _SC_CLK_TCK ); 363 | struct tms tms; 364 | if ( times( &tms ) != (clock_t)-1 ) 365 | return (double)tms.tms_utime / ticks; 366 | } 367 | #endif 368 | 369 | #if defined(CLOCKS_PER_SEC) 370 | { 371 | clock_t cl = clock( ); 372 | if ( cl != (clock_t)-1 ) 373 | return (double)cl / (double)CLOCKS_PER_SEC; 374 | } 375 | #endif 376 | 377 | #endif 378 | 379 | return -1; /* Failed. */ 380 | } 381 | 382 | #ifdef __cplusplus 383 | } 384 | #endif 385 | 386 | #endif /* __MINUNIT_H__ */ -------------------------------------------------------------------------------- /tests.c: -------------------------------------------------------------------------------- 1 | #include "minunit.h" 2 | #include "cansid.h" 3 | 4 | #define feed(x) mu_assert_int_eq('\0', cansid_process(&state, x).ascii) 5 | #define yields_self(x) mu_assert_int_eq(x, cansid_process(&state, x).ascii) 6 | #define state(x) mu_assert_int_eq(CANSID_ ## x, state.state) 7 | #define causes_reset(x) do { mu_assert_int_eq(x, cansid_process(&state, x).ascii); state(ESC); mu_assert_int_eq(state.style, state.next_style); } while (0) 8 | 9 | struct cansid_state state; 10 | 11 | void test_setup(void) { 12 | state = cansid_init(); 13 | } 14 | 15 | MU_TEST(init) { 16 | mu_assert_int_eq(state.style, 0x0F); 17 | mu_assert_int_eq(state.next_style, state.style); 18 | state(ESC); 19 | } 20 | 21 | MU_TEST(state_change) { 22 | feed('\x1B'); state(BRACKET); 23 | feed('['); state(PARSE); 24 | feed('3'); state(FGCOLOR); 25 | feed('1'); state(ENDVAL); 26 | feed(';'); state(PARSE); 27 | feed('4'); state(BGCOLOR); 28 | feed('2'); state(ENDVAL); 29 | feed(';'); state(PARSE); 30 | feed('='); state(EQUALS); 31 | feed('1'); state(ENDVAL); 32 | feed('m'); state(ESC); 33 | } 34 | 35 | MU_TEST(fail_parse) { 36 | feed('\x1B'); 37 | causes_reset(']'); 38 | 39 | feed('\x1B'); 40 | feed('['); 41 | causes_reset('m'); 42 | 43 | feed('\x1B'); 44 | feed('['); 45 | causes_reset(';'); 46 | 47 | feed('\x1B'); 48 | feed('['); 49 | causes_reset('2'); 50 | 51 | feed('\x1B'); 52 | feed('['); 53 | causes_reset('a'); 54 | 55 | feed('\x1B'); 56 | feed('['); 57 | feed('3'); 58 | feed('1'); 59 | causes_reset('1'); 60 | 61 | feed('\x1B'); 62 | feed('['); 63 | feed('3'); 64 | feed('1'); 65 | causes_reset('['); 66 | 67 | feed('\x1B'); 68 | feed('['); 69 | feed('='); 70 | causes_reset('='); 71 | 72 | feed('\x1B'); 73 | feed('['); 74 | feed('='); 75 | causes_reset(';'); 76 | } 77 | 78 | MU_TEST(succeed_parse) { 79 | yields_self('m'); 80 | yields_self('['); 81 | yields_self(';'); 82 | yields_self('a'); 83 | 84 | feed('\x1B'); 85 | feed('['); 86 | feed('3'); 87 | feed('1'); 88 | feed('m'); 89 | 90 | yields_self('m'); 91 | yields_self('['); 92 | yields_self(';'); 93 | yields_self('a'); 94 | } 95 | 96 | MU_TEST(fg_colors) { 97 | const unsigned char lookup_table[8] = { 0, 4, 2, 6, 1, 5, 3, 7 }; 98 | for (int i = 0; i < 8; i++) { 99 | feed('\x1B'); 100 | feed('['); 101 | feed('3'); 102 | feed(i + '0'); 103 | mu_assert_int_eq(lookup_table[i], state.next_style & 0x07); 104 | unsigned char style_temp = state.style; 105 | feed('m'); 106 | mu_assert_int_eq(lookup_table[i], state.style & 0x07); 107 | mu_assert_int_eq(lookup_table[i], cansid_process(&state, 'm').style & 0x07); 108 | mu_check(state.style != style_temp); 109 | } 110 | } 111 | 112 | MU_TEST(bg_colors) { 113 | const unsigned char lookup_table[8] = { 0, 4, 2, 6, 1, 5, 3, 7 }; 114 | state.style = 0x10; // Otherwise the mu_check fails on the first time 115 | for (int i = 0; i < 8; i++) { 116 | feed('\x1B'); 117 | feed('['); 118 | feed('4'); 119 | feed(i + '0'); 120 | mu_assert_int_eq(lookup_table[i] << 4, state.next_style & 0xF0); 121 | unsigned char style_temp = state.style; 122 | feed('m'); 123 | mu_assert_int_eq(lookup_table[i] << 4, state.style & 0xF0); 124 | mu_assert_int_eq(lookup_table[i] << 4, cansid_process(&state, 'm').style & 0xF0); 125 | mu_check(state.style != style_temp); 126 | } 127 | } 128 | 129 | MU_TEST(reset_state) { 130 | feed('\x1B'); 131 | feed('['); 132 | feed('3'); 133 | feed('1'); 134 | feed(';'); 135 | feed('4'); 136 | feed('1'); 137 | feed('m'); 138 | mu_assert_int_eq(4 | (1 << 3) | (4 << 4), state.style); 139 | feed('\x1B'); 140 | feed('['); 141 | feed('0'); 142 | feed('m'); 143 | mu_assert_int_eq(0x0F, state.style); 144 | } 145 | 146 | MU_TEST(bright_bit) { 147 | state.style = 0x00; 148 | feed('\x1B'); 149 | feed('['); 150 | feed('3'); 151 | feed('1'); 152 | feed(';'); 153 | feed('1'); 154 | feed('m'); 155 | mu_assert_int_eq(4 | (1 << 3), state.style); 156 | 157 | feed('\x1B'); 158 | feed('['); 159 | feed('3'); 160 | feed('1'); 161 | feed(';'); 162 | feed('='); 163 | feed('1'); 164 | feed('m'); 165 | } 166 | 167 | MU_TEST_SUITE(test_suite) { 168 | MU_RUN_TEST(init); 169 | MU_RUN_TEST(state_change); 170 | MU_RUN_TEST(fail_parse); 171 | MU_RUN_TEST(succeed_parse); 172 | MU_RUN_TEST(fg_colors); 173 | MU_RUN_TEST(bg_colors); 174 | MU_RUN_TEST(reset_state); 175 | } 176 | 177 | int main(int argc, char *argv[]) { 178 | MU_SUITE_CONFIGURE(test_setup, NULL); 179 | MU_RUN_SUITE(test_suite); 180 | MU_REPORT(); 181 | return minunit_fail != 0; 182 | } 183 | --------------------------------------------------------------------------------