home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / comm1 / slrn-110.lzh / SLEARN.SLT < prev    next >
Text File  |  1990-07-22  |  12KB  |  422 lines

  1. //
  2. //  Module:       slearn.slt
  3. //                Part of SLEARN  (SALT learn utility)  v1.10
  4. //                Copyright (C) 1989-1990 Paul Roub.  All rights reserved.
  5. //  Version:      1.10
  6. //  Description:  SALT script to invoke SLEARN.EXE from Telix.
  7. //
  8. //                Recommended usage:  using TFE, set up a dialing directory
  9. //                entry for the BBS you wish to learn a script for.  Include
  10. //                'slearn' as the Script field for that system.
  11. //
  12. //  NOTES:        This script assumes that CS.EXE and SLEARN.EXE are both
  13. //                available from the script directory.  This means they must
  14. //                either be IN the script directory itself, or on the DOS
  15. //                PATH.
  16. //
  17. //                Whenver possible, this script uses the run() function to
  18. //                run other programs , as it is faster and uses less memory.
  19. //                However, we do occasionally have to call some DOS commands,
  20. //                so dos() is used there.  This is a bit inconsistent, but it
  21. //                is important, especially when calling SLEARN.EXE.  If that
  22. //                takes too long, we could lose characters, thus rendering
  23. //                SLEARN all but useless.
  24. //
  25. //  Author:       Paul Roub
  26. //
  27. //<f>
  28.  
  29.  
  30. //  various strings used by this script
  31. //
  32. str       CmdLine [128];              //  SLEARN command line
  33. str       fn      [ 12];              //  name of script file to create
  34. str       Deffn   [ 12];              //  'best guess' script file name
  35.  
  36. str       tmpfn   [] = "slearn.$$$";  //  name of temporary SALT file
  37.  
  38.                                       //  comm parameters:
  39. str       BaudStr [  6];              //    baud rate
  40. str       SetStr  [  3];              //    settings (e.g. E71)
  41.  
  42.  
  43. //  Change these if you hate the colors this script asks questions in.
  44. //
  45. int       AnswerColor   = 112;        //  default: black on grey
  46. int       BoxColor      = 7;          //  default: grey on black
  47. int       QuestionColor = 15;         //  default: white on black
  48.  
  49.  
  50. //  Set UseSetScr = 0 if you don't want SLEARN to add the new script's name
  51. //  to TELIX.FON, or if you use more than one dialing directory.
  52. //  Remember that even if UseSetScr == 1, SLEARN will still ask your
  53. //  permission before adding the script to TELIX.FON.
  54. //
  55. int       UseSetScr    = 1;           // default: add new script to TELIX.FON
  56.  
  57.  
  58. //<f>
  59. main()
  60. {
  61.   int       result;
  62.   int       SaveFile;
  63.   str       numstr[5];
  64.   str       dummy[81];
  65.   int       fonfile;
  66.   int       EntryPos;
  67.   str       entname[25];
  68.  
  69.   itos(get_baud(), BaudStr);          // get comm. parameters  (baud)
  70.  
  71.   if      (get_parity() == 0)         //                       (parity)
  72.     SetStr = "N";
  73.   else if (get_parity() == 1)
  74.     SetStr = "E";
  75.   else // (get_parity() == 2)
  76.     SetStr = "O";
  77.  
  78.   if (get_datab() == 7)               //                       (data bits)
  79.     strcat(SetStr, "7");
  80.   else
  81.     strcat(SetStr, "8");
  82.  
  83.   if (get_stopb() == 1)               //                       (stop bits)
  84.     strcat(SetStr, "1");
  85.   else
  86.     strcat(SetStr, "2");
  87.  
  88.   if (_script_dir != "")              // change to script dir, if any
  89.     newdir(_script_dir);
  90.   else
  91.     newdir(_telix_dir);
  92.  
  93.   if (! filefind("slearn.cfg", 0, dummy))
  94.   {
  95.     call("slconfig");
  96.     if (_script_dir != "")            // change to script dir, if any
  97.       newdir(_script_dir);
  98.     else
  99.       newdir(_telix_dir);
  100.   }
  101.  
  102.                                       // build the command line:
  103.   CmdLine = "-b";                     //   baud
  104.   strcat(CmdLine, BaudStr );
  105.   strcat(CmdLine, " -s"   );          //   settings
  106.   strcat(CmdLine, SetStr  );
  107.   strcat(CmdLine, " "     );
  108.  
  109.   if (_strip_high)
  110.     strcat(CmdLine, "-h ");
  111.  
  112.   if (_entry_name != "")
  113.   {
  114.     strcat(CmdLine, "-n ^"");         //   entry name, if any
  115.     strcat(CmdLine, _entry_name);
  116.     strcat(CmdLine, "^" ");
  117.   }
  118.  
  119.   if (_entry_pass != "")              // _entry_pass, perhaps
  120.   {
  121.     strcat(CmdLine, "-w ^"");
  122.     strcat(CmdLine, _entry_pass);
  123.     strcat(CmdLine, "^" ");
  124.   }
  125.  
  126.   strcat(CmdLine, tmpfn   );          //   and the file to create
  127.  
  128.   printsc("running 'slearn.exe ");
  129.   printsc(CmdLine);
  130.   prints ("'");
  131.   result = run("slearn.exe", CmdLine, 0); // and run slearn
  132.  
  133.   clear_scr();
  134.  
  135.   if (result == -1)                   // Couldn't run it
  136.   {
  137.     prints("");
  138.     prints("Error: Couldn't find or run SLEARN.EXE.  SLEARN.EXE must either be in the");
  139.     prints("       TELIX script directory, or in a directory specified in the PATH");
  140.     prints("       environment variable.");
  141.     prints("");
  142.  
  143.     return;
  144.   }
  145.  
  146.   SaveFile = YesOrNo("Save the new script file?", "y");
  147.  
  148.   if (SaveFile)
  149.   {
  150.     DefSLTname(Deffn);                // get default SALT file name
  151.  
  152.     //  see what the user wants to call it
  153.     //
  154.     if (ask("Save with what name?", Deffn, fn, 12) == -1)
  155.       fn = Deffn;
  156.  
  157.     if (strpos(fn, ".", 0) == -1)     // tack on SLT extension if necessary
  158.       strcat(fn, ".slt");
  159.  
  160.     prints("");
  161.  
  162.     CmdLine = "copy ";                // copy temp file to permanent file
  163.     strcat(CmdLine, tmpfn);
  164.     strcat(CmdLine, " "  );
  165.     strcat(CmdLine, fn   );
  166.     dos(CmdLine, 0);
  167.  
  168.     CmdLine = "del ";                 // delete temp file
  169.     strcat(CmdLine, tmpfn);
  170.     dos(CmdLine, 0);
  171.  
  172.     SaveFile = YesOrNo("Compile the new script file?", "y");
  173.  
  174.     if (SaveFile)
  175.     {
  176.       result = run("cs.exe", fn, 0);  // and compile the new file
  177.  
  178.       if (result == -1)               // Couldn't run it
  179.       {
  180.         prints("");
  181.         prints("Error: Couldn't find or run CS.EXE.  CS.EXE must either be in the");
  182.         prints("       TELIX script directory, or in a directory specified in the PATH");
  183.         prints("       environment variable.");
  184.         prints("");
  185.       }
  186.     }
  187.   }
  188.   else
  189.   {
  190.     CmdLine = "del ";                 // otherwise, just trash the temp
  191.     strcat(CmdLine, tmpfn);
  192.     dos(CmdLine, 0);
  193.     result = 0;
  194.   }
  195.  
  196.   if (_telix_dir != "")               // get back where we started
  197.     newdir(_telix_dir);
  198.  
  199.   if (SaveFile && UseSetScr && (result == 0))
  200.     if (YesOrNo("Install this script in TELIX.FON?", "y"))
  201.     {
  202.       if (! filefind("zzz.fon", 0, dummy))
  203.       {
  204.         dos("copy telix.fon zzz.fon", 0);
  205.         status_wind("Dummy FON file ZZZ.FON was created.", 30);
  206.       }
  207.  
  208.       if (! filefind("zzz.fon", 0, dummy))
  209.         status_wind("Dummy FON file ZZZ.FON does not exist!  Can't write to TELIX.FON", 30);
  210.       else
  211.       {
  212.         loadfon("zzz.fon");
  213.  
  214.         dummy = "Setting script for entry ";
  215.         itos(_entry_enum, numstr);
  216.         strcat(dummy, numstr);
  217.         strcat(dummy, " (");
  218.         strcat(dummy, _entry_name);
  219.         strcat(dummy, ") to ^"");
  220.         strcat(dummy, fn);
  221.         strcat(dummy, "^"");
  222.  
  223.         status_wind(dummy, 20);
  224.  
  225.         fonfile = fopen("telix.fon", "r+");
  226.         if (fonfile <= 0)
  227.           status_wind("Error opening TELIX.FON", 20);
  228.         else
  229.         {
  230.           EntryPos = 64 + (86 * (_entry_enum - 1));
  231.           fseek(fonfile, EntryPos);
  232.           fread(entname, 25, fonfile);
  233.           setchr(entname, 25, 0);
  234.  
  235.           if (entname != _entry_name)
  236.             status_wind("Entry name does not match _entry_name.  Can't write to TELIX.FON");
  237.           else
  238.           {
  239.             setchr(fn, strpos(fn, ".", 0), 0);
  240.             fseek (fonfile, EntryPos + 46);
  241.             fwrite(fn, 12, fonfile);
  242.           }
  243.  
  244.           fclose(fonfile);
  245.         }
  246.  
  247.         loadfon("telix.fon");
  248.       }
  249.     }
  250.  
  251.   status_wind("Thank you for using SLEARN -- support shareware!", 15);
  252.  
  253.   return;
  254. }
  255.  
  256.  
  257. //<f>
  258. //
  259. //  Function:     DefSLTname
  260. //  Description:  Try to guess a default SALT script name from the Telix
  261. //                script directory and entry name.  We use the first 8
  262. //                alphanumeric characters in _entry_name, plus .SLT.  If
  263. //                _entry_name is blank (or devoid of alphanumerics), then we
  264. //                use the name 'new.slt' instead.
  265. //  Parameters:   str name - default name (returned)
  266. //  Returns:      nothing
  267. //
  268. DefSLTname(str name)
  269. {
  270.   int     DstCount, SrcCount;         // indices of source and dest strings
  271.  
  272.   DstCount = 0;
  273.   SrcCount = 0;
  274.  
  275.   while ((DstCount < 8) && (SrcCount < 25))
  276.   {
  277.     if (isalnum(subchr(_entry_name, SrcCount)))
  278.     {
  279.       setchr(name, DstCount, subchr(_entry_name, SrcCount));
  280.       DstCount = DstCount + 1;
  281.     }
  282.  
  283.     SrcCount = SrcCount + 1;
  284.   }
  285.  
  286.   setchr(name, DstCount, 0);
  287.  
  288.   if (strlen(name) == 0)
  289.     name = "new.slt";
  290.   else
  291.     strcat(name, ".slt");
  292.  
  293.   strlower(name);
  294.  
  295.   return;
  296. }
  297.  
  298.  
  299. //<f>
  300. //
  301. //  Function:     ask
  302. //  Description:  prompt the user for a string
  303. //  Parameters:   str question - prompt
  304. //                str default  - default value for answer
  305. //                str answer   - answer (returned)
  306. //                int max      - maximum length of answer
  307. //  Returns:      -1 if ESC hit
  308. //                length of answer otherwise
  309. //
  310. ask(str question, str default, str answer, int max)
  311. {
  312.   int       left, right;              // left and right sides of box
  313.   int       len;                      // length of longest string to display
  314.   int       result;                   // return code
  315.   int       SaveHandle;               // handle of saved screen area
  316.   int       top, bottom;              // top and bottom sides of box
  317.   int       x, y;                     // used to save/restore cursor position
  318.  
  319.   top    = 9;
  320.   bottom = 9 + 6;
  321.  
  322.   len = strlen(question);
  323.   if (len < strlen(default) + 9)
  324.     len = strlen(default) + 9;
  325.   if (len < max)
  326.     len = max;
  327.  
  328.   left = (80 - (len + 4)) / 2;
  329.   right = left + len + 3;
  330.  
  331.   x = getx();
  332.   y = gety();
  333.   SaveHandle = vsavearea(left, top, right, bottom);
  334.   box(left, top, right, bottom, 3, 0, BoxColor);
  335.  
  336.   pstraxy(question,    left + 2,  top + 2, QuestionColor);
  337.   pstraxy("Default: ", left + 2,  top + 3, BoxColor     );
  338.   pstraxy(default,     left + 11, top + 3, BoxColor     );
  339.  
  340.   //  Draw an 'input field' so the user can see right off the bat how long
  341.   //  'answer' is allowed to be.
  342.   //
  343.   pstraxy("            ", left + 2, bottom - 2, AnswerColor );
  344.  
  345.   result = getsxy(answer, max, left + 2, bottom - 2, AnswerColor);
  346.  
  347.   vrstrarea(SaveHandle);
  348.   gotoXY(x, y);
  349.  
  350.   if (result == 0)
  351.   {
  352.     answer = default;
  353.     result = strlen(answer);
  354.   }
  355.  
  356.   return(result);
  357. }
  358.  
  359.  
  360. //<f>
  361. //
  362. //  Function:     YesOrNo
  363. //  Description:  asks yes or no questions, with default
  364. //                displays question and default, and accepts one character
  365. //                if this character is CR or ESC, then default[0] is used
  366. //                keeps receiving characters until char is 'y' or 'n'
  367. //  Parameters:   str question - question to ask
  368. //                str default  - default string - "y" or "n"
  369. //  Returns:      1 if 'y' was entered
  370. //                2 if 'n' was entered
  371. //
  372. YesOrNo(str question, str default)
  373. {
  374.   int       ch;                       // user's input character
  375.   int       left, right;              // left and right sides of box
  376.   int       len;                      // length of question
  377.   int       SaveArea;                 // handle of saved screen area
  378.   int       top, bottom;              // top and bottom sides of box
  379.   int       x, y;                     // used to save/restore cursor position
  380.  
  381.   if (strlen(default) > 1)
  382.     setchr(default, 1, 0);
  383.  
  384.   strlower(default);
  385.   if ((default != "y") && (default != "n"))
  386.     default = " ";
  387.  
  388.   len = strlen(question);
  389.  
  390.   top = 10;
  391.   bottom = top + 4;
  392.   left = (80 - (len + 6)) / 2;
  393.   right = left + len + 5;
  394.  
  395.   x = getx();
  396.   y = gety();
  397.  
  398.   SaveArea = vsavearea(left, top, right, bottom);
  399.   box(left, top, right, bottom, 3, 0, BoxColor);
  400.  
  401.   pstraxy(question, left + 2,           top + 2, QuestionColor);
  402.   pstraxy(default,  left + 2 + len + 1, top + 2, AnswerColor);
  403.   gotoxy (left + 2 + len + 1, top + 2);
  404.  
  405.   do
  406.   {
  407.     ch = inkeyw();
  408.  
  409.     if ((ch == 13) || (ch == 27))
  410.       ch = subchr(default, 0);
  411.  
  412.     ch = tolower(ch);
  413.   } while ((ch != 'y') && (ch != 'n'));
  414.  
  415.   vrstrarea(SaveArea);
  416.  
  417.   gotoxy(x, y);
  418.  
  419.   return(ch == 'y');                  // true if 'y', false if 'n'
  420. }
  421.  
  422.