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.
6 # With -p, dump a Python status mask list translated from the C one.
8 # With -c, generate C code to dump client-side masks for debugging purposes.
10 # With -d, generate C code to dump demon-side masks for debugging purposes.
12 import sys, commands, glob, getopt
14 class SourceExtractor:
15 def __init__(self, sourcefile, suffix, clientside):
16 self.sourcefile = sourcefile
18 self.clientside = clientside
19 self.daemonfiles = ["gpsd.c", "libgpsd_core.c", "pseudonmea.c"] + glob.glob("driver_*.c") + ["gpsmon.c"] + glob.glob("monitor_*.c")
21 self.primitive_masks = []
22 for line in file(self.sourcefile):
23 if line.startswith("#define") and self.suffix in line:
25 self.masks.append((fields[1], fields[2]))
26 if fields[2].endswith("u"):
27 self.primitive_masks.append((fields[1], fields[2]))
29 def in_library(self, flag):
30 (status, output) = commands.getstatusoutput("grep %s libgps_core.c libgps_json.c gpsctl.c" % flag)
33 def in_daemon(self, flag):
34 (status, output) = commands.getstatusoutput("grep %s %s" % (flag, " ".join(self.daemonfiles)))
37 def relevant(self, flag):
39 return self.in_library(flag)
41 return self.in_daemon(flag)
43 if __name__ == '__main__':
45 (options, arguments) = getopt.getopt(sys.argv[1:], "cdpt")
46 pythonize = tabulate = False
47 clientgen = daemongen = False
48 for (switch, val) in options:
59 source = SourceExtractor("./gps.h", "_SET", clientside=True)
63 source = SourceExtractor("./gpsd.h", "_IS", clientside=False)
68 print "%-14s %8s %8s" % (" ", "Library", banner)
69 for (flag, value) in source.masks:
70 print "%-14s %8s" % (flag, source.relevant(flag))
72 for (d, v) in source.masks:
75 print "%-15s\t= %s" % (d, v)
76 if not pythonize and not tabulate:
78 for (d, v) in source.primitive_masks:
79 if source.relevant(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! */
90 const char *%s_maskdump(gps_mask_t set)
96 } *sp, names[] = {""" % (prefix, maxout + 3,)
97 for (flag, value) in source.primitive_masks:
99 if stem.endswith(source.suffix):
100 stem = stem[:-len(source.suffix)]
101 print "\t{%s,\t\"%s\"}," % (flag, stem)
105 memset(buf, '\\0', sizeof(buf));
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));
112 if (buf[1] != \'\\0\')
113 buf[strlen(buf)-1] = \'\\0\';
114 (void)strlcat(buf, "}", sizeof(buf));
118 except KeyboardInterrupt:
121 # The following sets edit modes for GNU EMACS