summaryrefslogtreecommitdiffstats
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __YENTA_H
#define __YENTA_H

#include <asm/io.h>

#define CB_SOCKET_EVENT		0x00
#define    CB_CSTSEVENT		0x00000001	/* Card status event */
#define    CB_CD1EVENT		0x00000002	/* Card detect 1 change event */
#define    CB_CD2EVENT		0x00000004	/* Card detect 2 change event */
#define    CB_PWREVENT		0x00000008	/* PWRCYCLE change event */

#define CB_SOCKET_MASK		0x04
#define    CB_CSTSMASK		0x00000001	/* Card status mask */
#define    CB_CDMASK		0x00000006	/* Card detect 1&2 mask */
#define    CB_PWRMASK		0x00000008	/* PWRCYCLE change mask */

#define CB_SOCKET_STATE		0x08
#define    CB_CARDSTS		0x00000001	/* CSTSCHG status */
#define    CB_CDETECT1		0x00000002	/* Card detect status 1 */
#define    CB_CDETECT2		0x00000004	/* Card detect status 2 */
#define    CB_PWRCYCLE		0x00000008	/* Socket powered */
#define    CB_16BITCARD		0x00000010	/* 16-bit card detected */
#define    CB_CBCARD		0x00000020	/* CardBus card detected */
#define    CB_IREQCINT		0x00000040	/* READY(xIRQ)/xCINT high */
#define    CB_NOTACARD		0x00000080	/* Unrecognizable PC card detected */
#define    CB_DATALOST		0x00000100	/* Potential data loss due to card removal */
#define    CB_BADVCCREQ		0x00000200	/* Invalid Vcc request by host software */
#define    CB_5VCARD		0x00000400	/* Card Vcc at 5.0 volts? */
#define    CB_3VCARD		0x00000800	/* Card Vcc at 3.3 volts? */
#define    CB_XVCARD		0x00001000	/* Card Vcc at X.X volts? */
#define    CB_YVCARD		0x00002000	/* Card Vcc at Y.Y volts? */
#define    CB_5VSOCKET		0x10000000	/* Socket Vcc at 5.0 volts? */
#define    CB_3VSOCKET		0x20000000	/* Socket Vcc at 3.3 volts? */
#define    CB_XVSOCKET		0x40000000	/* Socket Vcc at X.X volts? */
#define    CB_YVSOCKET		0x80000000	/* Socket Vcc at Y.Y volts? */

#define CB_SOCKET_FORCE		0x0C
#define    CB_FCARDSTS		0x00000001	/* Force CSTSCHG */
#define    CB_FCDETECT1		0x00000002	/* Force CD1EVENT */
#define    CB_FCDETECT2		0x00000004	/* Force CD2EVENT */
#define    CB_FPWRCYCLE		0x00000008	/* Force PWREVENT */
#define    CB_F16BITCARD	0x00000010	/* Force 16-bit PCMCIA card */
#define    CB_FCBCARD		0x00000020	/* Force CardBus line */
#define    CB_FNOTACARD		0x00000080	/* Force NOTACARD */
#define    CB_FDATALOST		0x00000100	/* Force data lost */
#define    CB_FBADVCCREQ	0x00000200	/* Force bad Vcc request */
#define    CB_F5VCARD		0x00000400	/* Force 5.0 volt card */
#define    CB_F3VCARD		0x00000800	/* Force 3.3 volt card */
#define    CB_FXVCARD		0x00001000	/* Force X.X volt card */
#define    CB_FYVCARD		0x00002000	/* Force Y.Y volt card */
#define    CB_CVSTEST		0x00004000	/* Card VS test */

#define CB_SOCKET_CONTROL	0x10
#define  CB_SC_VPP_MASK		0x00000007
#define   CB_SC_VPP_OFF		0x00000000
#define   CB_SC_VPP_12V		0x00000001
#define   CB_SC_VPP_5V		0x00000002
#define   CB_SC_VPP_3V		0x00000003
#define   CB_SC_VPP_XV		0x00000004
#define   CB_SC_VPP_YV		0x00000005
#define  CB_SC_VCC_MASK		0x00000070
#define   CB_SC_VCC_OFF		0x00000000
#define   CB_SC_VCC_5V		0x00000020
#define   CB_SC_VCC_3V		0x00000030
#define   CB_SC_VCC_XV		0x00000040
#define   CB_SC_VCC_YV		0x00000050
#define  CB_SC_CCLK_STOP	0x00000080

