"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / maskaudit.py.in
1 #!@PYTHON@
2 #
3 # This file is Copyright (c) 2010 by the GPSD project
4 # BSD terms apply: see the file COPYING in the distribution root for details.
5 #
6 # With -p, dump a Python status mask list translated from the C one.
7 #
8 # With -c, generate C code to dump client-side masks for debugging purposes.
9 #
10 # With -d, generate C code to dump demon-side masks for debugging purposes.
11
12 import sys, commands, glob, getopt
13
14 class SourceExtractor:
15     def __init__(self, sourcefile, suffix, clientside):
16         self.sourcefile = sourcefile
17         self.suffix = suffix
18         self.clientside = clientside
19         self.daemonfiles = ["gpsd.c", "libgpsd_core.c", "pseudonmea.c"] + glob.glob("driver_*.c") + ["gpsmon.c"] + glob.glob("monitor_*.c")
20         self.masks = []
21         self.primitive_masks = []
22         for line in file(self.sourcefile):
23             if line.startswith("#define") and self.suffix in line:
24                 fields = line.split()
25                 self.masks.append((fields[1], fields[2]))
26                 if fields[2].endswith("u"):
27                     self.primitive_masks.append((fields[1], fields[2]))
28     
29     def in_library(self, flag):
30         (status, output) = commands.getstatusoutput("grep %s libgps_core.c libgps_json.c gpsctl.c" % flag)
31         return status == 0
32
33     def in_daemon(self, flag):
34         (status, output) = commands.getstatusoutput("grep %s %s" % (flag, " ".join(self.daemonfiles)))
35         return status == 0
36
37     def relevant(self, flag):
38         if self.clientside:
39             return self.in_library(flag)
40         else:
41             return self.in_daemon(flag)
42     
43 if __name__ == '__main__':
44     try:
45         (options, arguments) = getopt.getopt(sys.argv[1:], "cdpt")
46         pythonize = tabulate = False
47         clientgen = daemongen = False
48         for (switch, val) in options:
49             if (switch == '-p'):
50                 pythonize = True
51             if (switch == '-c'):
52                 clientgen = True
53             if (switch == '-d'):
54                 daemongen = True
55             if (switch == '-t'):
56                 tabulate = True
57
58         if clientgen:
59             source = SourceExtractor("./gps.h", "_SET", clientside=True)
60             prefix = "gps"
61             banner = "Library"
62         elif daemongen:
63             source = SourceExtractor("./gpsd.h", "_IS", clientside=False)
64             prefix = "gpsd"
65             banner = "Daemon"
66
67         if tabulate:
68             print "%-14s        %8s %8s" % (" ", "Library", banner)
69             for (flag, value) in source.masks:
70                 print "%-14s    %8s" % (flag, source.relevant(flag))
71         if pythonize:
72             for (d, v) in source.masks:
73                 if v[-1] == 'u':
74                     v = v[:-1]
75                 print "%-15s\t= %s" % (d, v)
76         if not pythonize and not tabulate:
77             maxout = 0
78             for (d, v) in source.primitive_masks:
79                 if source.relevant(d):
80                     stem = d
81                     if stem.endswith(source.suffix):
82                         stem = stem[:-len(source.suffix)]
83                     maxout += len(stem) + 1
84             print """/* This code is generated.  Do not hand-hack it! */
85 #include <stdio.h>
86 #include <string.h>
87
88 #include \"gpsd.h\"
89
90 const char *%s_maskdump(gps_mask_t set)
91 {
92     static char buf[%d];
93     const struct {
94         gps_mask_t      mask;
95         const char      *name;
96     } *sp, names[] = {"""  % (prefix, maxout + 3,)
97             for (flag, value) in source.primitive_masks:
98                 stem = flag
99                 if stem.endswith(source.suffix):
100                     stem = stem[:-len(source.suffix)]
101                 print "\t{%s,\t\"%s\"}," % (flag, stem)
102             print '''\
103     };
104
105     memset(buf, '\\0', sizeof(buf));
106     buf[0] = '{';
107     for (sp = names; sp < names + sizeof(names)/sizeof(names[0]); sp++)
108         if ((set & sp->mask)!=0) {
109             (void)strlcat(buf, sp->name, sizeof(buf));
110             (void)strlcat(buf, "|", sizeof(buf));
111         }
112     if (buf[1] != \'\\0\')
113         buf[strlen(buf)-1] = \'\\0\';
114     (void)strlcat(buf, "}", sizeof(buf));
115     return buf;
116 }
117 '''
118     except KeyboardInterrupt:
119         pass
120
121 # The following sets edit modes for GNU EMACS
122 # Local Variables:
123 # mode:python
124 # End: