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