home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum11.lzh / SOFTWARE / C / EO / eo.c < prev    next >
C/C++ Source or Header  |  1989-06-07  |  6KB  |  187 lines

  1. /* eo           "execute on", another way to do wildcard commands
  2.  *              copyright 1989 by marc balmer, basel
  3.  *              e-mail: <marc@wopr.uucp>
  4.  *
  5.  *              this may only be used for non-commercial purposes
  6.  *              please let me know if you changed something, tnx!
  7.  *
  8.  
  9.  * edition history
  10.  * ---------------
  11.  * 24/06/88     started a first (but working) version
  12.  * 02/05/89     input may also come from stdin  or a pipe)
  13.  * 30/05/89     command execution with os9exec()
  14.  
  15.  * 06/06/89     fixed a bug in initialisation
  16.  */
  17.  
  18. #include <stdio.h>
  19.  
  20. #define TRUE 1
  21. #define FALSE 0
  22. #define MAXLEN 132
  23. #define MAXFILENAME 64
  24.  
  25. extern int os9forkc();
  26. extern char **environ;
  27.  
  28. char ver[5];
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     char **argblk,  /* the unexpanded argument block (with @'s)     */
  35.     **exargblk,     /* the expanded argument block (no @'s)         */
  36.     *name,          /* a line from the file                         */
  37.         *s, *p,         /* two multi-purpose pointers...                */
  38.         *shell,         /* name of the shell currently ussd             */
  39.         *getenv(),      /* get environment variable                     */
  40.     *malloc();
  41.     
  42.     int piped = 0,  /* flag if input comes from a pipe or a file    */
  43.     i, j,           /* ordinary integers...                         */
  44.         pid,            /* process id of child process                  */
  45.         result,         /* result code of child process                 */
  46.         errbrk = FALSE, /* flag wether to stop on error or not          */
  47.     quiet = FALSE,  /* flag for quiet mode                          */
  48.     first_cmd = 1;  /* first command in argument list               */
  49.         
  50.         strcpy(ver, "1.8");
  51.         
  52.     if (argc < 2)
  53.     {
  54.         printf("eo ver %s (c) by marc balmer, basel\n", ver);
  55.                 puts("type 'eo -?' to get some help\n");
  56.                 exit(0);
  57.     }
  58.  
  59.         name = malloc(MAXFILENAME);
  60.             
  61.         /* walk through arguments               */
  62.         
  63.         for (i = 1; i < argc && *argv[i] == '-'; i++)
  64.         {
  65.                 ++first_cmd;
  66.                 for (j = 1; j < strlen(argv[i]); j++)
  67.                         switch (*(argv[i] + j))
  68.                         {
  69.                                 case 'p':       piped = TRUE;
  70.                                                 break;
  71.                                 case 'q':       quiet = TRUE;
  72.                                                 break;
  73.                                 case 'e':       errbrk = TRUE;
  74.                                                 break;
  75.                                 case '?':       usage();
  76.                                                 exit(0);
  77.                                 default:        puts("illegal option");
  78.                                                 exit(0);
  79.                         }
  80.         }
  81.  
  82.         /* determine the file name              */
  83.         
  84.         if (!piped)
  85.                 strcpy(name, argv[first_cmd++]);
  86.  
  87.         /* get the name of the shell to use     */
  88.         
  89.         if ((shell = getenv("SHELL")) == '\0')
  90.         {
  91.                 shell = malloc(6);
  92.                 strcpy(shell, "shell");
  93.         }
  94.         
  95.  
  96.         /* build the argument block and check   */
  97.         /* if the command does make any sense   */
  98.         /* 'result' is abused for this purpose  */
  99.         
  100.         result = 0;
  101.         *argblk = shell;        
  102.     for (j = 1, i = first_cmd; i < argc; i++, j++)
  103.                 result += (index((*(argblk + j) = argv[i]), '@') != 0);
  104.     *(argblk + j) = 0;
  105.  
  106.         if (result == 0)
  107.     {
  108.         puts("the command you entered doesn't make much sense.");
  109.         exit(0);
  110.     }
  111.  
  112.         if (!piped)
  113.                 if (freopen(name, "r", stdin) == NULL)
  114.                 {
  115.                         printf("can't open file %s for input\n", name);
  116.                         exit(0);
  117.                 }
  118.  
  119.         /* allocate memory for exargblk                 */
  120.         
  121.         for (i = 0; *(argblk + i) != 0; i++)
  122.                 *(exargblk + i) = malloc(strlen(*(argblk + i)));
  123.                 
  124.     while (gets(name) != NULL)
  125.     {
  126.                 /* insert filename into exargblk        */
  127.  
  128.                 for (i = 0; *(argblk + i) != 0; i++)
  129.                 {
  130.                         strcpy(*(exargblk + i), *(argblk + i));
  131.                         while (index(*(exargblk + i), '@'))
  132.                         {
  133.                                 s = malloc(strlen(index(*(exargblk + i), ''')));
  134.                                 p = malloc(MAXLEN);
  135.                                 strcpy(p, *(exargblk + i));
  136.                                 strcpy(s, index(p, '@') + 1);
  137.                                 *((char *) index(p, '@')) = '\0';
  138.                                 strcat(strcat(p, name), s);
  139.                                 *(exargblk + i) = p;
  140.                                 free(s);                                
  141.                         }
  142.                 }
  143.                         
  144.                 if (!quiet)
  145.                 {
  146.                         for (i = 1; *(exargblk + i) != 0; i++)
  147.                                 printf("%s ", *(exargblk + i));
  148.                         puts(" ");
  149.                 }                        
  150.                 if ((pid = os9exec(os9forkc, *exargblk, exargblk, environ, 0, 0, 3)) > 0)
  151.                 {
  152.                         wait(&result);
  153.                         if (errbrk && result)
  154.                                 exit(result);
  155.                 }
  156.                 else
  157.                 {
  158.                         printf("can't execute %s\n", *exargblk);
  159.                         exit(0);
  160.                 }
  161.     }
  162. }
  163.  
  164. usage()
  165. {
  166.     printf("eo ver %s (c) by marc balmer, basel\n\n", ver);
  167.     puts("eo [-options] [file] [OS9 command]\n");
  168.     puts("execute a command on every line in [file]");
  169.     puts("a '@' character is replaced by the line in");
  170.     puts("[file]. options are:");
  171.     puts("    p    take the input lines from a");
  172.     puts("      pipe rather than from a file");
  173.     puts("      (e.g. 'dirr-u ! eo -p list @')");
  174.     puts("    q   don't display commands while");
  175.     puts("        executing, e.g.:\n");    
  176.     puts("        eo [file] -q copy @ /h0/tmp/@\n");
  177.     puts("        would execute the copy command");
  178.     puts("        on every filename in <file>");
  179.     puts("        without displaying the command");
  180.     puts("        everytime it is executed");
  181.     puts("    e    stop on error\n");
  182.     puts("you can easily generate the [file] file by typing");
  183.     puts("'dir -u>[file]'\n");
  184.     exit(0);
  185. }
  186.  
  187.