home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / mktran.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-23  |  5.9 KB  |  320 lines

  1. #
  2. /*    Program to help make a transcript file  */
  3.  
  4. #include    <stdio.h>
  5. #include    "d_returns.h"
  6. #include    "ll_log.h"
  7. #include    "d_proto.h"
  8. #include    "d_script.h"
  9.  
  10. #define        SCRIPT        "Script"
  11. #define        TRANFILE    "Tranfile"
  12. #define        LOGFILE        "Log"
  13.  
  14. struct ll_struct log = {
  15.     "Log",
  16.     "mktran log",
  17.     '\0177',
  18.     100,
  19.     0,
  20.     0,
  21.     0,
  22. };
  23.  
  24. FILE *input, *output;
  25. char *sfile;
  26.  
  27. main (argc, argv)
  28.   int argc;
  29.   char **argv;
  30.     {
  31.     register int command;
  32.     char linebuf[MAXSCRLINE +2], *fields[MAXFIELDS];
  33.     register int result;
  34.     int nfields;
  35.  
  36.     
  37.     /*  init various things:  logging, files, etc.  */
  38.     init (argc, argv);
  39.  
  40.     /*  run through that part of the script which already exists  */
  41.     printf("Starting to process existing script.\n");
  42.     if ((result = doscript ()) < 0)
  43.     {
  44.     printf("Problem encountered in processing existing script.\n");
  45.     printf("Return value of %d\n", result);
  46.     printf("Aborting.\n");
  47.     exit(-1);
  48.     }
  49.     printf("Existing script processed successfully\n");
  50.  
  51.     /*  Now add more to it  */
  52.     for (;;)
  53.     {
  54.     /*  Read a new command line from the terminal  */
  55.     printf ("Enter command: ");
  56.     fflush (stdout);
  57.  
  58.     command = d_cmdget (linebuf, &nfields, fields, stdin);
  59.     if (command < 0)
  60.     {
  61.         /*  may be an error;  may also be the command to turn
  62.          *  the line around.  Check it.
  63.          */
  64.         if (turnar (linebuf))
  65.         doread();
  66.         else
  67.         {
  68.         printf ("Unrecognized command.  getcmd returns %d\n", command);
  69.         printf ("Try again\n");
  70.         }
  71.         continue;
  72.     }
  73.  
  74.     /*  write the new line to the script  */
  75.     wrtscr ();
  76.  
  77.  
  78.     /*  process the new line  */
  79.     result = d_cmdproc (command, nfields, fields);
  80.  
  81.     /*  check on the results of the processing  */
  82.     switch (result)
  83.     {
  84.         case D_CONTIN:
  85.         continue;
  86.  
  87.         case D_OK:
  88.         quit ();
  89.  
  90.         case D_FATAL:
  91.         printf ("Fatal error on command line.  Abort\n");
  92.         quit ();
  93.     }
  94.     }
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. init (argc, argv)
  103.   int argc;
  104.   char **argv;
  105.     {
  106.     extern int errno;
  107.     extern FILE *d_scfp;
  108.     extern int d_master, d_snseq, d_rcvseq;
  109.     extern int d_debug;
  110.     register int result;
  111.     register int word;
  112.  
  113.     printf("Usage: %s [<script file> [<debug value> [<transcript file>]]]\n",
  114.             argv[0]);
  115.     /*  First, determine the name of the script file  */
  116.     if (argc > 1)
  117.     sfile = argv[1];
  118.     else
  119.     sfile = SCRIPT;
  120.  
  121.     /*  open it for appending  */
  122.     output = fopen (sfile, "a");
  123.     if (output == NULL)
  124.     {
  125.     printf ("Can't open '%s': errno %d - ", sfile, errno);
  126.     fflush (stdout);
  127.     perror ("");
  128.     exit (-1);
  129.     }
  130.     fseek (output, 0L, 2);
  131.  
  132.     /*  check for debugging  */
  133.     if (argc > 2)
  134.     d_debug = atoi (argv[2]);
  135.     else
  136.     d_debug = 0;
  137.  
  138.     /*  open the log and transcript files  */
  139.     if ((result = d_opnlog (&log)) < 0)
  140.     {
  141.     printf("Couldn't open log file;  d_opnlog returns %d\n", result);
  142.     exit(-1);
  143.     }
  144.     d_init ();
  145.  
  146.  
  147.     /*  check to see if a transcript is wanted  */
  148.     if (argc > 3)
  149.     if (atoi (argv[3]) != 0)
  150.         if ((result = d_tsopen (TRANFILE)) < 0)
  151.         return (result);
  152.  
  153.     d_master = 1;
  154.     d_snseq = 3;
  155.     d_rcvseq = 3;
  156.  
  157.     /*  at last, more or less done  */
  158.     return (0);
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. wrtscr ()
  167.     {
  168.     extern char d_origlin[];
  169.     register int length, nwrote;
  170.  
  171.     length = strlen (d_origlin);
  172.  
  173.     nwrote = fwrite (d_origlin, sizeof (char), length, output);
  174.     if (nwrote != length)
  175.     {
  176.     printf ("fwrite tried %d, but returned %d\n", length, nwrote);
  177.     printf ("Couldn't write to script file: ");
  178.     fflush (stdout);
  179.     perror ("");
  180.     fflush (stderr);
  181.     exit (-1);
  182.     }
  183.  
  184.     return;
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. doscript ()
  192.     {
  193.     extern FILE *d_scfp;
  194.     extern int errno;
  195.     int nselect, result;
  196.     char linebuf[MAXSCRLINE + 2],
  197.      *fields[MAXFIELDS];
  198.  
  199.  
  200.     /*  open the script file  */
  201.     if ((result = d_scopen (sfile, 0, (char **) 0)) < 0)
  202.     {
  203.     printf ("Couldn't open '%s': errno %d - ", sfile, errno);
  204.     fflush (stdout);
  205.     perror ("");
  206.     fflush (stderr);
  207.     exit (-1);
  208.     }
  209.  
  210.     /*  now process it as a normal script with the exception that
  211.      *  an EOF means read from the standard input instead of stop.
  212.      */
  213.     for (;;)
  214.     {
  215.     result = d_scrblk ();
  216.     switch (result)
  217.     {
  218.         case D_OK:
  219.         return (D_OK);
  220.  
  221.         case D_EOF:
  222.         case D_NFIELDS:
  223.         case D_QUOTE:
  224.         case D_UNKNOWN:
  225.         case D_FATAL:
  226.         /*  a fatal error  */
  227.         return (D_FATAL);
  228.  
  229.         case D_EOF:
  230.         /*  a normal termination since we expect the user to
  231.          *  add more immediately.
  232.          */
  233.         return (0);
  234.  
  235.         case D_NONFATAL:
  236.         /*  An error that may be recoverable.  If there is
  237.          *  an alternate, use it.
  238.          */
  239.         if (nselect <= 0)
  240.             /*  Not within a select block; therefore, not alt  */
  241.             return (D_FATAL);
  242.  
  243.         /*  We are within a select block;  go to the alternate  */
  244.         if ((result = d_nxtalt ()) < 0)
  245.             return (result);
  246.         switch (result)
  247.         {
  248.             case S_ALT:
  249.             continue;
  250.  
  251.             case S_SELEND:
  252.             /*  no more alternates;  return error  */
  253.             return (D_FATAL);
  254.  
  255.             default:
  256.             d_scerr ("Bad syntax scriptfile");
  257.             return (D_FATAL);
  258.         }
  259.  
  260.  
  261.         case S_SELST:
  262.         /*  The next line had better be an alternate  */
  263.         if (d_cmdget (linebuf, &nfields, fields, d_scfp) != S_ALT)
  264.         {
  265.             d_scerr ("Missing 'alternate' after 'begin'");
  266.             return (D_FATAL);
  267.         }
  268.         nselect++;
  269.         continue;
  270.  
  271.         case S_SELEND:
  272.         /*  verify that there was a select block being
  273.          *  looked at.  If so, then the last alternate
  274.          *  was the successful one.  Continue normally.
  275.          */
  276.         if (nselect-- <= 0)
  277.         {
  278.             d_scerr ("Inappropriate select end");
  279.             return (D_FATAL);
  280.         }
  281.         continue;
  282.  
  283.         case S_ALT:
  284.         /*  First, make sure the context was correct  */
  285.         if (nselect <= 0)
  286.         {
  287.             d_scerr ("Inappropriate alt\n");
  288.             return (D_FATAL);
  289.         }
  290.         /*  If we get here, then an alternate within a select
  291.          *  was completed successfully.  Move on.
  292.          */
  293.         if ((result = d_selend ()) < 0)
  294.             return (result);
  295.         nselect--;
  296.         switch (result)
  297.         {
  298.             case S_SELEND:
  299.             continue;
  300.  
  301.             default:
  302.             d_scerr ("error in format of script file");
  303.             return (D_FATAL);
  304.         }
  305.     }
  306.  
  307.     }
  308. }
  309.  
  310.  
  311.  
  312.  
  313. quit ()
  314.     {
  315.  
  316.     printf ("Quitting\n");
  317.     exit (0);
  318. }
  319.  
  320.