home *** CD-ROM | disk | FTP | other *** search
/ 1st Canadian Shareware Disc / 1st_Canadian_Shareware_Disc_1991.ISO / comms / slrn_100 / slearn.slt < prev    next >
Text File  |  1990-01-16  |  12KB  |  404 lines

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