summaryrefslogtreecommitdiffstats
path: root/contrib/syslinux-4.02/com32/cmenu/menugen.py
blob: 70ec1f87e9ddd7a5ba904e7ca7130fb340c9cad0 (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python

import sys, re, getopt

class Menusystem:

   types = {"run"      : "OPT_RUN",
            "inactive" : "OPT_INACTIVE",
            "checkbox" : "OPT_CHECKBOX",
            "radiomenu": "OPT_RADIOMENU",
            "sep"      : "OPT_SEP",
            "invisible": "OPT_INVISIBLE",
            "radioitem": "OPT_RADIOITEM",
            "exitmenu" : "OPT_EXITMENU",
            "login"    : "login", # special type
            "submenu"  : "OPT_SUBMENU"}

   entry_init = { "item" : "",
                  "info" : "",
                  "data" : "",
                  "ipappend" : 0, # flag to send in case of PXELINUX
                  "helpid" : 65535, # 0xFFFF
                  "shortcut":"-1",
                  "state"  : 0, # initial state of checkboxes
                  "argsmenu": "", # name of menu containing arguments
                  "perms"  : "", # permission required to execute this entry
                  "_updated" : None, # has this dictionary been updated
                  "type" : "run" }

   menu_init = {  "title" : "",
                  "row" : "0xFF", # let system decide position
                  "col" : "0xFF",
                  "_updated" : None,
                  "name" : "" }

   system_init ={ "videomode" : "0xFF",
                  "title" : "Menu System",
                  "top" : "1",
                  "left" : "1" ,
                  "bot" : "21",
                  "right":"79",
                  "helpdir" : "/isolinux/help",
                  "pwdfile" : "",
                  "pwdrow"  : "23",
                  "editrow" : "23",
                  "skipcondn"  : "0",
                  "skipcmd" : ".exit",
                  "startfile": "",
                  "onerrorcmd":".repeat",
                  "exitcmd"  : ".exit",
                  "exitcmdroot"  : "",
                  "timeout"  : "600",
                  "timeoutcmd":".beep",
                  "totaltimeout" : "0",
                  "totaltimeoutcmd" : ".wait"
                 }

   shift_flags = { "alt"  : "ALT_PRESSED",
                   "ctrl" : "CTRL_PRESSED",
                   "shift": "SHIFT_PRESSED",
                   "caps" : "CAPSLOCK_ON",
                   "num"  : "NUMLOCK_ON",
                   "ins"  : "INSERT_ON"
                 }

   reqd_templates = ["item","login","menu","system"]

   def __init__(self,template):
       self.state = "system"
       self.code_template_filename = template
       self.menus = []
       self.init_entry()
       self.init_menu()
       self.init_system()
       self.vtypes = " OR ".join(self.types.keys())
       self.vattrs = " OR ".join(filter(lambda x: x[0] != "_", self.entry.keys()))
       self.mattrs = " OR ".join(filter(lambda x: x[0] != "_", self.menu.keys()))

   def init_entry(self):
       self.entry = self.entry_init.copy()

   def init_menu(self):
       self.menu = self.menu_init.copy()

   def init_system(self):
       self.system = self.system_init.copy()

   def add_menu(self,name):
       self.add_item()
       self.init_menu()
       self.menu["name"] = name
       self.menu["_updated"] = 1
       self.menus.append( (self.menu,[]) )

   def add_item(self):
       if self.menu["_updated"]: # menu details have changed
          self.menus[-1][0].update(self.menu)
          self.init_menu()
       if self.entry["_updated"]:
          if not self.entry["info"]:
             self.entry["info"] = self.entry["data"]
          if not self.menus:
             print "Error before line %d" % self.lineno
             print "REASON: menu must be declared before a menu item is declared"
             sys.exit(1)
          self.menus[-1][1].append(self.entry)
       self.init_entry()

   def set_item(self,name,value):
       if not self.entry.has_key(name):
          msg = ["Unknown attribute %s in line %d" % (name,self.lineno)]
          msg.append("REASON: Attribute must be one of %s" % self.vattrs)
          return "\n".join(msg)
       if name=="type" and not self.types.has_key(value):
          msg = [ "Unrecognized type %s in line %d" % (value,self.lineno)]
          msg.append("REASON: Valid types are %s" % self.vtypes)
          return "\n".join(msg)
       if name=="shortcut":
          if (value <> "-1") and not re.match("^[A-Za-z0-9]$",value):
             msg = [ "Invalid shortcut char '%s' in line %d" % (value,self.lineno) ]
             msg.append("REASON: Valid values are [A-Za-z0-9]")
             return "\n".join(msg)
          elif value <> "-1": value = "'%s'" % value
       elif name in ["state","helpid","ipappend"]:
          try:
              value = int(value)
          except:
              return "Value of %s in line %d must be an integer" % (name,self.lineno)
       self.entry[name] = value
       self.entry["_updated"] = 1
       return ""

   def set_menu(self,name,value):
       if not self.menu.has_key(name):
          return "Error: Unknown keyword %s" % name
       self.menu[name] = value
       self.menu["_updated"] = 1
       return ""

   def set_system(self,name,value):
       if not self.system.has_key(name):
          return "Error: Unknown keyword %s" % name
       if name == "skipcondn":
          try: # is skipcondn a number?
             a = int(value)
          except: # it is a "-" delimited sequence
             value = value.lower()
             parts = [ self.shift_flags.get(x.strip(),None) for x in value.split("-") ]
             self.system["skipcondn"] = " | ".join(filter(None, parts))
       else:
          self.system[name] = value

   def set(self,name,value):
       # remove quotes if given
       if (value[0] == value[-1]) and (value[0] in ['"',"'"]): # remove quotes
          value = value[1:-1]
       if self.state == "system":
          err = self.set_system(name,value)
          if not err: return
       if self.state == "menu":
          err = self.set_menu(name,value)
          # change state to entry it menu returns error
          if err:
             err = None
             self.state = "item"
       if self.state == "item":
          err = self.set_item(name,value)

       if not err: return

       # all errors so return item's error message
       print err
       sys.exit(1)

   def print_entry(self,entry,fd):
       entry["type"] = self.types[entry["type"]]
       if entry["type"] == "login": #special type
          fd.write(self.templates["login"] % entry)
       else:
          fd.write(self.templates["item"] % entry)

   def print_menu(self,menu,fd):
       if menu["name"] == "main": self.foundmain = 1
       fd.write(self.templates["menu"] % menu)
       if (menu["row"] != "0xFF") or (menu["col"] != "0xFF"):
          fd.write('  set_menu_pos(%(row)s,%(col)s);\n' % menu)


   def output(self,filename):
       curr_template = None
       contents = []
       self.templates = {}
       regbeg = re.compile(r"^--(?P<name>[a-z]+) BEGINS?--\n$")
       regend = re.compile(r"^--[a-z]+ ENDS?--\n$")
       ifd = open(self.code_template_filename,"r")
       for line in ifd.readlines():
           b = regbeg.match(line)
           e = regend.match(line)
           if e: # end of template
              if curr_template:
                 self.templates[curr_template] = "".join(contents)
              curr_template = None
              continue
           if b:
              curr_template = b.group("name")
              contents = []
              continue
           if not curr_template: continue # lines between templates are ignored
           contents.append(line)
       ifd.close()

       missing = None
       for x in self.reqd_templates:
           if not self.templates.has_key(x): missing = x
       if missing:
           print "Template %s required but not defined in %s" % (missing,self.code_template_filename)

       if filename == "-":
          fd = sys.stdout
       else: fd = open(filename,"w")
       self.foundmain = None
       fd.write(self.templates["header"])
       fd.write(self.templates["system"] % self.system)
       for (menu,items) in self.menus:
           self.print_menu(menu,fd)
           for entry in items: self.print_entry(entry,fd)
       fd.write(self.templates["footer"])
       fd.close()
       if not self.foundmain:
          print "main menu not found"
          print self.menus
          sys.exit(1)

   def input(self,filename):
       if filename == "-":
          fd = sys.stdin
       else: fd = open(filename,"r")
       self.lineno = 0
       self.state = "system"
       for line in fd.readlines():
         self.lineno = self.lineno + 1
         if line and line[-1] in ["\r","\n"]: line = line[:-1]
         if line and line[-1] in ["\r","\n"]: line = line[:-1]
         line = line.strip()
         if line and line[0] in ["#",";"]: continue

         try:
           # blank line -> starting a new entry
           if not line:
              if self.state == "item": self.add_item()
              continue

           # starting a new section?
           if line[0] == "[" and line[-1] == "]":
              self.state = "menu"
              self.add_menu(line[1:-1])
              continue

           # add property of current entry
           pos = line.find("=") # find the first = in string
           if pos < 0:
              print "Syntax error in line %d" % self.lineno
              print "REASON: non-section lines must be of the form ATTRIBUTE=VALUE"
              sys.exit(1)
           attr = line[:pos].strip().lower()
           value = line[pos+1:].strip()
           self.set(attr,value)
         except:
            print "Error while parsing line %d: %s" % (self.lineno,line)
            raise
       fd.close()
       self.add_item()

def usage():
    print sys.argv[0]," [options]"
    print "--input=<file>    is the name of the .menu file declaring the menu structure"
    print "--output=<file>   is the name of generated C source"
    print "--template=<file> is the name of template to be used"
    print
    print "input and output default to - (stdin and stdout respectively)"
    print "template defaults to adv_menu.tpl"
    sys.exit(1)

def main():
    tfile = "adv_menu.tpl"
    ifile = "-"
    ofile = "-"
    opts,args = getopt.getopt(sys.argv[1:], "hi:o:t:",["input=","output=","template=","help"])
    if args:
       print "Unknown options %s" % args
       usage()
    for o,a in opts:
        if o in ["-i","--input"]:
           ifile = a
        elif o in ["-o", "--output"]:
           ofile = a
        elif o in ["-t","--template"]:
           tfile = a
        elif o in ["-h","--help"]:
           usage()

    inst = Menusystem(tfile)
    inst.input(ifile)
    inst.output(ofile)

if __name__ == "__main__":
   main()