4ce6f42e |
1 | /* -*-c-*- |
4ce6f42e |
2 | * |
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 | |
34 | #include "report.h" |
35 | #include "trace.h" |
36 | |
37 | /*----- Main code ---------------------------------------------------------*/ |
38 | |
39 | /* --- @traceopt@ --- * |
40 | * |
ba9edb26 |
41 | * Arguments: @const trace_opt *t@ = pointer to trace options table |
4ce6f42e |
42 | * @const char *p@ = option string supplied by user |
43 | * @unsigned f@ = initial tracing flags |
44 | * @unsigned bad@ = forbidden tracing flags |
45 | * |
46 | * Returns: Trace flags as set by user. |
47 | * |
48 | * Use: Parses an option string from the user and sets the |
49 | * appropriate trace flags. If the argument is null or a single |
50 | * `?' character, a help message is displayed. |
51 | */ |
52 | |
ba9edb26 |
53 | unsigned traceopt(const trace_opt *t, const char *p, |
54 | unsigned f, unsigned bad) |
4ce6f42e |
55 | { |
56 | unsigned sense = 1; |
57 | |
58 | /* --- Dump out help text --- */ |
59 | |
ba9edb26 |
60 | if (!p || !strcmp(p, "?")) { |
61 | const trace_opt *tt; |
4ce6f42e |
62 | puts("Trace options:"); |
ba9edb26 |
63 | for (tt = t; tt->ch; tt++) { |
64 | if (!(tt->f & ~bad) || !tt->help) |
4ce6f42e |
65 | continue; |
d4efbcd9 |
66 | printf(" `%c': %s\n", tt->ch, tt->help); |
4ce6f42e |
67 | } |
68 | return (f); |
69 | } |
70 | |
71 | /* --- Parse the string properly --- */ |
72 | |
73 | f = 0; |
74 | while (*p) { |
75 | switch (*p) { |
76 | case '+': |
77 | sense = 1; |
78 | break; |
79 | case '-': |
80 | sense = 0; |
81 | break; |
82 | default: { |
ba9edb26 |
83 | const trace_opt *tt; |
4ce6f42e |
84 | for (tt = t; tt->ch; tt++) { |
ba9edb26 |
85 | if (!(tt->f & ~bad)) |
4ce6f42e |
86 | continue; |
87 | if (tt->ch == *p) { |
88 | if (sense) |
89 | f |= (tt->f & ~bad); |
90 | else |
91 | f &= ~(tt->f & ~bad); |
ba9edb26 |
92 | goto ok; |
4ce6f42e |
93 | } |
4ce6f42e |
94 | } |
95 | moan("unknown trace option `%c'", *p); |
96 | ok:; |
97 | } break; |
98 | } |
99 | p++; |
100 | } |
101 | |
102 | return (f); |
103 | } |
104 | |
105 | /*----- That's all, folks -------------------------------------------------*/ |