home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / LES177AS.ZIP / SETARGV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-04  |  5.5 KB  |  182 lines

  1. /* setargv -- setup argv with wild card expansion                           */
  2. /* copyright 1987  Michael M Rubenstein                                     */
  3.  
  4. /* This program may be freely distributed provided no fee is assessed.      */
  5.  
  6. /* This file implements wild card expansion in argv for Turbo C 1.5.        */
  7. /* Strings of characters in either quotes (") or appostrophes (') on the    */
  8. /* command line are considered a single argument.  However, backslash       */
  9. /* escapes are not implemented.  A quote may be included in an argument     */
  10. /* which is enclosed in appostrophes and an appostrophe may be included     */
  11. /* in an argument enclosed in quotes.  Either may be included as an         */
  12. /* in an argument starting with any other character.                        */
  13.  
  14. /* Any argument which is not enclosed in quotes or appostrophes, does not   */
  15. /* begin with a hyphen (-), and which contains an asterisk (*) or question  */
  16. /* mark (?) will be expanded.  It is NOT an error for an argument to have a */
  17. /* null expansion (no matching files).  Only ordinary files (not            */
  18. /* directories or hidden or system files) will be included in the           */
  19. /* expansion.                                                               */
  20.  
  21. /* To use this function, simply compile it with the appropriate memory      */
  22. /* model and include in the link.  This can be accomplished very simply     */
  23. /* in the integrated environment by simply including this file in the       */
  24. /* project file.  In the command line version, simply include this file     */
  25. /* (or a precompiled .OBJ version) on the command line.                     */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <dir.h>
  30. #include <dos.h>
  31. #include <process.h>
  32. #include <errno.h>
  33.  
  34. #define FALSE           0
  35. #define TRUE            1
  36.  
  37. void putarg(unsigned char far *, unsigned char far *, int lcase);
  38.  
  39. extern int              _C0argc;
  40. extern char             **_C0argv;
  41. extern unsigned         _psp;
  42. extern unsigned         _envseg;
  43. extern unsigned         _envLng;
  44. extern unsigned char    _osmajor;
  45. extern void             _abort();
  46. extern char             *sbrk(int);
  47.  
  48. void _setargv__(void)
  49. {
  50.   unsigned char         far *cmdtail;
  51.   unsigned char         *firstarg;
  52.   unsigned char         far *cmdarg;
  53.   int                   wild;
  54.   int                   c;
  55.   unsigned char         buffer[129];
  56.   unsigned char         *p, *q;
  57.   unsigned char         *lastdir;
  58.   char                  **wargv;
  59.   int                   i;
  60.   struct ffblk          ffb;
  61.  
  62.   cmdtail = MK_FP(_psp, 0x81);
  63.   cmdtail[cmdtail[-1]] = '\0';      /* make sure null at end */
  64.   firstarg = (unsigned char *) sbrk(0);
  65.   _C0argc = 1;
  66.  
  67.   while (*cmdtail != '\0')
  68.   {
  69.     /* skip white space */
  70.     while (isascii(*cmdtail) && isspace(*cmdtail))
  71.       ++cmdtail;
  72.  
  73.     /* done with command loop if end of command tail */
  74.     if (*cmdtail == '\0')
  75.       break;
  76.  
  77.     /* if quoted string, just save the argument */
  78.     if ((c = *cmdtail) == '"' || c == '\'')
  79.     {
  80.       cmdarg = ++cmdtail;
  81.       while (*cmdtail != c && *cmdtail != '\0')
  82.         ++cmdtail;
  83.       putarg(cmdarg, cmdtail, 0);
  84.       if (*cmdtail != '\0')
  85.         ++cmdtail;
  86.       continue;
  87.     }
  88.  
  89.     /* find word */
  90.     cmdarg = cmdtail;
  91.     wild = FALSE;
  92.     p = lastdir = buffer;
  93.     while ((c = *cmdtail) != '\0'
  94.         && (!isascii(c) || !isspace(c)))
  95.     {
  96.       /* wild is TRUE if word contains * or ? */
  97.       wild |= (c == '*' || c == '?');
  98. /*  Next line was added to make the treatment of / and \ alike  */
  99.       if (c == '/') c = '\\';
  100.       *(p++) = c;
  101.  
  102.       /* lastdir points to the first character of the base file name */
  103.       if (c == '\\' || c == ':')
  104.         lastdir = p;
  105.       ++cmdtail;
  106.     }
  107.     *p = '\0';
  108.  
  109.     if (*cmdarg == '-' || *cmdarg == '+')
  110.       putarg(cmdarg, cmdtail, 0);
  111.     else if (wild)
  112.     {
  113.       c = findfirst((char *) buffer, &ffb, 0);
  114.       if (c)
  115.         putarg(cmdarg, cmdtail, 0);
  116.       else
  117.         while (!c)
  118.         {
  119.           for (p = lastdir, q = (unsigned char *) ffb.ff_name; *q != '\0';)
  120.                *(p++) = *(q++);
  121.           putarg(buffer, p, 1); /* use lowercase for filenames */
  122.           c = findnext(&ffb);
  123.         }
  124.     }
  125.     else
  126.       putarg(cmdarg, cmdtail, 1); /* use lowercase for filenames */
  127.   }
  128.  
  129.   /* allocate argv */
  130.   if ((wargv = (char **) sbrk(sizeof(char *) * (_C0argc + 1))) == (char **) -1)
  131.     abort();
  132.   _C0argv = wargv;
  133.  
  134.   /* store program name */
  135.   if (_osmajor < 3)
  136.     *(wargv++) = "C";
  137.   else
  138.   {
  139.       cmdtail = cmdarg = MK_FP(_envseg, _envLng + 2);
  140. #   if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  141.       *(wargv++) = sbrk(0);
  142.       while (*cmdtail != '\0')
  143.         ++cmdtail;
  144.       putarg(cmdarg, cmdtail, 0);
  145.       --_C0argc;
  146. #   else
  147.       *(wargv++) = (char *) cmdarg;
  148.       (void) sbrk(15);  /* kludge for TC++ library bug */
  149. #   endif
  150.   }
  151.  
  152.   /* store arguments */
  153.   for (i = _C0argc; --i;)
  154.   {
  155.     *(wargv++) = (char *) firstarg;
  156.     while(*++firstarg != '\0')
  157.       ;
  158.     ++firstarg;
  159.   }
  160.   *wargv = (char *) 0;
  161.   errno = 0;
  162. }
  163. #pragma startup _setargv__ 80
  164.  
  165. static void putarg(from, to, lcase)
  166.   unsigned char         far *from, far *to;
  167.   int lcase;
  168. {
  169.   char                  *p;
  170.  
  171.   if ((p = sbrk((unsigned)(to - from) + 1)) == (char *) -1)
  172.     abort();
  173.   while (from < to)
  174.     if (lcase)
  175.       *(p++) = tolower(*(from++));
  176.     else
  177.       *(p++) = *(from++);
  178.   *p = '\0';
  179.   ++_C0argc;
  180. }
  181.  
  182.