⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comp.g

📁 CNC 的开放码,EMC2 V2.2.8版
💻 G
📖 第 1 页 / 共 3 页
字号:
def document(filename, outfilename):    if outfilename is None:        outfilename = os.path.splitext(filename)[0] + ".9"    initialize()    f = open(filename).read()    a, b = f.split("\n;;\n", 1)    p = parse('File', a + "\n\n", filename)    if not p: raise SystemExit, 1    f = open(outfilename, "w")    has_personality = False    for name, type, array, dir, value, personality in pins:        if personality: has_personality = True    for name, type, array, dir, value, personality in params:        if personality: has_personality = True    print >>f, ".TH %s \"9\" \"%s\" \"EMC Documentation\" \"HAL Component\"" % (        comp_name.upper(), time.strftime("%F"))    print >>f, ".de TQ\n.br\n.ns\n.TP \\\\$1\n..\n"    print >>f, ".SH NAME\n"    doc = finddoc('component')        if doc and doc[2]:        if '\n' in doc[2]:            firstline, rest = doc[2].split('\n', 1)        else:            firstline = doc[2]            rest = ''        print >>f, "%s \\- %s" % (doc[1], firstline)    else:        rest = ''        print >>f, "%s" % doc[1]    print >>f, ".SH SYNOPSIS"    if options.get("userspace"):        print >>f, ".B %s" % comp_name    else:        if rest:            print >>f, rest        else:            print >>f, ".HP"            if options.get("singleton") or options.get("count_function"):                if has_personality:                    print >>f, ".B loadrt %s personality=\\fIP\\fB" % comp_name,                else:                    print >>f, ".B loadrt %s" % comp_name,            else:                if has_personality:                    print >>f, ".B loadrt %s [count=\\fIN\\fB] [personality=\\fIP,P,...\\fB]" % comp_name,                else:                    print >>f, ".B loadrt %s [count=\\fIN\\fB]" % comp_name,            for type, name, default, doc in modparams:                print >>f, "[%s=\\fIN\\fB]" % name,            print >>f            hasparamdoc = False            for type, name, default, doc in modparams:                if doc: hasparamdoc = True            if hasparamdoc:                print >>f, ".RS 4"                for type, name, default, doc in modparams:                    print >>f, ".TP"                    print >>f, "\\fB%s\\fR" % name,                    if default:                        print >>f, "[default: %s]" % default                    else:                        print >>f                    print >>f, doc                print >>f, ".RE"        if options.get("constructable") and not options.get("singleton"):            print >>f, ".PP\n.B newinst %s \\fIname\\fB" % comp_name    doc = finddoc('descr')        if doc and doc[1]:        print >>f, ".SH DESCRIPTION\n"        print >>f, "%s" % doc[1]    if not options.get("userspace"):        print >>f, ".SH FUNCTIONS"        for _, name, fp, doc in finddocs('funct'):            print >>f, ".TP"            print >>f, "\\fB%s\\fR" % to_hal_man(name),            if fp:                print >>f, "(uses floating-point)"            else:                print >>f            print >>f, doc    lead = ".TP"    print >>f, ".SH PINS"    for _, name, type, array, dir, doc, value, personality in finddocs('pin'):        print >>f, lead        print >>f, ".B %s\\fR" % to_hal_man(name),        print >>f, type, dir,        if array:            sz = name.count("#")            if isinstance(array, tuple):                print >>f, " (%s=%0*d..%s)" % ("M" * sz, sz, 0, array[1]),            else:                print >>f, " (%s=%0*d..%0*d)" % ("M" * sz, sz, 0, sz, array-1),        if personality:            print >>f, " [if %s]" % personality,        if value:            print >>f, "\\fR(default: \\fI%s\\fR)" % value        else:            print >>f, "\\fR"        if doc:            print >>f, doc            lead = ".TP"        else:            lead = ".TQ"    lead = ".TP"    if params:        print >>f, ".SH PARAMETERS"        for _, name, type, array, dir, doc, value, personality in finddocs('param'):            print >>f, lead            print >>f, ".B %s\\fR" % to_hal_man(name),            print >>f, type, dir,            if array:                sz = name.count("#")                print >>f, " (%s=%0*d..%0*d)" % ("M" * sz, sz, 0, sz, array-1),            if personality:                print >>f, " [if %s]" % personality,            if value:                print >>f, "\\fR(default: \\fI%s\\fR)" % value            else:                print >>f, "\\fR"            if doc:                print >>f, doc                lead = ".TP"            else:                lead = ".TQ"    doc = finddoc('see_also')        if doc and doc[1]:        print >>f, ".SH SEE ALSO\n"        print >>f, "%s" % doc[1]    doc = finddoc('license')        if doc and doc[1]:        print >>f, ".SH LICENSE\n"        print >>f, "%s" % doc[1]def process(filename, mode, outfilename):    tempdir = tempfile.mkdtemp()    try:        if outfilename is None:            if mode == PREPROCESS:                outfilename = os.path.splitext(filename)[0] + ".c"            else:                outfilename = os.path.join(tempdir,                    os.path.splitext(os.path.basename(filename))[0] + ".c")        initialize()        f = open(filename).read()        a, b = f.split("\n;;\n", 1)        p = parse('File', a + "\n\n", filename)        if not p: raise SystemExit, 1        f = open(outfilename, "w")        if options.get("userspace"):            if functions:                raise SystemExit, "Userspace components may not have functions"        else:            if not functions:                raise SystemExit, "Realtime component must have at least one function"        if not pins:            raise SystemExit, "Component must have at least one pin"        prologue(f)        lineno = a.count("\n") + 3        if "FUNCTION" in b:            f.write("#line %d \"%s\"\n" % (lineno, filename))            f.write(b)        elif len(functions) == 1:            f.write("FUNCTION(%s) {\n" % functions[0][0])            f.write("#line %d \"%s\"\n" % (lineno, filename))            f.write(b)            f.write("}\n")        else:            raise SystemExit, "Must use FUNCTION() when more than one function is defined"        epilogue(f)        f.close()        if mode != PREPROCESS:            if options.get("userspace"):                build_usr(tempdir, outfilename, mode, filename)            else:                build_rt(tempdir, outfilename, mode, filename)    finally:        shutil.rmtree(tempdir) def usage(exitval=0):    print """%(name)s: Build, compile, and install EMC HAL componentsUsage:           %(name)s [--compile|--preprocess|--document|--view-doc] compfile...    [sudo] %(name)s [--install|--install-doc] compfile...           %(name)s --compile --userspace cfile...    [sudo] %(name)s --install --userspace cfile...    [sudo] %(name)s --install --userspace pyfile...""" % {'name': os.path.basename(sys.argv[0])}    raise SystemExit, exitvaldef main():    mode = PREPROCESS    outfile = None    userspace = False    try:        opts, args = getopt.getopt(sys.argv[1:], "uijcpdo:h?",                           ['install', 'compile', 'preprocess', 'outfile=',                            'document', 'help', 'userspace', 'install-doc',                            'view-doc'])    except getopt.GetoptError:        usage(1)    for k, v in opts:        if k in ("-u", "--userspace"):            userspace = True        if k in ("-i", "--install"):            mode = INSTALL        if k in ("-c", "--compile"):            mode = COMPILE        if k in ("-p", "--preprocess"):            mode = PREPROCESS        if k in ("-d", "--document"):            mode = DOCUMENT        if k in ("-j", "--install-doc"):            mode = INSTALLDOC        if k in ("-j", "--view-doc"):            mode = VIEWDOC        if k in ("-o", "--outfile"):            if len(args) != 1:                raise SystemExit, "Cannot specify -o with multiple input files"            outfile = v         if k in ("-?", "-h", "--help"):            usage(0)    if outfile and mode != PREPROCESS and mode != DOCUMENT:        raise SystemExit, "Can only specify -o when preprocessing or documenting"    for f in args:        try:            basename = os.path.basename(os.path.splitext(f)[0])            if f.endswith(".comp") and mode == DOCUMENT:                document(f, outfile)                        elif f.endswith(".comp") and mode == VIEWDOC:                tempdir = tempfile.mkdtemp()                try:                    outfile = os.path.join(tempdir, basename + ".9")                    document(f, outfile)                    os.spawnvp(os.P_WAIT, "man", ["man", outfile])                finally:                    shutil.rmtree(tempdir)            elif f.endswith(".comp") and mode == INSTALLDOC:                manpath = os.path.join(BASE, "share/man/man9")                if not os.path.isdir(manpath):                    manpath = os.path.join(BASE, "docs/man/man9")                outfile = os.path.join(manpath, basename + ".9")                print "INSTALLDOC", outfile                document(f, outfile)                        elif f.endswith(".comp"):                process(f, mode, outfile)            elif f.endswith(".py") and mode == INSTALL:                lines = open(f).readlines()                if lines[0].startswith("#!"): del lines[0]                lines[0] = "#!%s\n" % sys.executable                outfile = os.path.join(BASE, "bin", basename)                try: os.unlink(outfile)                except os.error: pass                open(outfile, "w").writelines(lines)                os.chmod(outfile, 0555)            elif f.endswith(".c") and mode != PREPROCESS:                tempdir = tempfile.mkdtemp()                try:                    shutil.copy(f, tempdir)                    if userspace:                        build_usr(tempdir, os.path.join(tempdir, os.path.basename(f)), mode, f)                    else:                        build_rt(tempdir, os.path.join(tempdir, os.path.basename(f)), mode, f)                finally:                    shutil.rmtree(tempdir)             else:                raise SystemExit, "Unrecognized file type for mode %s: %r" % (modename[mode], f)        except:            ex_type, ex_value, exc_tb = sys.exc_info()            try:                os.unlink(outfile)            except: # os.error:                pass            raise ex_type, ex_value, exc_tbif __name__ == '__main__':    main()# vim:sw=4:sts=4:et

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -