From 0ceec7025f4ddc47f4e2b9f23f88a4eba0e1f73b Mon Sep 17 00:00:00 2001
From: Hisham Muhammad <hisham@gobolinux.org>
Date: Sat, 13 Aug 2011 15:06:05 -0300
Subject: Replace all print()'s by appropriate calls to send messages to stdout
 and stderr. Closes #11.

---
 src/luarocks/add.lua           | 10 +++++-----
 src/luarocks/admin_remove.lua  | 10 +++++-----
 src/luarocks/build.lua         |  8 ++++----
 src/luarocks/build/cmake.lua   |  6 +++---
 src/luarocks/build/make.lua    | 11 +++++++----
 src/luarocks/cache.lua         |  4 ++--
 src/luarocks/cfg.lua           | 16 +++++++--------
 src/luarocks/command_line.lua  | 10 +++++-----
 src/luarocks/deps.lua          | 31 +++++++++++++++--------------
 src/luarocks/download.lua      |  8 ++++----
 src/luarocks/fs/unix.lua       |  3 ++-
 src/luarocks/help.lua          | 16 +++++++--------
 src/luarocks/install.lua       | 16 +++++++--------
 src/luarocks/list.lua          |  8 ++++----
 src/luarocks/loader.lua        | 12 ++++++------
 src/luarocks/make_manifest.lua |  5 +++--
 src/luarocks/manif.lua         |  8 ++++----
 src/luarocks/pack.lua          |  2 +-
 src/luarocks/path.lua          | 10 ++++++----
 src/luarocks/remove.lua        | 29 ++++++++++++++--------------
 src/luarocks/rep.lua           |  2 +-
 src/luarocks/search.lua        | 36 +++++++++++++++++-----------------
 src/luarocks/show.lua          | 44 +++++++++++++++++++++---------------------
 src/luarocks/tools/tar.lua     |  8 ++++----
 src/luarocks/tools/zip.lua     | 12 +++++++-----
 src/luarocks/unpack.lua        |  8 ++++----
 src/luarocks/util.lua          | 26 ++++++++++++++++++-------
 src/luarocks/validate.lua      | 38 ++++++++++++++++++------------------
 28 files changed, 210 insertions(+), 187 deletions(-)

diff --git a/src/luarocks/add.lua b/src/luarocks/add.lua
index 5f9cf95c..0dcb74bb 100644
--- a/src/luarocks/add.lua
+++ b/src/luarocks/add.lua
@@ -62,12 +62,12 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server)
    local files = {}
    for i, rockfile in ipairs(rockfiles) do
       if fs.exists(rockfile) then
-         print("Copying file "..rockfile.." to "..local_cache.."...")
+         util.printout("Copying file "..rockfile.." to "..local_cache.."...")
          local absolute = fs.absolute_name(rockfile)
          fs.copy(absolute, local_cache)
          table.insert(files, dir.base_name(absolute))
       else
-         print("File "..rockfile.." not found")
+         util.printerr("File "..rockfile.." not found")
       end
    end
    if #files == 0 then
@@ -76,9 +76,9 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server)
 
    fs.change_dir(local_cache)
 
-   print("Updating manifest...")
+   util.printout("Updating manifest...")
    manif.make_manifest(local_cache)
-   print("Updating index.html...")
+   util.printout("Updating index.html...")
    index.make_index(local_cache)
 
    local login_info = ""
@@ -101,7 +101,7 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server)
       cmd = "curl "..login_info.." -T '{manifest,index.html,"..table.concat(files, ",").."}' "..login_url
    end
 
-   print(cmd)
+   util.printout(cmd)
    fs.execute(cmd)
 
    return true
diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin_remove.lua
index 99852e36..95213ecc 100644
--- a/src/luarocks/admin_remove.lua
+++ b/src/luarocks/admin_remove.lua
@@ -55,11 +55,11 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve
    for i, rockfile in ipairs(rockfiles) do
       local basename = dir.base_name(rockfile)
       local file = dir.path(local_cache, basename)
-      print("Removing file "..file.."...")
+      util.printout("Removing file "..file.."...")
       if fs.delete(file) then
          nr_files = nr_files + 1
       else
-         print("Failed removing "..file)
+         util.printerr("Failed removing "..file)
       end
    end
    if nr_files == 0 then
@@ -68,15 +68,15 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve
 
    fs.change_dir(local_cache)
 
-   print("Updating manifest...")
+   util.printout("Updating manifest...")
    manif.make_manifest(local_cache)
-   print("Updating index.html...")
+   util.printout("Updating index.html...")
    index.make_index(local_cache)
 
    local srv, path = server_path:match("([^/]+)(/.+)")
    local cmd = "rsync -Oavz --delete -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/"
 
-   print(cmd)
+   util.printout(cmd)
    fs.execute(cmd)
 
    return true
diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua
index 44f60df8..5bb3c3f7 100644
--- a/src/luarocks/build.lua
+++ b/src/luarocks/build.lua
@@ -79,7 +79,7 @@ function apply_patches(rockspec)
    if build.patches then
       extract_from_rockspec(build.patches)
       for patch, patchdata in util.sortedpairs(build.patches) do
