#!/usr/bin/env python
# Copyright (c) 2020 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

from __future__ import absolute_import, division, print_function
import sys

def countLeadingSpaces( s ):
   return len( s ) - len( s.lstrip( ' ' ) )

try:
   modeCmds = [ "" ]
   indentSpaces = [ 0 ]

   for line in sys.stdin:
      line = line.rstrip()
      lineIndentSpaces = countLeadingSpaces( line )

      if lineIndentSpaces == indentSpaces[ -1 ]:
         line = line.strip()

         # Handle comments specially. We don't want a comment to become
         # a mode prefix.
         if line and not line.startswith( "!" ):
            modeCmds[ -1 ] = line
            print( " ".join( modeCmds ) )
         elif lineIndentSpaces != 0:
            # Prefix indented comments
            print( " ".join( modeCmds ), line )
         else:
            # Don't prefix column 0 comments
            print( line )
         # indentSpaces[] doesn't need to be updated, because it didn't change.

      elif lineIndentSpaces > indentSpaces[ -1 ]:
         line = line.strip()
         # Handle comments specially. We don't want a comment to become
         # a mode prefix.
         if line:
            if not line.startswith( "!" ):
               modeCmds.append( line )
               print( " ".join( modeCmds ) )
               indentSpaces.append( lineIndentSpaces )
            else:
               # Prefix indented comments
               print( " ".join( modeCmds ), line )
         else:
            # It was just a blank line
            print( line )
      elif lineIndentSpaces < indentSpaces[ -1 ]:
         while indentSpaces[ -1 ] > lineIndentSpaces:
            indentSpaces.pop()
            modeCmds.pop()
         assert indentSpaces[ -1 ] == lineIndentSpaces
         modeCmds[ -1 ] = line.strip()
         if len( modeCmds ) == 1:
            print( line )
         else:
            print( " ".join( modeCmds ) )

except KeyboardInterrupt:
   sys.exit( 1 )
except IOError:
   sys.exit( 1 )