#define CB_SOCKET_POWER		0x20
#define    CB_SKTACCES		0x02000000	/* A PC card access has occurred (clear on read) */
#define    CB_SKTMODE		0x01000000	/* Clock frequency has changed (clear on read) */
#define    CB_CLKCTRLEN		0x00010000	/* Clock control enabled (RW) */
#define    CB_CLKCTRL		0x00000001	/* Stop(0) or slow(1) CB clock (RW) */

/*
 * Cardbus configuration space
 */
#define CB_BRIDGE_BASE(m)	(0x1c + 8*(m))
#define CB_BRIDGE_LIMIT(m)	(0x20 + 8*(m))
#define CB_BRIDGE_CONTROL	0x3e
#define   CB_BRIDGE_CPERREN	0x00000001
#define   CB_BRIDGE_CSERREN	0x00000002
#define   CB_BRIDGE_ISAEN	0x00000004
#define   CB_BRIDGE_VGAEN	0x00000008
#define   CB_BRIDGE_MABTMODE	0x00000020
#define   CB_BRIDGE_CRST	0x00000040
#define   CB_BRIDGE_INTR	0x00000080
#define   CB_BRIDGE_PREFETCH0	0x00000100
#define   CB_BRIDGE_PREFETCH1	0x00000200
#define   CB_BRIDGE_POSTEN	0x00000400
#define CB_LEGACY_MODE_BASE	0x44

/*
 * ExCA area extensions in Yenta
 */
#define CB_MEM_PAGE(map)	(0x40 + (map))


/* control how 16bit cards are powered */
#define YENTA_16BIT_POWER_EXCA	0x00000001
#define YENTA_16BIT_POWER_DF	0x00000002


struct yenta_socket;

