summary refs log tree commit diff stats
path: root/c/grains/src/grains.c
blob: b169dcfe638937897391f921ca6ec7b207b292bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
#include "grains.h"

uint64_t square(uint8_t index) {
    if (0 < index && index <= 64)
        return 1ULL << (index - 1);
    return 0;
}
uint64_t total(void) {
    return ((1ULL << 63) << 1) - 1;
}
itle='Blame the previous revision' href='/gbmor/getwtxt/blame/init.go?h=v0.4.14&id=3aaa9f3d3c5d5b59b6629897111b43c5caeadccc'>^
6dad137 ^


a0be15e ^

fdd9bd0 ^
cd635e6 ^
06cffd8 ^
fdd9bd0 ^
d083ce6 ^
06cffd8 ^
fb32863 ^
887c25e ^
fb32863 ^



fdd9bd0 ^

bd23ef0 ^
fdd9bd0 ^
e34697c ^
920306c ^
fdd9bd0 ^
efa99ed ^

fdd9bd0 ^
7410b8c ^



df1d1ef ^
fdd9bd0 ^

4695425 ^
2b0d4a5 ^
6c1b09b ^











6753171 ^



f2d85b6 ^
1f8b2ce ^
fdd9bd0 ^
06cffd8 ^
06cffd8 ^
c896e6b ^
e34697c ^
fdd9bd0 ^

7410b8c ^
fdd9bd0 ^
06cffd8 ^

f2d85b6 ^
1f8b2ce ^
f2d85b6 ^




bd23ef0 ^

f2d85b6 ^
67864b2 ^
cf6c849 ^





f2d85b6 ^

5310d08 ^







cfda931 ^
887c25e ^
fdd9bd0 ^

c896e6b ^
42a3e8a ^

cd635e6 ^
d083ce6 ^
5310d08 ^
cd635e6 ^
42a3e8a ^
b29e1c1 ^
d083ce6 ^
efa99ed ^

c896e6b ^
5310d08 ^



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
                                                      

        
                       

             
                   
              
              
 
                                     
                                

 
     

                                                             
                                                                                                            
                                                                                                
                                                                                                     
                                                                                                                           


                                                                                                                

 
                                 
                              
 
                                
                                 
 
                                    
                                



                                       

                                 
 
                                           
                            
 
                                    

                                    
                                                      



                                         
 

                                                 
                                 
 











                                                       



                                       
                    
                     
 
                    
                     
                      
                               

                         
                    
                           

 
                   
                     




                             

                             
                          
         





                              

 







                                       
                                                                          
 

                                     
 

                                                                                        
                                                   
                                                
                         
                                            
 
                                     
                                       

                                                  
                                                          



                                  
package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"html/template"
	"log"
	"os"
	"os/signal"
	"sync"
	"time"

	"github.com/getwtxt/registry"
	"github.com/spf13/pflag"
)

var (
	// Vers contains the version number set at build time
	Vers         string
	flagVersion  *bool   = pflag.BoolP("version", "v", false, "Display version information, then exit.")
	flagHelp     *bool   = pflag.BoolP("help", "h", false, "Display the quick-help screen.")
	flagMan      *bool   = pflag.BoolP("manual", "m", false, "Display the configuration manual.")
	flagConfFile *string = pflag.StringP("config", "c", "", "The name/path of the configuration file you wish to use.")
	flagAssets   *string = pflag.StringP("assets", "a", "", "The location of the getwtxt assets directory.")
	flagDBPath   *string = pflag.StringP("db", "d", "", "Path to the getwtxt database.")
	flagDBType   *string = pflag.StringP("dbtype", "t", "", "Type of database being used.")
)

// Holds the global configuration
var confObj = &Configuration{}

// Signals to close the log file
var closeLog = make(chan bool, 1)

// Used to transmit database pointer
var dbChan = make(chan dbase, 1)

// Used to transmit the wrapped tickers
// corresponding to the in-memory cache
// or the on-disk database.
var dbTickC = make(chan *tick, 1)
var cTickC = make(chan *tick, 1)

// Used to manage the landing page template
var tmpls *template.Template

// Holds the registry data in-memory
var twtxtCache = registry.NewIndex()

// List of other registries submitted to this registry
var remoteRegistries = &RemoteRegistries{
	Mu:   sync.RWMutex{},
	List: make([]string, 0),
}

// In-memory cache of static assets, specifically
// the parsed landing page and the stylesheet.
var staticCache = &staticAssets{}

func errFatal(context string, err error) {
	if err != nil {
		log.Fatalf(context+"%v\n", err.Error())
	}
}

func errLog(context string, err error) {
	if err != nil {
		log.Printf(context+"%v\n", err.Error())
	}
}

// I'm not using init() because it runs
// even during testing and was causing
// problems.
func initSvc() {
	checkFlags()
	titleScreen()

	initConfig()
	initLogging()
	initDatabase()
	tmpls = initTemplates()
	initPersistence()

	pingAssets()
	watchForInterrupt()
}

func checkFlags() {
	pflag.Parse()
	if *flagVersion {
		titleScreen()
		os.Exit(0)
	}
	if *flagHelp {
		titleScreen()
		helpScreen()
		os.Exit(0)
	}
	if *flagMan {
		titleScreen()
		helpScreen()
		manualScreen()
		os.Exit(0)
	}
}

// Watch for SIGINT aka ^C
// Close the log file then exit
func watchForInterrupt() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	go func() {
		for sigint := range c {
			log.Printf("Caught %v. Cleaning up ...\n", sigint)

			killTickers()
			killDB()

			confObj.Mu.RLock()
			log.Printf("Closed database connection to %v\n", confObj.DBPath)
			if !confObj.StdoutLogging {
				closeLog <- true
			}
			confObj.Mu.RUnlock()

			close(dbChan)
			close(closeLog)

			// Let everything catch up
			time.Sleep(100 * time.Millisecond)
			os.Exit(0)
		}
	}()
}