home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n15.zip / RUN.C < prev    next >
C/C++ Source or Header  |  1989-08-10  |  5KB  |  197 lines

  1.  
  2.  
  3. /* RUN.C */
  4. /* Compile with these options if QuickC: /W3 /Zp /Ox /I$(INCLUDE) /DMSC */
  5. /* Compile with these options if Turbo C: -DTC -O -w -I$(INCLUDE) -L$(LIB) */
  6.  
  7. #include <stdio.h>
  8. #include <process.h>
  9. #include <string.h>
  10.  
  11. #if defined(MSC)
  12. #include <malloc.h>
  13. #endif
  14.  
  15. #if defined(TC)
  16. #include <alloc.h>
  17. #endif
  18.  
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22.  
  23. /*
  24. #include <sys\types.h>
  25. #include <sys\stat.h>
  26. */
  27.  
  28. #include <io.h>
  29.  
  30. void    make_command(char *token);
  31. int     IsCommandCommand(char *command);
  32. void    RunProgram(char *command);
  33. void    NoMemoryExit(void);
  34. void    main(int argc, char **argv);
  35.  
  36. #ifndef TRUE
  37. #define TRUE 1
  38. #endif
  39.  
  40. #ifndef FALSE
  41. #define FALSE 0
  42. #endif
  43.  
  44.     /* Table of Internal DOS commands */
  45. char *Commandcoms[] =
  46. {
  47.     "CD","CHDIR","CLS","COPY","CTTY","DATE","DEL","DIR","ECHO","ERASE","MD",
  48.     "MKDIR","PATH","PAUSE", "PROMPT","RD","REN","RENAME","RMDIR","SET",
  49.     "TIME","TYPE","VER","VOL",NULL
  50. };
  51.  
  52. #define    DELIMETER ";"    /* semicolon delimiter */
  53. char *wsdelim = " \\/\t\n"; /* whitespace delimiters */
  54.  
  55. char *usage = "Usage: run command;command;...\n"
  56.               "or     run - Then enter each command followed by <RETURN>\n";
  57. char *command_prompt = "\nCommand: ";
  58.  
  59.  
  60. #define MAXCOMMANDLINE    128        /* command line size */
  61. #define MAXCOMMANDSTACK    100        /* max commands to 'stack' */
  62. #define MAXCOMMANDS        40        /* max command-line commands */
  63.  
  64. char *runstr[MAXCOMMANDSTACK];    /* pointers to commands     */
  65. int runcnt = 0;
  66.  
  67. void main(int argc, char **argv)
  68.     {
  69.     char cmdline[MAXCOMMANDLINE+1];    /* buffer for command line */
  70.     char *token;                    /* the associated pointer */
  71.     int n;
  72.     cmdline[0] = '\0';
  73.     if(argc > 1)        /* if arguments */
  74.         {
  75.         argv++;
  76.         while (argc--)
  77.             {
  78.             strcat(cmdline, *argv++);
  79.             strcat(cmdline, " ");
  80.             }
  81.  
  82.             /* parse out and malloc string for each command (';' separator) */
  83.         for(token = NULL;
  84.                 token = strtok((token ? NULL : cmdline), DELIMETER); )
  85.             {
  86.             make_command(token);
  87.             if(runcnt >= MAXCOMMANDS)    /* and quit if too many */
  88.                 break;
  89.             }
  90.         }
  91.     else                /* no command-line arguments*/
  92.         {
  93.         printf(command_prompt);
  94.  
  95.         while(strlen(gets(cmdline)) > 0)
  96.             {
  97.             make_command(cmdline);
  98.             if(runcnt >= MAXCOMMANDS)    /* and quit if too many */
  99.                 break;
  100.             printf(command_prompt);
  101.             }
  102.         }
  103.  
  104.         /* runcnt now has the # of commands given either way,
  105.            and the commands are pointed to by pointers in runstr
  106.          */
  107.     if(!runcnt)        /* no commands given: quit */
  108.         {
  109.         printf(usage);
  110.         exit(1);
  111.         }
  112.  
  113.     for(n = 0; n < runcnt; n++)        /* process each command */
  114.         {
  115.         printf("\n%s\n", runstr[n]);    /* echo out the command */
  116.  
  117.         if(IsCommandCommand(runstr[n]))    /* if DOS command */
  118.             {
  119.             if(system(runstr[n]))        /* load COMMAND.COM to run */
  120.                 perror("run (DOS cmd)");
  121.             }
  122.         else
  123.             RunProgram(runstr[n]);        /* else run as a program */
  124.  
  125.         free(runstr[n]);
  126.         }
  127.     exit(0);
  128.     }
  129.  
  130. void make_command(char *token)
  131.     {
  132.     if(!(runstr[runcnt] = malloc(strlen(token)+1)))
  133.         NoMemoryExit();
  134.     
  135.     strcpy(runstr[runcnt], token);
  136.     runcnt++;
  137.     }
  138.  
  139.  
  140. int IsCommandCommand(char *command)
  141.     {
  142.     register char **p, *token;
  143.     char temp[MAXCOMMANDLINE+1];    /* buffer for command line */
  144.     strcpy(temp,command);
  145.     token = strtok(temp, wsdelim);
  146.     strupr(token);
  147.     for(p = Commandcoms; *p; p++)
  148.         if(!strcmp(*p, token))
  149.             return TRUE;
  150.  
  151.     return FALSE;
  152.     }
  153.  
  154.  
  155. void RunProgram(char *command)
  156.     {
  157.     char *cmd_args[MAXCOMMANDS];    /* array of pointers for spawn */
  158.     char *token;
  159.     int arg = 2, i;
  160.     char *batch_command = "/c";
  161.     memset(cmd_args,0,sizeof(cmd_args));     /* clear the pointer array */
  162.     
  163.     cmd_args[0] = getenv("COMSPEC");
  164.     cmd_args[1] = batch_command;
  165.                                     /* set up the arguments */
  166.     if((token = strtok(command, wsdelim)) != NULL)
  167.         {
  168.         if(!(cmd_args[arg] = malloc(strlen(token)+1)))
  169.             NoMemoryExit();
  170.         strcpy(cmd_args[arg++], token);
  171.         for( ; token = strtok(NULL, wsdelim); arg++)
  172.             {
  173.             if(!(cmd_args[arg] = malloc(strlen(token)+1)))
  174.                 NoMemoryExit();
  175.             strcpy(cmd_args[arg], token);
  176.             }
  177.         }
  178.     if(spawnvp(P_WAIT, cmd_args[2], &cmd_args[2])) /* run the program...... */
  179.         if(cmd_args[0])
  180.             {
  181.             if(spawnvp(P_WAIT, cmd_args[0], cmd_args))
  182.                 perror("run (program)");
  183.             }
  184.         else
  185.             printf("COMSPEC= not set in environment, cannot execute\n");
  186.  
  187.     for( i = 2; i < arg; i++)    /* free each non-NULL pointer */
  188.         free(cmd_args[i]);
  189.     }
  190.  
  191. void NoMemoryExit(void)
  192.     {
  193.     printf("\nrun: Not enough memory to proceed");
  194.     exit(1);
  195.     }
  196.  
  197.