static int CheckInit(void) { static int initialised = 0; int i,j; int fail = 0; int nBlocks; int nAllocated = 0; if(initialised) { return 0; } ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks(); ned.block = ALLOCATE(sizeof(nandemul_Block*) * nBlocks ); if(!ned.block) return ENOMEM; for(i=fail=0; i <nBlocks; i++) { nandemul_Block *blk; if(!(blk = ned.block[i] = ALLOCATE(sizeof(nandemul_Block)))) { fail = 1; } else { for(j = 0; j < PAGES_PER_BLOCK; j++) { if((blk->page[j] = ALLOCATE(sizeof(nandemul_Page))) == 0) { fail = 1; } } nandemul2k_DoErase(i); ned.block[i]->damaged = 0; nAllocated++; } } if(fail) { //Todo thump pages for(i = 0; i < nAllocated; i++) { FREE(ned.block[i]); } FREE(ned.block); return ENOMEM; } ned.nBlocks = nBlocks; initialised = 1; return 1; }
static void handle_elif() #endif { if (iftop) { if (iftop->state == EXPECT_ELSE) { /* last cond was false... */ int cond; ifstate_t *p = iftop; /* pop previous condition */ iftop = p->next; FREE((char *) p); #ifdef LEXER *--outp = '\0'; add_input(sp); #endif cond = cond_get_exp(0); #ifdef LEXER if (*outp++) { yyerror("Condition too complex in #elif"); while (*outp++); #else if (*outp != '\n') { yyerror("Condition too complex in #elif"); #endif } else handle_cond(cond); } else {/* EXPECT_ENDIF */ /* * last cond was true...skip to end of * conditional */ skip_to("endif", (char *) 0); } } else { yyerrorp("Unexpected %celif"); } } static void handle_else (void) { if (iftop) { if (iftop->state == EXPECT_ELSE) { iftop->state = EXPECT_ENDIF; } else { skip_to("endif", (char *) 0); } } else { yyerrorp("Unexpected %cendif"); } } static void handle_endif (void) { if (iftop && (iftop->state == EXPECT_ENDIF || iftop->state == EXPECT_ELSE)) { ifstate_t *p = iftop; iftop = p->next; FREE((char *) p); } else { yyerrorp("Unexpected %cendif"); } } #define BNOT 1 #define LNOT 2 #define UMINUS 3 #define UPLUS 4 #define MULT 1 #define DIV 2 #define MOD 3 #define BPLUS 4 #define BMINUS 5 #define LSHIFT 6 #define RSHIFT 7 #define LESS 8 #define LEQ 9 #define GREAT 10 #define GEQ 11 #define EQ 12 #define NEQ 13 #define BAND 14 #define XOR 15 #define BOR 16 #define LAND 17 #define LOR 18 #define QMARK 19 static char _optab[] = {0, 4, 0, 0, 0, 26, 56, 0, 0, 0, 18, 14, 0, 10, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 50, 40, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 0, 1}; static char optab2[] = {BNOT, 0, 0, LNOT, '=', NEQ, 7, 0, 0, UMINUS, 0, BMINUS, 10, UPLUS, 0, BPLUS, 10, 0, 0, MULT, 11, 0, 0, DIV, 11, 0, 0, MOD, 11, 0, '<', LSHIFT, 9, '=', LEQ, 8, 0, LESS, 8, 0, '>', RSHIFT, 9, '=', GEQ, 8, 0, GREAT, 8, 0, '=', EQ, 7, 0, 0, 0, '&', LAND, 3, 0, BAND, 6, 0, '|', LOR, 2, 0, BOR, 4, 0, 0, XOR, 5, 0, 0, QMARK, 1}; #define optab1 (_optab-' ') static int cond_get_exp (int priority) { int c; int value, value2, x; #ifdef LEXER do c = exgetc(); while (is_wspace(c)); if (c == '(') { #else if ((c = exgetc()) == '(') { #endif value = cond_get_exp(0); #ifdef LEXER do c = exgetc(); while (is_wspace(c)); if (c != ')') { yyerror("bracket not paired in #if"); if (!c) *--outp = '\0'; } #else if ((c = exgetc()) != ')') yyerrorp("bracket not paired in %cif"); #endif } else if (ispunct(c)) { if (!(x = optab1[c])) { yyerrorp("illegal character in %cif"); return 0; } value = cond_get_exp(12); switch (optab2[x - 1]) { case BNOT: value = ~value; break; case LNOT: value = !value; break; case UMINUS: value = -value; break; case UPLUS: value = value; break; default: yyerrorp("illegal unary operator in %cif"); } } else { int base; if (!isdigit(c)) { #ifdef LEXER if (!c) { #else if (c == '\n') { #endif yyerrorp("missing expression in %cif"); } else yyerrorp("illegal character in %cif"); return 0; } value = 0; if (c != '0') base = 10; else { c = *outp++; if (c == 'x' || c == 'X') { base = 16; c = *outp++; } else base = 8; } for (;;) { if (isdigit(c)) x = -'0'; else if (isupper(c)) x = -'A' + 10; else if (islower(c)) x = -'a' + 10; else break; x += c; if (x > base) break; value = value * base + x; c = *outp++; } outp--; } for (;;) { #ifdef LEXER do c = exgetc(); while (is_wspace(c)); if (!ispunct(c)) break; #else if (!ispunct(c = exgetc())) break; #endif if (!(x = optab1[c])) break; value2 = *outp++; for (;; x += 3) { if (!optab2[x]) { outp--; if (!optab2[x + 1]) { yyerrorp("illegal operator use in %cif"); return 0; } break; } if (value2 == optab2[x]) break; } if (priority >= optab2[x + 2]) { if (optab2[x]) *--outp = value2; break; } value2 = cond_get_exp(optab2[x + 2]); switch (optab2[x + 1]) { case MULT: value *= value2; break; case DIV: if (value2) value /= value2; else yyerrorp("division by 0 in %cif"); break; case MOD: if (value2) value %= value2; else yyerrorp("modulo by 0 in %cif"); break; case BPLUS: value += value2; break; case BMINUS: value -= value2; break; case LSHIFT: value <<= value2; break; case RSHIFT: value >>= value2; break; case LESS: value = value < value2; break; case LEQ: value = value <= value2; break; case GREAT: value = value > value2; break; case GEQ: value = value >= value2; break; case EQ: value = value == value2; break; case NEQ: value = value != value2; break; case BAND: value &= value2; break; case XOR: value ^= value2; break; case BOR: value |= value2; break; case LAND: value = value && value2; break; case LOR: value = value || value2; break; case QMARK: #ifdef LEXER do c = exgetc(); while (isspace(c)); if (c != ':') { yyerror("'?' without ':' in #if"); outp--; return 0; } #else if ((c = exgetc()) != ':') yyerrorp("'?' without ':' in %cif"); #endif if (value) { cond_get_exp(1); value = value2; } else value = cond_get_exp(1); break; } } outp--; return value; } static void handle_cond (int c) { ifstate_t *p; if (!c) skip_to("else", "endif"); p = ALLOCATE(ifstate_t, TAG_COMPILER, "handle_cond"); p->next = iftop; iftop = p; p->state = c ? EXPECT_ENDIF : EXPECT_ELSE; }
int main ( int argc, char *argv[] ) { int exit_val; fribidi_boolean file_found; char *s; FILE *IN; text_width = 80; do_break = true; do_pad = true; do_mirror = true; do_clean = false; do_reorder_nsm = false; show_input = false; show_visual = true; show_basedir = false; show_ltov = false; show_vtol = false; show_levels = false; char_set = "UTF-8"; bol_text = NULL; eol_text = NULL; input_base_direction = FRIBIDI_PAR_ON; if ((s = (char *) getenv ("COLUMNS"))) { int i; i = atoi (s); if (i > 0) text_width = i; } #define CHARSETDESC 257 #define CAPRTL 258 /* Parse the command line with getopt library */ /* Must set argv[0], getopt uses it to generate error messages */ argv[0] = appname; while (1) { int option_index = 0, c; static struct option long_options[] = { {"help", 0, 0, 'h'}, {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, {"debug", 0, 0, 'd'}, {"test", 0, 0, 't'}, {"charset", 1, 0, 'c'}, #if FRIBIDI_MAIN_USE_ICONV_H+0 #else {"charsetdesc", 1, 0, CHARSETDESC}, {"caprtl", 0, 0, CAPRTL}, #endif /* FRIBIDI_MAIN_USE_ICONV_H */ {"showinput", 0, (int *) (void *) &show_input, true}, {"nopad", 0, (int *) (void *) &do_pad, false}, {"nobreak", 0, (int *) (void *) &do_break, false}, {"width", 1, 0, 'w'}, {"bol", 1, 0, 'B'}, {"eol", 1, 0, 'E'}, {"nomirror", 0, (int *) (void *) &do_mirror, false}, {"reordernsm", 0, (int *) (void *) &do_reorder_nsm, true}, {"clean", 0, (int *) (void *) &do_clean, true}, {"ltr", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_LTR}, {"rtl", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_RTL}, {"wltr", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_WLTR}, {"wrtl", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_WRTL}, {"basedir", 0, (int *) (void *) &show_basedir, true}, {"ltov", 0, (int *) (void *) &show_ltov, true}, {"vtol", 0, (int *) (void *) &show_vtol, true}, {"levels", 0, (int *) (void *) &show_levels, true}, {"novisual", 0, (int *) (void *) &show_visual, false}, {0, 0, 0, 0} }; c = getopt_long (argc, argv, "hVvdtc:w:B:E:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: break; case 'h': help (); break; case 'V': version (); break; case 'v': show_basedir = show_ltov = show_vtol = show_levels = true; break; case 'w': text_width = atoi (optarg); if (text_width <= 0) die2 ("invalid screen width `%s'\n", optarg); break; case 'B': bol_text = optarg; break; case 'E': eol_text = optarg; break; case 'd': if (!fribidi_set_debug (true)) die1 ("lib" FRIBIDI " must be compiled with DEBUG option to enable\nturn debug info on.\n"); break; case 't': do_clean = show_input = do_reorder_nsm = true; do_break = false; break; case 'c': char_set = my_fribidi_strdup (optarg); if (!char_set) die1 ("memory allocation failed for char_set!"); break; #if FRIBIDI_MAIN_USE_ICONV_H+0 #else case CAPRTL: char_set = "CapRTL"; break; case CHARSETDESC: char_set = optarg; char_set_num = fribidi_parse_charset (char_set); if (!char_set_num) die2 ("unrecognized character set `%s'\n", char_set); if (!fribidi_char_set_desc (char_set_num)) die2 ("no description available for character set `%s'\n", fribidi_char_set_name (char_set_num)); else printf ("Descriptions for character set %s:\n" "\n" "%s", fribidi_char_set_title (char_set_num), fribidi_char_set_desc (char_set_num)); exit (0); break; #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ case ':': case '?': die2 (NULL, NULL); break; default: break; } } #if FRIBIDI_MAIN_USE_ICONV_H+0 to_ucs4 = iconv_open ("WCHAR_T", char_set); from_ucs4 = iconv_open (char_set, "WCHAR_T"); #else /* !FRIBIDI_MAIN_USE_ICONV_H */ char_set_num = fribidi_parse_charset (char_set); #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ #if FRIBIDI_MAIN_USE_ICONV_H+0 if (to_ucs4 == (iconv_t) (-1) || from_ucs4 == (iconv_t) (-1)) #else /* !FRIBIDI_MAIN_USE_ICONV_H */ if (!char_set_num) #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ die2 ("unrecognized character set `%s'\n", char_set); fribidi_set_mirroring (do_mirror); fribidi_set_reorder_nsm (do_reorder_nsm); exit_val = 0; file_found = false; while (optind < argc || !file_found) { const char *filename; filename = optind < argc ? argv[optind++] : "-"; file_found = true; /* Open the infile for reading */ if (filename[0] == '-' && !filename[1]) { IN = stdin; } else { IN = fopen (filename, "r"); if (!IN) { fprintf (stderr, "%s: %s: no such file or directory\n", appname, filename); exit_val = 1; continue; } } /* Read and process input one line at a time */ { char S_[MAX_STR_LEN]; int padding_width, break_width; padding_width = show_input ? (text_width - 10) / 2 : text_width; break_width = do_break ? padding_width : 3 * MAX_STR_LEN; while (fgets (S_, sizeof (S_) - 1, IN)) { const char *new_line, *nl_found; FriBidiChar logical[MAX_STR_LEN]; char outstring[MAX_STR_LEN]; FriBidiParType base; FriBidiStrIndex len; nl_found = ""; S_[sizeof (S_) - 1] = 0; len = strlen (S_); /* chop */ if (S_[len - 1] == '\n') { len--; S_[len] = '\0'; new_line = "\n"; } else new_line = ""; /* TODO: handle \r */ #if FRIBIDI_MAIN_USE_ICONV_H+0 { char *st = S_, *ust = (char *) logical; int in_len = (int) len; len = sizeof logical; iconv (to_ucs4, &st, &in_len, &ust, (int *) &len); len = (FriBidiChar *) ust - logical; } #else /* !FRIBIDI_MAIN_USE_ICONV_H */ len = fribidi_charset_to_unicode (char_set_num, S_, len, logical); #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ { FriBidiChar *visual; FriBidiStrIndex *ltov, *vtol; FriBidiLevel *levels; FriBidiStrIndex new_len; fribidi_boolean log2vis; visual = show_visual ? ALLOCATE (FriBidiChar, len + 1 ) : NULL; ltov = show_ltov ? ALLOCATE (FriBidiStrIndex, len + 1 ) : NULL; vtol = show_vtol ? ALLOCATE (FriBidiStrIndex, len + 1 ) : NULL; levels = show_levels ? ALLOCATE (FriBidiLevel, len + 1 ) : NULL; /* Create a bidi string. */ base = input_base_direction; log2vis = fribidi_log2vis (logical, len, &base, /* output */ visual, ltov, vtol, levels); if (log2vis) { if (show_input) printf ("%-*s => ", padding_width, S_); new_len = len; /* Remove explicit marks, if asked for. */ if (do_clean) len = fribidi_remove_bidi_marks (visual, len, ltov, vtol, levels); if (show_visual) { printf ("%s", nl_found); if (bol_text) printf ("%s", bol_text); /* Convert it to input charset and print. */ { FriBidiStrIndex idx, st; for (idx = 0; idx < len;) { FriBidiStrIndex wid, inlen; wid = break_width; st = idx; #if FRIBIDI_MAIN_USE_ICONV_H+0 #else if (char_set_num != FRIBIDI_CHAR_SET_CAP_RTL) #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ while (wid > 0 && idx < len) { wid -= FRIBIDI_IS_EXPLICIT_OR_BN_OR_NSM (fribidi_get_bidi_type (visual[idx])) ? 0 : 1; idx++; } #if FRIBIDI_MAIN_USE_ICONV_H+0 #else else while (wid > 0 && idx < len) { wid--; idx++; } #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ if (wid < 0 && idx > st + 1) idx--; inlen = idx - st; #if FRIBIDI_MAIN_USE_ICONV_H+0 { char *str = outstring, *ust = (char *) (visual + st); int in_len = inlen * sizeof visual[0]; new_len = sizeof outstring; iconv (from_ucs4, &ust, &in_len, &str, (int *) &new_len); *str = '\0'; new_len = str - outstring; } #else /* !FRIBIDI_MAIN_USE_ICONV_H */ new_len = fribidi_unicode_to_charset (char_set_num, visual + st, inlen, outstring); #endif /* !FRIBIDI_MAIN_USE_ICONV_H */ if (FRIBIDI_IS_RTL (base)) printf ("%*s", (int) (do_pad ? (padding_width + strlen (outstring) - (break_width - wid)) : 0), outstring); else printf ("%s", outstring); if (idx < len) printf ("\n"); } } if (eol_text) printf ("%s", eol_text); nl_found = "\n"; } if (show_basedir) { printf ("%s", nl_found); printf ("Base direction: %s", (FRIBIDI_DIR_TO_LEVEL (base) ? "R" : "L")); nl_found = "\n"; } if (show_ltov) { FriBidiStrIndex i; printf ("%s", nl_found); for (i = 0; i < len; i++) printf ("%ld ", (long) ltov[i]); nl_found = "\n"; } if (show_vtol) { FriBidiStrIndex i; printf ("%s", nl_found); for (i = 0; i < len; i++) printf ("%ld ", (long) vtol[i]); nl_found = "\n"; } if (show_levels) { FriBidiStrIndex i; printf ("%s", nl_found); for (i = 0; i < len; i++) printf ("%d ", (int) levels[i]); nl_found = "\n"; } } else { exit_val = 2; } if (show_visual) free (visual); if (show_ltov) free (ltov); if (show_vtol) free (vtol); if (show_levels) free (levels); } if (*nl_found) printf (new_line); } } } return exit_val; }