blob: d628ea65836dfec736b8bec45a0b4aa9896fe71d [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// util/pager.c -- generic pager
4//
5// $Id: pager.c 8755 2014-04-26 13:13:40Z Zesstra $
6
Vanion50652322020-03-10 21:13:25 +01007#pragma strict_types
MG Mud User88f12472016-06-24 23:31:02 +02008#pragma save_types
MG Mud User88f12472016-06-24 23:31:02 +02009#pragma range_check
10#pragma no_clone
11
12inherit "/std/util/input";
13
14#include <pager.h>
15#include <sys_debug.h>
16#include <wizlevels.h>
17
18string prompt(mixed pinfo, string add)
19{
20 return sprintf(MSG_PROMPT+"%s", stringp(add)?add:"");
21}
22
23string 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 {
Arathornb3051452021-05-13 21:13:03 +020029 int start, end;
MG Mud User88f12472016-06-24 23:31:02 +020030
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
43mixed 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
Zesstra29da7472021-04-18 11:16:54 +020051varargs int eval_command(mixed input, mixed pinfo)
MG Mud User88f12472016-06-24 23:31:02 +020052{
53 string cprompt, tmp;
54 cprompt = "";
Zesstra29da7472021-04-18 11:16:54 +020055 if(input == "q" || input == "x")
MG Mud User88f12472016-06-24 23:31:02 +020056 {
57 if(closurep(pinfo[CTRL])) apply(pinfo[CTRL], pinfo[CARG]);
58 return 1;
59 }
60 else
Zesstra29da7472021-04-18 11:16:54 +020061 if(!input || (stringp(input) && !sizeof(input))) pinfo[CURL] += pinfo[PAGE];
62 else if(input != -1) return 0;
MG Mud User88f12472016-06-24 23:31:02 +020063
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
83varargs 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;
Vanion50652322020-03-10 21:13:25 +0100127 if ((this_interactive() && (j=({int})this_interactive()->QueryProp(P_MORE_FLAGS))) ||
MG Mud User88f12472016-06-24 23:31:02 +0200128 (this_player() && interactive(this_player()) &&
Vanion50652322020-03-10 21:13:25 +0100129 (j=({int})this_player()->QueryProp(P_MORE_FLAGS))))
MG Mud User88f12472016-06-24 23:31:02 +0200130 pinfo[FLAG] |= j;
131
132 pinfo[CURL] = 1;
133 eval_command(-1, pinfo);
134 return;
135}
136
137
138
139
140
141