github.com/flammit/u-root@v0.0.0-20171208172043-8997438e8634/cmds/ed/command.go (about)

     1  // Copyright 2012-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Options:
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  	"strconv"
    13  	"strings"
    14  )
    15  
    16  func Command(f Editor, c string, startLine, endLine int) error {
    17  	var err error
    18  	if len(c) == 0 {
    19  		_, err = f.Write(os.Stdout, f.Dot(), f.Dot())
    20  		f.Move(endLine + 1)
    21  		return err
    22  	}
    23  
    24  	a := c[1:]
    25  	debug("Process %c, args %v", c[0], a)
    26  	switch c[0] {
    27  	case 'q', 'e':
    28  		if f.IsDirty() {
    29  			f.Dirty(false)
    30  			return fmt.Errorf("f was dirty, no longer is, try again")
    31  		}
    32  	}
    33  	switch c[0] {
    34  	case 'd':
    35  		f.Replace([]byte{}, startLine, endLine)
    36  	case 'q':
    37  		os.Exit(1)
    38  	case 'e':
    39  		startLine, endLine = f.Range()
    40  		startLine = 0
    41  		fallthrough
    42  	case 'r':
    43  		fname := strings.TrimLeft(a, " \t")
    44  		debug("read %v @ %v, %v", f, startLine, endLine)
    45  		var r io.Reader
    46  		r, err = os.Open(fname)
    47  		debug("%v: r is %v, err %v", fname, r, err)
    48  		if err == nil {
    49  			_, err = f.Read(r, startLine, endLine)
    50  		}
    51  	case 's':
    52  		o := strings.SplitN(a[1:], a[0:1], 3)
    53  		if o[1] == "" {
    54  			o[1] = "" //f.pat
    55  		}
    56  		debug("after split o is %v", o)
    57  		err = f.Sub(o[0], o[1], o[2], startLine, endLine)
    58  	case 'w':
    59  		fname := strings.TrimLeft(a, " \t")
    60  		debug("NOT WRITINGT TO %v", fname)
    61  		_, err = f.Write(os.Stdout, startLine, endLine)
    62  	case 'p':
    63  		fmt.Printf("what the shit")
    64  		_, err = f.Print(os.Stdout, startLine, endLine)
    65  		fmt.Printf("shiw %v", err)
    66  	default:
    67  		err = fmt.Errorf("%c: unknown command", c[0])
    68  	}
    69  	return err
    70  }
    71  
    72  func DoCommand(f Editor, l string) error {
    73  	var err error
    74  	var startLine, endLine int
    75  	startLine = f.Dot()
    76  	if len(l) == 0 {
    77  		_, err = f.Write(os.Stdout, f.Dot(), f.Dot())
    78  		f.Move(f.Dot() + 1)
    79  		return err
    80  	}
    81  	switch {
    82  	case l[0] == '.':
    83  		debug(".\n")
    84  		startLine = f.Dot()
    85  		l = l[1:]
    86  	case l[0] == '$':
    87  		debug("$\n")
    88  		_, endLine = f.Range()
    89  		f.Move(endLine)
    90  		startLine = f.Dot()
    91  		l = l[1:]
    92  	case startsearch.FindString(l) != "":
    93  		pat := startsearch.FindString(l)
    94  		l = l[len(pat):]
    95  		debug("/\n")
    96  		fail("Pattern search: not yet")
    97  	case num.FindString(l) != "":
    98  		debug("num\n")
    99  		n := num.FindString(l)
   100  		if startLine, err = strconv.Atoi(n); err != nil {
   101  			return err
   102  		}
   103  		f.Move(startLine)
   104  		l = l[len(n):]
   105  	}
   106  	debug("cmd before endsearch is %v", l)
   107  	endLine = f.Dot()
   108  	if len(l) > 0 && l[0] == ',' {
   109  		l = l[1:]
   110  		if len(l) < 1 {
   111  			return fmt.Errorf("line ended at a ,?")
   112  		}
   113  		switch {
   114  		case l[0] == '.':
   115  			debug(".\n")
   116  			endLine = f.Dot()
   117  			l = l[1:]
   118  		case l[0] == '$':
   119  			debug("$\n")
   120  			_, endLine = f.Range()
   121  			l = l[1:]
   122  		case startsearch.FindString(l) != "":
   123  			debug("/\n")
   124  			fail("Pattern search: not yet")
   125  		case num.FindString(l) != "":
   126  			debug("num\n")
   127  			n := num.FindString(l)
   128  			if endLine, err = strconv.Atoi(n); err != nil {
   129  				return err
   130  			}
   131  			l = l[len(n):]
   132  		}
   133  	}
   134  	endLine++
   135  	debug("l before call to f.Command() is %v", l)
   136  	err = Command(f, l, startLine, endLine)
   137  	debug("Comand is done: f is %v", f)
   138  	return err
   139  }