Back to index

python3.2  3.2.2
Functions | Variables
ifdef Namespace Reference

Functions

def main
def process

Variables

list defs = []
list undefs = []

Function Documentation

def ifdef.main ( void  )

Definition at line 35 of file ifdef.py.

00035 
00036 def main():
00037     opts, args = getopt.getopt(sys.argv[1:], 'D:U:')
00038     for o, a in opts:
00039         if o == '-D':
00040             defs.append(a)
00041         if o == '-U':
00042             undefs.append(a)
00043     if not args:
00044         args = ['-']
00045     for filename in args:
00046         if filename == '-':
00047             process(sys.stdin, sys.stdout)
00048         else:
00049             f = open(filename, 'r')
00050             process(f, sys.stdout)
00051             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def ifdef.process (   fpi,
  fpo 
)

Definition at line 52 of file ifdef.py.

00052 
00053 def process(fpi, fpo):
00054     keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
00055     ok = 1
00056     stack = []
00057     while 1:
00058         line = fpi.readline()
00059         if not line: break
00060         while line[-2:] == '\\\n':
00061             nextline = fpi.readline()
00062             if not nextline: break
00063             line = line + nextline
00064         tmp = line.strip()
00065         if tmp[:1] != '#':
00066             if ok: fpo.write(line)
00067             continue
00068         tmp = tmp[1:].strip()
00069         words = tmp.split()
00070         keyword = words[0]
00071         if keyword not in keywords:
00072             if ok: fpo.write(line)
00073             continue
00074         if keyword in ('ifdef', 'ifndef') and len(words) == 2:
00075             if keyword == 'ifdef':
00076                 ko = 1
00077             else:
00078                 ko = 0
00079             word = words[1]
00080             if word in defs:
00081                 stack.append((ok, ko, word))
00082                 if not ko: ok = 0
00083             elif word in undefs:
00084                 stack.append((ok, not ko, word))
00085                 if ko: ok = 0
00086             else:
00087                 stack.append((ok, -1, word))
00088                 if ok: fpo.write(line)
00089         elif keyword == 'if':
00090             stack.append((ok, -1, ''))
00091             if ok: fpo.write(line)
00092         elif keyword == 'else' and stack:
00093             s_ok, s_ko, s_word = stack[-1]
00094             if s_ko < 0:
00095                 if ok: fpo.write(line)
00096             else:
00097                 s_ko = not s_ko
00098                 ok = s_ok
00099                 if not s_ko: ok = 0
00100                 stack[-1] = s_ok, s_ko, s_word
00101         elif keyword == 'endif' and stack:
00102             s_ok, s_ko, s_word = stack[-1]
00103             if s_ko < 0:
00104                 if ok: fpo.write(line)
00105             del stack[-1]
00106             ok = s_ok
00107         else:
00108             sys.stderr.write('Unknown keyword %s\n' % keyword)
00109     if stack:
00110         sys.stderr.write('stack: %s\n' % stack)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

list ifdef.defs = []

Definition at line 32 of file ifdef.py.

list ifdef.undefs = []

Definition at line 33 of file ifdef.py.