int X_com(int a, int b, int flags) { K_token atmp, btmp; atmp = K_gettoken(0, a); btmp = K_gettoken(1, b); if (flags & U_BYTE_COMPARE) { return (_X_strcmp(K_gettext(atmp), K_gettext(btmp), flags)); } else { return (_X_cmptokens(atmp, btmp, flags)); } #ifndef lint Z_fatal("this line should never be reached in com"); return (-1); /* Z_fatal never returns, but i need a this line here to stop lint from complaining */ #endif }
/* ** routine to compare two tokens */ static int _X_cmptokens(K_token p1, K_token p2, int flags) { if (K_gettype(p1) != K_gettype(p2)) { return (1); } switch (K_gettype(p1)) { case K_LIT: return (_X_strcmp(K_gettext(p1), K_gettext(p2), flags)); case K_FLO_NUM: return (_X_floatdiff(K_getfloat(p1), K_getfloat(p2), T_picktol(K_gettol(p1), K_gettol(p2)))); default: Z_fatal("fell off switch in _X_cmptokens"); return (-1); /* Z_fatal never returns, but i need a this line here to stop lint from complaining */ } }
/* ** initialize the global tolerance ** should be called only once at the beginning of the program */ void T_initdefault() { static int called_before = 0; if (called_before) { Z_fatal("T_initdefault called more than once\n"); } /* ** if the default tolerance was set somewhere else ** don't set it here */ if (T_isnull(_T_gtol)) { T_defatol(_T_ADEF); T_defrtol(_T_RDEF); } called_before = 1; }
static void _Y_doargs(int argc, char **argv, char **file1, char **file2, int *max_d) { char *ptr; /* ** mark maximum number of tokens as being unset */ *max_d = -1; while (argc > 1 && argv[1][0] == '-') { switch (argv[1][1]) { case 't': _Y_flags |= U_TOKENS; break; case 'w': _Y_flags |= U_INCLUDE_WS; break; case 'b': _Y_flags |= U_BYTE_COMPARE; break; case 'c': _Y_flags |= U_NO_CASE; break; case 'd': _Y_flags |= U_NEED_DECIMAL; break; case 'm': _Y_flags |= U_INC_SIGN; break; case 'a': SETPTR; T_defatol(ptr); break; case 'r': SETPTR; T_defrtol(ptr); break; case 'i': T_defitol(); break; case 'e': _Y_eflag = 1; break; case 'v': _Y_vflag = 1; break; case 'q': Z_setquiet(); break; case 's': SETPTR; _Y_cmdlines(ptr); break; case 'f': { extern FILE *fopen(const char *, const char *); char buf[Z_LINELEN]; FILE *cmdfile; SETPTR; if ((FILE *) NULL == (cmdfile = fopen(ptr, "r"))) { Z_fatal("can't open command file\n"); } while ((char *) NULL != (char *) fgets(buf, Z_LINELEN, cmdfile)) { C_addcmd(buf); } (void) fclose(cmdfile); break; } /* ** useful commands for ** the C programming language */ case 'C': C_addcmd("literal \" \" \\ "); C_addcmd("comment /* */ "); C_addcmd("literal && "); C_addcmd("literal || "); C_addcmd("literal <= "); C_addcmd("literal >= "); C_addcmd("literal != "); C_addcmd("literal == "); C_addcmd("literal -- "); C_addcmd("literal ++ "); C_addcmd("literal << "); C_addcmd("literal >> "); C_addcmd("literal -> "); C_addcmd("addalpha _ "); C_addcmd("tol a0 "); break; /* ** useful commands for ** the Bourne shell programming language */ case 'S': C_addcmd("literal ' ' \\ "); C_addcmd("comment # $ "); C_addcmd("tol a0 "); break; /* ** useful commands for ** the Fortran programming language */ case 'F': C_addcmd("literal ' ' ' "); C_addcmd("comment ^C $ "); C_addcmd("tol a0 "); break; /* ** useful commands for ** the Lisp programming language */ case 'L': C_addcmd("literal \" \" "); C_addcmd("comment ; $ "); C_addcmd("tol a0 "); break; /* ** useful commands for ** the Modula-2 programming language */ case 'M': C_addcmd("literal ' ' "); C_addcmd("literal \" \" "); C_addcmd("comment (* *) "); C_addcmd("literal := "); C_addcmd("literal <> "); C_addcmd("literal <= "); C_addcmd("literal >= "); C_addcmd("tol a0 "); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': *max_d = atoi(&argv[1][1]); break; default: Z_fatal("don't understand arguments\n"); } ++argv; --argc; } if (argc != 3) Z_fatal("spiff requires two file names.\n"); *file1 = argv[1]; *file2 = argv[2]; }
/* ** execute a single command */ static void _C_do_a_cmd(char *str) { /* ** place holder for the beginning of the string */ char *beginning = str; S_skipspace(&str); /* ** set the command string to allow embedded commands */ if (!S_wordcmp(str, "command")) { S_nextword(&str); if (strlen(str) >= Z_WORDLEN) { Z_fatal("command word is too long"); } S_wordcpy(_C_cmdword, str); } /* ** set the tolerances */ else if (!S_wordcmp(str, "tol")) { S_nextword(&str); T_tolline(str); } /* ** add a comment specification */ else if (!S_wordcmp(str, "comment")) { S_nextword(&str); if (strlen(str) >= Z_WORDLEN) { Z_fatal("command word is too long"); } W_addcom(str, 0); } else if (!S_wordcmp(str, "nestcom")) { S_nextword(&str); if (strlen(str) >= Z_WORDLEN) { Z_fatal("command word is too long"); } W_addcom(str, 1); } /* ** add a literal string specification */ else if (!S_wordcmp(str, "literal")) { S_nextword(&str); if (strlen(str) >= Z_WORDLEN) { Z_fatal("command word is too long"); } W_addlit(str); } else if (!S_wordcmp(str, "resetcomments")) { W_clearcoms(); } else if (!S_wordcmp(str, "resetliterals")) { W_clearlits(); } else if (!S_wordcmp(str, "beginchar")) { S_nextword(&str); W_setbolchar(*str); } else if (!S_wordcmp(str, "endchar")) { S_nextword(&str); W_seteolchar(*str); } else if (!S_wordcmp(str, "addalpha")) { S_nextword(&str); P_addalpha(str); } else if ((0 == strlen(str)) || !S_wordcmp(str, "rem") || ('#' == *str)) { /* do nothing */ } else { (void) sprintf(Z_err_buf, "don't understand command %s\n", beginning); Z_fatal(Z_err_buf); } return; }
/* ** compare two F_floats using a tolerance */ static int _X_floatdiff(F_float p1,F_float p2,T_tol the_tol) { F_float diff, float_tmp; T_tol tol_tmp; /* ** check for null tolerance list */ if (T_isnull(the_tol)) { Z_fatal("_X_floatdiff called with a null tolerance"); } /* ** look for an easy answer. i.e -- check ** to see if any of the tolerances are of type T_IGNORE ** or if the numbers are too small to exceed an absolute ** tolerance. ** if so, return immediately */ for(tol_tmp=the_tol; !(T_isnull(tol_tmp)) ;tol_tmp=T_getnext(tol_tmp)) { if ((T_IGNORE == T_gettype(tol_tmp)) || /* ** take a look at the exponents before you bother ** with the mantissas */ ((T_ABSOLUTE == T_gettype(tol_tmp)) && !F_zerofloat(T_getfloat(tol_tmp)) && (F_getexp(p1) < F_getexp(T_getfloat(tol_tmp))-1) && (F_getexp(p2) < F_getexp(T_getfloat(tol_tmp))-1))) { return(0); } } /* ** ok, we're going to have to do some arithmetic, so ** first find the magnitude of the difference */ if (F_getsign(p1) != F_getsign(p2)) { diff = F_floatmagadd(p1,p2); } else { diff = F_floatsub(p1,p2); } /* ** now check to see if the difference exceeds any tolerance */ for(tol_tmp=the_tol; !(T_isnull(tol_tmp)) ;tol_tmp=T_getnext(tol_tmp)) { float_tmp = T_getfloat(tol_tmp); if (T_gettype(tol_tmp) == T_ABSOLUTE) { /* do nothing */ } else if (T_gettype(tol_tmp) == T_RELATIVE) { if (F_floatcmp(p1,p2) > 0) { float_tmp = F_floatmul(p1, float_tmp); } else { float_tmp = F_floatmul(p2, float_tmp); } } else { Z_fatal("bad value for type of tolerance in floatdiff"); } /* ** if we pass this tolerance, then we're done */ if (F_floatcmp(diff,float_tmp) <= 0) { return(0); } } /* ** all of the tolerances were exceeded */ return(1); }