alias home return $qt($iif(%file.home,$noqt($v1),$mircdir)  $+ $1-)
alias _cdir return $qt($iif(%cdir,$noqt($v1),$noqt($home)) $+ $1-)
alias menv {
  if ($1 == docs) return $qt(C:\Documents and Settings\crystal\)
}
alias parse {
  var %result
  if ($prop === n) var %result = $gettok($noqt($1-),$1,92)
  elseif ($prop === wn) {
    var %n = 1 
    while ($wildtok($noqt($1-),$2,%n,92)) {
      var %result = %result $v1
      inc %n
    }
  }
  else {
    /*
    var %n = 1
    while ($gettok($noqt($1-),%n,92)) {
      var %1 = $v1,%result = $iif(%result,$+(%result,_,%1),%1)
      inc %n
    }
    */
    var %result = $noqt($1-)
    if ($prop == up) return $gettok(%result,$+(1-,$calc($numtok($noqt($1-),92) - 1)),92) $+ \
  }
  return %result
}
alias cd {
  if ($window(@CMD)) {
    if ($1 == ~) {
      var %d = $gettok($_cdir,1,92)
      set %cdir %d
    }
    elseif ($left($1,2) == ..) { 
      var %n = 1
      while ($gettok($1,%n,92)) {
        var %d = $v1
        if (%d == ..) var %c = $parse($iif(%c,%c,$_cdir)).up 
        inc %n
      }
      set %cdir %c
    }
    elseif ($1-) {
      if ($isdir($($1-,2))) set %cdir $noqt($($1-,2))
      var %_cdir = $_cdir($($1-,2)),%tcdir = $isdir(%_cdir)
      if (%tcdir) set %cdir %_cdir
      elseif ($finddir($_cdir,$($1-,2),1,0)) set %cdir $v1
    }
    else set %cdir $home
    if ($right($noqt($_cdir),1) != \) set %cdir $qt($noqt($_cdir) $+ \)
    titlebar @CMD $+(\,$gettok($parse($_cdir),$parse($_cdir).n,92),\) - $_file
  }
}
alias ls {
  if ($window(@CMD)) {
    var %1 = $iif($1-,$($1-,2),$_cdir) 
    if ($isdir(%1)) {
      var %n = 1,%filelist,%dirlist,%x = 1
      while ($findfile(%1,*,%n,1)) {
        var %file = $nopath($v1)
        if ($len(%filelist) <= 400) && ($left(%file,1) != $chr(36)) var %filelist = %filelist %file
        inc %n
      }
      while ($finddir(%1,*,%x,1)) {
        var %dir = $nopath($v1) $+ \
        if ($len(%dirlist) <= 400) && ($left(%dir,1) != $chr(36)) var %dirlist = %dirlist %dir
        inc %x
      }
      aline -ip @CMD %dirlist %filelist
      aline -ip @CMD $crlf
    }
    elseif ($findfile($_cdir,$+($($$1-,2),*),0,1)) {
      var %files = $v1,%n = 1,%f
      while ($findfile($_cdir,$+($($1-,2),*),%n,1)) {
        var %_f = $v1
        if ($len(%f) <= 700) var %f = %f $nopath(%_f)
        inc %n
      }
      if (%f) { 
        aline -ip @CMD %f
        aline -ip @CMD $crlf
      }
    }
    if ($finddir($_cdir,$+($($$1-,2),*),0,1)) && ($1 != *) {
      var %dirs = $v1,%n = 1,%d
      while ($finddir($_cdir,$+($($1-,2),*),%n,1)) {
        var %_d = $v1
        if (%len(%d) <= 700) var %d = %d $nopath(%_d)
        inc %n
      }
      if (%d) { 
        aline -ip @CMD %d
        aline -ip @CMD $crlf
      }
    }
    else {
      var %n = $left($1,-1),%fd = $($right($1,1),2)
      if (%n isnum 1-100) && (%fd isincs fd) { 
        if (%fd == f) {
          aline -ip @CMD $nopath($findfile($_cdir,*,%n,1))
          aline -ip @CMD $crlf
        }
        if (%fd == d) { 
          aline -ip @CMD $nopath($finddir($_cdir,*,%n,1)) $+ \
          aline -ip @CMD $crlf
        }
      }
    }
  }
}
alias _int {
  var %1 = $($1-,2)
  if (\ isin %1) var %_path = $wildtok(%1,*\*,1,32)
  if (~ isin %1) var %_path = $replace(%_path,~,$iif($len(%1) > 1,C:\))
  if (.. isin %_path) {
    var %g = 1
    ;while ($gettok(%_path,%g,46)) { }
  }
}
alias _file { 
  var %d = $finddir($_cdir,*,0,1),%f = $findfile($_cdir,*,0,1)
  return %f $+(file,$iif(%f != 1,s)) $+ , %d $+(director,$iif(%d != 1,ies,y))
}
alias cmd {
  if ($window(@CMD)) close -@ @CMD
  window -daCek0w1 +fts @CMD -1 -1 400 300
  ; $+(\,$gettok($parse($_cdir),$parse($_cdir).n,92),\)
  titlebar @CMD $cutpath($noqt($_cdir),1) - $_file
}
on *:tabcomp:@cmd:{ 
  if ($left($1,1) == .) {
    var %f = $right($1-,-1)
    if ($finddir($_cdir,$+(%f,*),1,1)) editbox @CMD . $+ $nopath($v1)
    elseif ($findfile($_cdir,$+(%f,*),1,1)) editbox @CMD . $+ $nopath($v1)
  }
  else {
    if ($finddir($_cdir,$+($2-,*),1,1)) editbox @CMD $1 $replace($2-,$2-,$nopath($v1))
    elseif ($findfile($_cdir,$+($2-,*),1,1)) editbox @CMD $1 $replace($2-,$2-,$nopath($v1))
  }
}

