summaryrefslogtreecommitdiff
path: root/mkheader.py
blob: d74444bee5bd26f69bba835e2d3667068d090185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# mkheader.py - create a C header file skeleton (#ifndef/#define) from filename
#
# Copyright © 2009 Tobias Klauser <tklauser@distanz.ch>
#
# THE BEER-WARE LICENSE (Revision 42):
# <tklauser@distanz.ch> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.
#  Tobias Klauser

import os, sys
import getopt
import re

def usage():
    print("""usage: %s [OPTION] FILE...

 -b  do not use full path for the guard name, just the basename
 -e  launch editor after creating files. Uses editor from $EDITOR envvar.
 -f  overwrite existing files
 -s  print generated to stdout instead of writing it to file
 -h  show this help and exit""" % (os.path.basename(sys.argv[0])))

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "befsh")
    except getopt.GetoptError, err:
        print(str(err))
        usage()
        sys.exit(2)

    if len(args) < 1:
        usage()
        sys.exit(2)

    overwrite = False
    launch_editor = False
    just_basename = False
    to_stdout = False
    for o, a in opts:
        if o == "-b":
            just_basename = True
        elif o == "-e":
            launch_editor = True
        elif o == "-f":
            overwrite = True
        elif o == "-s":
            to_stdout = True
        elif o == "-h":
            usage()
            sys.exit()
        else:
            assert False, "unhandled option"

    if to_stdout and launch_editor:
        print("Options -e and -s cannot be used together")
        sys.exit(2)

    for f in args:
        if just_basename:
            s = os.path.basename(f)
        else:
            s = f

        if not to_stdout and not overwrite and os.path.exists(f):
            print("Header file '%s' already exists. Use -f to override." % f)
            continue

        q = re.compile('[^a-zA-Z0-9_]')
        define = q.sub('_', s.upper())
        if to_stdout:
            fd = sys.stdout
        else:
            print("Creating %s..." % f)
            fd = open(f, 'w')
        fd.write('#ifndef _%s_\n' % define)
        fd.write('#define _%s_\n\n' % define)
        fd.write('/* TODO: Your code here */\n\n')
        fd.write('#endif /* _%s_ */' % define)
        if not to_stdout:
            fd.close()

    if launch_editor:
        editor = os.getenv("EDITOR")
        if editor is None:
            editor = "vi"

        # Fork an editor for each file as not all editors support
        # editing multiple files at once (e.g. splitted view in vim)
        for f in args:
            print("Editing header '%s' with '%s'" % (f, editor))

            pid = os.fork()
            if pid == 0:	# child process
                os.execvp(editor, [editor, f])
                sys.exit(0)
            else:		    # parent process
                os.waitpid(pid, 0)

if __name__ == '__main__':
    main()