diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/sh/htc.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/sh/htc.cc')
| -rw-r--r-- | src/sh/htc.cc | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/sh/htc.cc b/src/sh/htc.cc new file mode 100644 index 0000000..9a87f2c --- /dev/null +++ b/src/sh/htc.cc | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <sh/htc.h> | ||
| 21 | #include <hdb/buffer.h> | ||
| 22 | #include <mt/lock.h> | ||
| 23 | |||
| 24 | const mstring RCPATH=htchome()+sref("/htc/htc.rc"); | ||
| 25 | const mstring left="{",right="}"; | ||
| 26 | static fp_stream mout(stdout),merr(stderr); | ||
| 27 | |||
| 28 | static int unbalanced(const sref& in) | ||
| 29 | { | ||
| 30 | static const charset tag("{[("); | ||
| 31 | if(in.empty()) return 1; | ||
| 32 | sref s=in.right_first_of(tag); | ||
| 33 | while(s.nempty()) { | ||
| 34 | int p=-1; | ||
| 35 | switch(*s) { | ||
| 36 | case '{': p=s.skip(cset_braces); break; | ||
| 37 | case '[': p=s.skip(cset_indices); break; | ||
| 38 | case '(': p=s.skip(cset_para); break; | ||
| 39 | } | ||
| 40 | if(p<0) return 1; | ||
| 41 | s=s.adv(p).right_first_of(tag); | ||
| 42 | } | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | main(int argc,char* argv[]) try | ||
| 47 | { | ||
| 48 | NODELOCK(USERMAP); | ||
| 49 | HTC htc(ms_empty); | ||
| 50 | htc_user user(htc.toks); | ||
| 51 | htc_req req(htc,user.toks); req.uri=Cwd(); | ||
| 52 | Env env(htc.ops,req,XSYSTEM); | ||
| 53 | if(argc>1) { | ||
| 54 | for(int i=2;i<argc;i++) env.xpush(left+sref(argv[i])+right); | ||
| 55 | mstring buffer; LoadBuffer(buffer,argv[1]); | ||
| 56 | if(*buffer=='#') buffer.erase(0,buffer.find('\n')+1); | ||
| 57 | env.eval(mout,buffer,1); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | mstring buffer,line; | ||
| 61 | if(buffer.load(RCPATH.c_str())>=0) { | ||
| 62 | Env e(env,user.toks,RCPATH); | ||
| 63 | mstream m; | ||
| 64 | e.parse(m,buffer); | ||
| 65 | } | ||
| 66 | while(!feof(stdin)) { | ||
| 67 | merr<<"htc>"; | ||
| 68 | buffer.clear(); | ||
| 69 | while(!feof(stdin)&&unbalanced(buffer)) { | ||
| 70 | getline(line,stdin); | ||
| 71 | buffer.append(line); | ||
| 72 | buffer+='\n'; | ||
| 73 | } | ||
| 74 | try { | ||
| 75 | env.parse(mout,buffer); | ||
| 76 | mout<<"\n"; | ||
| 77 | } | ||
| 78 | catch(const merror_t& e) { | ||
| 79 | merr<<e.desc<<"\n"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | catch(const merror_t& e) { | ||
| 86 | merr<<"ERROR: ["<<e.desc<<"]\n"; | ||
| 87 | return -1; | ||
| 88 | } | ||
| 89 | catch(...) { | ||
| 90 | merr<<"ERROR: [FATAL]\n"; | ||
| 91 | return -1; | ||
| 92 | } | ||
