home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume10 / parseargs / traceset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-16  |  3.9 KB  |  202 lines

  1. #ifdef NODEBUG
  2. #undef NODEBUG
  3. #endif
  4. #define TRACESTUFF
  5. #include <useful.h>
  6. #include <ctype.h>
  7.  
  8. VERSIONID("$Header: traceset.c,v 2.0 89/12/24 00:56:33 eric Exp $");
  9.  
  10. /*
  11. **  TRACESET -- set trace flags
  12. **
  13. **    Parameters:
  14. **        s -- the string describing the trace flags desired.
  15. **
  16. **    Returns:
  17. **        none.
  18. **
  19. **    Side Effects:
  20. **        Sets values in the trace vector.
  21. **
  22. **    Globals:
  23. **        _TraceVect -- used by TRACE and TRACEF to determine the
  24. **            current trace flag settings.
  25. **
  26. **    Syntax:
  27. **        The argument points to a set of comma separated trace
  28. **        flag settings.  Each setting is a trace flag name, or
  29. **        a range of trace flags separated by a hyphen.  The
  30. **        setting may be followed by a dot and a level.  For
  31. **        example, "5,23-26.4" sets trace flag 5 to level 1 (the
  32. **        default), and flags 23 through 26 to level 4.
  33. **
  34. **        Trace flags names may be numeric or symbolic.  Symbolic
  35. **        names are looked up in the file "lib/traceflags", which
  36. **        is found somewhere in the search path.  This file has a
  37. **        simple "name <white-space> value" syntax.
  38. **
  39. **    Author:
  40. **        Eric Allman
  41. **        University of California, Berkeley
  42. */
  43.  
  44. STATIC FILE    *_TraceSymFile =    FILENULL;
  45. STATIC BOOL    _TraceSymCant =        FALSE;
  46. unsigned char    _TraceVect[_TRACE_SIZE];
  47.  
  48. VOID
  49. traceset(s)
  50.     char *s;
  51. {
  52.     int lo;
  53.     int hi;
  54.     int lev;
  55.  
  56.     if (s == CHARNULL || *s == '\0')
  57.         s = "0-999";
  58.  
  59.     do
  60.     {
  61.         /* get the low limit of the range */
  62.         lo = _tracetok(&s, TRUE);
  63.         if (lo < 0)
  64.             lo = 0;
  65.  
  66.         /* get the high limit of the range */
  67.         if (*s == '-')
  68.             hi = _tracetok(&s, TRUE);
  69.         else
  70.             hi = lo;
  71.         if (hi >= _TRACE_SIZE)
  72.             hi = _TRACE_SIZE - 1;
  73.  
  74.         /* get the trace level */
  75.         if (*s == '.')
  76.             lev = _tracetok(&s, FALSE);
  77.         else
  78.             lev = 1;
  79.         if (lev < 0)
  80.             lev = 1;
  81.  
  82.         /* now do the actual setting */
  83.         while (lo <= hi)
  84.             _TraceVect[lo++] = lev;
  85.     } while (*s != '\0');
  86.  
  87.     if (_TraceSymFile != FILENULL)
  88.     {
  89.         fclose(_TraceSymFile);
  90.         _TraceSymFile = FILENULL;
  91.     }
  92. }
  93. /*
  94. **  _TRACETOK -- extract a token
  95. **
  96. **    Parameters:
  97. **        sp -- an indirect pointer to the string form.  It is
  98. **            updated to point to the token terminator.
  99. **        symok -- if TRUE, a symbolic representation can be
  100. **            used.
  101. **
  102. **    Returns:
  103. **        The value of the token.
  104. **        -1 on error.
  105. **
  106. **    Side Effects:
  107. **        none.
  108. */
  109.  
  110. STATIC int
  111. _tracetok(sp, symok)
  112.     char **sp;
  113.     BOOL symok;
  114. {
  115.     register char *s = *sp;
  116.     register char *tp;
  117.     char tbuf[MAXWORDLEN + 1];
  118.     char dbuf[MAXINPUTLINE];
  119.  
  120.     /* skip leading cruft */
  121.     while (*s != '\0' && !isalnum(*s))
  122.         s++;
  123.  
  124.     /* extract token */
  125.     for (tp = tbuf; isalnum(*s); *tp++ = *s++)
  126.         continue;
  127.     *tp = '\0';
  128.  
  129.     /* drop trailing cruft */
  130.     while (isspace(*s))
  131.         s++;
  132.     *sp = s;
  133.  
  134.     /* now decode token */
  135.     if (isdigit(tbuf[0]))
  136.         return (atoi(tbuf));
  137.     if (!symok)
  138.         return (-1);
  139.  
  140.     /* look up symbol */
  141.     if (!_TraceSymCant && _TraceSymFile == FILENULL)
  142.     {
  143.         extern FILE *openpath ARGS((char *, char *, char *));
  144.  
  145.         _TraceSymFile = openpath("lib/traceflags", "r", CHARNULL);
  146.     }
  147.     if (_TraceSymFile == FILENULL)
  148.     {
  149.         _TraceSymCant = TRUE;
  150.         return (-1);
  151.     }
  152.     rewind(_TraceSymFile);
  153.  
  154.     /* search through the trace file for a matching line */
  155.     while (fgets(dbuf, sizeof dbuf, _TraceSymFile) != CHARNULL)
  156.     {
  157.         register char *ep;
  158.         extern char *_tracematch ARGS((char *, char *));
  159.  
  160.         /* comment? */
  161.         if (dbuf[0] == '#' || dbuf[0] == '\n')
  162.             continue;
  163.  
  164.         /* does it match? */
  165.         ep = _tracematch(tbuf, dbuf);
  166.         if (ep != CHARNULL)
  167.             return (atoi(ep));
  168.     }
  169.     return (-1);
  170. }
  171.  
  172. STATIC char *
  173. _tracematch(tp, dp)
  174.     register char *tp;
  175.     register char *dp;
  176. {
  177.     for ( ; *tp != '\0'; tp++, dp++)
  178.     {
  179.         /* if we have an exact match, continue trying */
  180.         if (*tp == *dp)
  181.             continue;
  182.  
  183.         /* well, let's try a caseless match */
  184.         if (isupper(*tp) && tolower(*tp) == *dp)
  185.             continue;
  186.         if (isupper(*dp) && tolower(*dp) == *tp)
  187.             continue;
  188.  
  189.         /* well foo...  I guess it doesn't match */
  190.         return (CHARNULL);
  191.     }
  192.  
  193.     /* check the boundary conditions */
  194.     if (!isspace(*dp))
  195.         return (CHARNULL);
  196.  
  197.     /* drop the trailing white space and return the pointer */
  198.     while (isspace(*dp))
  199.         dp++;
  200.     return (dp);
  201. }
  202.