home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: WPS_PM / WPS_PM.zip / console.zip / Console / src / common / cmdline.c next >
C/C++ Source or Header  |  1997-07-30  |  3KB  |  80 lines

  1. /******************************************************************************\
  2. |*                                                                            *|
  3. |* Command-line parsing library                                               *|
  4. |* Copyright (C) 1997 by FRIENDS software                                     *|
  5. |* All Rights Reserved                                                        *|
  6. |* Portability: universal                                                     *|
  7. |*                                                                            *|
  8. |* This program is free software; you can redistribute it and/or modify       *|
  9. |* it under the terms of the GNU General Public License as published by       *|
  10. |* the Free Software Foundation; either version 2 of the License, or          *|
  11. |* (at your option) any later version.                                        *|
  12. |*                                                                            *|
  13. |* This program is distributed in the hope that it will be useful,            *|
  14. |* but WITHOUT ANY WARRANTY; without even the implied warranty of             *|
  15. |* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *|
  16. |* GNU General Public License for more details.                               *|
  17. |*                                                                            *|
  18. |* You should have received a copy of the GNU General Public License          *|
  19. |* along with this program; if not, write to the Free Software                *|
  20. |* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  *|
  21. |*                                                                            *|
  22. \******************************************************************************/
  23.  
  24. #define INCL_DOS
  25. #include <os2.h>
  26. #include <string.h>
  27. #include <malloc.h>
  28. #include "strop.h"
  29. #include "cmdline.h"
  30.  
  31. char *CmdLine(int argc, char *argv[])
  32. {
  33.  char *Line, *Cmd;
  34.  PTIB tib;
  35.  PPIB pib;
  36.  
  37.  if (DosGetInfoBlocks(&tib, &pib))
  38.   return "";
  39.  
  40.  Cmd = pib->pib_pchcmd + strlen(pib->pib_pchcmd) + 1;
  41.  Line = malloc(strlen(Cmd) + 1);
  42.  strcpy(Line, Cmd);
  43.  return Line;
  44. }
  45.  
  46. void ParseCmdLine(char *Line, int (*Opt)(char *), int (*Str)(char *))
  47. {
  48.  int len;
  49.  char *tmp;
  50.  if (!Line) return;
  51.  len = strlen(Line);
  52.  memcpy(tmp = malloc(len), Line, len + 1);
  53.  while (tmp[0])
  54.  {
  55.   int dc;
  56.   dc = 0;
  57.   while ((tmp[dc]) && ((tmp[dc] == SPACE) || (tmp[dc] == TAB))) dc++;
  58.   strdel(tmp, 0, dc);
  59.   if (tmp[0] == '-')
  60.   { if (&Opt && tmp[0]) dc = Opt(tmp); else dc = 1; }
  61.   else
  62.   { if (&Str && tmp[0]) dc = Str(tmp); else dc = 1; }
  63.   strdel(tmp, 0, dc);
  64.  }
  65.  free(tmp);
  66. }
  67.  
  68. int OptState(char Switch, boolean *State)
  69. {
  70.  switch (Switch)
  71.  {
  72.   case '-':
  73.    *State = FALSE; return 1;
  74.   case '+':
  75.    *State = TRUE; return 1;
  76.   default:
  77.    *State = TRUE; return 0;
  78.  }
  79. }
  80.