home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume23 / asp / part01 / asp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-16  |  6.6 KB  |  299 lines

  1. /*
  2.  * This code is copyright ADE Muffett, September 1991, and is distributed as
  3.  * part of the ASP .plan description language compiler.  This code is freely
  4.  * redistributable as long as this copyright notice remains intact. No
  5.  * responsibility is assumed by the author for any situation which arises
  6.  * from the use of this code, including insanity, late nights, or disk
  7.  * storage problems.
  8.  */
  9.  
  10. /* Unix is a Bellmark of Lab Tradeoratories */
  11.  
  12. #include "asp.h"
  13. #undef TESTING
  14.  
  15. /* GLOBAL DATA */
  16. int speed;
  17. int smooth;
  18. char stdline[SCREENWIDTH + 1];
  19. char anim_buffer[SCREENWIDTH + 1];
  20. char foreground[SCREENWIDTH + 1];
  21. char background[SCREENWIDTH + 1];
  22. struct anim_object aobjects[MAX_ANIM_OBJS];
  23. /* END OF GLOBAL DATA */
  24.  
  25. struct command_structure
  26. {
  27.     char *codeword;
  28.     void (*fn) ();
  29.     char has_args;
  30. };
  31.  
  32. struct command_call
  33. {
  34.     int c_index;
  35.     char *invocation;
  36. };
  37.  
  38. static FILE *fp;
  39. static char version[] = "asp3.2(c)aem@aber";
  40. static struct command_call command_calls[COMMAND_COUNT];
  41. static struct command_structure command_name[COMMAND_COUNT];
  42.  
  43. void
  44. Smooth (command)
  45.     char *command;
  46. {
  47.     sscanf (command, "%*s %d", &smooth);
  48. }
  49.  
  50. void
  51. Speed (command)
  52.     char *command;
  53. {
  54.     sscanf (command, "%*s %d", &speed);
  55. }
  56.  
  57. void
  58. Flush ()
  59. {
  60.     command_calls[0].c_index = 0;
  61. }
  62.  
  63. char *
  64. BuildArgs (commstring, name_index)
  65.     char *commstring;
  66.     int name_index;
  67. {
  68.     char cbuf[255];
  69.  
  70.     if (!strcmp (command_name[name_index].codeword, ".object") ||
  71.     !strcmp (command_name[name_index].codeword, ".speed") ||
  72.     !strcmp (command_name[name_index].codeword, ".smooth"))
  73.     {
  74.     return (CopyString (commstring));
  75.     }
  76.     if (!strcmp (command_name[name_index].codeword, ".foreground"))
  77.     {
  78.     fgets (cbuf, 255, fp);
  79.     NullSet (foreground, sizeof (foreground));
  80.     strncpy (foreground, cbuf, SCREENWIDTH);
  81.     return ((char *) NULL);
  82.     }
  83.     if (!strcmp (command_name[name_index].codeword, ".background"))
  84.     {
  85.     fgets (cbuf, 255, fp);
  86.     NullSet (background, sizeof (background));
  87.     strncpy (background, cbuf, SCREENWIDTH);
  88.     return ((char *) NULL);
  89.     }
  90.     fprintf (stderr, "%s: NYI\n", commstring);
  91.     return ((char *) 0);
  92. }
  93.  
  94. void
  95. NewFunction (cw, fn, args)
  96.     char *cw;
  97.     void (*fn) ();
  98.     char args;
  99. {
  100.     static int command_count = 1;    /* load commands from 1st element */
  101.  
  102.     command_name[command_count].codeword = cw;
  103.     command_name[command_count].fn = fn;
  104.     command_name[command_count].has_args = args;
  105.     command_count++;
  106.     command_name[command_count].codeword = NULL;
  107. }
  108.  
  109. void
  110. ParseCommands (cstring)
  111.     register char *cstring;
  112. {
  113.     int i;
  114.     int comm_number;
  115.     char *ptr;
  116.     int get_next_line;
  117.     char otherbuffer[255];
  118.     static char dot = '.';
  119.  
  120.     comm_number = 0;
  121.     command_calls[comm_number].c_index = 0;
  122.  
  123.   start_parsing:
  124.     i = strlen (cstring) - 1;
  125.     if (cstring[i] == '\\')
  126.     {
  127.     get_next_line = 1;
  128.     cstring[i] = '\0';
  129.     } else
  130.     {
  131.     get_next_line = 0;
  132.     }
  133.  
  134.     for (ptr = (char *) strchr (cstring, dot);
  135.      ptr;
  136.      ptr = (char *) strchr (ptr, dot))
  137.     {
  138.     if (!*ptr)        /* no more commands */
  139.     {
  140.         break;
  141.     }
  142.     for (i = 1; command_name[i].codeword; i++)
  143.     {
  144.         if (!strncmp (ptr, command_name[i].codeword,
  145.               strlen (command_name[i].codeword)))
  146.         {
  147.         command_calls[comm_number].c_index = i;
  148.  
  149.         if (command_name[i].has_args != 'Y')
  150.         {
  151.             command_calls[comm_number].invocation = (char *) 0;
  152.             comm_number++;
  153.             break;
  154.         } else
  155.         {
  156.             command_calls[comm_number].invocation = BuildArgs (ptr, i);
  157.             comm_number++;
  158.             break;
  159.         }
  160.         }
  161.     }
  162.  
  163.     if (!command_name[i].codeword)
  164.     {
  165.         fprintf (stderr, "Error: unknown command '%s'\n", ptr);
  166.     }
  167.     ptr++;            /* make this neater */
  168.     }
  169.     if (get_next_line)
  170.     {
  171.     fgets (otherbuffer, 255, fp);
  172.     Trim (otherbuffer);
  173.     cstring = otherbuffer;
  174.     goto start_parsing;
  175.     }
  176.     command_calls[comm_number++].c_index = 0;
  177. }
  178.  
  179. void
  180. ExecuteCommandsOn (dstring)
  181.     register char *dstring;
  182. {
  183.     register int i;
  184.     register int j;
  185.  
  186.     for (i = 0; command_calls[i].c_index; i++)
  187.     {
  188.     j = command_calls[i].c_index;
  189. #ifdef TESTING
  190.     printf ("executing '%s' as '%s' on \n'%s'\n", command_name[j].codeword,
  191.         command_calls[i].invocation, dstring);
  192. #else
  193.     (*command_name[j].fn) (command_calls[i].invocation, dstring);
  194. #endif
  195.     }
  196. }
  197.  
  198. int
  199. main (argc, argv)
  200.     int argc;
  201.     char *argv[];
  202. {
  203.     char command_buffer[255];
  204.     char line_buffer[255];
  205.     register char *ptr;
  206.     register int i;
  207.     static char dot = '.';
  208.  
  209.     srand (time (0));
  210.  
  211.     NewFunction (".wspatter", WordSpatter, 'N');
  212.     NewFunction (".wipe", Wipe, 'N');
  213.     NewFunction (".tshow", TShow, 'N');
  214.     NewFunction (".speed", Speed, 'Y');
  215.     NewFunction (".spatter", Spatter, 'N');
  216.     NewFunction (".smooth", Smooth, 'Y');
  217.     NewFunction (".show", Show, 'N');
  218.     NewFunction (".scrollof", ScrollOffForward, 'N');
  219.     NewFunction (".scrollob", ScrollOffBackward, 'N');
  220.     NewFunction (".scrollf", ScrollForward, 'N');
  221.     NewFunction (".scrollb", ScrollBackward, 'N');
  222.     NewFunction (".reset", ResetAnim, 'N');
  223.     NewFunction (".p", Pause, 'N');
  224.     NewFunction (".object", AddObject, 'Y');
  225.     NewFunction (".noop", Noop, 'N');
  226.     NewFunction (".nl", LineFeed, 'N');
  227.     NewFunction (".macro", NULL, 'N');
  228.     NewFunction (".letter3", Letter3, 'N');
  229.     NewFunction (".letter2", Letter2, 'N');
  230.     NewFunction (".letter1", Letter1, 'N');
  231.     NewFunction (".laymte", LayMiddleToEnds, 'N');
  232.     NewFunction (".layf", LayForward, 'N');
  233.     NewFunction (".layetm", LayEndsToMiddle, 'N');
  234.     NewFunction (".layb", LayBackward, 'N');
  235.     NewFunction (".foreground", Noop, 'Y');
  236.     NewFunction (".flush", Flush, 'N');
  237.     NewFunction (".flash", Flash, 'N');
  238.     NewFunction (".fade", Fade, 'N');
  239.     NewFunction (".clear", Clear, 'N');
  240.     NewFunction (".background", Noop, 'Y');
  241.     NewFunction (".anim", Animate, 'N');
  242.  
  243.     if (isatty (fileno (stdin)))
  244.     {
  245.     if (argc == 1)
  246.     {
  247.         fprintf (stderr, "Usage:\t%s <filename>\n", argv[0]);
  248.         exit (1);
  249.     }
  250.     fp = fopen (argv[1], "r");
  251.     if (!fp)
  252.     {
  253.         perror (argv[1]);
  254.         exit (1);
  255.     }
  256.     } else
  257.     {
  258.     fp = stdin;
  259.     }
  260.  
  261.     for (*line_buffer = '\0'; !feof (fp); *line_buffer = '\0')
  262.     {
  263.     fgets (line_buffer, 255, fp);
  264.  
  265.     if (!*line_buffer || *line_buffer == '\n')    /* allow blank lines */
  266.     {
  267.         if (!command_calls[0].c_index)
  268.         {
  269.         putchar ('\n');
  270.         }
  271.         continue;
  272.     }
  273.     Trim (line_buffer);    /* trim w/s */
  274.  
  275.     if (*line_buffer == dot)
  276.     {
  277.         ParseCommands (line_buffer);
  278.         continue;
  279.     }
  280.     if (!command_calls[0].c_index)    /* don't faff around... */
  281.     {
  282.         printf ("%s\n", line_buffer);
  283.         continue;
  284.     }
  285.     ExecuteCommandsOn (line_buffer);
  286.     }
  287.     fclose (fp);
  288.  
  289.     printf ("\n\r%s\r", version);
  290.     i = strlen (version);
  291.     while (i--)
  292.     {
  293.     putchar (' ');
  294.     }
  295.     putchar ('\r');
  296.  
  297.     return (0);
  298. }
  299.