home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Puppeteer1.1 / Source / terminalPuppet.m < prev    next >
Encoding:
Text File  |  1994-03-23  |  1.8 KB  |  101 lines

  1. /*
  2.  * Puppeteer 1.1
  3.  *
  4.  * Copyright (c) 1994 Primitive Software Ltd.  All rights reserved.
  5.  *
  6.  * Author: Dave Griffiths <dave@prim.demon.co.uk>
  7.  */
  8.  
  9. #import "Puppeteer.h"
  10.  
  11. void
  12. output(id puppet, FILE *fp, int delay)
  13. {
  14.     char buf[1024], *s;
  15.     
  16.     /*
  17.      * Make the app the active application.
  18.      */
  19.     [puppet postActivate:YES];
  20.     
  21.     /*
  22.      * Process one line at a time.
  23.      */
  24.     while (fgets(buf, sizeof(buf), fp)) {
  25.         /*
  26.          * Wait the specified time before outputting the line.
  27.          */
  28.         usleep(delay);
  29.         
  30.         /*
  31.          * Send the line.
  32.          */
  33.         for (s=buf; *s && s<&buf[1024]; s++)
  34.             [puppet postKeyCode:*s window:NX_KEYWINDOW flags:0];
  35.     }
  36. }
  37.  
  38. void
  39. main(argc, argv)
  40. int argc;
  41. char **argv;
  42. {
  43.     int c, errflg = 0, delay = 250000;
  44.     char *ifile = NULL, *application = "Terminal";
  45.     id puppet;
  46.     FILE *fp;
  47.     extern char *optarg;
  48.  
  49.     while ((c = getopt(argc, argv, "a:d:f:")) != EOF)
  50.     switch (c) {
  51.     case 'a':
  52.         application = optarg;
  53.         break;
  54.     case 'd':
  55.         delay = atoi(optarg)*1000;
  56.         break;
  57.     case 'f':
  58.         ifile = optarg;
  59.         break;
  60.     case '?':
  61.     default:
  62.         errflg++;
  63.         break;
  64.     }
  65.     if (errflg) {
  66.         fprintf(stderr, "Usage: terminalPuppet [-a application] [-d delay] [-f file]\n");
  67.         exit(2);
  68.     }
  69.     
  70.     /*
  71.      * Create the application puppet, launching the app if necessary.
  72.      */
  73.     puppet = [Puppeteer connectToApp:application launch:YES];
  74.     if (!puppet) {
  75.         fprintf(stderr, "Could not connect to %s\n", application);
  76.         exit(1);
  77.     }
  78.     
  79.     /*
  80.      * Attach the strings. The app will then be ready to accept events.
  81.      */    
  82.     [puppet attachStrings];
  83.     
  84.     /*
  85.      * Now output the file to the application. If no file was specified, read
  86.      * from standard input.
  87.      */
  88.     if (ifile) {
  89.         if (!(fp = fopen(ifile, "r")))
  90.             fprintf(stderr, "Could not open %s\n", ifile);
  91.     } else
  92.         fp = stdin;
  93.     if (fp)
  94.         output(puppet, fp, delay);
  95.  
  96.     /*
  97.      * Release strings. This is necessary for the app to continue to respond to
  98.      * real user events.
  99.      */
  100.     [puppet releaseStrings];
  101. }