home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bm_rm.zip / NEXTTOK.C < prev    next >
C/C++ Source or Header  |  1989-08-02  |  4KB  |  75 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /* Copyright (C) 1989 Brian B. McGuinness
  5.                       15 Kevin Road
  6.                       Scotch Plains, NJ 07076
  7.  
  8. This function is free software; you can redistribute it and/or modify it under 
  9. the terms of the GNU General Public License as published by the Free Software 
  10. Foundation; either version 1, or (at your option) any later version.
  11.  
  12. This function is distributed in the hope that it will be useful, but WITHOUT 
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  14. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  15. details.
  16.  
  17. You should have received a copy of the GNU General Public License along with 
  18. this function; if not, write to the Free Software Foundation, Inc., 675 Mass 
  19. Ave, Cambridge, MA 02139, USA. */
  20.  
  21. /*----------------------------------------------------------------------------*/
  22. /* nexttok() - Return a pointer to the next cmd line token to be processed    */
  23. /*                                                                            */
  24. /* argc    = pointer to command line argument count                           */
  25. /* argv    = pointer to command line argument array                           */
  26. /* inpflag = pointer to current input status flag:                            */
  27. /*             zero if we're returning tokens from the command line,          */
  28. /*             nonzero if we're reading tokens from standard input            */
  29. /*             This value should be preserved between calls to this routine.  */
  30. /*                                                                            */
  31. /* Normally, inpflag is zero the first time this routine is called, so we     */
  32. /* begin by returning tokens from the command line.  When the token "@" is    */
  33. /* found on the command line, we start reading tokens from standard input.    */
  34. /* This continues until we encounter EOF, at which time we resume taking      */
  35. /* tokens from the command line.  Occurrances of the "@" token in the         */
  36. /* standard input stream are ignored.                                         */
  37. /*                                                                            */
  38. /* When we reach the end of the command line, we return NULL.                 */
  39. /*----------------------------------------------------------------------------*/
  40.  
  41. char *nexttok (int *argc, char ***argv, int *inpflag) {
  42.  
  43. #define BUFLEN 128
  44. static char buffer[BUFLEN]="";
  45. static char *cp=buffer;
  46.  
  47. while (1) {     /* Keep trying until we find a token or finish the cmd line */
  48.  
  49.   /* IF WE'RE HANDLING '@' THEN TRY TO GET NEXT TOKEN FROM STANDARD INPUT */
  50.   if (*inpflag) {
  51.     if (cp == NULL || !*cp) {   /* Need to read new line of input */
  52.       if (NULL == fgets (buffer, BUFLEN, stdin)) {
  53.         *inpflag = 0;           /* EOF: resume scanning command line */
  54.         continue;
  55.       }
  56.       cp=strtok (buffer, " ,;\t\n\r");
  57.     }
  58.     else cp=strtok (NULL, " ,;\t\n\r");
  59.     if (cp == NULL) continue;           /* No tokens left on this input line */
  60.     if (!strcmp (cp, "@")) continue;    /* Ignore '@' flag in input */
  61.     return cp;                          /* Found a token!  Return it */
  62.   }
  63.  
  64.   /* OTHERWISE, GET NEXT TOKEN FROM THE COMMAND LINE */
  65.   else {
  66.     if (!--*argc) return (char *) NULL; /* No more tokens to process */
  67.     if (!strcmp (*(++*argv),"@")) {     /* Check for '@' std input flag */
  68.       *inpflag = 1;
  69.       continue;
  70.     }
  71.     return **argv;                      /* Return next cmd line token */
  72.   }
  73. }
  74. } /* end nexttok() */
  75.