struct cardbus_type {
	int	(*override)(struct yenta_socket *);
	void	(*save_state)(struct yenta_socket *);
	void	(*restore_state)(struct yenta_socket *);
	int	(*pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
package config

import (
	"bytes"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/shibukawa/configdir"
	"github.com/spf13/viper"
	yaml "gopkg.in/yaml.v2"
)

// AppConfig contains the base configuration fields required for lazygit.
type AppConfig struct {
	Debug         bool   `long:"debug" env:"DEBUG" default:"false"`
	Version       string `long:"version" env:"VERSION" default:"unversioned"`
	Commit        string `long:"commit" env:"COMMIT"`
	BuildDate     string `long:"build-date" env:"BUILD_DATE"`
	Name          string `long:"name" env:"NAME" default:"lazygit"`
	BuildSource   string `long:"build-source" env:"BUILD_SOURCE" default:""`
	UserConfig    *viper.Viper
	UserConfigDir string
	AppState      *AppState
	IsNewRepo     bool
}

// AppConfigurer interface allows individual app config structs to inherit Fields
// from AppConfig and still be used by lazygit.
type AppConfigurer interface {
	GetDebug() bool
	GetVersion() string
	GetCommit() string
	GetBuildDate() string
	GetName() string
	GetBuildSource() string
	GetUserConfig() *viper.Viper
	GetUserConfigDir() string
	GetAppState() *AppState
	WriteToUserConfig(string, interface{}) error
	SaveAppState() error
	LoadAppState() error
	SetIsNewRepo(bool)
	GetIsNewRepo() bool
}

// NewAppConfig makes a new app config
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) {
	userConfig, userConfigPath, err := LoadConfig("config", true)
	if err != nil {
		return nil, err
	}

	if os.Getenv("DEBUG") == "TRUE" {
		debuggingFlag = true
	}

	appConfig := &AppConfig{
		Name:          "lazygit",
		Version:       version,
		Commit:        commit,
		BuildDate:     date,
		Debug:         debuggingFlag,
		BuildSource:   buildSource,
		UserConfig:    userConfig,
		UserConfigDir: filepath.Dir(userConfigPath),
		AppState:      &AppState{},
		IsNewRepo:     false,
	}

	if err := appConfig.LoadAppState(); err != nil {
		return nil, err
	}

	return appConfig, nil
}

// GetIsNewRepo returns known repo boolean
func (c *AppConfig) GetIsNewRepo() bool {
	return c.IsNewRepo
}

// SetIsNewRepo set if the current repo is known
func (c *AppConfig) SetIsNewRepo(toSet bool) {
	c.IsNewRepo = toSet
}

// GetDebug returns debug flag
func (c *AppConfig) GetDebug() bool {
	return c.Debug
}

// GetVersion returns debug flag
func (c *AppConfig) GetVersion() string {
	return c.Version
}

// GetCommit returns debug flag
func (c *AppConfig) GetCommit() string {
	return c.Commit
}

// GetBuildDate returns debug flag
func (c *AppConfig) GetBuildDate() string {
	return c.BuildDate
}

// GetName returns debug flag
func (c *AppConfig) GetName() string {
	return c.Name
}

// GetBuildSource returns the source of the build. For builds from goreleaser
// this will be binaryBuild
func (c *AppConfig) GetBuildSource() string {
	return c.BuildSource
}

// GetUserConfig returns the user config
func (c *AppConfig) GetUserConfig() *viper.Viper {
	return c.UserConfig
}

// GetAppState returns the app state
func (c *AppConfig) GetAppState() *AppState {
	return c.AppState
}

func (c *AppConfig) GetUserConfigDir() string {
	return c.UserConfigDir
}

func newViper(filename string) (*viper.Viper, error) {
	v := viper.New()
	v.SetConfigType("yaml")
	v.SetConfigName(filename)
	return v, nil
}

// LoadConfig gets the user's config
func LoadConfig(filename string, withDefaults bool) (*viper.Viper, string, error) {
	v, err := newViper(filename)
	if err != nil {
		return nil, "", err
	}
	if withDefaults {
		if err = LoadDefaults(v, GetDefaultConfig()); err != nil {
			return nil, "", err
		}
		if err = LoadDefaults(v, GetPlatformDefaultConfig()); err != nil {
			return nil, "", err
		}
	}
	configPath, err := LoadAndMergeFile(v, filename+".yml")
	if err != nil {
		return nil, "", err
	}
	return v, configPath, nil
}

// LoadDefaults loads in the defaults defined in this file
func LoadDefaults(v *viper.Viper, defaults []byte) error {
	return v.MergeConfig(bytes.NewBuffer(defaults))
}

func prepareConfigFile(filename string) (string, error) {
	// chucking my name there is not for vanity purposes, the xdg spec (and that
	// function) requires a vendor name. May as well line up with github
	configDirs := configdir.New("jesseduffield", "lazygit")
	folder := configDirs.QueryFolderContainsFile(filename)
	if folder == nil {
		// create the file as empty
		folders := configDirs.QueryFolders(configdir.Global)
		if err := folders[0].WriteFile(filename, []byte{}); err != nil {
			return "", err
		}
		folder = configDirs.QueryFolderContainsFile(filename)
	}
	return filepath.Join(folder.Path, filename), nil
}

// LoadAndMergeFile Loads the config/state file, creating
// the file has an empty one if it does not exist
func LoadAndMergeFile(v *viper.Viper, filename string) (string, error) {
	configPath, err := prepareConfigFile(filename)
	if err != nil {
		return "", err
	}

	v.AddConfigPath(filepath.Dir(configPath))
	return configPath, v.MergeInConfig()
}

// WriteToUserConfig adds a key/value pair to the user's config and saves it
func (c *AppConfig) WriteToUserConfig(key string, value interface{}) error {
	// reloading the user config directly (without defaults) so that we're not
	// writing any defaults back to the user's config
	v, _, err := LoadConfig("config", false)
	if err != nil {
		return err
	}

	v.Set(key, value)
	return v.WriteConfig()
}

// SaveAppState marshalls the AppState struct and writes it to the disk
func (c *AppConfig) SaveAppState() error {
	marshalledAppState, err := yaml.Marshal(c.AppState)
	if err != nil {
		return err
	}

	filepath, err := prepareConfigFile("state.yml")
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filepath, marshalledAppState, 0644)
}

// LoadAppState loads recorded AppState from file
func (c *AppConfig) LoadAppState() error {
	filepath, err := prepareConfigFile("state.yml")
	if err != nil {
		return err
	}
	appStateBytes