summaryrefslogtreecommitdiff
path: root/graphics/mol2chemfig/lua/mol2chemfig_client_1_2.lua
blob: fabf68cce9c2f47d8a6e29b0651079f2c9a467b4 (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
113
114
115
116
117
-- Command-line-driven web client for mol2chemfig.
local os = require("os")
local io = require("io")

local json = require("json")
require("rpc") -- modifies json in place to add rpc namespace

local progname = arg[0]
local client_version = "1.2"

server = json.rpc.proxy(server_address)
local server_info, error = server.info({progname, client_version})

if error then
    io.stderr:write("Error: ", error)
    os.exit()
end

-- if we get here, the server responds as expected.

function printhelp()
    print (server_info['help_text'])
    os.exit()
end

-- Split input into options and arguments. Only arguments
-- at the end are accepted.

local arguments = {}
local i = #arg

if i == 0 then  -- no input at all
    printhelp()
end

noargs = server_info['noarg_shortopts']

-- separate options and arguments
while i > 0 and arg[i]:sub(1,1) ~= '-' do
    local j = i - 1

    if j == 0 or
              arg[j]:sub(1,1) ~= '-' or
              arg[j]:sub(2,2) == '-' or
              string.find(noargs, arg[j]:sub(2,2))
    then
        table.insert(arguments,#arguments+1,arg[i])
        table.remove(arg, i)
    end

    i = j
end

-- what remains in arg after removing arguments are options
local user_options = table.concat(arg, ' ')


-- first, see whether the user requests help or the program version.
local version_opts = server_info['version_opts']
local s

for i,s in ipairs(version_opts) do
    if string.find(user_options, s) then
        print(server_info['version_text'])
        print("On your system, the client is installed in: " .. progname)
        os.exit()
    end
end

local help_opts = server_info['help_opts']

for i,s in ipairs(help_opts) do
    if string.find(user_options, s) then
        printhelp()
    end
end

-- if we get here, the user should have provided exactly one argument.

if #arguments ~= 1 then
    print ('Please provide exactly one argument (must come last, after any options)')
    os.exit()
end

-- have exactly one argument. read it directly, or treat it as a file name?

local direct_input = false
local direct_opts = server_info['direct_opts']

for i,s in ipairs(direct_opts) do
    if string.find(user_options, s) then
        direct_input = true
    end
end

local data

if direct_input == false then
    f = io.open(arguments[1])
    if f then
        data = f:read("*all")
    else
        print ("File '" .. arguments[1] .. "' not found")
        os.exit()
    end
else
    data = arguments[1]
end

-- process options and data
local result, error = server.process({progname, user_options, data})

if error then
    io.stderr:write(error .. '\n')
else
    print(result)
end