1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006-2007, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (at your option
) any later version
.
9 * This program is distributed
in the hope that it will be useful
,
10 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the
12 * GNU General Public License for more details
.
14 * You should have received a copy
of the GNU General Public License
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
19 (* APT package database querying
*)
21 structure Apt
:> APT
= struct
23 fun validName s
= CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"_" orelse ch
= #
"-" orelse ch
= #
".") s
24 andalso (size s
> 0 andalso String.sub (s
, 0) <> #
"-")
26 (* Copyed from the portal
, doesn
't exactly go out this
in the most
27 direct way
, or does it?
*)
30 if not (validName name
) then
34 val proc
= Unix
.execute ("/usr/bin/apt-cache", ["show", name
])
35 val inf
= Unix
.textInstreamOf proc
37 val _
= TextIO.inputLine
inf (* in every
let* lies an imperative program
in disguise
*)
39 fun loop (section
, descr
) =
40 case TextIO.inputLine inf
of
41 NONE
=> (section
, descr
)
43 if size line
>= 9 andalso String.substring (line
, 0, 9) = "Section: " then
44 loop (SOME (String.substring (line
, 9, size line
- 10)), descr
)
45 else if size line
>= 13 andalso String.substring (line
, 0, 13) = "Description: " then
46 loop (section
, SOME (String.substring (line
, 13, size line
- 14)))
47 else if size line
>= 16 andalso String.substring (line
, 0, 16) = "Description-en: " then
48 loop (section
, SOME (String.substring (line
, 16, size line
- 17)))
52 (case loop (NONE
, NONE
) of
53 (SOME section
, SOME descr
) => SOME
{section
= section
, description
= descr
}
55 before ignore (Unix
.reap proc
)
61 val proc
= Unix
.execute ("/usr/bin/apt-cache", ["policy", name
])
62 val inf
= Unix
.textInstreamOf proc
64 val _
= TextIO.inputLine inf
66 (case TextIO.inputLine inf
of
69 case String.tokens
Char.isSpace line
of
70 [_
, "(none)"] => false
73 before ignore (Unix
.reap proc
)