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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/*! The primary interface of this library is meant to expose a very
simple command-reply model for frontends, and to allow gradual
addition of more advanced functionality. For now, only basic
functionality exists.

Using Rink as a library for uses other than simple unit conversion
tools is not currently well supported, and if you wish to do so,
please make issues for any problems you have.

There are currently a number of hardcoded `println!`s and `unwrap()`s
because most of this code was written in a day without much thought
towards making it into a library.

For basic flow of the library, the `bins/` directory has a few
examples. The REPL is very simple, and the IRC bot shows how Rink can
work in a multi-user environment without sessions or meaningful
stdin/stdout.

## Example

```rust
use rink::*;

let mut ctx = load().unwrap();
println!("{}", one_line(&mut ctx, "kWh / year -> W").unwrap());
```
*/

#![cfg_attr(feature = "nightly", feature(proc_macro))]

extern crate gmp;
extern crate chrono;
extern crate strsim;
extern crate chrono_tz;
#[cfg(feature = "chrono-humanize")]
extern crate chrono_humanize;
#[cfg(feature = "sandbox")]
extern crate libc;
#[cfg(feature = "sandbox")]
extern crate ipc_channel;
#[cfg(feature = "currency")]
extern crate hyper;
#[cfg(feature = "currency")]
extern crate hyper_native_tls;
#[cfg(feature = "currency")]
extern crate xml;
#[cfg(feature = "currency")]
extern crate json;
#[cfg(feature = "nightly")]
extern crate serde;
#[cfg(feature = "nightly")]
#[macro_use]
extern crate serde_derive;

pub mod text_query;
pub mod context;
pub mod eval;
pub mod num;
pub mod number;
pub mod date;
pub mod factorize;
pub mod gnu_units;
pub mod ast;
pub mod value;
pub mod reply;
pub mod search;
pub mod load;
pub mod substance;
pub mod formula;
#[cfg(feature = "currency")]
pub mod currency;
#[cfg(feature = "currency")]
pub mod btc;

pub use number::Number;
pub use context::Context;
pub use value::Value;

use std::env;
use std::convert::From;
use std::path::PathBuf;
use std::collections::BTreeMap;
use std::fs::File;
use std::time::Duration;

const DATA_FILE_URL: &'static str = "https://raw.githubusercontent.com/tiffany352/rink-rs/master/definitions.units";

#[cfg(target_os = "linux")]
pub fn config_dir() -> Result<PathBuf, String> {
    env::var("XDG_CONFIG_HOME")
        .map(From::from)
        .or_else(|_| {
            env::home_dir()
                .ok_or("Home dir not present".to_owned())
                .map(From::from)
                .map(|mut x: PathBuf| { x.push(".config/"); x })
        })
}

#[cfg(target_os = "windows")]
pub fn config_dir() -> Result<PathBuf, String> {
    env::var("APPDATA")
        .map(From::from)
        .or_else(|_| {
            env::home_dir()
                .ok_or("Home dir not present".to_owned())
                .map(From::from)
                .map(|mut x: PathBuf| { x.push("AppData\\Roaming"); x })
        })
}

#[cfg(target_os = "macos")]
pub fn config_dir() -> Result<PathBuf, String> {
    env::home_dir()
        .ok_or("Home dir not present".to_owned())
        .map(From::from)
        .map(|mut x: PathBuf| { x.push("Library/Application Support"); x})
}

#[cfg(feature = "currency")]
fn load_currency() -> Option<Result<ast::Defs, String>> {
    Some(currency::load())
}

#[cfg(not(feature = "currency"))]
fn load_currency() -> Option<Result<ast::Defs, String>> {
    None
}

#[cfg(feature = "currency")]
fn load_btc() -> Option<Result<ast::Defs, String>> {
    Some(btc::load())
}

#[cfg(not(feature = "currency"))]
fn load_btc() -> Option<Result<ast::Defs, String>> {
    None
}

#[cfg(feature = "gpl")]
static DEFAULT_FILE: Option<&'static str> = Some(include_str!("../definitions.units"));
#[cfg(not(feature = "gpl"))]
static DEFAULT_FILE: Option<&'static str> = None;

static DATES_FILE: &'static str = include_str!("../datepatterns.txt");
static CURRENCY_FILE: &'static str = include_str!("../currency.units");

