4ce6f42e |
1 | /* -*-c-*- |
2 | * |
4ce6f42e |
3 | * Parsing tracing options |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
d4efbcd9 |
8 | /*----- Licensing notice --------------------------------------------------* |
4ce6f42e |
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. |
d4efbcd9 |
16 | * |
4ce6f42e |
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. |
d4efbcd9 |
21 | * |
4ce6f42e |
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 | |
4ce6f42e |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <stdio.h> |
31 | #include <string.h> |
32 | #include <stdlib.h> |
33 | |
36188114 |
34 | #include "macros.h" |
4ce6f42e |
35 | #include "report.h" |
36 | #include "trace.h" |
37 | |
38 | /*----- Main code ---------------------------------------------------------*/ |
39 | |
40 | /* --- @traceopt@ --- * |
41 | * |
ba9edb26 |
42 | * Arguments: @const trace_opt *t@ = pointer to trace options table |
4ce6f42e |
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 | |
ba9edb26 |
54 | unsigned traceopt(const trace_opt *t, const char *p, |
55 | unsigned f, unsigned bad) |
4ce6f42e |
56 | { |
57 | unsigned sense = 1; |
58 | |
59 | /* --- Dump out help text --- */ |
60 | |
36188114 |
61 | if (!p || STRCMP(p, ==, "?")) { |
ba9edb26 |
62 | const trace_opt *tt; |
4ce6f42e |
63 | puts("Trace options:"); |
ba9edb26 |
64 | for (tt = t; tt->ch; tt++) { |
65 | if (!(tt->f & ~bad) || !tt->help) |
4ce6f42e |
66 | continue; |
d4efbcd9 |
67 | printf(" `%c': %s\n", tt->ch, tt->help); |
4ce6f42e |
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: { |
ba9edb26 |
84 | const trace_opt *tt; |
4ce6f42e |
85 | for (tt = t; tt->ch; tt++) { |
ba9edb26 |
86 | if (!(tt->f & ~bad)) |
4ce6f42e |
87 | continue; |
88 | if (tt->ch == *p) { |
89 | if (sense) |
90 | f |= (tt->f & ~bad); |
91 | else |
92 | f &= ~(tt->f & ~bad); |
ba9edb26 |
93 | goto ok; |
4ce6f42e |
94 | } |
4ce6f42e |
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 -------------------------------------------------*/ |