--- rxvt-unicode/src/grkelot.C 2004/01/31 03:27:45 1.4 +++ rxvt-unicode/src/grkelot.C 2004/02/01 01:34:41 1.5 @@ -84,35 +84,43 @@ typedef unsigned int u_int; typedef unsigned long u_long; -typedef struct s_xlat { +typedef struct s_xlat + { u_int first, last; u_int *pval; /* array of translated values */ -} K_XLAT; + } +K_XLAT; -typedef struct s_switch { +typedef struct s_switch + { u_char type; /* Ascii, Virtual, Scan */ u_int code; u_char nextstate; u_char on; /* current state of key: 0 = off */ -} K_SWITCH; + } +K_SWITCH; -typedef struct s_state { +typedef struct s_state + { u_int num_xlat; /* number of translations */ K_XLAT *xlat; /* State translations ((dynamic - realloc'ed) */ u_int num_switcher; /* number of switcher keys */ K_SWITCH switcher[MAX_SWITCHER]; /* switcher keys to other states */ u_char life; /* 0 = until switched by key */ u_char prev_state; /* filled when jumped to a new state */ -} K_STATE; + } +K_STATE; /* type for each one of the different greek standards (xlat types) */ -typedef struct s_xlat_type { +typedef struct s_xlat_type + { char *plain; char *accent; char *accent_xtra; char *umlaut; char *acc_uml; -} XLAT_TYPE; + } +XLAT_TYPE; /* --- Local Data ------------ */ static K_STATE State[MAX_STATES]; @@ -148,10 +156,10 @@ * (not recommended: stick to just these 2 if not only the ELOT one) */ static XLAT_TYPE xlat_type[] = -{ + { {elot_xlat_plain, elot_xlat_acc, elot_xlat_acc_xtra, elot_xlat_uml, elot_xlat_umacc}, {i437_xlat_plain, i437_xlat_acc, i437_xlat_acc_xtra, i437_xlat_uml, i437_xlat_umacc}, -}; + }; /* the current trasnaltion type */ static XLAT_TYPE *xlat_now = &xlat_type[GREEK_ELOT928]; @@ -166,34 +174,34 @@ void kstate_setcurr(int stateno) { - u_char prev_state; + u_char prev_state; - if ((u_int) stateno > (u_int) MAX_STATES) - return; - if (pStateNow->life == 1) - prev_state = pStateNow->prev_state; - else - prev_state = nStateNow; - pStateNow = &State[nStateNow = stateno]; - pStateNow->prev_state = prev_state; + if ((u_int) stateno > (u_int) MAX_STATES) + return; + if (pStateNow->life == 1) + prev_state = pStateNow->prev_state; + else + prev_state = nStateNow; + pStateNow = &State[nStateNow = stateno]; + pStateNow->prev_state = prev_state; } void kstate_init(void) { - pStateNow->num_xlat = pStateNow->num_switcher = pStateNow->life = pStateNow->prev_state = 0; - pStateNow->xlat = NULL; + pStateNow->num_xlat = pStateNow->num_switcher = pStateNow->life = pStateNow->prev_state = 0; + pStateNow->xlat = NULL; } void kstate_end(void) { - int i; + int i; - for (i = 0; i < pStateNow->num_xlat; i++) - free(pStateNow->xlat[i].pval); - if (pStateNow->num_xlat > 0) - free(pStateNow->xlat); + for (i = 0; i < pStateNow->num_xlat; i++) + free(pStateNow->xlat[i].pval); + if (pStateNow->num_xlat > 0) + free(pStateNow->xlat); } /* @@ -203,48 +211,50 @@ void kstate_init_all(int greek_mode) { -/* the translation tables for the 4 FSM states for ELOT-928 mappings */ - int i; + /* the translation tables for the 4 FSM states for ELOT-928 mappings */ + int i; - for (i = 0; i < MAX_STATES; i++) { - kstate_setcurr(i); - kstate_init(); + for (i = 0; i < MAX_STATES; i++) + { + kstate_setcurr(i); + kstate_init(); } - if (greek_mode < 0 || greek_mode >= NUM_XLAT_TYPES) /* avoid death */ - greek_mode = GREEK_ELOT928; - xlat_now = &xlat_type[greek_mode]; - kstate_setcurr(0); - kstate_add_xlat(xlat_now->plain); - kstate_add_switcher("A;:1"); - kstate_add_switcher("A::2"); - kstate_set_life("L0"); - - kstate_setcurr(1); - kstate_add_xlat(xlat_now->accent); - kstate_add_xlat(xlat_now->accent_xtra); - kstate_add_switcher("A::3"); - kstate_set_life("L1"); - - kstate_setcurr(2); - kstate_add_xlat(xlat_now->umlaut); - kstate_add_switcher("A;:3"); - kstate_set_life("L1"); - - kstate_setcurr(3); - kstate_add_xlat(xlat_now->acc_uml); - kstate_set_life("L1"); + if (greek_mode < 0 || greek_mode >= NUM_XLAT_TYPES) /* avoid death */ + greek_mode = GREEK_ELOT928; + xlat_now = &xlat_type[greek_mode]; + kstate_setcurr(0); + kstate_add_xlat(xlat_now->plain); + kstate_add_switcher("A;:1"); + kstate_add_switcher("A::2"); + kstate_set_life("L0"); + + kstate_setcurr(1); + kstate_add_xlat(xlat_now->accent); + kstate_add_xlat(xlat_now->accent_xtra); + kstate_add_switcher("A::3"); + kstate_set_life("L1"); + + kstate_setcurr(2); + kstate_add_xlat(xlat_now->umlaut); + kstate_add_switcher("A;:3"); + kstate_set_life("L1"); + + kstate_setcurr(3); + kstate_add_xlat(xlat_now->acc_uml); + kstate_set_life("L1"); } void kstate_end_all(void) { - int i; + int i; - for (i = 0; i < MAX_STATES; i++) { - kstate_setcurr(i); - kstate_end(); + for (i = 0; i < MAX_STATES; i++) + { + kstate_setcurr(i); + kstate_end(); } - kstate_setcurr(0); + kstate_setcurr(0); } /* @@ -253,37 +263,39 @@ void kstate_reset(void) { - kstate_setcurr(0); + kstate_setcurr(0); } void kstate_add_xlat(char *str) { - K_XLAT *xlat; - u_int *pval_tmp; - char *sval; - int i; - - if (str == NULL) - return; -/* add a new xlat table in state */ - if (pStateNow->num_xlat == 0) { - pStateNow->xlat = malloc(sizeof(K_XLAT)); - } else /* prefer contiguous data, realloc */ - pStateNow->xlat = realloc(pStateNow->xlat, (pStateNow->num_xlat + 1) * sizeof(K_XLAT)); - xlat = &pStateNow->xlat[pStateNow->num_xlat]; -/* parse str and derive first, last, values */ - xlat->first = (u_int) atoi(strtok(str, "-")); - xlat->last = (u_int) atoi(strtok(NULL, ":")); - i = 0; - pval_tmp = calloc(MAX_VAL, sizeof(K_XLAT)); - while ((sval = strtok(NULL, ",")) != NULL) - pval_tmp[i++] = (u_int) (atoi(sval)); - xlat->pval = calloc(i, sizeof(K_XLAT)); - if (xlat->pval != NULL) - memcpy(xlat->pval, pval_tmp, i * sizeof(u_int)); - free(pval_tmp); - pStateNow->num_xlat++; + K_XLAT *xlat; + u_int *pval_tmp; + char *sval; + int i; + + if (str == NULL) + return; + /* add a new xlat table in state */ + if (pStateNow->num_xlat == 0) + { + pStateNow->xlat = malloc(sizeof(K_XLAT)); + } + else /* prefer contiguous data, realloc */ + pStateNow->xlat = realloc(pStateNow->xlat, (pStateNow->num_xlat + 1) * sizeof(K_XLAT)); + xlat = &pStateNow->xlat[pStateNow->num_xlat]; + /* parse str and derive first, last, values */ + xlat->first = (u_int) atoi(strtok(str, "-")); + xlat->last = (u_int) atoi(strtok(NULL, ":")); + i = 0; + pval_tmp = calloc(MAX_VAL, sizeof(K_XLAT)); + while ((sval = strtok(NULL, ",")) != NULL) + pval_tmp[i++] = (u_int) (atoi(sval)); + xlat->pval = calloc(i, sizeof(K_XLAT)); + if (xlat->pval != NULL) + memcpy(xlat->pval, pval_tmp, i * sizeof(u_int)); + free(pval_tmp); + pStateNow->num_xlat++; } /* @@ -292,84 +304,87 @@ void kstate_add_switcher(char *str) { - K_SWITCH *switcher; + K_SWITCH *switcher; - if (str == NULL) - return; - if (pStateNow->num_switcher >= MAX_SWITCHER) - return; - switcher = &pStateNow->switcher[pStateNow->num_switcher]; - switch (switcher->type = str[0]) { - case 'A': /* ascii eg: A;:2 */ - switcher->code = str[1]; - switcher->nextstate = atoi(&str[3]); - break; + if (str == NULL) + return; + if (pStateNow->num_switcher >= MAX_SWITCHER) + return; + switcher = &pStateNow->switcher[pStateNow->num_switcher]; + switch (switcher->type = str[0]) + { + case 'A': /* ascii eg: A;:2 */ + switcher->code = str[1]; + switcher->nextstate = atoi(&str[3]); + break; } - switcher->on = 0; - pStateNow->num_switcher++; + switcher->on = 0; + pStateNow->num_switcher++; } /* L1 or L0 */ void kstate_set_life(char *str) { - pStateNow->life = atoi(&str[1]); + pStateNow->life = atoi(&str[1]); } unsigned int kstate_cxlat(unsigned int c) { - int i; + int i; -/* check for ascii switcher */ - for (i = 0; i < pStateNow->num_switcher; i++) - if (pStateNow->switcher[i].type == 'A' && /* only ascii here */ - c == pStateNow->switcher[i].code) { - kstate_setcurr(pStateNow->switcher[i].nextstate); - pStateNow->switcher[i].on = 1; - return ((unsigned int)-1); - } -/* do translation */ - for (i = 0; i < pStateNow->num_xlat; i++) - if (c >= pStateNow->xlat[i].first && c <= pStateNow->xlat[i].last) { - c = pStateNow->xlat[i].pval[c - pStateNow->xlat[i].first]; - break; - } -/* switch back to previous state if life of current is 1 */ - if (pStateNow->life == 1) - kstate_setcurr(pStateNow->prev_state); - return (c); + /* check for ascii switcher */ + for (i = 0; i < pStateNow->num_switcher; i++) + if (pStateNow->switcher[i].type == 'A' && /* only ascii here */ + c == pStateNow->switcher[i].code) + { + kstate_setcurr(pStateNow->switcher[i].nextstate); + pStateNow->switcher[i].on = 1; + return ((unsigned int)-1); + } + /* do translation */ + for (i = 0; i < pStateNow->num_xlat; i++) + if (c >= pStateNow->xlat[i].first && c <= pStateNow->xlat[i].last) + { + c = pStateNow->xlat[i].pval[c - pStateNow->xlat[i].first]; + break; + } + /* switch back to previous state if life of current is 1 */ + if (pStateNow->life == 1) + kstate_setcurr(pStateNow->prev_state); + return (c); } #ifdef RXVT void greek_init(void) { - kstate_init_all(GreekMode); + kstate_init_all(GreekMode); } void greek_end(void) { - kstate_end_all(); + kstate_end_all(); } void greek_reset(void) { - kstate_reset(); + kstate_reset(); } void greek_setmode(int greek_mode) { - GreekMode = greek_mode; + GreekMode = greek_mode; } int greek_getmode(void) { - return (GreekMode); + return (GreekMode); } /* @@ -378,16 +393,17 @@ int greek_xlat(char *s, int num_chars) { - int i, count; - unsigned int c; + int i, count; + unsigned int c; - for (i = 0, count = 0; i < num_chars; i++) { - c = kstate_cxlat((unsigned int)s[i]); - if (c != -1) - s[count++] = (char)c; + for (i = 0, count = 0; i < num_chars; i++) + { + c = kstate_cxlat((unsigned int)s[i]); + if (c != -1) + s[count++] = (char)c; } - s[count] = '\0'; - return (count); + s[count] = '\0'; + return (count); } @@ -395,15 +411,15 @@ int main(void) { -/*char text[] = "abcdef;aGDZXC"; */ - char text[] = "abcdef;a:ibgdezhuiklmnjoprstyfxcv"; + /*char text[] = "abcdef;aGDZXC"; */ + char text[] = "abcdef;a:ibgdezhuiklmnjoprstyfxcv"; - kstate_init_all(GREEK_ELOT928); - printf("text: %s\n", text); - greek_xlat(text, strlen(text)); - printf("xlat'ed text: %s\n", text); - kstate_end_all(); - return 0; + kstate_init_all(GREEK_ELOT928); + printf("text: %s\n", text); + greek_xlat(text, strlen(text)); + printf("xlat'ed text: %s\n", text); + kstate_end_all(); + return 0; } #endif #endif /* RXVT */