chiark / gitweb /
url: Allow various `safe' characters unquoted in URL strings.
[mLib] / traceopt.c
1 /* -*-c-*-
2  *
3  * $Id: traceopt.c,v 1.3 2004/04/08 01:36:13 mdw Exp $
4  *
5  * Parsing tracing options
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "report.h"
37 #include "trace.h"
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 /* --- @traceopt@ --- *
42  *
43  * Arguments:   @const trace_opt *t@ = pointer to trace options table
44  *              @const char *p@ = option string supplied by user
45  *              @unsigned f@ = initial tracing flags
46  *              @unsigned bad@ = forbidden tracing flags
47  *
48  * Returns:     Trace flags as set by user.
49  *
50  * Use:         Parses an option string from the user and sets the
51  *              appropriate trace flags.  If the argument is null or a single
52  *              `?' character, a help message is displayed.
53  */
54
55 unsigned traceopt(const trace_opt *t, const char *p,
56                   unsigned f, unsigned bad)
57 {
58   unsigned sense = 1;
59
60   /* --- Dump out help text --- */
61
62   if (!p || !strcmp(p, "?")) {
63     const trace_opt *tt;
64     puts("Trace options:");
65     for (tt = t; tt->ch; tt++) {
66       if (!(tt->f & ~bad) || !tt->help)
67         continue;
68       printf("  `%c': %s\n", tt->ch, tt->help);
69     }
70     return (f);
71   }
72
73   /* --- Parse the string properly --- */
74
75   f = 0;
76   while (*p) {
77     switch (*p) {
78       case '+':
79         sense = 1;
80         break;
81       case '-':
82         sense = 0;
83         break;
84       default: {
85         const trace_opt *tt;
86         for (tt = t; tt->ch; tt++) {
87           if (!(tt->f & ~bad))
88             continue;
89           if (tt->ch == *p) {
90             if (sense)
91               f |= (tt->f & ~bad);
92             else
93               f &= ~(tt->f & ~bad);
94             goto ok;
95           }
96         }
97         moan("unknown trace option `%c'", *p);
98       ok:;
99       } break;
100     }
101     p++;
102   }
103
104   return (f);
105 }
106
107 /*----- That's all, folks -------------------------------------------------*/