-         print("Applying patch "..patch.."...")
+         util.printout("Applying patch "..patch.."...")
          local ok, err = fs.apply_patch(tostring(patch), patchdata)
          if not ok then
             return nil, "Failed applying patch "..patch
@@ -170,7 +170,7 @@ function build_rockspec(rockspec_file, need_to_fetch, minimal_mode)
 
       -- Temporary compatibility
       if build.type == "module" then
-         print("Do not use 'module' as a build type. Use 'builtin' instead.")
+         util.printout("Do not use 'module' as a build type. Use 'builtin' instead.")
          build.type = "builtin"
       end
 
@@ -239,8 +239,8 @@ function build_rockspec(rockspec_file, need_to_fetch, minimal_mode)
    end
 
    local root_dir = path.root_dir(cfg.rocks_dir)
-   print()
-   print(name.." "..version.." is now built and installed in "..root_dir.." "..license)
+   util.printout()
+   util.printout(name.." "..version.." is now built and installed in "..root_dir.." "..license)
    
    util.remove_scheduled_function(rollback)
    return true
diff --git a/src/luarocks/build/cmake.lua b/src/luarocks/build/cmake.lua
index 7fd1fcc8..295f5df6 100644
--- a/src/luarocks/build/cmake.lua
+++ b/src/luarocks/build/cmake.lua
@@ -39,15 +39,15 @@ function run(rockspec)
       args = args .. ' -D' ..k.. '="' ..v.. '"'
    end
 
-   if not fs.execute("cmake . " ..args) then
+   if not fs.execute(rockspec.variables.CMAKE.." . " ..args) then
       return nil, "Failed cmake."
    end
    
-   if not fs.execute("make -fMakefile") then
+   if not fs.execute(rockspec.variables.MAKE.." -fMakefile") then
       return nil, "Failed building."
    end
 
-   if not fs.execute("make -fMakefile install") then
+   if not fs.execute(rockspec.variables.MAKE.." -fMakefile install") then
       return nil, "Failed installing."
    end
    return true
diff --git a/src/luarocks/build/make.lua b/src/luarocks/build/make.lua
index 37486e5d..80c0d4f4 100644
--- a/src/luarocks/build/make.lua
+++ b/src/luarocks/build/make.lua
@@ -13,7 +13,7 @@ local cfg = require("luarocks.cfg")
 -- @param variables table: A table containing string-string key-value
 -- pairs representing variable assignments to be passed to make.
 -- @return boolean: false if any errors occurred, true otherwise.
-local function make_pass(pass, target, variables)
+local function make_pass(make_cmd, pass, target, variables)
    assert(type(pass) == "boolean")
    assert(type(target) == "string")
    assert(type(variables) == "table")
@@ -23,7 +23,7 @@ local function make_pass(pass, target, variables)
       table.insert(assignments, k.."="..v)
    end
    if pass then
-      return fs.execute(cfg.make.." "..target, unpack(assignments))
+      return fs.execute(make_cmd.." "..target, unpack(assignments))
    else
       return true
    end
@@ -74,11 +74,14 @@ function run(rockspec)
       end
    end
 
-   local ok = make_pass(build.build_pass, build.build_target, build.build_variables)
+   -- backwards compatibility 
+   local make_cmd = cfg.make or rockspec.variables.MAKE
+
+   local ok = make_pass(make_cmd, build.build_pass, build.build_target, build.build_variables)
    if not ok then
       return nil, "Failed building."
    end
-   ok = make_pass(build.install_pass, build.install_target, build.install_variables)
+   ok = make_pass(make_cmd, build.install_pass, build.install_target, build.install_variables)
    if not ok then
       return nil, "Failed installing."
    end
diff --git a/src/luarocks/cache.lua b/src/luarocks/cache.lua
index 77c07d8b..d3fbdf5b 100644
--- a/src/luarocks/cache.lua
+++ b/src/luarocks/cache.lua
@@ -6,6 +6,7 @@ module("luarocks.cache", package.seeall)
 local fs = require("luarocks.fs")
 local cfg = require("luarocks.cfg")
 local dir = require("luarocks.dir")
+local util = require("luarocks.util")
 
 function split_server_url(server, url, user, password)
    local protocol, server_path = dir.split_url(url)
@@ -40,7 +41,7 @@ function refresh_local_cache(server, url, user, password)
       return nil, "Failed creating local cache dir."
    end
    fs.change_dir(local_cache)
-   print("Refreshing cache "..local_cache.."...")
+   util.printout("Refreshing cache "..local_cache.."...")
 
    -- TODO abstract away explicit 'wget' call
    local ok = false
@@ -58,4 +59,3 @@ function refresh_local_cache(server, url, user, password)
    end
    return local_cache, protocol, server_path, user, password
 end
-
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua
index a9789e21..25f22e26 100644
--- a/src/luarocks/cfg.lua
+++ b/src/luarocks/cfg.lua
@@ -1,6 +1,6 @@
 
-local rawset, next, table, pairs, print, require, io, os, setmetatable, pcall, ipairs, package, type, assert =
-      rawset, next, table, pairs, print, require, io, os, setmetatable, pcall, ipairs, package, type, assert
+local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, type, assert =
+      rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, type, assert
 
 --- Configuration for LuaRocks.
 -- Tries to load the user's configuration file and
