home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch12 / tinycmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  4.3 KB  |  128 lines

  1. /*
  2.     TINYCMD.C
  3.  
  4.     A simple command interpreter for OS/2.
  5.  
  6.     Compile with:  C> cl tinycmd.c
  7.  
  8.     Usage is:  C> tinycmd
  9.  
  10.     Copyright (C) 1988 Ray Duncan
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <memory.h>
  16.                                         /* macro to return number of
  17.                                            elements in a structure */
  18. #define DIM(x) (sizeof(x) / sizeof(x[0]))
  19.  
  20. #define INPSIZE 80                      /* maximum input length */
  21.  
  22. #define API unsigned extern far pascal    /* OS/2 API prototypes */
  23.  
  24. API DosExecPgm(char far *, int, int, char far *, char far *, 
  25.                int far *, char far *);
  26. API DosGetVersion(char far *);
  27.  
  28. unsigned intrinsic(char *);             /* local function prototypes */
  29. void extrinsic(char *);
  30. void cls_cmd(void);
  31. void ver_cmd(void);
  32. void exit_cmd(void);
  33.  
  34. struct _commands {                      /* intrinsic commands table */
  35.     char *name;                         /* command name */
  36.     int  (*fxn)();                      /* command function */
  37.     } 
  38.       commands[] = { "CLS",   cls_cmd,  /* built-in commands */         
  39.                      "VER",   ver_cmd,
  40.                      "EXIT",  exit_cmd, };
  41.  
  42. main(int argc, char *argv[])
  43. {   
  44.     char input[INPSIZE];                /* keyboard input buffer */
  45.  
  46.     while(1)                            /* main interpreter loop */
  47.     {   
  48.         printf("\n>> ");                /* display prompt */
  49.         memset(input, 0, INPSIZE);      /* initialize input buffer */
  50.         gets(input);                    /* get keyboard entry */
  51.         strtok(input, " ;,=\t");        /* break out first token */
  52.     strupr(input);            /* and fold to uppercase */
  53.  
  54.         if(! intrinsic(input))          /* if intrinsic command,
  55.                                            run its function */
  56.         extrinsic(input);        /* else assume EXE file */
  57.     }
  58. }
  59.  
  60. /*      
  61.     Try to match first token of command line with intrinsic
  62.     command table.  If a match is found, run the associated 
  63.     routine and return true; otherwise, return false.
  64. */
  65. unsigned intrinsic(char *input)
  66. {   
  67.     int i;                              /* scratch variable */
  68.  
  69.     for(i=0; i < DIM(commands); i++)    /* search command table */
  70.     {   
  71.         if(! strcmp(commands[i].name, input))
  72.         { 
  73.             (*commands[i].fxn)();       /* if match, run routine */
  74.             return(1);                  /* and return true */
  75.         }
  76.     }
  77.     return(0);                          /* no match, return false */
  78. }
  79.  
  80. /* 
  81.     Append .EXE to first token and attempt to execute program, 
  82.     passing address of argument strings block and null pointer 
  83.     for environment (child process inherits TINYCMD's environment).
  84. */
  85. void extrinsic(char *input)
  86. {   
  87.     char pbuff[INPSIZE];                /* buffer for program name */
  88.     unsigned status;                    /* value from DosExecPgm */
  89.     int cinfo[2];                       /* child process info */
  90.  
  91.     strcpy(pbuff, input);               /* copy first command token 
  92.                                            to local buffer */
  93.     strcat(pbuff, ".EXE");              /* append .EXE to token */
  94.  
  95.                                         /* try to execute program */
  96.     if(DosExecPgm(NULL,         /* object name buffer pointer */
  97.           0,            /* object name buffer length */
  98.                   0,                    /* synchronous exec. mode */
  99.                   input,                /* argument strings */
  100.                   NULL,                 /* use parent's environment */
  101.                   cinfo,                /* receives termination info */
  102.           pbuff))        /* program name pointer */
  103.  
  104.         printf("\nBad command or filename\n");
  105. }
  106.  
  107. /*      
  108.     These are the subroutines for the intrinsic commands.
  109. */
  110. void cls_cmd(void)                      /* CLS command */
  111. {   
  112.     printf("\033[2J");                  /* ANSI escape sequence */
  113. }                                       /* to clear screen */
  114.  
  115. void ver_cmd(void)                      /* VER command */
  116. {   
  117.     char verinfo[2];                    /* receives version info */
  118.  
  119.     DosGetVersion(verinfo);        /* get and display version */
  120.     printf("\nOS/2 version %d.%02d\n", verinfo[0], verinfo[1]);
  121. }
  122.  
  123. void exit_cmd(void)                     /* EXIT command */
  124. {   
  125.     exit(0);                            /* terminate TINYCMD */
  126. }
  127. 
  128.