prntfile.cpp 6.47 KB
Newer Older
1
/* prntfile.cpp */
2
// vi: tabstop=4
3
4
5
6
7
8
9
10
11

/* Synchronet file print/display routines */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
12
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

/****************************************************************************/
/* Prints a file remotely and locally, interpreting ^A sequences, checks    */
/* for pauses, aborts and ANSI. 'str' is the path of the file to print      */
/* Called from functions menu and text_sec                                  */
/****************************************************************************/
46
bool sbbs_t::printfile(char *str, long mode)
47
{
48
49
	char* buf;
	char* p;
rswindell's avatar
rswindell committed
50
51
	int file;
	BOOL wip=FALSE,rip=FALSE,html=FALSE;
rswindell's avatar
rswindell committed
52
	long l,length,savcon=console;
53
54
	FILE *stream;

55
56
	p=strrchr(str,'.');
	if(p!=NULL) {
rswindell's avatar
rswindell committed
57
		if(stricmp(p,".wip")==0) {
rswindell's avatar
rswindell committed
58
			wip=TRUE;
rswindell's avatar
rswindell committed
59
60
61
			mode|=P_NOPAUSE;
		}
		else if(stricmp(p,".rip")==0) {
rswindell's avatar
rswindell committed
62
			rip=TRUE;
rswindell's avatar
rswindell committed
63
64
65
			mode|=P_NOPAUSE;
		}
		else if(stricmp(p,".html")==0)  {
rswindell's avatar
rswindell committed
66
			html=TRUE;
rswindell's avatar
rswindell committed
67
68
			mode|=(P_HTML|P_NOPAUSE);
		}
69
	}
70

rswindell's avatar
rswindell committed
71
	if(mode&P_NOABORT || wip || rip || html) {
72
73
		if(online==ON_REMOTE && console&CON_R_ECHO) {
			rioctl(IOCM|ABORT);
rswindell's avatar
rswindell committed
74
75
76
77
			rioctl(IOCS|ABORT); 
		}
		sys_status&=~SS_ABORT; 
	}
78

79
	if(!(mode&P_NOCRLF) && !tos && !wip && !rip && !html) {
80
		CRLF;
81
	}
82

83
	fexistcase(str);
84
	if((stream=fnopen(&file,str,O_RDONLY|O_DENYNONE))==NULL) {
85
86
87
88
89
90
91
92
		if(!(mode&P_NOERROR)) {
			lprintf(LOG_NOTICE,"!Error %d (%s) opening: %s"
				,errno,strerror(errno),str);
			bputs(text[FileNotFound]);
			if(SYSOP) bputs(str);
			CRLF;
		}
		return false; 
rswindell's avatar
rswindell committed
93
	}
94

95
	length=(long)filelength(file);
96
	if(length<0) {
rswindell's avatar
rswindell committed
97
		fclose(stream);
98
		errormsg(WHERE,ERR_CHK,str,length);
99
		return false;
rswindell's avatar
rswindell committed
100
	}
deuce's avatar
deuce committed
101
	if((buf=(char*)malloc(length+1L))==NULL) {
rswindell's avatar
rswindell committed
102
		fclose(stream);
103
		errormsg(WHERE,ERR_ALLOC,str,length+1L);
104
		return false; 
105
106
107
108
109
	}
	l=lread(file,buf,length);
	fclose(stream);
	if(l!=length)
		errormsg(WHERE,ERR_READ,str,length);
110
	else {
111
112
		buf[l]=0;
		putmsg(buf,mode);
rswindell's avatar
rswindell committed
113
	}
deuce's avatar
deuce committed
114
	free(buf); 
115

rswindell's avatar
rswindell committed
116
	if((mode&P_NOABORT || wip || rip || html) && online==ON_REMOTE) {
117
		SYNC;
rswindell's avatar
rswindell committed
118
119
		rioctl(IOSM|ABORT); 
	}
120
121
122
	if(rip)
		ansi_getlines();
	console=savcon;
123
	return true;
rswindell's avatar
rswindell committed
124
}
125