@@ -18,7 +18,7 @@ module("luarocks.cfg")
 -- Load site-local global configurations
 local ok, config = pcall(require, "luarocks.config")
 if not ok then
-   print("Site-local luarocks/config.lua file not found. Incomplete installation?")
+   io.stderr:write("Site-local luarocks/config.lua file not found. Incomplete installation?\n")
    config = {}
 end
 
@@ -35,8 +35,8 @@ if popen_ok then
       popen_result:close()
    end
 else
-   print("Your version of Lua does not support io.popen,")
-   print("which is required by LuaRocks. Please check your Lua installation.")
+   io.stderr:write("Your version of Lua does not support io.popen,\n")
+   io.stderr:write("which is required by LuaRocks. Please check your Lua installation.\n")
    os.exit(1)
 end
 
@@ -171,8 +171,8 @@ if detected.windows then
    defaults.variables.LUA_INCDIR = config.LUA_INCDIR and config.LUA_INCDIR:gsub("\\", "/") or "c:/lua5.1/include"
    defaults.variables.LUA_LIBDIR = config.LUA_LIBDIR and config.LUA_LIBDIR:gsub("\\", "/") or "c:/lua5.1/lib"
    defaults.cmake_generator = "MinGW Makefiles"
-   defaults.make = "nmake" -- TODO: Split Windows flavors between mingw and msvc
    defaults.makefile = "Makefile.win"
+   defaults.variables.MAKE = "nmake" -- TODO: Split Windows flavors between mingw and msvc
    defaults.variables.CC = "cl"
    defaults.variables.RC = "rc"
    defaults.variables.WRAPPER = config.LUAROCKS_PREFIX .. "\\2.0\\rclauncher.obj"
@@ -236,8 +236,8 @@ if detected.unix then
    defaults.variables.LUA_LIBDIR = config.LUA_LIBDIR or "/usr/local/lib"
    defaults.variables.CFLAGS = "-O2"
    defaults.cmake_generator = "Unix Makefiles"
-   defaults.make = "make"
    defaults.platforms = { "unix" }
+   defaults.variables.MAKE = "make"
    defaults.variables.CC = "cc"
    defaults.variables.LD = "ld"
    defaults.variables.LIBFLAG = "-shared"
@@ -286,8 +286,8 @@ end
 
 if detected.freebsd then
    defaults.arch = "freebsd-"..proc
-   defaults.make = "gmake"
    defaults.platforms = {"unix", "bsd", "freebsd"}
+   defaults.variables.MAKE = "gmake"
    defaults.variables.CC = "gcc"
    defaults.variables.LD = "gcc"
    defaults.variables.LIBFLAG = "-shared"
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua
index 8b74a65f..50502257 100644
--- a/src/luarocks/command_line.lua
+++ b/src/luarocks/command_line.lua
@@ -15,9 +15,9 @@ local function die(message)
 
    local ok, err = pcall(util.run_scheduled_functions)
    if not ok then
-      print("\nLuaRocks "..cfg.program_version.." internal bug (please report at luarocks-developers@lists.luaforge.net):\n"..err)
+      util.printerr("\nLuaRocks "..cfg.program_version.." internal bug (please report at luarocks-developers@lists.luaforge.net):\n"..err)
    end
-   print("\nError: "..message)
+   util.printerr("\nError: "..message)
    os.exit(1)
 end
 
@@ -70,9 +70,9 @@ function run_command(...)
    local command
    
    if flags["version"] then
-      print(program_name.." "..cfg.program_version)
-      print(program_description)
-      print()
+      util.printout(program_name.." "..cfg.program_version)
+      util.printout(program_description)
+      util.printout()
       os.exit(0)
    elseif flags["help"] or #nonflags == 0 then
       command = "help"
diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua
index 26c43047..27062cbb 100644
--- a/src/luarocks/deps.lua
+++ b/src/luarocks/deps.lua
@@ -17,6 +17,7 @@ local cfg = require("luarocks.cfg")
 local manif_core = require("luarocks.manif_core")
 local path = require("luarocks.path")
 local dir = require("luarocks.dir")
