This file is indexed.

/usr/bin/crudini is in crudini 0.7-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf8
#
# Copyright (C) 2013-2015, Pádraig Brady <P@draigBrady.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GPLv2, the GNU General Public License version 2, as
# published by the Free Software Foundation. http://gnu.org/licenses/gpl.html

import atexit
from cStringIO import StringIO
import ConfigParser
import contextlib
import errno
import getopt
import hashlib
import iniparse
import os
import pipes
import shutil
import string
import sys
import tempfile


def error(message=None):
    if message:
        sys.stderr.write(message+'\n')


def delete_if_exists(path):
    """Delete a file, but ignore file not found error.
    """
    try:
        os.unlink(path)
    except EnvironmentError as e:
        if e.errno != errno.ENOENT:
            print str(e)
            raise


# XXX: should be done in iniparse.  Used to
# add support for ini files without a section
class AddDefaultSection():
    def __init__(self, fp):
        self.fp = fp
        self.first = True

    def readline(self):
        if self.first:
            self.first = False
            return '[%s]' % iniparse.DEFAULTSECT
        else:
            return self.fp.readline()


class FileLock(object):
    """Advisory file based locking.  This should be reasonably cross platform
       and also work over distributed file systems."""
    def __init__(self, exclusive=False):
        # In inplace mode, the process must be careful to not close this fp
        # until finished, nor open and close another fp associated with the
        # file.
        self.fp = None
        self.locked = False

        if os.name == 'nt':
            import msvcrt

            def lock(self):
                msvcrt.locking(self.fp, msvcrt.LK_LOCK, 1)
                self.locked = True

            def unlock(self):
                if self.locked:
                    msvcrt.locking(self.fp, msvcrt.LK_UNLCK, 1)
                self.locked = False

        else:
            import fcntl

            def lock(self):
                operation = fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH
                fcntl.lockf(self.fp, operation)
                self.locked = True

            def unlock(self):
                if self.locked:
                    fcntl.lockf(self.fp, fcntl.LOCK_UN)
                self.locked = False

        FileLock.lock = lock
        FileLock.unlock = unlock


class LockedFile(FileLock):
    """Open a file with advisory locking.  This provides the Isolation
       property of ACID, to avoid missing writes.  In addition this provides AC
       properties of ACID if crudini is the only logic accessing the ini file.
       This should work on most platforms and distributed file systems.

       Caveats in --inplace mode:
        - File must be writeable
        - File should be generally non readable to avoid read lock DoS.
       Caveats in replace mode:
        - Possibility of stale lock files left on crash leading to deadlock.
        - Less responsive when there is contention."""

    def __init__(self, filename, operation, inplace, create):

        self.fp_cmp = None
        self.filename = filename
        self.operation = operation

        FileLock.__init__(self, operation != "--get")

        atexit.register(self.delete)

        open_mode = os.O_RDONLY
        if operation != "--get":
            # We're only reading here, but we check now for write
            # permissions we'll need in --inplace case to avoid
            # redundant processing.
            # Also an exlusive lock needs write perms anyway.
            open_mode = os.O_RDWR

            if create and operation != '--del':
                open_mode += os.O_CREAT

        try:
            self.fp = os.fdopen(os.open(self.filename, open_mode, 0o666))
            if inplace:
                # In general readers (--get) are protected by file_replace(),
                # but using read lock here gives AC of the ACID properties
                # when only accessing the file through crudini even with
                # file_rewrite().
                self.lock()
            else:
                # The file may have been renamed since the open so recheck
                while True:
                    self.lock()
                    fpnew = os.fdopen(os.open(self.filename, open_mode, 0o666))
                    if os.path.sameopenfile(self.fp.fileno(), fpnew.fileno()):
                        # Note we don't fpnew.close() here as that would break
                        # any existing fcntl lock (fcntl.lockf is an fcntl lock
                        # despite the name).  We don't use flock() at present
                        # as that's less consistent across platforms and may
                        # be an fcntl lock on NFS anyway for example.
                        self.fp_cmp = fpnew
                        break
                    else:
                        self.fp.close()
                        self.fp = fpnew
        except EnvironmentError as e:
            # Treat --del on a non existing file as operating on NULL data
            # which will be deemed unchanged, and thus not re{written,created}
            # We don't exit early here so that --verbose is also handled.
            if create and operation == '--del' \
               and e.errno in (errno.ENOTDIR, errno.ENOENT):
                self.fp = StringIO('')
            else:
                error(str(e))
                sys.exit(1)

    def delete(self):
        # explicit close so closed in correct order if taking lock multiple
        # times, and also explicit "delete" needed to avoid implicit __del__
        # after os module is unloaded.
        self.unlock()
        if self.fp:
            self.fp.close()
        if self.fp_cmp:
            self.fp_cmp.close()