/// Creates a context by searching standard directories for definitions.units.
pub fn load() -> Result<Context, String> {
    use std::io::Read;
    use std::fs::File;
    use std::path::Path;

    let mut path = try!(config_dir());
    path.push("rink/");
    let load = |name| {
        File::open(name)
        .and_then(|mut f| {
            let mut buf = vec![];
            try!(f.read_to_end(&mut buf));
            Ok(String::from_utf8_lossy(&*buf).into_owned())
        })
    };
    let units =
        load(Path::new("definitions.units").to_path_buf())
        .or_else(|_| load(path.join("definitions.units")))
        .or_else(|_| DEFAULT_FILE.map(|x| x.to_owned()).ok_or(format!(
            "Did not exist in search path and binary is not compiled with `gpl` feature")))
        .map_err(|e| format!(
            "Failed to open definitions.units: {}\n\
             If you installed with `gpl` disabled, then you need to obtain definitions.units \
             separately. Here is the URL, download it and put it in {:?}.\n\
             \n\
             {}\n\
             \n",
            e, &path, DATA_FILE_URL));
    let units = try!(units);
    let dates =
        load(Path::new("datepatterns.txt").to_path_buf())
        .or_else(|_| load(path.join("datepatterns.txt")))
        .unwrap_or_else(|_| DATES_FILE.to_owned());

    let mut iter = gnu_units::TokenIterator::new(&*units).peekable();
    let units = gnu_units::parse(&mut iter);
    let dates = date::parse_datefile(&*dates);
    let ecb = load_currency();
    let btc = load_btc();
    let currency_defs = {
        let defs = load(Path::new("currency.units").to_path_buf())
            .or_else(|_| load(path.join("currency.units")))
            .unwrap_or_else(|_| CURRENCY_FILE.to_owned());
        let mut iter = gnu_units::TokenIterator::new(&*defs).peekable();
        let currency = gnu_units::parse(&mut iter);
        currency
    };
    let currency = {
        let mut defs = vec![];
        if let Some(Ok(mut ecb)) = ecb {
            defs.append(&mut ecb.defs)
        } else if let Some(Err(e)) = ecb {
            println!("Failed to load ECB currency data: {}", e);
        }
        if let Some(Ok(mut btc)) = btc {
            defs.append(&mut btc.defs)
        } else if let Some(Err(e)) = btc {
            println!("Failed to load BTC currency data: {}", e);
        }
        let mut currency_defs = currency_defs;
        defs.append(&mut currency_defs.defs);
        ast::Defs {
            defs: defs
        }
    };

    let mut ctx = context::Context::new();
    ctx.load(units);
    ctx.load_dates(dates);
    ctx.load(currency);
    Ok(ctx)
}

/// Evaluates a single line within a context.
pub fn one_line(ctx: &mut Context, line: &str) -> Result<String, String> {
    let mut iter = text_query::TokenIterator::new(line.trim()).peekable();
    let expr = text_query::parse_query(&mut iter);
    let res = ctx.eval_outer(&expr);
    res.as_ref().map(ToString::to_string).map_err(ToString::to_string)
}

#[cfg(feature = "sandbox")]
pub fn one_line_sandbox(line: &str) -> String {
    use libc;
    use std::io::Error;
    use ipc_channel::ipc::{IpcOneShotServer, IpcSender};

    pub unsafe fn fork<F: FnOnce()>(child_func: F) -> libc::pid_t {
        match libc::fork() {
            -1 => panic!("Fork failed: {}", Error::last_os_error()),
            0 => { child_func(); unreachable!() },
            pid => pid,
        }
    }

    println!("Executing query: {}", line);

    let (server, server_name) = IpcOneShotServer::new().unwrap();

    let child = || {
        println!("Child connecting..");
        let tx = IpcSender::connect(server_name).unwrap();
        println!("Child connected");

        tx.send("".to_owned()).unwrap();

        unsafe {
            let limit = libc::rlimit {
                // 100 megabytes
                rlim_cur: 100_000_000,
                rlim_max: 100_000_000,
            };
            let res = libc::setrlimit(libc::RLIMIT_AS, &limit);
            if res == -1 {
                panic!("Setrlimit RLIMIT_AS failed: {}", Error::last_os_error())
            }
            let limit = libc::rlimit {
                // 15 seconds
                rlim_cur: 15,
                rlim_max: 15
            };
            let res = libc::setrlimit(libc::RLIMIT_CPU, &limit);
            if res == -1 {
                panic!("Setrlimit RLIMIT_AS failed: {}", Error::last_os_error())
            }
        }

        let mut ctx = load().unwrap();
        ctx.short_output = true;
        let reply = match one_line(&mut ctx, line) {
            Ok(v) => v,
            Err(e) => e
        };
        tx.send(reply).unwrap();
        println!("Wrote reply");

        ::std::process::exit(0)
    };

    let pid = unsafe { fork(child) };

    println!("Child started");

    let (rx, _) = server.accept().unwrap();

    println!("Child accepted");

    let status = unsafe {
        let mut status = 0;
        let res = libc::waitpid(pid, &mut status, 0);
        if res == -1 {
            panic!("Waitpid failed: {}", Error::last_os_error())
        }
        if libc::WIFEXITED(status) {
            println!("Child exited normally with status {}", libc::WEXITSTATUS(status));
        }
        if libc::WIFSIGNALED(status) {
            println!("Child was killed by signal {}", libc::WTERMSIG(status));
        }
        status
    };

    let res = match rx.try_recv() {
        Ok(res) => res,
        Err(_) if unsafe { libc::WIFSIGNALED(status) && libc::WTERMSIG(status) == libc::SIGXCPU } =>
            format!("Calculation timed out"),
        // :(
        Err(ref e) if format!("{}", e) == "IoError: Connection reset by peer (os error 104)" =>
            format!("Calculation ran out of memory"),
        Err(e) => format!("{}", e)
    };

    println!("Calculation result: {:?}", res);

    res
}

