78d82bdbe04f80808967ac142ad51a15dcd90623
[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 "report.h"
35 #include "trace.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @traceopt@ --- *
40 *
41 * Arguments: @const trace_opt *t@ = pointer to trace options table
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
53 unsigned traceopt(const trace_opt *t, const char *p,
54 unsigned f, unsigned bad)
55 {
56 unsigned sense = 1;
57
58 /* --- Dump out help text --- */
59
60 if (!p || !strcmp(p, "?")) {
61 const trace_opt *tt;
62 puts("Trace options:");
63 for (tt = t; tt->ch; tt++) {
64 if (!(tt->f & ~bad) || !tt->help)
65 continue;
66 printf(" `%c': %s\n", tt->ch, tt->help);
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: {
83 const trace_opt *tt;
84 for (tt = t; tt->ch; tt++) {
85 if (!(tt->f & ~bad))
86 continue;
87 if (tt->ch == *p) {
88 if (sense)
89 f |= (tt->f & ~bad);
90 else
91 f &= ~(tt->f & ~bad);
92 goto ok;
93 }
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 -------------------------------------------------*/