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