:module system
:base
:help.m modsys.m
addset nomods&
alias modprobe {
	if ([$0]) {
                if (!match($0 $nomods)) {
			@module = [$(modpath)$0]
	                ^load $module
	                if (!match($after($count(/ $module) / $module) $mods)) {
		                push mods $after($count(/ $module) / $module)
			}
		}
        }
	{
	        fe ($glob($(corepath)modules\/*.m)) module {
	                if (!match($module $nomods)) { 
				^load $module 
				if (!match($after($count(/ $module) / $module) $mods)) {
					push mods $after($count(/ $module) / $module)
				}
			}
	        }
	}
	^restub modprobe modsys.m
}
alias stubprobe {
        if ([$0]) {
                @module = [$(modpath)$0]
                if (!match($after($count(/ $module) / $module) $nomods)) {
                        if (!match($after($count(/ $module) / $module) $mods)) {
                                push mods $after($count(/ $module) / $module)
                        }
                        @ file = open($module R)
                        while (!eof($file)) {
                                @ line = read($file)
                                if ([$word(0 $line)]==[alias]) {
					^restub $word(1 $line) $after($count(/ $module) / $module)
                                }
                        }
                        @close($file)
                }

        }
        {
                fe ($glob($(corepath)modules\/*.m)) module {
                        if (!match($after($count(/ $module) / $module) $nomods)) {
                                if (!match($after($count(/ $module) / $module) $mods)) {
                                        push mods $after($count(/ $module) / $module)
                                }
                                @ file = open($module R)
                                while (!eof($file)) {
                                        @ line = read($file)
                                        if ([$word(0 $line)]==[alias]) {
                                                ^restub $word(1 $line) $after($count(/ $module) / $module)
                                        }
                                }
                                @close($file)
                        }
                }
        }
}
eval addhelp lsmod&lsmod&lists module information
alias lsmod {
	@mods = msar(!  ! !$mods)
	echo $fparse(format_header module system)
	echo $fparse(format_midbar $(c4)[$(c2)?$(c4)] [$(c2)module    $(c4)] [$(c2)description         $(c4)] [$(c2)type  $(c4)] [$(c2)dependencies        $(c4)])
	fe ($glob($(corepath)modules\/*.m)) module {
		@ :module = after($count(/ $module) / $module)
		@ :modfile = twiddle($(modpath)$module)
                @ :mfile = open($modfile R T)
                @ :desc = after(: $read($mfile))
		@ :type = after(: $read($mfile))
		@ :needs = after(: $read($mfile))
		@ close($mfile)
		if (match($module $mods)) {
			@ loaded = [û]
		}
		{
			@ loaded = [ ]
		}
		echo $fparse(format_midbar $(c4)[$(c2)$loaded$(c4)] [$(c3)$[10]module$(c4)] [$(c3)$[20]desc$(c4)] [$(c3)$[6]type$(c4)] [$(c3)$[20]needs$(c4)])
	}
	echo $fparse(format_footer modules system)
	^restub lsmod modsys.m
}
eval addhelp rmmod&rmmod $(c4)[$(c3)module$(c4)]&removes the module specified.
alias rmmod {
	if ([$0]) {
		if (right(2 $0) == [.m]) {
			@ module = [$0]
		}
		{
			@ module = [$(0).m]
		}
                @ :file = glob($(modpath)$module)
                @ :mfile = open($file R T)
                @ :desc = after(: $read($mfile))
                @ :type = after(: $read($mfile))
                @ :needs = after(: $read($mfile))
                @ close($mfile)
		if (match($module $nomods)) {
			xecho -b error: $module is not loaded
		}
		{
			^assign -deps
		        fe ($glob($(corepath)modules\/*.m)) _module {
		                @ :_module = after($count(/ $_module) / $_module)
		                @ :_modfile = twiddle($(modpath)$_module)
		                @ :_mfile = open($_modfile R T)
		                @ :_desc = after(: $read($_mfile))
		                @ :_type = after(: $read($_mfile))
		                @ :_needs = after(: $read($_mfile))
		                @ close($_mfile)
				if (match($module $_needs)) {
					^push deps $_module
				}
			}
			if (numwords($deps) != [0]) {
				xecho -b error: dependent modules are loaded $(c4)[$(c3)$deps$(c4)]
			}
			{
				^push nomods $module
				@ mods = msar(!$module !!$mods)
				@ mods = msar(!$module!!$mods)
				xecho -b removed module $module $(c4)[$(c3)$desc$(c4)]
				@ :file = open($glob($(modpath)$module) R)
                                while (!eof($file)) {
                                        @ :line = read($file)
                                        if ([$word(0 $line)] == [alias]) {
                                                ^alias -$word(1 $line)  
                                        }
					if ([$word(0 $line)] == [on]) {
						if (before(\{ $line)) {
							^eval $restw(0 $strip(^ $before(\{ $line)\{ \}))
						}
						if (before(# $line)) {
							^eval $restw(0 $strip(^ $before(# $line)\{ \}))
						}
					}
					if ([$word(0 $line)] == [^on]) {
                                 		if (before(\{ $line)) {
                                        		^eval $restw(0 $strip(^ $before(\{ $line)\{ \}))
                                 	}
                                 	if (before(# $line)) {
                                 		        ^eval $restw(0 $strip(^ $before(# $line)\{ \})) 
                                 		}
                                        }
					if ([$word(0 $line)] == [addconf]) {
						^remconf $restw(2 $line)
					}
                                        if ([$word(0 $line)] == [addset]) {
						^remset $restw(1 $line)
					}
                                        if ([$word(1 $line)] == [addhelp]) {
						^remhelp $restw(2 $line)
					}
					if ([$word(0 $line)] == [addhelp]) {
                                                ^remhelp $restw(1 $line)
                                        }
				}
                                @close($file)
			}
		}
	}
	{
		^chelp rmmod
	}
	^restub rmmod modsys.m
}
eval addhelp insmod&insmod $(c4)[$(c3)module$(c4)]&loads the module specified.
alias insmod {
	if ([$0]) {
                if (right(2 $0) == [.m]) { 
                        @ module = [$0]    
                }
                {
                        @ module = [$(0).m]
                }
		if (match($module $mods)) {
			xecho -b error: $module is already loaded
		}
		{
			^assign -failed
	                @ :file = glob($(modpath)$module)
	                @ :mfile = open($file R T)
	                @ :desc = after(: $read($mfile))
	                @ :type = after(: $read($mfile))
	                @ :needs = after(: $read($mfile))
	                @ close($mfile)
			for (@i=1,i <= [${numwords($needs) + 1}],@i++) {
				if (match($word($i $needs) $nomods)) {
					xecho -b error: failed dependency $word($i $needs)
					@ :failed = [1]
				}
			}
			if (!failed) {
				@ nomods = msar(!$module !!$nomods)
				@ nomods = msar(!$module!!$nomods)
				@ :modd = [$module]
				modprobe $modd
				stubprobe $modd
				xecho -b inserted module $modd $(c4)[$(c3)$desc$(c4)]
			}
		}
	}
	^restub insmod modsys.m
}