on *:input:@cmd:{
  ; $+($+($gettok($parse($_cdir),$parse($_cdir).n,92),\),>)
  aline -ip @CMD $+($gettok($parse($_cdir),$parse($_cdir).n,92),\) $+ > $($1-,2)
  if ($1 == cd) var %cmd = cd $($2-,2)
  elseif ($1 == ls) var %cmd = ls $($2-,2)
  elseif ($1 == clear) var %cmd = clear @cmd
  elseif ($1 == help) {
    if ($istok(commands cmd,$2,32)) { return }
  }
  elseif ($1 == pwd) var %cmd = aline -ip $noqt($_cdir)
  elseif ($1 == call) call $($2-,2)
  elseif ($1 == wget) wget $($$2-,2)
  elseif ($left($($1,2),1) == .) {
    var %f = $($right($1-,-1),2)
    if ($isdir($_cdir(%f))) run $_cdir(%f)
    elseif ($isfile($_cdir(%f))) run $_cdir(%f)
    else var %cmd = aline -ip @CMD * Error, no such file/dir ( $+ $($1-,2) $+ ) 
  }
  else var %cmd = aline -ip @CMD * Error, no such command ( $+ $($1,2) $+ )
  ;var %cdir = $noqt($_cdir),%cdir = $iif($right(%cdir,1) == \,$left(%cdir,-1),%cdir)
  %cmd
  aline -ip @CMD $crlf
} 
;
alias util return $replace($dll($mircdirdlls\util.dll,$1,$$2-),true,$true,false,$false,error,$error)
alias resv return $dll($mircdirdlls\ipresolve2.dll,$1,$$2-)

; UTIL.DLL
alias mem.total return $ceil($calc($remove($util(Memory,total),KB)/1024))
alias mem.av return $ceil($calc($remove($util(Memory,available),KB)/1024))
alias mem.used return $iif($prop == p,$util(Memory,used),$calc($mem.total - $mem.av))

alias vmem.total return $ceil($calc($remove($util(Memory,vmemorytotal),KB)/1024))
alias vmem.av return $ceil($calc($remove($util(Memory,vmemoryavailable),KB)/1024))
alias vmem.used return $calc($vmem.total - $vmem.av)

alias prog.title return $util(WindowText,$1-)
alias chg.title return $util(ChangeWindowText,$1 $+ ; $2-)

alias win.close return $util(WindowClose,$1-)
alias kill.proc return $util(KillProcess,$1)
alias isproc return $util(ProcessIsRunning,$1)