# Note we use RawConfigParser rather than SafeConfigParser
# to avoid unwanted variable interpolation.
# Note iniparse doesn't currently support allow_no_value=True.
class CrudiniConfigParser(iniparse.RawConfigParser):
    def __init__(self, preserve_case=False):
        iniparse.RawConfigParser.__init__(self)
        if preserve_case:
            self.optionxform = str


class Print():
    """Use for default output format."""

    def section_header(self, section):
        """Print section header.

        :param section: str
        """

        print section

    def name_value(self, name, value, section=None):
        """Print parameter.

        :param name: str
        :param value: str
        :param section: str (default 'None')
        """

        print name or value


class PrintIni(Print):
    """Use for ini output format."""

    def section_header(self, section):
        print "[%s]" % section

    def name_value(self, name, value, section=None):
        print name, '=', value.replace('\n', '\n ')


class PrintLines(Print):
    """Use for lines output format."""

    def name_value(self, name, value, section=None):
        # Both unambiguous and easily parseable by shell. Caveat is
        # that sections and values with spaces are awkward to split in shell
        if section:
            line = '[ %s ]' % section
            if name:
                line += ' '
        if name:
            line += '%s' % name
        if value:
            line += ' = %s' % value.replace('\n', '\\n')
        print line


class PrintSh(Print):
    """Use for shell output format."""

    @staticmethod
    def _valid_sh_identifier(
        i,
        safe_chars=frozenset(string.ascii_letters + string.digits + '_')
    ):
        """Provide validation of the output identifiers as it's dangerous to
        leave validation to shell. Consider for example doing eval on this in
        shell: rm -Rf /;oops=val

        :param i: str
        :param sh_safe_id_chars: frozenset
        :return: bool
        """

        if i[0] in string.digits:
            return False
        for c in i:
            if c not in safe_chars:
                return False
        return True

    def name_value(self, name, value, section=None):
        if not PrintSh._valid_sh_identifier(name):
            error('Invalid sh identifier: %s' % name)
            sys.exit(1)
        sys.stdout.write("%s=%s\n" % (name, pipes.quote(value)))


class Crudini():
    mode = fmt = update = inplace = cfgfile = output = section = param = \
        value = vlist = listsep = verbose = None

    locked_file = None
    section_explicit_default = False
    data = None
    conf = None
    added_default_section = False
    _print = None

    # The following exits cleanly on Ctrl-C,
    # while treating other exceptions as before.
    @staticmethod
    def cli_exception(type, value, tb):
        if not issubclass(type, KeyboardInterrupt):
            sys.__excepthook__(type, value, tb)

    @staticmethod
    @contextlib.contextmanager
    def remove_file_on_error(path):
        """Protect code that wants to operate on PATH atomically.
        Any exception will cause PATH to be removed.
        """
        try:
            yield
        except Exception:
            t, v, tb = sys.exc_info()
            delete_if_exists(path)
            raise t, v, tb

    @staticmethod
    def file_replace(name, data):
        """Replace file as atomically as possible,
        fulfilling and AC properties of ACID.
        This is essentially using method 9 from:
        http://www.pixelbeat.org/docs/unix_file_replacement.html

        Caveats:
         - Changes ownership of the file being edited
           by non root users (due to POSIX interface limitations).
         - Loses any extended attributes of the original file
           (due to the simplicity of this implementation).
         - Existing hardlinks will be separated from the
           newly replaced file.
         - Ignores the write permissions of the original file.
         - Requires write permission on the directory as well as the file.
         - With python2 on windows we don't fulfil the A ACID property.

        To avoid the above caveats see the --inplace option.
        """
        (f, tmp) = tempfile.mkstemp(".tmp", prefix=name+".", dir=".")

        with Crudini.remove_file_on_error(tmp):
            shutil.copystat(name, tmp)

            if hasattr(os, 'fchown') and os.geteuid() == 0:
                st = os.stat(name)
                os.fchown(f, st.st_uid, st.st_gid)

            os.write(f, data)
            os.fsync(f)  # See http://stackoverflow.com/q/7433057/4421
            os.close(f)

            if hasattr(os, 'replace'):  # >= python 3.3
                os.replace(tmp, name)  # atomic even on windos
            elif os.name == 'posix':
                os.rename(tmp, name)  # atomic on POSIX
            else:
                backup = tmp+'.backup'
                os.rename(name, backup)
                os.rename(tmp, name)
                delete_if_exists(backup)

            # Sync out the new directory entry also
            O_DIRECTORY = os.O_DIRECTORY if hasattr(os, 'O_DIRECTORY') else 0
            dirfd = os.open(os.path.dirname(name) or '.', O_DIRECTORY)
            os.fsync(dirfd)
            os.close(dirfd)

    @staticmethod
    def file_rewrite(name, data):
        """Rewrite file inplace avoiding the caveats
        noted in file_replace().

        Caveats:
         - Not Atomic as readers may see incomplete data for a while.
         - Not Consistent as multiple writers may overlap.
         - Less Durable as existing data truncated before I/O completes.
         - Requires write access to file rather than write access to dir.
        """
        with open(name, 'w') as f:
            f.write(data)
            f.flush()
            os.fsync(f.fileno())

    @staticmethod
    def init_iniparse_defaultsect():
        try:
            iniparse.DEFAULTSECT
        except AttributeError:
            iniparse.DEFAULTSECT = 'DEFAULT'

    # TODO item should be items and split also
    # especially in merge mode
    @staticmethod
    def update_list(curr_val, item, mode, sep):
        curr_items = []
        use_space = True
        if curr_val:
            if sep is None:
                use_space = ' ' in curr_val or ',' not in curr_val
                curr_items = [v.strip() for v in curr_val.split(",")]
            else:
                curr_items = curr_val.split(sep)

        if mode == "--set":
            if item not in curr_items:
                curr_items.append(item)
        elif mode == "--del":
            try:
                curr_items.remove(item)
            except ValueError:
                pass

        if sep is None:
            sep = ","
            if use_space:
                sep += " "

        return sep.join(curr_items)

    def usage(self, exitval=0):
        cmd = os.path.basename(sys.argv[0])
        output = sys.stderr if exitval else sys.stdout
        output.write("""\
A utility for manipulating ini files

Usage: %s --set [OPTION]...   config_file section   [param] [value]
  or:  %s --get [OPTION]...   config_file [section] [param]
  or:  %s --del [OPTION]...   config_file section   [param] [list value]
  or:  %s --merge [OPTION]... config_file [section]

Options:

  --existing[=WHAT]  For --set, --del and --merge, fail if item is missing,
                       where WHAT is 'file', 'section', or 'param', or if
                       not specified; all specified items.
  --format=FMT       For --get, select the output FMT.
                       Formats are sh,ini,lines
  --inplace          Lock and write files in place.
                       This is not atomic but has less restrictions
                       than the default replacement method.
  --list             For --set and --del, update a list (set) of values
  --list-sep=STR     Delimit list values with \"STR\" instead of \" ,\"
  --output=FILE      Write output to FILE instead. '-' means stdout
  --verbose          Indicate on stderr if changes were made
""" % (cmd, cmd, cmd, cmd)
        )
        sys.exit(exitval)

    def parse_options(self):

        # Handle optional arg to long option
        # The gettopt module should really support this
        for i, opt in enumerate(sys.argv):
            if opt == '--existing':
                sys.argv[i] = '--existing='
            elif opt == '--':
                break

        try:
            long_options = [
                'del',
                'existing=',
                'format=',
                'get',
                'help',
                'inplace',
                'list',
                'list-sep=',
                'merge',
                'output=',
                'set',
                'verbose',
                'version'
            ]
            opts, args = getopt.getopt(sys.argv[1:], '', long_options)
        except getopt.GetoptError, e:
            error(str(e))
            self.usage(1)

        for o, a in opts:
            if o in ('--help',):
                self.usage(0)
            elif o in ('--version',):
                print 'crudini 0.7'
                sys.exit(0)
            elif o in ('--verbose',):
                self.verbose = True
            elif o in ('--set', '--del', '--get', '--merge'):
                if self.mode:
                    error('One of --set|--del|--get|--merge can be specified')
                    self.usage(1)
                self.mode = o
            elif o in ('--format',):
                self.fmt = a
                if self.fmt not in ('sh', 'ini', 'lines'):
                    error('--format not recognized: %s' % self.fmt)
                    self.usage(1)
            elif o in ('--existing',):
                self.update = a or 'param'  # 'param' implies all must exist
                if self.update not in ('file', 'section', 'param'):
                    error('--existing item not recognized: %s' % self.update)
                    self.usage(1)
            elif o in ('--inplace',):
                self.inplace = True
            elif o in ('--list',):
                self.vlist = "set"  # TODO support combos of list, sorted, ...
            elif o in ('--list-sep',):
                self.listsep = a
            elif o in ('--output',):
                self.output = a

        if not self.mode:
            error('One of --set|--del|--get|--merge must be specified')
            self.usage(1)

        try:
            self.cfgfile = args[0]
            self.section = args[1]
            self.param = args[2]
            self.value = args[3]
        except IndexError:
            pass

        if not self.output:
            self.output = self.cfgfile

        if self.cfgfile is None:
            self.usage(1)
        if self.section is None and self.mode in ('--del', '--set'):
            self.usage(1)
        if self.param is not None and self.mode in ('--merge',):
            self.usage(1)
        if self.value is not None and self.mode not in ('--set',):
            if not (self.mode == '--del' and self.vlist):
                error('A value should not be specified with %s' % self.mode)
                self.usage(1)

        if self.mode == '--merge' and self.fmt == 'sh':
            # I'm not sure how useful is is to support this.
            # printenv will already generate a mostly compat ini format.
            # If you want to also include non exported vars (from `set`),
            # then there is a format change.
            error('sh format input is not supported at present')
            sys.exit(1)

        # Protect against generating non parseable ini files
        if self.section and ('[' in self.section or ']' in self.section):
            error("section names should not contain '[' or ']': %s" %
                  self.section)
            sys.exit(1)
        if self.param and self.param.startswith('['):
            error("param names should not start with '[': %s" % self.param)
            sys.exit(1)

        if self.fmt == 'lines':
            self._print = PrintLines()
        elif self.fmt == 'sh':
            self._print = PrintSh()
        elif self.fmt == 'ini':
            self._print = PrintIni()
        else:
            self._print = Print()

    def _has_default_section(self):
        fp = StringIO(self.data)
        for line in fp:
            if line.startswith('[%s]' % iniparse.DEFAULTSECT):
                return True
        return False

    def _chksum(self, data):
        h = hashlib.md5()
        h.update(data)
        return h.digest()

    def _parse_file(self, filename, add_default=False, preserve_case=False):
        try:
            if self.data is None:
                # Read all data up front as this is done by iniparse anyway
                # Doing it here will avoid rereads on reparse and support
                # correct parsing of stdin
                if filename == '-':
                    self.data = sys.stdin.read()
                else:
                    self.data = self.locked_file.fp.read()
                if self.mode != '--get':
                    # compare checksums to flag any changes
                    # (even spacing or case adjustments) with --verbose,
                    # and to avoid rewriting the file if not necessary
                    self.chksum = self._chksum(self.data)

                if self.data.startswith('\n'):
                    self.newline_at_start = True
                else:
                    self.newline_at_start = False

            fp = StringIO(self.data)
            if add_default:
                fp = AddDefaultSection(fp)

            conf = CrudiniConfigParser(preserve_case=preserve_case)
            conf.readfp(fp)
            return conf
        except EnvironmentError as e:
            error(str(e))
            sys.exit(1)

    def parse_file(self, filename, preserve_case=False):
        self.added_default_section = False
        self.data = None

        if filename != '-':
            self.locked_file = LockedFile(filename, self.mode, self.inplace,
                                          not self.update)

        try:
            conf = self._parse_file(filename, preserve_case=preserve_case)

            if not conf.items(iniparse.DEFAULTSECT):
                # Check if there is just [DEFAULT] in a file with no
                # name=values to avoid adding a duplicate section.
                if not self._has_default_section():
                    # reparse with inserted [DEFAULT] to be able to add global
                    # opts etc.
                    conf = self._parse_file(
                        filename,
                        add_default=True,
                        preserve_case=preserve_case
                    )
                    self.added_default_section = True

        except ConfigParser.MissingSectionHeaderError:
            conf = self._parse_file(
                filename,
                add_default=True,
                preserve_case=preserve_case
            )
            self.added_default_section = True
        except ConfigParser.ParsingError as e:
            error(str(e))
            sys.exit(1)

        self.data = None
        return conf

    def set_name_value(self, section, param, value):
        curr_val = None

        if self.update in ('param', 'section'):
            if param is None:
                if not (
                    section == iniparse.DEFAULTSECT or
                    self.conf.has_section(section)
                ):
                    raise ConfigParser.NoSectionError(section)
            else:
                try:
                    curr_val = self.conf.get(section, param)
                except ConfigParser.NoSectionError:
                    if self.update == 'section':
                        raise
                except ConfigParser.NoOptionError:
                    if self.update == 'param':
                        raise
        elif (section != iniparse.DEFAULTSECT and
                not self.conf.has_section(section)):
            if self.mode == "--del":
                return
            else:
                # Note this always adds a '\n' before the section name
                # resulting in double spaced sections or blank line at
                # the start of a new file to which a new section is added.
                # We handle the empty file case at least when writing.
                self.conf.add_section(section)

        if param is not None:
            if self.update not in ('param', 'section'):
                try:
                    curr_val = self.conf.get(section, param)
                except ConfigParser.NoOptionError:
                    if self.mode == "--del":
                        return
            if value is None:
                value = ''
            if self.vlist:
                value = self.update_list(
                    curr_val,
                    value,
                    self.mode,
                    self.listsep
                )
            self.conf.set(section, param, value)

    def command_set(self):
        """Insert a section/parameter."""

        self.set_name_value(self.section, self.param, self.value)

    def command_merge(self):
        """Merge an ini file from another ini."""

        for msection in [iniparse.DEFAULTSECT] + self.mconf.sections():
            if msection == iniparse.DEFAULTSECT:
                defaults_to_strip = {}
            else:
                defaults_to_strip = self.mconf.defaults()
            items = self.mconf.items(msection)
            set_param = False
            for item in items:
                # XXX: Note this doesn't update an item in section
                # if matching value also in default (global) section.
                if defaults_to_strip.get(item[0]) != item[1]:
                    ignore_errs = (ConfigParser.NoOptionError,)
                    if self.section is not None:
                        msection = self.section
                    elif self.update not in ('param', 'section'):
                        ignore_errs += (ConfigParser.NoSectionError,)
                    try:
                        set_param = True
                        self.set_name_value(msection, item[0], item[1])
                    except ignore_errs:
                        pass
            # For empty sections ensure the section header is added
            if not set_param and self.section is None:
                self.set_name_value(msection, None, None)

    def command_del(self):
        """Delete a section/parameter."""

        if self.param is None:
            if self.section == iniparse.DEFAULTSECT:
                for name in self.conf.defaults():
                    self.conf.remove_option(iniparse.DEFAULTSECT, name)
            else:
                if not self.conf.remove_section(self.section) \
                   and self.update in ('param', 'section'):
                    raise ConfigParser.NoSectionError(self.section)
        elif self.value is None:
            try:
                if not self.conf.remove_option(self.section, self.param) \
                   and self.update == 'param':
                    raise ConfigParser.NoOptionError(self.section, self.param)
            except ConfigParser.NoSectionError:
                if self.update in ('param', 'section'):
                    raise
        else:  # remove item from list
            self.set_name_value(self.section, self.param, self.value)

    def command_get(self):
        """Output a section/parameter"""

        if self.fmt != 'lines':
            if self.section is None:
                if self.conf.defaults():
                    self._print.section_header(iniparse.DEFAULTSECT)
                for item in self.conf.sections():
                    self._print.section_header(item)
            elif self.param is None:
                if self.fmt == 'ini':
                    self._print.section_header(self.section)
                if self.section == iniparse.DEFAULTSECT:
                    defaults_to_strip = {}
                else:
                    defaults_to_strip = self.conf.defaults()
                for item in self.conf.items(self.section):
                    # XXX: Note this strips an item from section
                    # if matching value also in default (global) section.
                    if defaults_to_strip.get(item[0]) != item[1]:
                        if self.fmt:
                            val = item[1]
                        else:
                            val = None
                        self._print.name_value(item[0], val)
            else:
                val = self.conf.get(self.section, self.param)
                if self.fmt:
                    name = self.param
                else:
                    name = None
                self._print.name_value(name, val)
        else:
            if self.section is None:
                sections = self.conf.sections()
                if self.conf.defaults():
                    sections.insert(0, iniparse.DEFAULTSECT)
            else:
                sections = (self.section,)
            if self.param is not None:
                val = self.conf.get(self.section, self.param)
                self._print.name_value(self.param, val, self.section)
            else:
                for section in sections:
                    if section == iniparse.DEFAULTSECT:
                        defaults_to_strip = {}
                    else:
                        defaults_to_strip = self.conf.defaults()
                    items = False
                    for item in self.conf.items(section):
                        # XXX: Note this strips an item from section
                        # if matching value also in default (global) section.
                        if defaults_to_strip.get(item[0]) != item[1]:
                            val = item[1]
                            self._print.name_value(item[0], val, section)
                            items = True
                    if not items:
                        self._print.name_value(None, None, section)

    def run(self):
        if sys.stdin.isatty():
            sys.excepthook = Crudini.cli_exception

        Crudini.init_iniparse_defaultsect()
        self.parse_options()

        self.section_explicit_default = False
        if self.section == '':
            self.section = iniparse.DEFAULTSECT
        elif self.section == iniparse.DEFAULTSECT:
            self.section_explicit_default = True

        if self.mode == '--merge':
            self.mconf = self.parse_file('-', preserve_case=True)

        self.madded_default_section = self.added_default_section

        try:
            self.conf = self.parse_file(self.cfgfile)
        except ConfigParser.ParsingError as e:
            error('Error parsing %s: %s' % (self.cfgfile, e.message))
            sys.exit(1)

        # Take the [DEFAULT] header from the input if present
        if (
            self.mode == '--merge' and
            self.update not in ('param', 'section') and
            not self.madded_default_section and
            self.mconf.items(iniparse.DEFAULTSECT)
        ):
            self.added_default_section = self.madded_default_section

        try:
            if self.mode == '--set':
                self.command_set()
            elif self.mode == '--merge':
                self.command_merge()
            elif self.mode == '--del':
                self.command_del()
            elif self.mode == '--get':
                self.command_get()
        except ConfigParser.NoSectionError as e:
            error('Section not found: %s' % e.section)
            sys.exit(1)
        except ConfigParser.NoOptionError:
            error('Parameter not found: %s' % self.param)
            sys.exit(1)

        if self.mode != '--get':
            # XXX: Ideally we should just do conf.write(f) here,
            # but to avoid iniparse issues, we massage the data a little here
            str_data = str(self.conf.data)
            if len(str_data) and str_data[-1] != '\n':
                str_data += '\n'

            if (
                (
                    self.added_default_section and
                    not (
                        self.section_explicit_default and
                        self.mode in ('--set', '--merge')
                    )
                ) or
                (
                    self.mode == '--del' and
                    self.section == iniparse.DEFAULTSECT and
                    self.param is None
                )
            ):
                # See note at add_section() call above detailing
                # where this extra \n comes from that we handle
                # here for the edge case of new files.
                default_sect = '[%s]\n' % iniparse.DEFAULTSECT
                if not self.newline_at_start and \
                   str_data.startswith(default_sect + '\n'):
                    str_data = str_data[len(default_sect) + 1:]
                else:
                    str_data = str_data.replace(default_sect, '', 1)

            try:
                changed = self.chksum != self._chksum(str_data)

                if self.output == '-':
                    sys.stdout.write(str_data)
                elif changed:
                    if self.inplace:
                        self.file_rewrite(self.output, str_data)
                    else:
                        self.file_replace(self.output, str_data)

                if self.verbose:
                    def quote_val(val):
                        return pipes.quote(val).replace('\n', '\\n')
                    what = ' '.join(map(quote_val,
                                        filter(bool,
                                               [self.mode, self.cfgfile,
                                                self.section, self.param,
                                                self.value])))
                    sys.stderr.write('%s: %s\n' %
                                     (('unchanged', 'changed')[changed], what))
            except EnvironmentError as e:
                error(str(e))
                sys.exit(1)


def main():
    crudini = Crudini()
    return crudini.run()

if __name__ == "__main__":
    sys.exit(main())