126
bool sbbs_t::printtail(char *str, int lines, long mode)
127
{
rswindell's avatar
rswindell committed
128
129
130
131
	char*	buf;
	char*	p;
	int		file,cur=0;
	long	length,l;
132
133
134
135

	if(mode&P_NOABORT) {
		if(online==ON_REMOTE) {
			rioctl(IOCM|ABORT);
rswindell's avatar
rswindell committed
136
137
138
139
			rioctl(IOCS|ABORT); 
		}
		sys_status&=~SS_ABORT; 
	}
140
	if(!tos) {
rswindell's avatar
rswindell committed
141
142
		CRLF; 
	}
143
	fexistcase(str);
144
	if((file=nopen(str,O_RDONLY|O_DENYNONE))==-1) {
145
146
147
148
149
150
151
152
		if(!(mode&P_NOERROR)) {
			lprintf(LOG_NOTICE,"!Error %d (%s) opening: %s"
				,errno,strerror(errno),str);
			bputs(text[FileNotFound]);
			if(SYSOP) bputs(str);
			CRLF;
		}
		return false; 
rswindell's avatar
rswindell committed
153
	}
154
	length=(long)filelength(file);
rswindell's avatar
rswindell committed
155
156
157
	if(length<0) {
		close(file);
		errormsg(WHERE,ERR_CHK,str,length);
158
		return false;
rswindell's avatar
rswindell committed
159
	}
deuce's avatar
deuce committed
160
	if((buf=(char*)malloc(length+1L))==NULL) {
161
162
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,length+1L);
163
		return false; 
rswindell's avatar
rswindell committed
164
	}
165
166
	l=lread(file,buf,length);
	close(file);
rswindell's avatar
rswindell committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	if(l!=length)
		errormsg(WHERE,ERR_READ,str,length);
	else {
		buf[l]=0;
		p=(buf+l)-1;
		if(*p==LF) p--;
		while(*p && p>buf) {
			if(*p==LF)
				cur++;
			if(cur>=lines) {
				p++;
				break; 
			}
			p--; 
rswindell's avatar
rswindell committed
181
		}
rswindell's avatar
rswindell committed
182
		putmsg(p,mode);
rswindell's avatar
rswindell committed
183
	}
184
185
	if(mode&P_NOABORT && online==ON_REMOTE) {
		SYNC;
rswindell's avatar
rswindell committed
186
187
		rioctl(IOSM|ABORT); 
	}
deuce's avatar
deuce committed
188
	free(buf);
189
	return true;
190
191
192
}

/****************************************************************************/
193
/* Displays a menu file (e.g. from the text/menu directory)                 */
194
/****************************************************************************/
195
bool sbbs_t::menu(const char *code, long mode)
196
{
197
198
199
    char path[MAX_PATH+1];
	const char *next= "msg";
	const char *last = "asc";
200
201
202

	sys_status&=~SS_ABORT;
	if(menu_file[0])
203
		SAFECOPY(path,menu_file);
204
	else {
rswindell's avatar
rswindell committed
205
		long term = term_supports();
206
207
208
209
210
211
212
213
214
215
216
217
		do {
			if((term&RIP) && menu_exists(code, "rip", path))
				break;
			if((term&(ANSI|COLOR)) == ANSI && menu_exists(code, "mon", path))
				break;
			if((term&ANSI) && menu_exists(code, "ans", path))
				break;
			if((term&PETSCII) && menu_exists(code, "seq", path))
				break;
			if(term&NO_EXASCII) {
				next = "asc";
				last = "msg";
rswindell's avatar
rswindell committed
218
			}
219
220
221
222
			if(menu_exists(code, next, path))
				break;
			menu_exists(code, last, path);
		} while(0);
223
	}
224

225
	mode |= P_OPENCLOSE | P_CPM_EOF;
226
227
	if(column == 0)
		mode |= P_NOCRLF;
228
	return printfile(path, mode);
229
230
}

231
bool sbbs_t::menu_exists(const char *code, const char* ext, char* path)
232
{
233
234
235
	char pathbuf[MAX_PATH+1];
	if(path == NULL)
		path = pathbuf;
236

237
238
239
240
241
242
243
244
245
	if(menu_file[0]) {
		strncpy(path, menu_file, MAX_PATH);
		return fexistcase(path) ? true : false;
	}

	/* Either <menu>.asc or <menu>.msg is required */
	if(ext == NULL)
		return menu_exists(code, "asc", path)
			|| menu_exists(code, "msg", path);
246

247
	backslash(menu_dir);
248
249
	safe_snprintf(path, MAX_PATH, "%smenu/%s%s.%ucol.%s"
		,cfg.text_dir, menu_dir, code, cols, ext);
rswindell's avatar
rswindell committed
250
251
	if(fexistcase(path))
		return true;
252
253
	safe_snprintf(path, MAX_PATH, "%smenu/%s%s.%s"
		,cfg.text_dir, menu_dir, code, ext);
deuce's avatar
deuce committed
254
255
	return fexistcase(path) ? true : false;
}