+local util = require("luarocks.util")
 
 local operators = {
    ["=="] = "==",
@@ -135,7 +136,7 @@ function parse_version(vstring)
          -- extract a word
          token, rest = vstring:match("^(%a+)[%.%-%_]*(.*)")
          if not token then
-            print("Warning: version number '"..vstring.."' could not be parsed.")
+            util.printerr("Warning: version number '"..vstring.."' could not be parsed.")
             version[i] = 0
             break
          end
@@ -435,34 +436,34 @@ function fulfill_dependencies(rockspec)
    local matched, missing, no_upgrade = match_deps(rockspec)
 
    if next(no_upgrade) then
-      print("Missing dependencies for "..rockspec.name.." "..rockspec.version..":")
+      util.printerr("Missing dependencies for "..rockspec.name.." "..rockspec.version..":")
       for _, dep in pairs(no_upgrade) do
-         print(show_dep(dep))
+         util.printerr(show_dep(dep))
       end
       if next(missing) then
          for _, dep in pairs(missing) do
-            print(show_dep(dep))
+            util.printerr(show_dep(dep))
          end
       end
-      print()
+      util.printerr()
       for _, dep in pairs(no_upgrade) do
-         print("This version of "..rockspec.name.." is designed for use with")
-         print(show_dep(dep)..", but is configured to avoid upgrading it")
-         print("automatically. Please upgrade "..dep.name.." with")
-         print("   luarocks install "..dep.name)
-         print("or choose an older version of "..rockspec.name.." with")
-         print("   luarocks search "..rockspec.name)
+         util.printerr("This version of "..rockspec.name.." is designed for use with")
+         util.printerr(show_dep(dep)..", but is configured to avoid upgrading it")
+         util.printerr("automatically. Please upgrade "..dep.name.." with")
+         util.printerr("   luarocks install "..dep.name)
+         util.printerr("or choose an older version of "..rockspec.name.." with")
+         util.printerr("   luarocks search "..rockspec.name)
       end
       return nil, "Failed matching dependencies."
    end
 
    if next(missing) then
-      print()
-      print("Missing dependencies for "..rockspec.name..":")
+      util.printerr()
+      util.printerr("Missing dependencies for "..rockspec.name..":")
       for _, dep in pairs(missing) do
-         print(show_dep(dep))
+         util.printerr(show_dep(dep))
       end
-      print()
+      util.printerr()
 
       for _, dep in pairs(missing) do
          -- Double-check in case dependency was filled during recursion.
diff --git a/src/luarocks/download.lua b/src/luarocks/download.lua
index 79ff5394..6ae5f7af 100644
--- a/src/luarocks/download.lua
+++ b/src/luarocks/download.lua
@@ -72,10 +72,10 @@ function run(...)
          end
          return all_ok, any_err
       else
-         print("Multiple search results were returned.")
-         print()
-         print("Search results:")
-         print("---------------")
+         util.printerr("Multiple search results were returned.")
+         util.printout()
+         util.printout("Search results:")
+         util.printout("---------------")
          search.print_results(results)
          return nil, "Please narrow your query or use --all."
       end
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua
index c5769bac..0930cadb 100644
--- a/src/luarocks/fs/unix.lua
+++ b/src/luarocks/fs/unix.lua
@@ -10,6 +10,7 @@ local fs = require("luarocks.fs")
 local cfg = require("luarocks.cfg")
 local dir = require("luarocks.dir")
 local fs = require("luarocks.fs")
+local util = require("luarocks.util")
 
 math.randomseed(os.time())
 
@@ -103,7 +104,7 @@ function is_actual_binary(filename)
       local first = file:read()
       if not first then
          file:close()
-         print("Warning: could not read "..filename)
+         util.printerr("Warning: could not read "..filename)
          return false
       end
       if first:match("#!.*lua") then
diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua
index 5162b917..a2e64b5d 100644
--- a/src/luarocks/help.lua
+++ b/src/luarocks/help.lua
@@ -25,7 +25,7 @@ function run(...)
    local flags, command = util.parse_flags(...)
 
    if not command then
-      print([[
+      util.printout([[
 LuaRocks ]]..cfg.program_version..[[, a module deployment system for Lua
 
 ]]..program_name..[[ - ]]..program_description..[[
@@ -51,18 +51,18 @@ Supported commands:
       table.sort(names)
       for _, name in ipairs(names) do
          local command = commands[name]
-         print(name, command.help_summary)
+         util.printout(name, command.help_summary)
       end
    else
       command = command:gsub("-", "_")
       if commands[command] then
          local arguments = commands[command].help_arguments or "<argument>"
-         print()
-         print(program_name.." "..command.." "..arguments)
-         print()
-         print(command.." - "..commands[command].help_summary)
-         print()
-         print(commands[command].help)
+         util.printout()
+         util.printout(program_name.." "..command.." "..arguments)
+         util.printout()
+         util.printout(command.." - "..commands[command].help_summary)
+         util.printout()
+         util.printout(commands[command].help)
       else
          return nil, "Unknown command '"..command.."'"
       end
diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua
index 016e73bb..d7d87192 100644
--- a/src/luarocks/install.lua
+++ b/src/luarocks/install.lua
@@ -85,8 +85,8 @@ function install_binary_rock(rock_file)
    end
 
    local root_dir = path.root_dir(cfg.rocks_dir)
-   print()
-   print(name.." "..version.." is now installed in "..root_dir.." "..license)
+   util.printout()
+   util.printout(name.." "..version.." is now installed in "..root_dir.." "..license)
    
    util.remove_scheduled_function(rollback)
    return true
@@ -123,14 +123,14 @@ function run(...)
          return nil, err
       elseif type(results) == "string" then
          local url = results
-         print("Installing "..url.."...")
+         util.printout("Installing "..url.."...")
          return run(url)
       else
-         print()
-         print("Could not determine which rock to install.")
-         print()
-         print("Search results:")
-         print("---------------")
+         util.printout()
+         util.printerr("Could not determine which rock to install.")
+         util.printout()
+         util.printout("Search results:")
+         util.printout("---------------")
          search.print_results(results)
          return nil, (next(results) and "Please narrow your query." or "No results found.")
       end
diff --git a/src/luarocks/list.lua b/src/luarocks/list.lua
index 567ee188..1943f932 100644
--- a/src/luarocks/list.lua
+++ b/src/luarocks/list.lua
@@ -26,10 +26,10 @@ function run(...)
    for _, tree in ipairs(cfg.rocks_trees) do
       search.manifest_search(results, path.rocks_dir(tree), query)
    end
-   print()
-   print("Installed rocks:")
-   print("----------------")
-   print()
+   util.printout()
+   util.printout("Installed rocks:")
+   util.printout("----------------")
+   util.printout()
    search.print_results(results, false)
    return true
 end
diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua
index 258c45a2..c3cba55a 100644
--- a/src/luarocks/loader.lua
+++ b/src/luarocks/loader.lua
@@ -1,7 +1,7 @@
 
 local global_env = _G
-local package, require, assert, ipairs, pairs, os, print, table, type, next, unpack =
-      package, require, assert, ipairs, pairs, os, print, table, type, next, unpack
+local package, require, ipairs, pairs, table, type, next, unpack =
+      package, require, ipairs, pairs, table, type, next, unpack
 
 module("luarocks.loader")
 
@@ -64,14 +64,14 @@ function add_context(name, version)
          return nil
       end
       for _, dep in ipairs(pkgdeps) do
-         local package, constraints = dep.name, dep.constraints
+         local pkg, constraints = dep.name, dep.constraints
    
          for _, tree in pairs(rocks_trees) do
-            local entries = tree.manifest.repository[package]
+            local entries = tree.manifest.repository[pkg]
             if entries then
-               for version, packages in pairs(entries) do
+               for version, pkgs in pairs(entries) do
                   if (not constraints) or deps.match_constraints(deps.parse_version(version), constraints) then
-                     add_context(package, version)
+                     add_context(pkg, version)
                   end
                end
             end
diff --git a/src/luarocks/make_manifest.lua b/src/luarocks/make_manifest.lua
index 4701a45a..07d2fd05 100644
--- a/src/luarocks/make_manifest.lua
+++ b/src/luarocks/make_manifest.lua
@@ -6,6 +6,7 @@ module("luarocks.make_manifest", package.seeall)
 local manif = require("luarocks.manif")
 local index = require("luarocks.index")
 local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
 
 help_summary = "Compile a manifest file for a repository."
 
@@ -22,11 +23,11 @@ function run(repo)
    assert(type(repo) == "string" or not repo)
    repo = repo or cfg.rocks_dir
   
-   print("Making manifest for "..repo)
+   util.printout("Making manifest for "..repo)
    
    local ok, err = manif.make_manifest(repo)
    if ok then
-      print("Generating index.html for "..repo)
+      util.printout("Generating index.html for "..repo)
       index.make_index(repo)
    end
    return ok, err
diff --git a/src/luarocks/manif.lua b/src/luarocks/manif.lua
index a7211d70..8ce555d8 100644
--- a/src/luarocks/manif.lua
+++ b/src/luarocks/manif.lua
@@ -190,9 +190,9 @@ local function update_dependencies(manifest)
                if missing then
                   for miss, _ in pairs(missing) do
                      if miss == current then
-                        print("Tree inconsistency detected: "..current.." has no rockspec.")
+                        util.printerr("Tree inconsistency detected: "..current.." has no rockspec.")
                      else
-                        print("Missing dependency for "..pkg.." "..version..": "..miss)
+                        util.printerr("Missing dependency for "..pkg.." "..version..": "..miss)
                      end
                   end
                end
@@ -277,11 +277,11 @@ function update_manifest(name, version, repo)
    assert(type(version) == "string")
    repo = path.rocks_dir(repo or cfg.root_dir)
 
-   print("Updating manifest for "..repo)
+   util.printout("Updating manifest for "..repo)
 
    local manifest, err = load_manifest(repo)
    if not manifest then
-      print("No existing manifest. Attempting to rebuild...")
+      util.printerr("No existing manifest. Attempting to rebuild...")
       local ok, err = make_manifest(repo)
       if not ok then
          return nil, err
diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua
index 5546a649..e22bdc38 100644
--- a/src/luarocks/pack.lua
+++ b/src/luarocks/pack.lua
@@ -172,7 +172,7 @@ function run(...)
    if err then
       return nil, err
    else
-      print("Packed: "..file)
+      util.printout("Packed: "..file)
       return true
    end
 end
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua
index 3ff0db8a..83b84530 100644
--- a/src/luarocks/path.lua
+++ b/src/luarocks/path.lua
@@ -6,6 +6,7 @@ module("luarocks.path", package.seeall)
 
 local dir = require("luarocks.dir")
 local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
 
 help_summary = "Return the currently configured package path."
 help_arguments = ""
@@ -301,14 +302,15 @@ function versioned_name(file, prefix, name, version)
 end
 
 --- Driver function for "path" command.
+-- This platform distinction is not in fs to avoid depending on that module here.
 -- @return boolean This function always succeeds.
 function run(...)
    if cfg.is_platform("unix") then
-      print("export LUA_PATH='"..package.path.."'")
-      print("export LUA_CPATH='"..package.cpath.."'")
+      util.printout("export LUA_PATH='"..package.path.."'")
+      util.printout("export LUA_CPATH='"..package.cpath.."'")
    elseif cfg.is_platform("windows") then
-      print("SET LUA_PATH="..package.path)
-      print("SET LUA_CPATH="..package.cpath)
+      util.printout("SET LUA_PATH="..package.path)
+      util.printout("SET LUA_CPATH="..package.cpath)
    end
    return true
 end
diff --git a/src/luarocks/remove.lua b/src/luarocks/remove.lua
index d77f28fa..caa683ee 100644
--- a/src/luarocks/remove.lua
+++ b/src/luarocks/remove.lua
@@ -61,7 +61,7 @@ end
 local function delete_versions(name, versions) 
 
    for version, _ in pairs(versions) do
-      print("Removing "..name.." "..version.."...")
+      util.printout("Removing "..name.." "..version.."...")
       local ok, err = rep.delete_version(name, version)
       if not ok then return nil, err end
    end
@@ -98,19 +98,19 @@ function run(...)
       local version = next(versions)
       local second = next(versions, version)
       
-      print("Checking stability of dependencies on the absence of")
-      print(name.." "..table.concat(util.keys(versions), ", ").."...")
-      print()
+      util.printout("Checking stability of dependencies on the absence of")
+      util.printout(name.." "..table.concat(util.keys(versions), ", ").."...")
+      util.printout()
       
       local dependents = check_dependents(name, versions)
       
       if #dependents == 0 or flags["force"] then
          if #dependents > 0 then
-            print("The following packages may be broken by this forced removal:")
+            util.printerr("The following packages may be broken by this forced removal:")
             for _, dependent in ipairs(dependents) do
-               print(dependent.name.." "..dependent.version)
+               util.printerr(dependent.name.." "..dependent.version)
             end
-            print()
+            util.printerr()
          end
          local ok, err = delete_versions(name, versions)
          if not ok then return nil, err end
@@ -118,19 +118,20 @@ function run(...)
          if not ok then return nil, err end
       else
          if not second then
-            print("Will not remove "..name.." "..version..".")
-            print("Removing it would break dependencies for: ")
+            util.printerr("Will not remove "..name.." "..version..".")
+            util.printerr("Removing it would break dependencies for: ")
          else
-            print("Will not remove all versions of "..name..".")
-            print("Removing them would break dependencies for: ")
+            util.printerr("Will not remove all versions of "..name..".")
+            util.printerr("Removing them would break dependencies for: ")
          end
          for _, dependent in ipairs(dependents) do
-            print(dependent.name.." "..dependent.version)
+            util.printerr(dependent.name.." "..dependent.version)
          end
-         print()
-         print("Use --force to force removal (warning: this may break modules).")
+         util.printerr()
+         util.printerr("Use --force to force removal (warning: this may break modules).")
          return nil, "Failed removing."
       end
    end
+   util.printout("Removal successful.")
    return true
 end
diff --git a/src/luarocks/rep.lua b/src/luarocks/rep.lua
index 322ab166..8bbc1b52 100644
--- a/src/luarocks/rep.lua
+++ b/src/luarocks/rep.lua
@@ -153,7 +153,7 @@ function run_hook(rockspec, hook_name)
    end
    local hook = hooks[hook_name]
    if hook then
-      print(hook)
+      util.printout(hook)
       if not fs.execute(hook) then
          return nil, "Failed running "..hook_name.." hook."
       end
diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua
index f8188ee5..493c2f45 100644
--- a/src/luarocks/search.lua
+++ b/src/luarocks/search.lua
@@ -292,17 +292,17 @@ function print_results(results, show_repo, long)
    show_repo = true -- show_repo == nil and true or show_repo
    
    for package, versions in util.sortedpairs(results) do
-      print(package)
+      util.printout(package)
       for version, repos in util.sortedpairs(versions, deps.compare_versions) do
          if show_repo then
             for _, repo in ipairs(repos) do
-               print("   "..version.." ("..repo.arch..") - "..repo.repo)
+               util.printout("   "..version.." ("..repo.arch..") - "..repo.repo)
             end
          else
-            print("   "..version)
+            util.printout("   "..version)
          end
       end
-      print()
+      util.printout()
    end
 end
 
@@ -348,10 +348,10 @@ function act_on_src_or_rockspec(action, name, version)
    if type(results) == "string" then
       return action(results)
    elseif type(results) == "table" and next(results) then
-      print("Multiple search results were returned.")
-      print()
-      print("Search results:")
-      print("---------------")
+      util.printout("Multiple search results were returned.")
+      util.printout()
+      util.printout("Search results:")
+      util.printout("---------------")
       print_results(results)
       return nil, "Please narrow your query."
    else
@@ -381,21 +381,21 @@ function run(...)
    if not results then
       return nil, err
    end
-   print()
-   print("Search results:")
-   print("===============")
-   print()
+   util.printout()
+   util.printout("Search results:")
+   util.printout("===============")
+   util.printout()
    local sources, binaries = split_source_and_binary_results(results)
    if next(sources) and not flags["binary"] then
-      print("Rockspecs and source rocks:")
-      print("---------------------------")
-      print()
+      util.printout("Rockspecs and source rocks:")
+      util.printout("---------------------------")
+      util.printout()
       print_results(sources, true)
    end
    if next(binaries) and not flags["source"] then    
-      print("Binary and pure-Lua rocks:")
-      print("--------------------------")
-      print()
+      util.printout("Binary and pure-Lua rocks:")
+      util.printout("--------------------------")
+      util.printout()
       print_results(binaries, true)
    end
    return true
diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua
index 34837c19..f968bec8 100644
--- a/src/luarocks/show.lua
+++ b/src/luarocks/show.lua
@@ -99,39 +99,39 @@ function run(...)
    if not manifest then return nil,err end
    local minfo = manifest.repository[name][version][1]
 
-   if flags["tree"] then print(repo)
-   elseif flags["rock-dir"] then print(directory)
-   elseif flags["home"] then print(descript.homepage)
-   elseif flags["modules"] then print(keys_as_string(minfo.modules))
-   elseif flags["deps"] then print(keys_as_string(minfo.dependencies))
-   elseif flags["rockspec"] then print(rockspec_file)
-   elseif flags["mversion"] then print(version)
+   if flags["tree"] then util.printout(repo)
+   elseif flags["rock-dir"] then util.printout(directory)
+   elseif flags["home"] then util.printout(descript.homepage)
+   elseif flags["modules"] then util.printout(keys_as_string(minfo.modules))
+   elseif flags["deps"] then util.printout(keys_as_string(minfo.dependencies))
+   elseif flags["rockspec"] then util.printout(rockspec_file)
+   elseif flags["mversion"] then util.printout(version)
    else
-      print()
-      print(rockspec.package.." "..rockspec.version.." - "..descript.summary)
-      print()
+      util.printout()
+      util.printout(rockspec.package.." "..rockspec.version.." - "..descript.summary)
+      util.printout()
       if descript.detailed then
-         print(format_text(descript.detailed))
-         print()
+         util.printout(format_text(descript.detailed))
+         util.printout()
       end
       if descript.license then
-         print("License: ", descript.license)
+         util.printout("License: ", descript.license)
       end
       if descript.homepage then
-         print("Homepage: ", descript.homepage)
+         util.printout("Homepage: ", descript.homepage)
       end
-      print("Installed in: ", repo)
+      util.printout("Installed in: ", repo)
       if next(minfo.modules) then
-         print()
-         print("Modules:")
-         print("\t"..keys_as_string(minfo.modules, "\n\t"))
+         util.printout()
+         util.printout("Modules:")
+         util.printout("\t"..keys_as_string(minfo.modules, "\n\t"))
       end
       if next(minfo.dependencies) then
-         print()
-         print("Depends on:")
-         print("\t"..keys_as_string(minfo.dependencies, "\n\t"))
+         util.printout()
+         util.printout("Depends on:")
+         util.printout("\t"..keys_as_string(minfo.dependencies, "\n\t"))
       end
-      print()
+      util.printout()
    end
    return true
 end
diff --git a/src/luarocks/tools/tar.lua b/src/luarocks/tools/tar.lua
index 0a3e07a4..e47172fa 100644
--- a/src/luarocks/tools/tar.lua
+++ b/src/luarocks/tools/tar.lua
@@ -3,6 +3,7 @@ module("luarocks.tools.tar", package.seeall)
 
 local fs = require("luarocks.fs")
 local dir = require("luarocks.dir")
+local util = require("luarocks.util")
 
 local blocksize = 512
 
@@ -96,7 +97,7 @@ function untar(filename, destdir)
       if not block then break end
       local header, err = read_header_block(block)
       if not header then
-         print(err)
+         util.printerr(err)
       end
 
       local file_data = tar_handle:read(math.ceil(header.size / blocksize) * blocksize):sub(1,header.size)
@@ -131,12 +132,11 @@ function untar(filename, destdir)
             fs.chmod(pathname, header.mode)
          end
       end
-      print(pathname)
       --[[
       for k,v in pairs(header) do
-         print("[\""..tostring(k).."\"] = "..(type(v)=="number" and v or "\""..v:gsub("%z", "\\0").."\""))
+         util.printout("[\""..tostring(k).."\"] = "..(type(v)=="number" and v or "\""..v:gsub("%z", "\\0").."\""))
       end
-      print()
+      util.printout()
       --]]
    end
    return true
diff --git a/src/luarocks/tools/zip.lua b/src/luarocks/tools/zip.lua
index 36adc7e2..19f6af85 100644
--- a/src/luarocks/tools/zip.lua
+++ b/src/luarocks/tools/zip.lua
@@ -116,11 +116,13 @@ local function zipwriter_add(self, file)
    if ok then
       local buf = fin:read("*a")
       if not buf then
-         break
-      end
-      ok = self:write_file_in_zip(buf)
-      if not ok then
-         err = "error in writing "..file.." in the zipfile"
+         err = "error reading "..file
+         ok = false
+      else
+         ok = self:write_file_in_zip(buf)
+         if not ok then
+            err = "error in writing "..file.." in the zipfile"
+         end
       end
    end
    if fin then
diff --git a/src/luarocks/unpack.lua b/src/luarocks/unpack.lua
index c73264d0..dacafa31 100644
--- a/src/luarocks/unpack.lua
+++ b/src/luarocks/unpack.lua
@@ -116,10 +116,10 @@ local function run_unpacker(file)
             return nil, "Failed copying unpacked rockspec into unpacked source directory."
          end
       end
-      print()   
-      print("Done. You may now enter directory ")
-      print(dir.path(dir_name, rockspec.source.dir))
-      print("and type 'luarocks make' to build.")
+      util.printout()   
+      util.printout("Done. You may now enter directory ")
+      util.printout(dir.path(dir_name, rockspec.source.dir))
+      util.printout("and type 'luarocks make' to build.")
    end
    util.remove_scheduled_function(rollback)
    return true
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua
index e0c01421..0ff36f3e 100644
--- a/src/luarocks/util.lua
+++ b/src/luarocks/util.lua
@@ -124,12 +124,6 @@ end
 
 local var_format_pattern = "%$%((%a[%a%d_]+)%)"
 
---- Display a warning message.
--- @param msg string: the warning message
-function warning(msg)
-   print("Warning: "..msg)
-end
-
 --- Create a new shallow copy of a table: a new table with
 -- the same keys and values. Keys point to the same objects as
 -- the original table (ie, does not copy recursively).
@@ -257,6 +251,24 @@ function starts_with(s, prefix)
    return s:sub(1,#prefix) == prefix
 end
 
+--- Print a line to standard output
+function printout(...)
+   io.stdout:write(table.concat({...},"\t"))
+   io.stdout:write("\n")
+end
+
+--- Print a line to standard error
+function printerr(...)
+   io.stdout:write(table.concat({...},"\t"))
+   io.stderr:write("\n")
+end
+
+--- Display a warning message.
+-- @param msg string: the warning message
+function warning(msg)
+   printerr("Warning: "..msg)
+end
+
 -- from http://lua-users.org/wiki/SplitJoin
 -- by PhilippeLhoste
 function split_string(str, delim, maxNb)
@@ -302,7 +314,7 @@ which logically are exactly not equivalent to the original code.
 This routine can serve for pretty formating tables with
 proper indentations, apart from printing them:
 
-print(table.show(t, "t"))   -- a typical use
+io.write(table.show(t, "t"))   -- a typical use
 
 Heavily based on "Saving tables with cycles", PIL2, p. 113.
 
diff --git a/src/luarocks/validate.lua b/src/luarocks/validate.lua
index ef402988..fbffadc7 100644
--- a/src/luarocks/validate.lua
+++ b/src/luarocks/validate.lua
@@ -51,7 +51,7 @@ end
 local function validate_rockspec(file)
    local ok, err, errcode = build.build_rockspec(file, true)
    if not ok then
-      print(err)
+      util.printerr(err)
    end
    return ok, err, errcode
 end
@@ -59,7 +59,7 @@ end
 local function validate_src_rock(file)
    local ok, err, errcode = build.build_rock(file, false)
    if not ok then
-      print(err)
+      util.printerr(err)
    end
    return ok, err, errcode
 end
@@ -67,7 +67,7 @@ end
 local function validate_rock(file)
    local ok, err, errcode = install.install_binary_rock(file)
    if not ok then
-      print(err)
+      util.printerr(err)
    end
    return ok, err, errcode
 end
@@ -93,8 +93,8 @@ local function validate(repo, flags)
          sandbox = prepare_sandbox(file)
       end
       local ok, err, errcode
-      print()
-      print("Verifying "..pathname)      
+      util.printout()
+      util.printout("Verifying "..pathname)
       if file:match("%.rockspec$") then
          ok, err, errcode = validate_rockspec(pathname)
       elseif file:match("%.src%.rock$") then
@@ -123,32 +123,32 @@ local function validate(repo, flags)
       fs.delete(sandbox)
    end
    restore_settings(settings)
-   print()
-   print("Results:")
-   print("--------")
-   print("OK: "..tostring(#results.ok))
+   util.printout()
+   util.printout("Results:")
+   util.printout("--------")
+   util.printout("OK: "..tostring(#results.ok))
    for _, entry in ipairs(results.ok) do
-      print(entry.file)
+      util.printout(entry.file)
    end
    for errcode, errors in pairs(results) do
       if errcode ~= "ok" then
-         print()
-         print(errcode.." errors: "..tostring(#errors))
+         util.printout()
+         util.printout(errcode.." errors: "..tostring(#errors))
          for _, entry in ipairs(errors) do
-            print(entry.file, entry.err)
+            util.printout(entry.file, entry.err)
          end
       end
    end
 
-   print()
-   print("Summary:")
-   print("--------")
+   util.printout()
+   util.printout("Summary:")
+   util.printout("--------")
    local total = 0
    for errcode, errors in pairs(results) do
-      print(errcode..": "..tostring(#errors))
+      util.printout(errcode..": "..tostring(#errors))
       total = total + #errors
    end
-   print("Total: "..total)
+   util.printout("Total: "..total)
    return true
 end
 
@@ -156,7 +156,7 @@ function run(...)
    local flags, repo = util.parse_flags(...)
    repo = repo or cfg.rocks_dir
 
-   print("Verifying contents of "..repo)
+   util.printout("Verifying contents of "..repo)
 
    return validate(repo, flags)
 end
-- 
cgit v1.2.3-55-g6feb