MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | // MorgenGrauen MUDlib |
| 2 | // |
| 3 | // util/pager.c -- generic pager |
| 4 | // |
| 5 | // $Id: pager.c 8755 2014-04-26 13:13:40Z Zesstra $ |
| 6 | |
| 7 | #pragma strong_types |
| 8 | #pragma save_types |
| 9 | #pragma pedantic |
| 10 | #pragma range_check |
| 11 | #pragma no_clone |
| 12 | |
| 13 | inherit "/std/util/input"; |
| 14 | |
| 15 | #include <pager.h> |
| 16 | #include <sys_debug.h> |
| 17 | #include <wizlevels.h> |
| 18 | |
| 19 | string prompt(mixed pinfo, string add) |
| 20 | { |
| 21 | return sprintf(MSG_PROMPT+"%s", stringp(add)?add:""); |
| 22 | } |
| 23 | |
| 24 | string fread(mixed pinfo, int begin, int c) |
| 25 | { |
| 26 | if(begin > pinfo[MAXL] || begin < 1) return 0; |
| 27 | if(pinfo[FILE]) return read_file(pinfo[TEXT], begin, c); |
| 28 | else |
| 29 | { |
| 30 | int start, end, l, x; |
| 31 | |
| 32 | if(member(pinfo[JUNK], begin)) |
| 33 | { |
| 34 | start = pinfo[JUNK][begin]; |
| 35 | if(!(end = pinfo[JUNK][begin+c])) |
| 36 | end = sizeof(pinfo[TEXT]); |
| 37 | else end--; |
| 38 | return pinfo[TEXT][start..end]; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | mixed call(closure ctrl, mixed ctrlargs) |
| 45 | { |
| 46 | if (!ctrl||!closurep(ctrl)) return 0; |
| 47 | if(!pointerp(ctrlargs)) return funcall(ctrl); |
| 48 | return apply(ctrl, ctrlargs); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | varargs int eval_command(mixed in, mixed pinfo) |
| 53 | { |
| 54 | string cprompt, tmp; |
| 55 | cprompt = ""; |
| 56 | if(in == "q" || in == "x") |
| 57 | { |
| 58 | if(closurep(pinfo[CTRL])) apply(pinfo[CTRL], pinfo[CARG]); |
| 59 | return 1; |
| 60 | } |
| 61 | else |
| 62 | if(!in || (stringp(in) && !sizeof(in))) pinfo[CURL] += pinfo[PAGE]; |
| 63 | else if(in != -1) return 0; |
| 64 | |
| 65 | if(pinfo[CURL] >= pinfo[MAXL]) |
| 66 | return (call(pinfo[CTRL], pinfo[CARG]), 1); |
| 67 | if(pinfo[CURL] <= 0) pinfo[CURL] = 1; |
| 68 | if(pinfo[CURL] == 1) cprompt = MSG_TOP; |
| 69 | if(pinfo[CURL] + pinfo[PAGE] >= pinfo[MAXL]) cprompt = MSG_BOTTOM; |
| 70 | if(!tmp = fread(pinfo, pinfo[CURL], pinfo[PAGE])) |
| 71 | return 0; /* (write(MSG_OOPS+"\n"), 0); */ |
| 72 | if (query_once_interactive(this_object())) |
| 73 | tell_object(this_object(), tmp); |
| 74 | else write(tmp); |
| 75 | if(!(pinfo[FLAG] & E_PROMPT) && |
| 76 | (pinfo[CURL] + pinfo[PAGE] >= pinfo[MAXL] || !pinfo[PAGE])) |
| 77 | return (call(pinfo[CTRL], pinfo[CARG]), 1); |
| 78 | if(!(pinfo[FLAG] & E_CAT) && |
| 79 | (pinfo[MAXL] > pinfo[PAGE] || (pinfo[FLAG] & E_PROMPT))) |
| 80 | input(#'prompt, ({ pinfo, cprompt }), #'eval_command, ({ pinfo })); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | varargs public void More(string txt, int file, |
| 85 | mixed ctrl, mixed ctrlargs, |
| 86 | int flags) |
| 87 | { |
| 88 | int j; |
| 89 | mixed tmp, pinfo; |
| 90 | if(!txt) return call(ctrl, ctrlargs); |
| 91 | // TEXT, FILE, CURL, MAXL, REGX, PAGE, CTRL, CARG |
| 92 | pinfo = ({ txt, file, 1, 1, 20, 0, ctrl, ctrlargs, flags }); |
| 93 | |
| 94 | if (pinfo[FILE]) |
| 95 | { |
| 96 | if (file_size(pinfo[TEXT])<50000) |
| 97 | { |
| 98 | pinfo[TEXT]=read_file(pinfo[TEXT]); |
| 99 | if (!pinfo[TEXT]) |
| 100 | { |
| 101 | call(ctrl, ctrlargs); |
| 102 | return; |
| 103 | } |
| 104 | pinfo[FILE]=0; |
| 105 | } |
| 106 | else if (file_size(pinfo[TEXT])>1048576) |
| 107 | { |
| 108 | tell_object(this_player()||this_object(),break_string(sprintf( |
| 109 | "Die Datei '%s' kann nicht angezeigt werden, da " |
| 110 | "sie groesser als 1 MB ist. %s",pinfo[TEXT], |
| 111 | IS_LEARNER(this_player()||this_object())?"":"Bitte verstaendige " |
| 112 | "diesbezueglich umgehend einen Magier."),78)); |
| 113 | call(ctrl,ctrlargs); |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if(!pinfo[FILE] && pinfo[TEXT][<1..<1] != "\n") pinfo[TEXT] += "\n"; |
| 119 | pinfo += ({ ([1:0]) }); |
| 120 | if(!pinfo[FILE]) |
| 121 | while((j = strstr(pinfo[TEXT], "\n", j+1)) != -1) |
| 122 | pinfo[JUNK][++pinfo[MAXL]] = j+1; |
| 123 | else |
| 124 | while(tmp = read_file(pinfo[TEXT], pinfo[MAXL], MAX_LINE_READ)) |
| 125 | pinfo[MAXL] += sizeof(explode(tmp, "\n"))+1; |
| 126 | pinfo[PAGE] = PAGELENGTH; |
| 127 | if(!pinfo[PAGE]) pinfo[FLAG] |= E_CAT; |
| 128 | if ((this_interactive() && (j=(int)this_interactive()->QueryProp(P_MORE_FLAGS))) || |
| 129 | (this_player() && interactive(this_player()) && |
| 130 | (j=(int)this_player()->QueryProp(P_MORE_FLAGS)))) |
| 131 | pinfo[FLAG] |= j; |
| 132 | |
| 133 | pinfo[CURL] = 1; |
| 134 | eval_command(-1, pinfo); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |