~samiam/MaraDNS

ref: 3.5.0002 MaraDNS/tools/bind2csv2.py -rwxr-xr-x 35.8 KiB View raw
db09084f — Sam Trenholme MaraDNS release 3.5.0002 3 months ago
                                                                                
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
#!/usr/bin/python

# Note: As of 2019, this script will run both in python2 and python3

# Copyright (c) 2006-2007,2019 Sam Trenholme
# 
# TERMS
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# This software is provided 'as is' with no guarantees of correctness or
# fitness for purpose.

# This Python script converts a BIND zone file in to a CSV2 zone file

# This program works as a state machine; we have various states and
# convert the input in to the output depending on what state we are in.
# This is not a pure finite state machine, but is one based on the model
# of the CSV2 zone file parser

import sys
import os
import re
import string
from struct import *

ERROR = -1

# We use the compiled regular expressions to determine what input we 
# are getting in a given FSM (finite state machine) state
is_space = re.compile("[ \t]")
is_nonspace = re.compile("[^ \t\r\n]")
is_comment = re.compile("\#[^\n]+$")
is_newline = re.compile("[\r\n]")
is_white = re.compile("[ \t\r\n]")
is_newrec = re.compile("[A-Za-z0-9\-\_\$]")
is_dlabel = re.compile("[A-Za-z0-9\-\_]")
is_number = re.compile("[0-9]")
is_numdot = re.compile("[0-9\.]")
is_hexcolon = re.compile("[0-9a-fA-F\:]")
is_letter = re.compile("[A-Za-z]")
is_pstate = re.compile("paren$")

# This class stores various bits of information we need to look at
# or manipulate while processing the BIND zone file
class buf:
	# Constructor
	def __init__(self):
		self.pre_l = "" # Anything before the dlabel
		self.l = "" # Dlabel of the record we're writing to
		self.thisrr = "" # The current record we're writing to
		self.o = "" # "Write" (for writing things)
		self.s = "" # SOA records; must be at top of CSV2 zone file
		self.n = "" # NS records; must be right after SOA records
		self.rrtype = 3 # 1: SOA RR 2: NS RR 3: Normal RR
		self.rrmode = 1 # Used for Origin/TTL handling 
                                # 1 SOA, 2 NS, 3 Normal RR
		self.lseen = 0 # Whether the label has been seen
		self.origin = "%" # The current origin for this zone
		self.ttl = "" # The current TTL for this zone
		self.norm_ttl = "" # The current TTL for this record
		self.minttl = "86400" # The minimum TTL for this zone
		self.hasttl = 0 # Does this record have an explicit TTL?
		self.soa_hasttl = 0 # Does the SOA record have a TTL?
		self.nsttl = "" # The TTL for the NS records (BIND sets
                                # this to the TTL that the last NS record
				# in a zone has)
		self.isns = 0 # Whether this record is a NS record

	# Add a tilde to the end of the record (Private method)
	def addtilde(self):
		if len(self.thisrr) < 1:
			return	
		while self.thisrr[-1] == " " or self.thisrr[-1] == "\n" or \
                      self.thisrr[-1] == "\t" or self.thisrr[-1] == "\t":
			self.thisrr = self.thisrr[:-1]
			if len(self.thisrr) < 1:
				return
		if is_comment.search(self.thisrr):
			self.thisrr = self.thisrr + "\n"
		else:
			self.thisrr = self.thisrr + " "
		if is_nonspace.search(self.thisrr):
			self.thisrr += "~\n"
		
	# Methods for the current domain label for a given rr
	# Add to label (Public method)
	def label_add(self, a):
		self.lseen = 1
		self.l += a

	# Get label (Public method)
	def label_get(self):
		return self.l

	# Reset label (Public method)
	def label_reset(self):
		self.l = ""

	# Tell the class we have seen the dlabel (Public method)
	def label_seen(self):
		self.lseen = 1

	# Add a character to the area before the Dlabel (private method)
	def pre_write(self, a):
		self.pre_l += a

	# Add a character to the area after the Dlabel (private method)
	def post_write(self, a):
		self.thisrr += a

	# Add a character to the current RR (public method)
	def write(self, a):
		if self.lseen == 0:
			self.pre_write(a)
		else:
			self.post_write(a)

	# Make a character the first character of the default TTL (public)
	def minttl_set(self, a):
		self.minttl = a

	# Add a character to the default TTL (public method)
	def minttl_append(self, a):
		self.minttl += a

	# Make a character the first character of the TTL in a /ttl
	# command (public)
	def slash_ttl_set(self, a):
		self.ttl = a

	# Add a character to the TTL in a /ttl command (public method)
	def slash_ttl_append(self, a):
		self.ttl += a

	# Make a character the first character of the TTL for an ordinary
	# specified TTL (public method)
	def normal_ttl_set(self, a):
		self.norm_ttl = a

	# Add a character to the TTL for an ordinary specified TTL (public)
	def normal_ttl_append(self, a):
		self.norm_ttl += a

	# Make a character the first character of the default origin (public)
	def origin_set(self, a):
		self.origin = a

	# Add a character to the default origin (public method)
	def origin_append(self, a):
		self.origin += a

	# Inform this object that this record has a TTL (public method)
	def hasttl_set(self):
		self.hasttl = 1

	# Inform this object that this record's current record's a SOA,
        # which may set the soa_hasttl flag (public)
	def issoa(self):
		if self.hasttl == 1:
			self.soa_hasttl = 1

	# Process a dcommand that we have received (public method)
	def dcommand_process(self):
		if self.l == "/ttl":
			return "prettl"
		elif self.l == "/origin":
			return "preorigin"
		else:
			print("Unknown directive " + self.l)
			return "error"

	# Add a current RR to a stream (private method)
	def rr_write(self):
		self.addtilde()
		oput = self.pre_l
		# If this is a record that doesn't "belong" here...
		if self.rrmode != self.rrtype:
			# Then the record will need an explicit origin
 			# set if it needs an origin (ends in "%") and
			# an explicit TTL set if the record doesn't
			# have a TTL
			if len(self.l) > 0 and self.l[-1] == "%":
				self.l = self.l[:-1] + origin
			# If this record does not have an explicit TTL...
			if self.hasttl == 0:
				if self.ttl != "":
					self.thisrr = " +" + self.ttl + \
						      " " + self.thisrr
				elif self.soa_hasttl == 0:
					self.thisrr = " +" + self.minttl + \
						      " " + self.thisrr
				else:
					self.thisrr = " +" + self.norm_ttl + \
						      " " + self.thisrr
		# This handles the case of the SOA having a TTL record;
		# in which case the default TTL is the last explicitly
		# set TTL (or $TTL if that has been set)
		elif self.hasttl == 0 and self.soa_hasttl == 1 and \
			self.ttl == "":
				self.thisrr = " +" + self.norm_ttl + \
					" " + self.thisrr
		oput += self.l
		oput += self.thisrr	
		self.thisrr = ""
		self.pre_l = ""
		self.lseen = 0
		self.hasttl = 0
		self.isns = 0
		return oput

	# Add the current RR to the stream of normal RRs (private method)
	def owrite(self):
		self.o += self.rr_write()

	# Add the current RR to the stream of SOA RRs (private method)
	def swrite(self):
		self.s += self.rr_write()

	# Add the current RR to the stream of NS RRs (private method)
	def nwrite(self):
		self.n += self.rr_write()

	# Flush the current RR, adding it to the appropiate stream, and
	# get ready to start a new RR (Public method)
	def flush_rr(self): 
		if is_nonspace.search(self.thisrr):
			if self.rrtype == 1: 
				self.swrite()
			elif self.rrtype == 2: 
				if self.rrmode == 1:
					self.rrmode = 2
				self.nwrite()
			elif self.rrtype == 3: 
				if self.rrmode == 2:
					self.rrmode = 3
				self.owrite()
			else:
				if self.rrmode == 2:
					self.rrmode = 3
				self.owrite()
			self.rrtype = 3

	# Make the current RR a "soa" RR (Public method)
	def set_soa(self):
		self.rrtype = 1

	# Make the current RR a "ns" RR (Public method)
	def set_ns(self):
		self.rrtype = 2

	# Make ths current RR a "normal" RR (Public, unused method)
	def set_normal(self):
		self.rrtype = 3

	# Read all of the three RR streams combined together in to a 
        # full zone file (Public method)
	def read(self):
		if self.soa_hasttl == 0:
			return "/ttl " + self.minttl + " ~\n" + self.s + \
			self.n + self.o
		else:
			return self.s + self.n + self.o

# "Output print"; we give this a short name because this function is
# called so often.  This puts a string on the output tape

def op(o,a):
	o.write(a)

# The actions we do when the finite state machine is in various
# states

# Process a comment
def process_comment(i,o):
	z = 0
	op(o,"#")
	while z < 1000:
		a = i.read(1).decode('utf-8')
		if is_newline.match(a):
			return a
		else:
			op(o,a)
		z += 1
	return a
			
# Based on the rrtype they gave us, determine what state we should be
# in next

def get_rr_type(rrname):
	rrname = rrname.lower()
	if rrname == "in":
		return "error"
	elif rrname == "a":
		return "rr_a"
	elif rrname == "ns":
		return "rr_ns"
	elif rrname == "cname":
		return "rr_1dlabel"
	elif rrname == "ptr":
		return "rr_1dlabel"
	elif rrname == "soa":
		return "rr_soa"
	elif rrname == "mx":
		return "rr_mx"
	elif rrname == "aaaa":
		return "rr_aaaa"
	elif rrname == "srv":
		return "rr_srv"
	elif rrname == "txt":
		return "rr_txt"
	elif rrname == "spf":
		return "rr_txt"
	# Obscure RRs follow
	elif rrname == "hinfo":
		return "rr_txt" # TXT with mandatory two fields
	elif rrname == "wks":
		#return "rr_wks" # Tricky variable-argument corner case
		return "rr_variargs" # I'll just let the csv2 code parse this
	elif rrname == "mb":
		return "rr_1dlabel"
	elif rrname == "md":
		return "rr_1dlabel"
	elif rrname == "mf":
		return "rr_1dlabel"
	elif rrname == "mg":
		return "rr_1dlabel"
	elif rrname == "mr":
		return "rr_1dlabel"
	elif rrname == "minfo":
		return "rr_2dlabels"
	elif rrname == "afsdb":
		return "rr_mx"
	elif rrname == "rp":
		return "rr_2dlabels"
	elif rrname == "x25":
		return "rr_txt"
	elif rrname == "isdn":
		return "rr_txt"
	elif rrname == "rt":
		return "rr_mx"
	elif rrname == "nsap":
		#return "rr_hex"
		return "rr_variargs" # I'll just let the csv2 code parse this
	elif rrname == "px":
		#return "rr_px"
		return "rr_variargs" # I'll just let the csv2 code parse this
	elif rrname == "gpos":
		return "rr_txt" # TXT with three mandatory fields
	elif rrname == "loc":
		#return "rr_loc" # Complicated RR with variable # of fields
		return "rr_variargs" # I'll just let the csv2 code parse this
	else:	
		print("Error: Unknown RR " + rrname)
		return "error"

# Generic handler that handles parenthesis in a zone file
def handle_paren(a,o,state,buffer):
	paren = ""
	if is_pstate.search(state):
		paren = "_paren"
	if paren == "" and is_newline.match(a):
		print("Error: Premature termination of RR")
		return ("error", "", 1, paren)
	if paren == "_paren" and a == "(":
		print("Error: Parens don't nest")
		retrun ("error", "", 1, paren)
	if paren == "" and a == "(":
		op(o," ")
		return (state + "_paren", buffer, 1, paren)
	if paren == "_paren" and a == ")":
		op(o," ")
		paren = ""
		return (state[:-6], buffer, 1, paren)
	return (state,buffer,0,paren)

# Between the rrtype and the rrdata in the zone file
def prrtype(a,o,state,buffer):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return (state, buffer)
	if is_white.match(a):
		op(o,a)
		return (state,buffer)
	x = get_rr_type(buffer)
	# NS record is a 1-dlabel record we put near the top of the zone
        # file.
	if x == "rr_ns":
		x = "rr_1dlabel"
		o.set_ns()
	# SOA record is a complicated RR type we put at the top of the
        # zone file.
	if x == "rr_soa":
		o.set_soa()
	return (x + pstr, "")
		
# Between the rrdata and the next rr in the zone file
def postrr(a,o,state,buffer):
	if is_newline.match(a) and not is_pstate.search(state):
		# Flush out this RR
		op(o,a)
		o.flush_rr()
		return("pre_rr",buffer)
	if is_white.match(a):
		op(o,a)
		return (state,buffer)
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return (state, buffer)
	print("Error: Unexpected character after RR " + a)
	return("error","")

# After the first non-escaped newline at the end of a rr in the
# zone file
def pre_rr(a,o,state,buffer):
	if is_space.match(a):
		return ("possible_lo",buffer)
	if is_white.match(a):
		op(o,a)
		return (state,buffer)
	elif is_newrec.match(a):
		if a == "$":
			o.label_reset()
			o.label_add("/")
			return ("dcommand",buffer)
		else:
			o.label_reset()
			o.label_add(a)
			return("dlabel",buffer)
	print("Error: Unexpected character before RR " + a)
	return("error","")

# In the whitespace before a default TTL ("/ttl 12345")
def prettl(a,o,state,buffer):
	if is_white.match(a):
		op(o,a)
		return(state, buffer)
	if is_number.match(a):	
		op(o,a)
		o.slash_ttl_set(a)
		return ("sttl", buffer)
	print("Error: unexpected character before TTL " + a)
	return("error","")

# In a default TTL ("/ttl 12345")
def sttl(a,o,state,buffer):
	if is_white.match(a):
		op(o,a)
		return("postrr", buffer)
	if is_number.match(a):	
		op(o,a)
		o.slash_ttl_append(a)
		return ("sttl", buffer)
	print("Error: unexpected character in TTL " + a)
	return("error","")

# In the whitespace before an origin ("/origin foo")
def preorigin(a,o,state,buffer):
	if is_white.match(a):
		op(o,a)
		return(state, buffer)
	if is_dlabel.match(a):	
		op(o,a)
		o.origin_set(a)
		return ("origin", buffer)
	print("Error: unexpected character before origin " + a)
	return("error","")

# In an origin ("/origin foo")
def origin(a,o,state,buffer):
	if is_white.match(a):
		op(o,a)
		return("postrr", buffer)
	if is_dlabel.match(a):	
		op(o,a)
		o.origin_append(a)
		return ("origin", buffer)
	print("Error: unexpected character in origin " + a)
	return("error","")

# After a space at the beginning of an RR and before a newline
def possible_lo(a,o,state,buffer):
	if is_space.match(a):
		return (state,buffer)
	if is_white.match(a):
		return ("pre_rr",buffer)
	# Output the stored dlabel from the last record
	o.label_seen()
	op(o," ")
	if is_number.match(a):
		op(o,"+" + a)
		o.hasttl_set()
		o.normal_ttl_set(a)
		return ("ttl",buffer)
	if is_letter.match(a):
		return ("rrtype",a)      

# After a space at the beginning of an RR and before the TTL/IN/RRTYPE
def post_lo(a,o,state,buffer):
	return (a,o,state,buffer);
		
# In TTL
def ttl(a,o,state,buffer):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return (state, buffer)
	if is_white.match(a):
		op(o,a)
		return ("pdlabel" + pstr, buffer)
	if is_number.match(a):
		op(o,a)
		o.normal_ttl_append(a)
		return ("ttl" + pstr,buffer)
	print("Unexpected character in TTL " + a)
	return("error","")

# Various handlers for the rr types that we may see

# A: Internet IP address 
def rr_a(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_newline.match(a) and not is_pstate.search(state):
		return ("postrr",buffer)
	if is_white.match(a):
		op(o,a)
		return ("postrr" + pstr,buffer)
	if is_numdot.match(a):
		op(o,a)
		return (state,buffer)	
	print("Error: Unexpected character in A RR " + a)
	return("error","")

# AAAA: Ipv6 Internet IP address 
def rr_aaaa(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_newline.match(a) and not is_pstate.search(state):
		return ("postrr",buffer)
	if is_white.match(a):
		op(o,a)
		return ("postrr" + pstr,buffer)
	if is_hexcolon.match(a):
		op(o,a)
		return (state,buffer)	
	print("Error: Unexpected character in A RR " + a)
	return("error","")

# Generic handler for NS records or anything else that has a single dlabel
def rr_1dlabel(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_newline.match(a) and not is_pstate.search(state):
		return ("postrr",buffer)
	if is_white.match(a):
		# I think BIND zone files require a final dot in dlabels
                # also, or will append the domain name.  If not, this
                # bit of code will change to not have the '+ ".%"'
		op(o,a + ".%")
		return ("postrr" + pstr,buffer)
	if is_dlabel.match(a):
		op(o,a)
		return (state,buffer)
	if a == ".":
		op(o,a)
		return ("rr_1dlabel_dot" + pstr, buffer)
	print("Error: Unexpected character in RR " + a)
	return("error","")

# In the dot (".") of a dlabel	
def rr_1dlabel_dot(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_newline.match(a) and not is_pstate.search(state):
		return ("postrr",buffer)
	# End of RR after dot
	if is_white.match(a):
		op(o,a)
		return ("postrr" + pstr,buffer)
	if is_dlabel.match(a):
		op(o,a)
		return ("rr_1dlabel" + pstr,buffer)
	print("Error: Unexpected character in RR " + a)
	return("error","")

# Generic handlers for dlabels and numbers

# In a dot in a dlabel
def rr_generic_dlabel_dot(a,o,state,buffer,next):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_white.match(a):
		op(o,a)
		return(next + pstr, buffer)
	if is_dlabel.match(a):  
		op(o,a)
		if pstr == "_paren":
			return(state[:-10] + pstr, buffer)
		return (state[:-4], buffer)
	print("Error: unexpected character in RR dlabel dot " + a)
	return("error","")

# In the whitespace before a dlabel		
def rr_generic_dlabel_pre(a,o,state,buffer,next):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return(state, buffer)
	if is_white.match(a):
		op(o,a)
		return(state, buffer)
	if is_dlabel.match(a):	
		op(o,a)
		return (next + pstr, buffer)
	print("Error: unexpected character before RR dlabel " + a)
	return("error","")

# In a dlabel
def rr_generic_dlabel(a,o,state,buffer,next):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_white.match(a):
		op(o,".%" + a)
		return(next + pstr, buffer)
	if is_dlabel.match(a):	
		op(o,a)
		return (state, buffer)
	if a == ".":	
		op(o,a)
		if pstr == "_paren":
			return(state[:-10] + "_dot" + pstr, buffer)
		return (state + "_dot", buffer)
	print("Error: unexpected character in RR dlabel " + a)
	return("error","")

# In the whitespace before a number		
def rr_generic_number_pre(a,o,state,buffer,next):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return(state, buffer)
	if is_white.match(a):
		op(o,a)
		return(state, buffer)
	if is_number.match(a):	
		op(o,a)
		return (next + pstr, buffer)
	print("Error: unexpected character before RR number field " + a)
	return("error","")

# In a number
def rr_generic_number(a,o,state,buffer,next):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_white.match(a):
		op(o,a)
		return(next + pstr, buffer)
	if is_number.match(a):	
		op(o,a)
		return (state, buffer)
	print("Error: unexpected character in numer field of RR " + a)
	return("error","")

# In the whitespace before the SOA minimum field
def rr_soa_minimum_pre(a,o,state,buffer,next):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return(state, buffer)
	if is_white.match(a):
		op(o,a)
		return(state, buffer)
	if is_number.match(a):	
		op(o,a)
		o.minttl_set(a)
		return (next + pstr, buffer)
	print("Error: unexpected character before RR number field " + a)
	return("error","")

# In SOA minimum field
def rr_soa_minimum(a,o,state,buffer,next):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_white.match(a):
		op(o,a)
		return(next + pstr, buffer)
	if is_number.match(a):	
		op(o,a)
		o.minttl_append(a)
		return (state, buffer)
	print("Error: unexpected character in numer field of RR " + a)
	return("error","")

# SOA: Start of authority record (2 dlabels, 5 numbers)
def rr_soa(a,o,state,buffer):
	o.issoa()
	return rr_generic_dlabel(a,o,state,buffer,"rr_soa_2_pre")

def rr_soa_dot(a,o,state,buffer):
	return rr_generic_dlabel_dot(a,o,state,buffer,"rr_soa_2_pre")

def rr_soa_2_pre(a,o,state,buffer):
	return rr_generic_dlabel_pre(a,o,state,buffer,"rr_soa_2")

def rr_soa_2(a,o,state,buffer):
	return rr_generic_dlabel(a,o,state,buffer,"rr_soa_3_pre")

def rr_soa_2_dot(a,o,state,buffer):
	return rr_generic_dlabel_dot(a,o,state,buffer,"rr_soa_3_pre")

def rr_soa_3_pre(a,o,state,buffer):
	return rr_generic_number_pre(a,o,state,buffer,"rr_soa_3")

def rr_soa_3(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_soa_4_pre")

def rr_soa_4_pre(a,o,state,buffer):
	return rr_generic_number_pre(a,o,state,buffer,"rr_soa_4")

def rr_soa_4(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_soa_5_pre")

def rr_soa_5_pre(a,o,state,buffer):
	return rr_generic_number_pre(a,o,state,buffer,"rr_soa_5")

def rr_soa_5(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_soa_6_pre")

def rr_soa_6_pre(a,o,state,buffer):
	return rr_generic_number_pre(a,o,state,buffer,"rr_soa_6")

def rr_soa_6(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_soa_7_pre")

def rr_soa_7_pre(a,o,state,buffer):
	return rr_soa_minimum_pre(a,o,state,buffer,"rr_soa_7")

def rr_soa_7(a,o,state,buffer):
	return rr_soa_minimum(a,o,state,buffer,"postrr")

# MX: Mail exchange record (1 number, 1 dlabel)
def rr_mx(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_mx_2_pre")

def rr_mx_pre(a,o,state,buffer):
	return rr_generic_number_pre(a,o,state,buffer,"rr_mx")

def rr_mx_2_pre(a,o,state,buffer):
	return rr_generic_dlabel_pre(a,o,state,buffer,"rr_mx_2")

def rr_mx_2(a,o,state,buffer):
	return rr_generic_dlabel(a,o,state,buffer,"postrr")

def rr_mx_2_dot(a,o,state,buffer):
	return rr_generic_dlabel_dot(a,o,state,buffer,"postrr")

# generic handlers for records with 2 dlabels
def rr_2dlabels(a,o,state,buffer):
	return rr_generic_dlabel(a,o,state,buffer,"rr_mx_2_pre")

def rr_2dlabels_dot(a,o,state,buffer):
	return rr_generic_dlabel_dot(a,o,state,buffer,"rr_mx_2_pre")

# SRV record (3 numbers, 1 dlabel)
def rr_srv(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_srv_2_pre")

def rr_srv_2_pre(a,o,state,buffer):
	return rr_generic_number_pre(a,o,state,buffer,"rr_srv_2")

# Since the last fields of a SRV record are identical to a MX
# record, we can simply jump to the MX record after the second
# SRV field
def rr_srv_2(a,o,state,buffer):
	return rr_generic_number(a,o,state,buffer,"rr_mx_pre")

# Generic handler for obscure RRs, some of which have a variable number
# of arguments (PX, WKS, NSAP, and LOC)

def rr_variargs(a,o,state,buffer):
	if is_newline.match(a) and not is_pstate.search(state):
		return ("postrr",buffer)
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return(state, buffer)
	op(o,a)
	return(state, buffer)
	
# TXT record, outside of quotes

def rr_txt(a,o,state,buffer):
	if is_newline.match(a) and not is_pstate.search(state):
		return ("postrr",buffer)
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return(state, buffer)
	if is_white.match(a):
		op(o,a)
		return(state, buffer)
	if a == "\"":
		return("rr_txt_4" + pstr,buffer)
	print("Error: unexpected character ouside of quotes in TXT RR " + a)
	return("error","")

# rr_txt_2: In TXT record between quotes

def rr_txt_2(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if(len(a) == 1):
		q = unpack("B",a)[0];
	else:
		print("Error: Unexpected char length in TXT RR " + a)
		return("error","")
	# If q is printable ASCII and q isn't ["#'|~\]
	if q >= 32 and q <= 125 and q != 34 and q != 35 \
	           and q != 39 and q != 124 and q != 92:
		op(o,a)	
	# 34: "
	elif q == 34: 
		op(o,"\'")
		return("rr_txt" + pstr,buffer)
	# 92: \
	elif q == 92:
		return("rr_txt_backslash" + pstr,buffer)
	else:
		op(o,"\'\\x%02x" % q)
		return("rr_txt_3" + pstr,buffer)
	return(state,buffer)

# rr_txt_3: Quoting non-printable characters in a TXT record
def rr_txt_3(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if(len(a) == 1):
		q = unpack("B",a)[0];
	else:
		print("Error: Unexpected char length in TXT RR " + a)
		return("error","")
	# If q is printable ASCII and q isn't ["#'|~]
	if q >= 32 and q <= 125 and q != 34 and q != 35 \
	           and q != 39 and q != 124 and q != 92:
		op(o,"\'" + a)	
		return("rr_txt_2" + pstr,buffer)
	# 34: "
	elif q == 34: 
		op(o," ")
		return("rr_txt" + pstr,buffer)
	# 92: \ (backslash) 
	elif q == 92:
		op(o,"\'")
		return("rr_txt_backslash" + pstr,buffer)
	else:
		op(o,"\\x%02x" % q)
		return("rr_txt_3" + pstr,buffer)
	return(state,buffer)

# rr_txt_4: Right after opening quote of TXT record

def rr_txt_4(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if(len(a) == 1):
		q = unpack("B",a)[0];
	else:
		print("Error: Unexpected char length in TXT RR " + a)
		return("error","")
	# If q is printable ASCII and q isn't ["#'|~\]
	if q >= 32 and q <= 125 and q != 34 and q != 35 \
	           and q != 39 and q != 124 and q != 92:
		op(o,"\'" + a)	
		return("rr_txt_2" + pstr,buffer)
	# 34: " (empty TXT field)
	elif q == 34: 
		op(o,"\'\'")
		return("rr_txt" + pstr,buffer)
	# 92: \ (backslash) 
	elif q == 92:
		op(o,"\'")
		return("rr_txt_backslash" + pstr,buffer)
	else:
		op(o,"\\x%02x" % q)
		return("rr_txt_3" + pstr,buffer)
	print("Unexpected error in rr_txt_4")
	return("error","")

# rr_txt_backslash: Right after a backslash in a TXT record
def rr_txt_backslash(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_number.match(a):
		# Use the buffer to store the numeric value
		return("rr_txt_numeric" + pstr,a)
	# Otherwise
	if(len(a) == 1):
		q = unpack("B",a)[0];
	else:
		print("Error: Unexpected char length in TXT RR " + a)
		return("error","")
	# If q is printable ASCII and q isn't [#'|~]
	if q >= 32 and q <= 125 and q != 35 \
	           and q != 39 and q != 124:
		op(o,a)	
	else:
		op(o,"\'\\x%02x" % q)
		return("rr_txt_3" + pstr,buffer)
	return("rr_txt_2" + pstr,buffer)

# rr_txt_numeric: In the middle of a backslashed number
def rr_txt_numeric(a,o,state,buffer):
	pstr = ""
	if is_pstate.search(state):
		pstr = "_paren"
	if is_number.match(a):
		# Use the buffer to store the numeric value
		return("rr_txt_numeric",buffer + a)
	x = buffer 
	if int(x) < 256:
		op(o,"\'\\x%02x\'" % int(x))
	else:
		print("Error: Value of backslashed number " + x + " too high")
		return("error","")
	# Otherwise
	if(len(a) == 1):
		q = unpack("B",a)[0];
	else:
		print("Error: Unexpected char length in TXT RR " + a)
		return("error","")
	# If q is printable ASCII and q isn't [#'|~\]
	if q >= 32 and q <= 125 and q != 35 and q != 34 \
	           and q != 39 and q != 124 and q != 92:
		op(o,a)	
	# 34: "
	elif q == 34: 
		op(o,"\'")
		return("rr_txt" + pstr,buffer)
	else:
		op(o,"\'\\x%02x" % q)
		return("rr_txt_3" + pstr,buffer)
	return("rr_txt_2" + pstr,buffer)

# In rrtype (The "A", "MX", "NS", etc. label)
def rrtype(a,o,state,buffer):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return (state, buffer)
	if is_dlabel.match(a):
		return (state,buffer + a)
	if is_white.match(a):
		# In the case of "IN", we go back to the state of being
                # after the dlabel, where we look for either a TTL or a 
                # RRTYPE
		if buffer.lower() != "in":
			op(o,buffer)
		else:
			op(o,a)
			return("pdlabel" + pstr, "")
		op(o,a)
		return ("prrtype" + pstr, buffer)
	print("Invalid character in RR type " + a)
	return ("error","")

# After dlabel (name of record we have info for) and before TTL/rrtype
def pdlabel(a,o,state,buffer):
	(state, buffer, paren, pstr) = handle_paren(a,o,state,buffer)
	if paren == 1:
		return (state, buffer)
	if is_white.match(a):
		op(o,a)
		return (state, buffer)
	if is_number.match(a):
		op(o,"+" + a)
		o.hasttl_set()
		o.normal_ttl_set(a)
		return ("ttl" + pstr,buffer)
	if is_letter.match(a):
		return ("rrtype" + pstr,a)	
	print("Unexpected character after dlabel " + a)
	return("error","")

# In a "." character in the dlabel (name of machine we have record for)	
def dlabel_dot(a,o,state,buffer):
	if is_dlabel.match(a):
		o.label_add(a)
		return ("dlabel",buffer)
	elif is_space.match(a):
		op(o,a)
		return ("pdlabel",buffer)
	else:
		print("Unexpected chatacter near dlabel " + a)
		return ("error","")

# In DCommand (a "slash" command like "/ttl" or "/origin")
def dcommand(a,o,state,buffer):
	if is_letter.match(a):
		o.label_add(a.lower())
		return ("dcommand",buffer)
	elif is_space.match(a):
		op(o,a)
		return (o.dcommand_process(),buffer)
	else:
		print("Unexpected chatacter in slash command " + a)
		return ("error","")

# In the dlabel (name of machine we are looking at record for)
def dlabel_s(a,o,state,buffer):
	if is_dlabel.match(a):
		o.label_add(a)
		return (state,buffer)
	elif is_space.match(a):
		o.label_add(".%" + a)
		return ("pdlabel",buffer)
	elif a == ".":
		#op(o,a)
		o.label_add(a)
		return ("dlabel_dot",buffer)
	else:
		print("Unexpected character in dlabel " + a)
		return ("error","")	

# Initial state at beginning of file				
def init_s(a,o,state,buffer):
	if is_white.match(a):
		op(o,a)
		return (state,buffer)
	elif is_newrec.match(a):
		if a == "$":
			o.label_reset()
			o.label_add("/")
			return ("dcommand",buffer)
		else:
			o.label_add(a)
			return("dlabel",buffer)
	else:
		print("Error: Unexpected character at new file start " + a)
		return("error","")
		
# END machine states

# This routine goes to the appropriate routine for the state we are in
def process_char(a,o,state,buffer):
	# print "Char is " + a + " state is " + state # DEBUG

        # Special states (beginning of file, etc)
	if state == "init_state":
		(state, buffer) = init_s(a,o,state,buffer)
	elif state == "dlabel":
		(state,buffer) = dlabel_s(a,o,state,buffer)
	elif state == "dlabel_dot":
		(state,buffer) = dlabel_dot(a,o,state,buffer)
	elif state == "dcommand":
		(state,buffer) = dcommand(a,o,state,buffer)
	elif state == "prettl":
		(state,buffer) = prettl(a,o,state,buffer)
	elif state == "sttl":
		(state,buffer) = sttl(a,o,state,buffer)
	elif state == "preorigin":
		(state,buffer) = preorigin(a,o,state,buffer)
	elif state == "origin":
		(state,buffer) = origin(a,o,state,buffer)
	elif state == "pdlabel" or state == "pdlabel_paren":
		(state,buffer) = pdlabel(a,o,state,buffer)
	elif state == "ttl" or state == "ttl_paren":
		(state,buffer) = ttl(a,o,state,buffer)
	elif state == "rrtype" or state == "rrtype_paren":
		(state,buffer) = rrtype(a,o,state,buffer)
	elif state == "prrtype" or state == "prrtype_paren":
		(state,buffer) = prrtype(a,o,state,buffer)
	elif state == "possible_lo":
		(state,buffer) = possible_lo(a,o,state,buffer)
	# We put an if instead of an elif here because prrtype, when it 
        # hits the first character of the rr, "falls through" since 
        # what we do with the character we see depends on the rrtype
	
	# The A RR
	if state == "rr_a" or state == "rr_a_paren":
		(state,buffer) = rr_a(a,o,state,buffer)

	# Generic handlers for 1 dlabel RRs: NS, CNAME, PTR, and the
	# obscure MB, MD, MF, MG, and MR records
	elif state == "rr_1dlabel" or state == "rr_1dlabel_paren":
		(state,buffer) = rr_1dlabel(a,o,state,buffer)
	elif state == "rr_1dlabel_dot" or state == "rr_1dlabel_dot_paren":
		(state,buffer) = rr_1dlabel_dot(a,o,state,buffer)

	# The SOA RRs
	elif state == "rr_soa" or state == "rr_soa_paren":
		(state,buffer) = rr_soa(a,o,state,buffer)
	elif state == "rr_soa_dot" or state == "rr_soa_dot_paren":
		(state,buffer) = rr_soa_dot(a,o,state,buffer)
	elif state == "rr_soa_2_pre" or state == "rr_soa_2_pre_paren":
		(state,buffer) = rr_soa_2_pre(a,o,state,buffer)
	elif state == "rr_soa_2" or state == "rr_soa_2_paren":
		(state,buffer) = rr_soa_2(a,o,state,buffer)
	elif state == "rr_soa_2_dot" or state == "rr_soa_2_dot_paren":
		(state,buffer) = rr_soa_2_dot(a,o,state,buffer)
	elif state == "rr_soa_3_pre" or state == "rr_soa_3_pre_paren":
		(state,buffer) = rr_soa_3_pre(a,o,state,buffer)
	elif state == "rr_soa_3" or state == "rr_soa_3_paren":
		(state,buffer) = rr_soa_3(a,o,state,buffer)
	elif state == "rr_soa_4_pre" or state == "rr_soa_4_pre_paren":
		(state,buffer) = rr_soa_4_pre(a,o,state,buffer)
	elif state == "rr_soa_4" or state == "rr_soa_4_paren":
		(state,buffer) = rr_soa_4(a,o,state,buffer)
	elif state == "rr_soa_5_pre" or state == "rr_soa_5_pre_paren":
		(state,buffer) = rr_soa_5_pre(a,o,state,buffer)
	elif state == "rr_soa_5" or state == "rr_soa_5_paren":
		(state,buffer) = rr_soa_5(a,o,state,buffer)
	elif state == "rr_soa_6_pre" or state == "rr_soa_6_pre_paren":
		(state,buffer) = rr_soa_6_pre(a,o,state,buffer)
	elif state == "rr_soa_6" or state == "rr_soa_6_paren":
		(state,buffer) = rr_soa_6(a,o,state,buffer)
	elif state == "rr_soa_7" or state == "rr_soa_7_paren":
		(state,buffer) = rr_soa_7(a,o,state,buffer)
	elif state == "rr_soa_7_pre" or state == "rr_soa_7_pre_paren":
		(state,buffer) = rr_soa_7_pre(a,o,state,buffer)

	# The MX RR (and also the obscure AFSDB and RT RRs)
	elif state == "rr_mx" or state == "rr_mx_paren":
		(state,buffer) = rr_mx(a,o,state,buffer)
	elif state == "rr_mx_2_pre" or state == "rr_mx_2_pre_paren":
		(state,buffer) = rr_mx_2_pre(a,o,state,buffer)
	elif state == "rr_mx_2" or state == "rr_mx_2_paren":
		(state,buffer) = rr_mx_2(a,o,state,buffer)
	elif state == "rr_mx_2_dot" or state == "rr_mx_2_dot_paren":
		(state,buffer) = rr_mx_2_dot(a,o,state,buffer)
	# We have a rr_mx_pre handler because the SRV record
        # uses the MX handler for the last two fields
	elif state == "rr_mx_pre" or state == "rr_mx_pre_paren":
		(state,buffer) = rr_mx(a,o,state,buffer)

	# The SRV RR
	elif state == "rr_srv" or state == "rr_srv_paren":
		(state,buffer) = rr_srv(a,o,state,buffer)
	elif state == "rr_srv_2" or state == "rr_srv_2_paren":
		(state,buffer) = rr_srv_2(a,o,state,buffer)
	elif state == "rr_srv_2_pre" or state == "rr_srv_2_pre_paren":
		(state,buffer) = rr_srv_2_pre(a,o,state,buffer)

	# The AAAA RR
	elif state == "rr_aaaa" or state == "rr_aaaa_paren":
		(state,buffer) = rr_aaaa(a,o,state,buffer)

	# The TXT RR
	elif state == "rr_txt" or state == "rr_txt_paren":
		(state,buffer) = rr_txt(a,o,state,buffer)
	elif state == "rr_txt_2" or state == "rr_txt_2_paren":
		(state,buffer) = rr_txt_2(a,o,state,buffer)
	elif state == "rr_txt_3" or state == "rr_txt_3_paren":
		(state,buffer) = rr_txt_3(a,o,state,buffer)
	elif state == "rr_txt_4" or state == "rr_txt_4_paren":
		(state,buffer) = rr_txt_4(a,o,state,buffer)
	elif state == "rr_txt_backslash" or state == "rr_txt_backslash_paren":
		(state,buffer) = rr_txt_backslash(a,o,state,buffer)
	elif state == "rr_txt_numeric" or state == "rr_txt_numeric_paren":
		(state,buffer) = rr_txt_numeric(a,o,state,buffer)

	# Generic handler for RR with 2 dlabels (MINFO, etc)
	elif state == "rr_2dlabels" or state == "rr_2dlabels_paren":
		(state,buffer) = rr_2dlabels(a,o,state,buffer)
	elif state == "rr_2dlabels_dot" or state == "rr_2dlabels_dot_paren":
		(state,buffer) = rr_2dlabels_dot(a,o,state,buffer)

        # Obscure RRs that take a variable number of arguments
	elif state == "rr_variargs":
		(state,buffer) = rr_variargs(a,o,state,buffer)

        # Before or after the RR
	if state == "postrr" or state == "postrr_paren":
		(state,buffer) = postrr(a,o,state,buffer)
	elif state == "pre_rr":
		(state,buffer) = pre_rr(a,o,state,buffer)

	return (state, buffer)

# Process a given file; this allows this program to process multiple
# BIND zone files at the same time
def process_file(filename):
	if os.access(filename,os.R_OK) != 1:
		print("Can not read " + filename)
		return ERROR
	
	outf = filename + ".csv2"

	if os.path.isfile(outf) == 1:
		print("Warning: " + outf + " already exists, overwriting")

	if os.path.isdir(filename) == 1:
		print("Error: " + outf + " is a directory, skipping")
		return ERROR

	i = open(filename,"rb")
	o = open(outf,"w")
	oz = buf()

	state = "init_state"
	buffer = ""

	x = 0
	linenum = 1
	while x < 100000:
		a = i.read(1).decode('utf-8')
		if a == "":
			break
		if a == ";":
			a = process_comment(i,oz)
		if a == "\n":
			linenum += 1
		(state, buffer) = process_char(a,oz,state,buffer)
		if state == "error" or state == "error_paren":
			print("Error found, no longer processing this file")
			print("Error is on line " + str(linenum) +
			      " of file " + filename)
			return ERROR
		x += 1


	o.write(
"""# This MaraDNS CSV2 zone file was converted from a BIND zone file by
# the bind2csv2.py tool

""")
	oz.flush_rr() # Make sure to flush out the last RR we read
	o.write(oz.read())

	print(outf + " written")


# MAIN

if len(sys.argv) < 3 or sys.argv[1] != "-c":
	print("Usage: bind2csv2.py -c {file list}")
	print("Where {file list} is a list of files you want to make " +
              "csv2 zone files of.")
	sys.exit()

list = sys.argv[2:]
for item in list:
	print("Processing zone file " + item)
	process_file(item)