From d7548d044923a991914486432439612f0ddb982a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 10 Oct 2021 14:31:04 +0100 Subject: [PATCH] Move bidi gettype main() into its own file. That's what I've usually been doing with any main()s I find under ifdef; there's no reason this should be an exception. If we're keeping it in the code at all, we should ensure it carries on compiling. I've also created a new header file bidi.h, containing pieces of the bidi definitions shared between bidi.c and the new source file. --- CMakeLists.txt | 4 ++ terminal/bidi.c | 122 ++++------------------------------------ terminal/bidi.h | 60 ++++++++++++++++++++ terminal/bidi_gettype.c | 53 +++++++++++++++++ 4 files changed, 128 insertions(+), 111 deletions(-) create mode 100644 terminal/bidi.h create mode 100644 terminal/bidi_gettype.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 18c76aa4..debd6e1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,10 @@ add_executable(test_wildcard target_compile_definitions(test_wildcard PRIVATE TEST) target_link_libraries(test_wildcard utils ${platform_libraries}) +add_executable(bidi_gettype + terminal/bidi_gettype.c) +target_link_libraries(bidi_gettype guiterminal utils ${platform_libraries}) + add_executable(plink ${platform}/plink.c be_all_s.c) diff --git a/terminal/bidi.c b/terminal/bidi.c index 05d15b3d..4c55b86e 100644 --- a/terminal/bidi.c +++ b/terminal/bidi.c @@ -25,74 +25,18 @@ #include "putty.h" #include "misc.h" - -#define LMASK 0x3F /* Embedding Level mask */ -#define OMASK 0xC0 /* Override mask */ -#define OISL 0x80 /* Override is L */ -#define OISR 0x40 /* Override is R */ - -/* For standalone compilation in a testing mode. - * Still depends on the PuTTY headers for snewn and sfree, but can avoid - * _linking_ with any other PuTTY code. */ -#ifdef TEST_GETTYPE -#define safemalloc malloc -#define safefree free -#endif - -/* Shaping Helpers */ -#define STYPE(xh) ((((xh) >= SHAPE_FIRST) && ((xh) <= SHAPE_LAST)) ? \ -shapetypes[(xh)-SHAPE_FIRST].type : SU) /*))*/ -#define SISOLATED(xh) (shapetypes[(xh)-SHAPE_FIRST].form_b) -#define SFINAL(xh) ((xh)+1) -#define SINITIAL(xh) ((xh)+2) -#define SMEDIAL(ch) ((ch)+3) - -#define leastGreaterOdd(x) ( ((x)+1) | 1 ) -#define leastGreaterEven(x) ( ((x)+2) &~ 1 ) +#include "bidi.h" /* function declarations */ static void flipThisRun( bidi_char *from, unsigned char *level, int max, int count); static int findIndexOfRun( unsigned char *level, int start, int count, int tlevel); -static unsigned char getType(int ch); static unsigned char setOverrideBits( unsigned char level, unsigned char override); static int getPreviousLevel(unsigned char *level, int from); static void doMirror(unsigned int *ch); -/* character types */ -enum { - L, - LRE, - LRO, - R, - AL, - RLE, - RLO, - PDF, - EN, - ES, - ET, - AN, - CS, - NSM, - BN, - B, - S, - WS, - ON -}; - -/* Shaping Types */ -enum { - SL, /* Left-Joining, doesn't exist in U+0600 - U+06FF */ - SR, /* Right-Joining, ie has Isolated, Final */ - SD, /* Dual-Joining, ie has Isolated, Final, Initial, Medial */ - SU, /* Non-Joining */ - SC /* Join-Causing, like U+0640 (TATWEEL) */ -}; - typedef struct { char type; wchar_t form_b; @@ -354,7 +298,7 @@ perl -ne 'split ";"; $num = hex $_[0]; $type = $_[4];' \ UnicodeData.txt */ -static unsigned char getType(int ch) +unsigned char bidi_getType(int ch) { static const struct { int first, last, type; @@ -1036,7 +980,7 @@ bool is_rtl(int c) */ const int mask = (1<0 && (getType(line[j].wc) == WS)) { + while (j>0 && (bidi_getType(line[j].wc) == WS)) { j--; } if (j < (count-1)) { @@ -1564,14 +1508,14 @@ int do_bidi(bidi_char *line, int count) levels[j] = paragraphLevel; } for (i=0; i=i ; j--) { levels[j] = paragraphLevel; } @@ -1979,47 +1923,3 @@ static void doMirror(unsigned int *ch) } } } - -#ifdef TEST_GETTYPE - -#include -#include - -int main(int argc, char **argv) -{ - static const struct { int type; char *name; } typetoname[] = { -#define TYPETONAME(X) { X , #X } - TYPETONAME(L), - TYPETONAME(LRE), - TYPETONAME(LRO), - TYPETONAME(R), - TYPETONAME(AL), - TYPETONAME(RLE), - TYPETONAME(RLO), - TYPETONAME(PDF), - TYPETONAME(EN), - TYPETONAME(ES), - TYPETONAME(ET), - TYPETONAME(AN), - TYPETONAME(CS), - TYPETONAME(NSM), - TYPETONAME(BN), - TYPETONAME(B), - TYPETONAME(S), - TYPETONAME(WS), - TYPETONAME(ON), -#undef TYPETONAME - }; - int i; - - for (i = 1; i < argc; i++) { - unsigned long chr = strtoul(argv[i], NULL, 0); - int type = getType(chr); - assert(typetoname[type].type == type); - printf("U+%04x: %s\n", (unsigned)chr, typetoname[type].name); - } - - return 0; -} - -#endif diff --git a/terminal/bidi.h b/terminal/bidi.h new file mode 100644 index 00000000..eca80c21 --- /dev/null +++ b/terminal/bidi.h @@ -0,0 +1,60 @@ +/* + * Header file shared between bidi.c and its tests. Not used by + * anything outside the bidi subsystem. + */ + +#ifndef PUTTY_BIDI_H +#define PUTTY_BIDI_H + +#define LMASK 0x3F /* Embedding Level mask */ +#define OMASK 0xC0 /* Override mask */ +#define OISL 0x80 /* Override is L */ +#define OISR 0x40 /* Override is R */ + +/* Shaping Helpers */ +#define STYPE(xh) ((((xh) >= SHAPE_FIRST) && ((xh) <= SHAPE_LAST)) ? \ +shapetypes[(xh)-SHAPE_FIRST].type : SU) /*))*/ +#define SISOLATED(xh) (shapetypes[(xh)-SHAPE_FIRST].form_b) +#define SFINAL(xh) ((xh)+1) +#define SINITIAL(xh) ((xh)+2) +#define SMEDIAL(ch) ((ch)+3) + +#define leastGreaterOdd(x) ( ((x)+1) | 1 ) +#define leastGreaterEven(x) ( ((x)+2) &~ 1 ) + +/* Function declarations used outside bidi.c */ +unsigned char bidi_getType(int ch); + +/* character types */ +enum { + L, + LRE, + LRO, + R, + AL, + RLE, + RLO, + PDF, + EN, + ES, + ET, + AN, + CS, + NSM, + BN, + B, + S, + WS, + ON +}; + +/* Shaping Types */ +enum { + SL, /* Left-Joining, doesn't exist in U+0600 - U+06FF */ + SR, /* Right-Joining, ie has Isolated, Final */ + SD, /* Dual-Joining, ie has Isolated, Final, Initial, Medial */ + SU, /* Non-Joining */ + SC /* Join-Causing, like U+0640 (TATWEEL) */ +}; + +#endif /* PUTTY_BIDI_H */ diff --git a/terminal/bidi_gettype.c b/terminal/bidi_gettype.c new file mode 100644 index 00000000..a3b765ae --- /dev/null +++ b/terminal/bidi_gettype.c @@ -0,0 +1,53 @@ +/* + * Standalone test program that exposes the minibidi getType function. + */ + +#include +#include + +#include "putty.h" +#include "misc.h" +#include "bidi.h" + +void out_of_memory(void) +{ + fprintf(stderr, "out of memory!\n"); + exit(2); +} + +int main(int argc, char **argv) +{ + static const struct { int type; char *name; } typetoname[] = { +#define TYPETONAME(X) { X , #X } + TYPETONAME(L), + TYPETONAME(LRE), + TYPETONAME(LRO), + TYPETONAME(R), + TYPETONAME(AL), + TYPETONAME(RLE), + TYPETONAME(RLO), + TYPETONAME(PDF), + TYPETONAME(EN), + TYPETONAME(ES), + TYPETONAME(ET), + TYPETONAME(AN), + TYPETONAME(CS), + TYPETONAME(NSM), + TYPETONAME(BN), + TYPETONAME(B), + TYPETONAME(S), + TYPETONAME(WS), + TYPETONAME(ON), +#undef TYPETONAME + }; + int i; + + for (i = 1; i < argc; i++) { + unsigned long chr = strtoul(argv[i], NULL, 0); + int type = bidi_getType(chr); + assert(typetoname[type].type == type); + printf("U+%04x: %s\n", (unsigned)chr, typetoname[type].name); + } + + return 0; +}