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