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 >
Wrap
C/C++ Source or Header
|
1989-06-07
|
6KB
|
187 lines
/* eo "execute on", another way to do wildcard commands
* copyright 1989 by marc balmer, basel
* e-mail: <marc@wopr.uucp>
*
* this may only be used for non-commercial purposes
* please let me know if you changed something, tnx!
*
* edition history
* ---------------
* 24/06/88 started a first (but working) version
* 02/05/89 input may also come from stdin or a pipe)
* 30/05/89 command execution with os9exec()
* 06/06/89 fixed a bug in initialisation
*/
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define MAXLEN 132
#define MAXFILENAME 64
extern int os9forkc();
extern char **environ;
char ver[5];
main(argc, argv)
int argc;
char *argv[];
{
char **argblk, /* the unexpanded argument block (with @'s) */
**exargblk, /* the expanded argument block (no @'s) */
*name, /* a line from the file */
*s, *p, /* two multi-purpose pointers... */
*shell, /* name of the shell currently ussd */
*getenv(), /* get environment variable */
*malloc();
int piped = 0, /* flag if input comes from a pipe or a file */
i, j, /* ordinary integers... */
pid, /* process id of child process */
result, /* result code of child process */
errbrk = FALSE, /* flag wether to stop on error or not */
quiet = FALSE, /* flag for quiet mode */
first_cmd = 1; /* first command in argument list */
strcpy(ver, "1.8");
if (argc < 2)
{
printf("eo ver %s (c) by marc balmer, basel\n", ver);
puts("type 'eo -?' to get some help\n");
exit(0);
}
name = malloc(MAXFILENAME);
/* walk through arguments */
for (i = 1; i < argc && *argv[i] == '-'; i++)
{
++first_cmd;
for (j = 1; j < strlen(argv[i]); j++)
switch (*(argv[i] + j))
{
case 'p': piped = TRUE;
break;
case 'q': quiet = TRUE;
break;
case 'e': errbrk = TRUE;
break;
case '?': usage();
exit(0);
default: puts("illegal option");
exit(0);
}
}
/* determine the file name */
if (!piped)
strcpy(name, argv[first_cmd++]);
/* get the name of the shell to use */
if ((shell = getenv("SHELL")) == '\0')
{
shell = malloc(6);
strcpy(shell, "shell");
}
/* build the argument block and check */
/* if the command does make any sense */
/* 'result' is abused for this purpose */
result = 0;
*argblk = shell;
for (j = 1, i = first_cmd; i < argc; i++, j++)
result += (index((*(argblk + j) = argv[i]), '@') != 0);
*(argblk + j) = 0;
if (result == 0)
{
puts("the command you entered doesn't make much sense.");
exit(0);
}
if (!piped)
if (freopen(name, "r", stdin) == NULL)
{
printf("can't open file %s for input\n", name);
exit(0);
}
/* allocate memory for exargblk */
for (i = 0; *(argblk + i) != 0; i++)
*(exargblk + i) = malloc(strlen(*(argblk + i)));
while (gets(name) != NULL)
{
/* insert filename into exargblk */
for (i = 0; *(argblk + i) != 0; i++)
{
strcpy(*(exargblk + i), *(argblk + i));
while (index(*(exargblk + i), '@'))
{
s = malloc(strlen(index(*(exargblk + i), ''')));
p = malloc(MAXLEN);
strcpy(p, *(exargblk + i));
strcpy(s, index(p, '@') + 1);
*((char *) index(p, '@')) = '\0';
strcat(strcat(p, name), s);
*(exargblk + i) = p;
free(s);
}
}
if (!quiet)
{
for (i = 1; *(exargblk + i) != 0; i++)
printf("%s ", *(exargblk + i));
puts(" ");
}
if ((pid = os9exec(os9forkc, *exargblk, exargblk, environ, 0, 0, 3)) > 0)
{
wait(&result);
if (errbrk && result)
exit(result);
}
else
{
printf("can't execute %s\n", *exargblk);
exit(0);
}
}
}
usage()
{
printf("eo ver %s (c) by marc balmer, basel\n\n", ver);
puts("eo [-options] [file] [OS9 command]\n");
puts("execute a command on every line in [file]");
puts("a '@' character is replaced by the line in");
puts("[file]. options are:");
puts(" p take the input lines from a");
puts(" pipe rather than from a file");
puts(" (e.g. 'dirr-u ! eo -p list @')");
puts(" q don't display commands while");
puts(" executing, e.g.:\n");
puts(" eo [file] -q copy @ /h0/tmp/@\n");
puts(" would execute the copy command");
puts(" on every filename in <file>");
puts(" without displaying the command");
puts(" everytime it is executed");
puts(" e stop on error\n");
puts("you can easily generate the [file] file by typing");
puts("'dir -u>[file]'\n");
exit(0);
}