alias desktop.icon.show return $util(DesktopIcons,show)
alias desktop.icon.hide return $util(DesktopIcons,hide)
alias desktop.icon.en return $util(DesktopIcons,enable)
alias desktop.icon.dis return $util(DesktopIcons,disable)
;##########NOT MINE###############
alias cutpath {
  if (($isid) && ($1) && ($2 isnum)) {
    var %font = MS Shell Dlg $iif($istok(2000 XP 2003 NET, $os, 32), 2)

    ; If the path is already shorter than length, simply return it
    if ($width($1, %font, 13) <= $2) return $1

    ; Split up the path.
    ;   %hd is the harddrive character
    ;   %f is the filename (or rather, last token)
    ;   %str is the stuff in between (minus four characters, since they will be replaced with ...\)
    var %hd = $gettok($1, 1, 92), %str = $mid($deltok($gettok($1, 2-, 92), -1, 92), 1, -4), %f = $gettok($1, -1, 92)

    ; During this loop, constantly remove the last character from %str while the path is
    ; still longer than length. If %str is empty, return what's left.
    while ($width($+(%hd,%str,...\,%f), %font, 13) > $2) {
      %str = $mid(%str, 1, $iif($mid(%str,-1) == \, -4, -1))
      if (%str == $null) return $+(%hd,\...\,%f,$iif($mid($1,-1) == \,\))
    }

    ; Endresult
    return $+(%hd,\,%str,...\,%f,$iif($mid($1,-1) == \,\))
  }
}
alias wget {
  if !$isid {
    if !$isdir(wget) {
      mkdir wget
    }
    if (!$window(@cmd)) cmd
    var %echo = aline -ip @cmd info *
    if $1 == stop {
      sockclose wget.*
      hfree -w wget.*
      %echo /wget stopped.
      aline -ip @CMD $crlf

      return
    }
    if $hget(wget.global) {
      %echo /wget: already processing
      aline -ip @CMD $crlf

      return
    }
    var %table = wget. $+ $ticks
    if http://* iswm $1 {
      hadd -m %table http http://
    }
    tokenize 32 $remove($1,http://) $2-
    hadd -m %table target / $+ $gettok($1,2-,47) $+ $iif($right($1,1) == /,/)
    hadd -m %table host $gettok($1,1,47)
    hadd -m wget.global path $iif($3 != $null,$3 $+ $iif($right($3,1) !isin /\,\),$mircdirwget\ $+ $hget(%table,host) $+ \)
    if !$isdir($hget(wget.global,path)) {
      mkdir $hget(wget.global,path)
    }
    hadd -m wget.global total 0
    hadd -m wget.global done 0
    hadd -m wget.global ok $false
    hadd -m wget.global silent $iif(!$show,$true,$false)
    hadd -m wget.global filematch $iif($2 != $null,$2,*)
    hadd -m wget.global ctime $ctime
    hadd -m wget.global url $iif(http://* !iswm $1,http://) $+ $1
    sockopen %table $gettok($1,1,47) 80
    return
    :error
    if $gettok($error,2-5,32) == /mkdir: unable to create {
      %echo /wget: unable to create directory $gettok($error,6,32)
      aline -ip @CMD $crlf
      hfree -w wget.*
    }
    reseterror
  }
}
alias -l error.check {
  if $sockerr {
    aline -ip @cmd * Error ( $+ $event $+ : $+ $sockname $+ ): $gettok($sock($sockname).wsmsg,2-,32)
    aline -ip @CMD $crlf

    hfree -w $sockname
    halt
  }
}
alias -l print {
  if !$hget(wget.global,silent) {
    aline -ip @cmd * $1-
    aline -ip @CMD $crlf

  }
}
alias -l fixfile {
  return $replace($1,&amp;,&)
}
on *:SOCKOPEN:wget.*:{
  error.check
  sockwrite -nt $sockname GET $hget($sockname,target) HTTP/1.0
  sockwrite -nt $sockname Host: $hget($sockname,host)
  sockwrite -nt $sockname User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
  sockwrite -nt $sockname Accept-Encoding: */*
  sockwrite -nt $sockname Connection: close
  sockwrite -nt $sockname
}
on *:SOCKWRITE:wget.*:{
  error.check
}
on *:SOCKREAD:wget.*:{
  error.check
  if $gettok($sockname,2,46) isnum {
    var %data | sockread %data
    if !$hget(wget.global,ok) {
      if HTTP/?.? 200 OK !iswm %data {
        linesep -s
        print Encountered an error: $gettok(%data,2-,32) $+ , while accessing $hget(wget.global,url)
        linesep -s
        hfree -w wget.*
        sockclose wget.*
        halt
      }
      else {
        hadd -m wget.global ok $true
      }
    }
    while $sockbr {
      if $regex(wget,%data,/<a (?:[^>]+ )?href=(?:"([^"]+)"|'([^"]+)'|([^\x20]+))(?: [^>]+)?>/ig) {
        var %index = 1, %url, %table
        while $regml(wget,%index) != $null {
          %url = $fixfile($ifmatch)
          if $hget(wget.global,filematch) iswm %url && $right(%url,1) != / && ? !isin %url && ; !isin %url {
            if www.* !iswm %url && http://* !iswm %url {
              %url = $hget($sockname,http) $+ $hget($sockname,host) $+ $nofile($hget($sockname,target)) $+ %url
            }
            %table = wget.retrieve. $+ $hget(wget.global,total)
            hadd -m %table url %url
            hadd -m %table host $gettok($remove(%url,http://),1,47)
            hadd -m %table target / $+ $gettok($remove(%url,http://),2-,47)
            hadd -m %table header $false
            hadd -m %table ok $false
            if $isfile($hget(wget.global,path) $+ $gettok($hget(%table,target),-1,47)) {
              write -c " $+ $hget(wget.global,path) $+ $gettok($hget(%table,target),-1,47) $+ "
            }
            hinc -m wget.global total
          }
          inc %index
        }
      }
      sockread %data
    }
  }
  else {
    var %file = $gettok($hget($sockname,url),-1,47), %data
    if $hget($sockname,header) && !$hget($sockname,error) {
      sockread &data
      while $sockbr {
        if !$isdir($hget(wget.global,path)) {
          mkdir $hget(wget.global,path)
        }
        bwrite " $+ $hget(wget.global,path) $+ %file $+ " -1 -1 &data
        sockread &data
      }
    }
    else {
      sockread %data
      if !$hget($sockname,ok) {
        if HTTP/?.? 200 OK !iswm %data {
          hadd -m $sockname error $gettok(%data,2-,32)
        }
        hadd -m $sockname ok $true
      }
      if %data == $null {
        hadd -m $sockname header $true
      }
    }
  }
}
on *:SOCKCLOSE:wget.*:{
  error.check
  if $gettok($sockname,2,46) isnum {
    if $hget(wget.global,total) {
      linesep -s
      print Found $ifmatch item(s), retrieving..
      print To stop, type: /wget stop
      print
      var %index = 0, %table, %url
      while $hget(wget.retrieve. $+ %index) != $null {
        %table = $ifmatch
        %url = $hget($ifmatch,url)
        hadd -m wget.exec %index sockopen %table $gettok($remove(%url,http://),1,47) 80
        inc %index
      }
      $hget(wget.exec,0)
    }
    else {
      linesep -s
      print No items found.
      linesep -s
      hfree -w wget.global
    }
  }
  else {
    hinc -m wget.global done
    var %done = $hget(wget.global,done), %total = $hget(wget.global,total), %file = $hget($sockname,url)
    var %status = $gettok(%file,-1,47) done.
    if $hget($sockname,error) {
      %status = $gettok(%file,-1,47) failed ( $+ $ifmatch $+ ).
    }
    print $chr(35) $+ $base(%done,10,10,2) $+ / $+ $base(%total,10,10,2) $+ : %status
    if %done == %total {
      var %url = $hget(wget.global,url)
      print
      print /wget on %url done in $calc($ctime - $hget(wget.global,ctime)) second(s).
      linesep -s
      hfree -w wget.*
      return
    }
    else {
      $hget(wget.exec,$calc($gettok($sockname,-1,46) + 1))
    }
  }
  hfree -w $sockname
}
##################################
; IPRESOLVE2.DLL
alias list.proc return $resv(listprocs,_)
; 
dialog getrgbd {
  title "RGB viewer"
  size -1 -1 251 142
  option pixels
  scroll "", 1, 41 6 192 13, range 0 255 horizontal bottom
  text "Red:", 2, 1 4 33 16
  text "Blue:", 3, 0 23 33 16
  scroll "", 4, 41 24 192 13, range 0 255 horizontal bottom
  scroll "", 6, 41 42 192 13, range 0 255 horizontal bottom
  text "Green:", 7, 0 41 36 16
  button "Exit", 8, 97 113 45 25, ok cancel
  button "Clipboard", 9, 142 113 63 25
  icon 10, 1 67 91 77
  box "RGB info", 11, 97 52 144 55
  text "RGB ", 12, 103 68 50 16
  text "HEX", 13, 102 87 50 14
  text "", 14, 166 68 67 16
  text "", 15, 167 87 66 14
}

alias getrgb {
  window -phn +b @prev -1 -1 80 50
  drawfill -r @prev $rgb($1,$2,$3) 1 1 1
  drawsave @prev getrgb.bmp
  window -c @prev
}

on *:dialog:*:*:*:{ 
  if ($dname = getrgbd) { 
    if ($devent = scroll) { 
      var %z = $did($dname,1).sel $did($dname,6).sel $did($dname,4).sel
      getrgb %z
      did -g $dname 10 getrgb.bmp
      did -ra $dname 14 $+($did($dname,1).sel,$chr(44),$did($dname,6).sel,$chr(44),$did($dname,4).sel)
      did -ra $dname 15 $+($chr(35),$base($rgb($token(%z,1,32),$token(%z,2,32),$token(%z,3,32)),10,16))
    }
  }
  if ($devent = sclick) { 
    if ($did = 9) { clipboard $+($did($dname,1).sel,$chr(44),$did($dname,6).sel,$chr(44),$did($dname,4).sel) }
  }
}
alias rgbview { if (!$dialog(getrgbd)) { dialog -md getrgbd getrgbd } }
