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