fn btree_merge<K: ::std::cmp::Ord+Clone, V:Clone, F:Fn(&V, &V) -> Option<V>>(
    left: &BTreeMap<K, V>, right: &BTreeMap<K, V>, merge_func: F
) -> BTreeMap<K, V> {
    let mut res = BTreeMap::new();
    let mut a = left.iter().peekable();
    let mut b = right.iter().peekable();
    loop {
        match (a.peek().cloned(), b.peek().cloned()) {
            (Some((akey, aval)), Some((bkey, bval))) if akey == bkey => {
                if let Some(v) = merge_func(aval, bval) {
                    res.insert(akey.clone(), v);
                }
                a.next();
                b.next();
            },
            (Some((akey, _)), Some((bkey, bval))) if akey > bkey => {
                res.insert(bkey.clone(), bval.clone());
                b.next();
            },
            (Some((akey, aval)), Some((bkey, _))) if akey < bkey => {
                res.insert(akey.clone(), aval.clone());
                a.next();
            },
            (Some(_), Some(_)) => panic!(),
            (None, Some((bkey, bval))) => {
                res.insert(bkey.clone(), bval.clone());
                b.next();
            },
            (Some((akey, aval)), None) => {
                res.insert(akey.clone(), aval.clone());
                a.next();
            },
            (None, None) => break,
        }
    }
    res
}

#[cfg(feature = "hyper")]
fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
    use std::fmt::Display;
    use std::time::SystemTime;
    use std::fs;
    use std::io::{Read, Write};
    use hyper::Client;
    use hyper::status::StatusCode;
    use hyper::net::HttpsConnector;
    use hyper_native_tls::NativeTlsClient;

    fn ts<T:Display>(x: T) -> String {
        format!("{}", x)
    }
    let mut path = try!(config_dir());
    path.push("rink/");
    let mut tmppath = path.clone();
    path.push(file);
    let tmpfile = format!("{}.part", file);
    tmppath.push(tmpfile);

    File::open(path.clone())
        .map_err(ts)
        .and_then(|f| {
            let stats = try!(f.metadata().map_err(ts));
            let mtime = try!(stats.modified().map_err(ts));
            let now = SystemTime::now();
            let elapsed = try!(now.duration_since(mtime).map_err(ts));
            if elapsed > expiration {
                Err(format!("File is out of date"))
            } else {
                Ok(f)
            }
        })
        .or_else(|_| {
            try!(fs::create_dir_all(path.parent().unwrap()).map_err(|x| format!("{}", x)));
            let mut f = try!(File::create(tmppath.clone()).map_err(|x| format!("{}", x)));

            let ssl = NativeTlsClient::new().unwrap();
            let connector = HttpsConnector::new(ssl);
            let client = Client::with_connector(connector);
            let mut res = try!(client.get(url).send().map_err(|x| format!("{}", x)));
            if res.status != StatusCode::Ok {
                return Err(format!("Request failed with status code {}", res.status))
            }
            let mut buf = vec![0; 8192];
            loop {
                match res.read(&mut buf) {
                    Ok(0) => break,
                    Ok(n) => {
                        try!(f.write(&buf[..n]).map_err(|x| format!("{}", x)));
                    },
                    Err(e) => return Err(format!("{}", e))
                }
            }
            try!(f.sync_all().map_err(|x| format!("{}", x)));
            drop(f);
            try!(fs::rename(tmppath.clone(), path.clone())
                 .map_err(|x| format!("{}", x)));
            File::open(path).map_err(|x| format!("{}", x))
        })
}