// LAD.C #include #include #include #include "../Basics/basic.h" #define Debug true static uint MaxVarNum = 0; static char** Variable = NULL; void SubstituteVariables (char* S) { if (StrIsEmpty (S)) return; ForEver { char* VarStart = strstr (S, "(*"); if (VarStart == NULL) break; for (uint i = 2; VarStart [i] != 0 && VarStart [i] != ')'; i++); assert (VarStart [i] != 0); uint VarNum; if (sscanf (& VarStart [2], "%u", & VarNum) != 1) RunError; assert (VarNum > 0 && VarNum <= MaxVarNum); StrReplace (VarStart, i + 1, Variable [VarNum - 1]); } } boolean Execute (const char* InFName, uint& LineNum) { FILE* FIn = fopen (InFName, "r"); if (FIn == NULL) { printf ("Cannot open file \"%s\"\n", InFName); exit (1); } SkipLines (FIn, LineNum); // Preparing parameter file printf ("\n"); const uint SLen = 1024; char Program [SLen] = ""; char ParamFName [SLen]; FILE* FOut = NULL; char S [SLen]; while (ReadLine (FIn, S, SLen)) { LineNum++; StrTrimTrailing (S); SubstituteVariables (S); if (StrIsEmpty (S)) if (FOut == NULL) /*Nothing*/; else break; else if (S [0] == '*') // Comment /*Nothing*/; else if (S [0] == ':' || S [0] == '!') { if (FOut != NULL) { printf ("Line %u: %s\n", LineNum, S); printf ("Blank line should be inserted\n"); exit (1); } boolean BuiltinCommand = (S [0] == '!'); S [0] = ' '; StrTrimLeading (S); if (! BuiltinCommand) strcpy (Program, "/rutcor/u1/brover/c/LAD/"); strcat (Program, S); tmpnam (ParamFName); //strcat (ParamFName, "-"); ?? //strcat (ParamFName, S); ?? strcat (ParamFName, ".in"); FOut = fopen (ParamFName, "w"); assert (FOut != NULL); } else { if (FOut == NULL) { printf ("Line %u: %s\n", LineNum, S); printf ("\": \" should be specified\n"); exit (1); } uint i = strlen (S); while (S [i] != ' ' && S [i] != ':') i--; fprintf (FOut, "%s\n", & S [i + 1]); printf ("%s\n", S); } } fclose (FIn); if (FOut == NULL) return false; boolean ParamExist = ! FileEmpty (FOut); fclose (FOut); // Execution if (ParamExist) { strcat (Program, " < "); strcat (Program, ParamFName); //strcat (Program, " > /dev/null"); } printf ("\n%s\n", Program); int Result = system (Program); // Delete ParamFName remove (ParamFName); return Result == 0; } void Instruction () { printf ("Logical Analysis of Data: 3 steps in one run\n"); printf ("Usage: LAD [ ...]\n"); printf (", K >=1, substitues expression \"(*K)\" in the parameter file\n"); printf ("Example of parameter file is \"LAD.in\"\n"); exit (2); } int main (int argc, char *argv[]) { if (argc < 2) Instruction (); MaxVarNum = argc - 2; Variable = new char* [MaxVarNum]; for (uint i = 0; i < MaxVarNum; i++) Variable [i] = argv [i + 2]; uint LineNum = 0; while (Execute (argv [1], LineNum)); delete Variable; return 0; }