summaryrefslogtreecommitdiff
path: root/configure.tcl
blob: 6f11546578fd640563ff54a3cfb4f00b34c14c13 (plain)
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
## Set your variables here.

namespace eval http {
    # The directory which files are searched for.
    variable srv {/home/aleksei/tcl-httpd/www/}

		namespace eval cache {
		variable precache {
			{/fonts/}
		}
	}

	namespace eval module {
		variable targets {};
		source modules/template.tcl
	}
}

proc temp {} {
	# Run through and bring in all targets
	foreach i [namespace children ::http::module] {
		set ::http::module::targets [concat $::http::module::targets $i::targets]
		# If there are duplicate elements, then error.
		if [expr [llength [lsort -unique $::http::module::targets]] != [llength $::http::module::targets]] {
			error "A duplicate target coming from $i."
		}
	}
}

## Validate configuration variables.
## Especially file exists content
## Especially validate the existence of necessary components in $content



set config [open /home/aleksei/tcl-httpd/tcl-httpd.conf]


## Configure the server, whilst verifying options.

namespace eval temp {
	set error_state 0;
	set error_message "Invalid lines:";
	set line_number 1;


	namespace eval verify {
		proc srv {s} {

		}

	}

	## Configuration files can take preemptive actions, such as preloading files
	## into the cache or loading a user written module.
	namespace eval actions {		
		proc @MODULE {v} {
			if [info exists ::http::configure::module_path] {
				set path [string cat $::http::configure::module_path $v ".tcl"]
				if { [file exists $path] && [file isFile $path] } {
					namespace eval [string cat "::http::module::" $v] {
						source $path
					}
				} else {
					error "Fatal Error: While trying to import module '$b', file $path' doesn't exist."
				}
			} else {
				error "Fatal Error: Tried to import module '$b' without a module_path"
			}
		}
		
		proc @PRECACHE {filename} {
			::http::cache::add $filename
		}
	}

	while {[gets $config line] != -1} {
		
		variable regexp_comment       {^[[:space:]]*\#};
		variable regexp_form_variable {^[[:lower:]_]+=[[:alnum:][:punct:]]+$};
		variable regexp_form_action   {^@[[:upper:]]+ [[:alnum:][:punct:]]+$};

		if [regexp $regexp_comment $line] {
			# Line that is a comment.
		} elseif [regexp $regexp_form_variable $line] {
			set n [string first "=" $line]
			set a [string range $line 0 [expr $n-1]]
			set b [string range $line [expr $n+1]   end]
			# RUN VERIFICATION
			#namespace eval ::http::configure "$a $b"
			namespace eval ::http::configure "variable $a $b"
		} elseif [regexp $regexp_form_action $line] {
			## Line of form @ACTION arguments
			set n [string first " " $line]
			set a [string range $line 0 [expr $n-1]]
			set b [string range $line [expr $n+1] end]
			# PERFORM ACTION
			namespace eval ::http::actions "$a $b"
		} else {
			append error_message " $line_number"
			set error_state 1;
		}
		incr line_number;
	}

	if $error_state {
		error "ERROR: There were multiple errors in your configuration.\n$error_message"
	}
}

## Delete the namespace afterwards.
namespace delete temp;