prntfile.cpp 7.59 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
 *																			*
 * 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"
40
#include "utf8.h"
41

42 43 44 45
#ifndef PRINTFILE_MAX_LINE_LEN
#define PRINTFILE_MAX_LINE_LEN (1024*1024)
#endif

46 47 48 49 50
/****************************************************************************/
/* 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                                  */
/****************************************************************************/
51
bool sbbs_t::printfile(const char* fname, long mode, long org_cols, JSObject* obj)
52
{
53
	char* buf;
54
	char fpath[MAX_PATH+1];
55
	char* p;
rswindell's avatar
rswindell committed
56
	int file;
57
	BOOL rip=FALSE;
rswindell's avatar
rswindell committed
58
	long l,length,savcon=console;
59 60
	FILE *stream;

61 62 63
	SAFECOPY(fpath, fname);
	fexistcase(fpath);
	p=getfext(fpath);
64
	if(p!=NULL) {
65
		if(stricmp(p,".rip")==0) {
rswindell's avatar
rswindell committed
66
			rip=TRUE;
rswindell's avatar
rswindell committed
67
			mode|=P_NOPAUSE;
68 69
		} else if(stricmp(p, ".seq") == 0) {
			mode |= P_PETSCII;
70 71
		} else if(stricmp(p, ".utf8") == 0) {
			mode |= P_UTF8;
rswindell's avatar
rswindell committed
72
		}
73
	}
74

75
	if(mode&P_NOABORT || rip) {
76 77
		if(online==ON_REMOTE && console&CON_R_ECHO) {
			rioctl(IOCM|ABORT);
rswindell's avatar
rswindell committed
78 79 80 81
			rioctl(IOCS|ABORT); 
		}
		sys_status&=~SS_ABORT; 
	}
82

83
	if(!(mode&P_NOCRLF) && !tos && !rip) {
84
		CRLF;
85
	}
86

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

98
	length=(long)filelength(file);
99
	if(length < 1) {
rswindell's avatar
rswindell committed
100
		fclose(stream);
101 102 103 104 105
		if(length < 0) {
			errormsg(WHERE,ERR_CHK,fpath,length);
			return false;
		}
		return true;
rswindell's avatar
rswindell committed
106
	}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	if(mode&P_OPENCLOSE) {
		if((buf=(char*)malloc(length+1L))==NULL) {
			fclose(stream);
			errormsg(WHERE,ERR_ALLOC,fpath,length+1L);
			return false; 
		}
		l=lread(file,buf,length);
		fclose(stream);
		if(l!=length)
			errormsg(WHERE,ERR_READ,fpath,length);
		else {
			buf[l]=0;
			if((mode&P_UTF8) && !term_supports(UTF8))
				utf8_normalize_str(buf);
122
			putmsg(buf,mode,org_cols, obj);
123 124 125
		}
		free(buf);
	} else {	// Line-at-a-time mode
126 127 128
		uint tmpatr = curatr;
		if(!(mode&P_SAVEATR))
			attr(LIGHTGRAY);
129 130 131 132 133 134 135 136
		if(length > PRINTFILE_MAX_LINE_LEN)
			length = PRINTFILE_MAX_LINE_LEN;
		if((buf=(char*)malloc(length+1L))==NULL) {
			fclose(stream);
			errormsg(WHERE,ERR_ALLOC,fpath,length+1L);
			return false; 
		}
		while(!feof(stream) && !msgabort()) {
137
			if(fgets(buf, length + 1, stream) == NULL)
138 139 140
				break;
			if((mode&P_UTF8) && !term_supports(UTF8))
				utf8_normalize_str(buf);
141
			if(putmsg(buf, mode|P_SAVEATR, org_cols, obj) != '\0') // early-EOF?
142
				break;
143 144
		}
		free(buf);
rswindell's avatar
rswindell committed
145
		fclose(stream);
146 147
		if(!(mode&P_SAVEATR))
			attr(tmpatr);
rswindell's avatar
rswindell committed
148
	}
149

150
	if((mode&P_NOABORT || rip) && online==ON_REMOTE) {
151
		SYNC;
rswindell's avatar
rswindell committed
152 153
		rioctl(IOSM|ABORT); 
	}
154 155 156
	if(rip)
		ansi_getlines();
	console=savcon;
157
	return true;
rswindell's avatar
rswindell committed
158
}
159

160
bool sbbs_t::printtail(const char* fname, int lines, long mode, long org_cols, JSObject* obj)
161
{
rswindell's avatar
rswindell committed
162
	char*	buf;
163
	char	fpath[MAX_PATH+1];
rswindell's avatar
rswindell committed
164 165 166
	char*	p;
	int		file,cur=0;
	long	length,l;
167

168 169
	SAFECOPY(fpath, fname);
	fexistcase(fpath);
170 171 172
	if(mode&P_NOABORT) {
		if(online==ON_REMOTE) {
			rioctl(IOCM|ABORT);
rswindell's avatar
rswindell committed
173 174 175 176
			rioctl(IOCS|ABORT); 
		}
		sys_status&=~SS_ABORT; 
	}
177
	if(!tos) {
rswindell's avatar
rswindell committed
178 179
		CRLF; 
	}
180
	if((file=nopen(fpath,O_RDONLY|O_DENYNONE))==-1) {
181 182
		if(!(mode&P_NOERROR)) {
			lprintf(LOG_NOTICE,"!Error %d (%s) opening: %s"
183
				,errno,strerror(errno),fpath);
184
			bputs(text[FileNotFound]);
185
			if(SYSOP) bputs(fpath);
186 187 188
			CRLF;
		}
		return false; 
rswindell's avatar
rswindell committed
189
	}
190
	length=(long)filelength(file);
rswindell's avatar
rswindell committed
191 192
	if(length<0) {
		close(file);
193
		errormsg(WHERE,ERR_CHK,fpath,length);
194
		return false;
rswindell's avatar
rswindell committed
195
	}
deuce's avatar
deuce committed
196
	if((buf=(char*)malloc(length+1L))==NULL) {
197
		close(file);
198
		errormsg(WHERE,ERR_ALLOC,fpath,length+1L);
199
		return false; 
rswindell's avatar
rswindell committed
200
	}
201 202
	l=lread(file,buf,length);
	close(file);
rswindell's avatar
rswindell committed
203
	if(l!=length)
204
		errormsg(WHERE,ERR_READ,fpath,length);
rswindell's avatar
rswindell committed
205 206 207 208 209 210 211 212 213 214 215 216
	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
217
		}
218
		putmsg(p,mode,org_cols, obj);
rswindell's avatar
rswindell committed
219
	}
220 221
	if(mode&P_NOABORT && online==ON_REMOTE) {
		SYNC;
rswindell's avatar
rswindell committed
222 223
		rioctl(IOSM|ABORT); 
	}
deuce's avatar
deuce committed
224
	free(buf);
225
	return true;
226 227 228
}

/****************************************************************************/
229
/* Displays a menu file (e.g. from the text/menu directory)                 */
230
/****************************************************************************/
231
bool sbbs_t::menu(const char *code, long mode, JSObject* obj)
232
{
233 234 235
    char path[MAX_PATH+1];
	const char *next= "msg";
	const char *last = "asc";
236 237 238

	sys_status&=~SS_ABORT;
	if(menu_file[0])
239
		SAFECOPY(path,menu_file);
240
	else {
rswindell's avatar
rswindell committed
241
		long term = term_supports();
242 243 244 245 246 247 248 249 250 251 252 253
		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
254
			}
255 256 257 258
			if(menu_exists(code, next, path))
				break;
			menu_exists(code, last, path);
		} while(0);
259
	}
260

261
	mode |= P_OPENCLOSE | P_CPM_EOF;
262 263
	if(column == 0)
		mode |= P_NOCRLF;
264
	return printfile(path, mode, /* org_cols: */0, obj);
265 266
}

267
bool sbbs_t::menu_exists(const char *code, const char* ext, char* path)
268
{
269 270 271
	char pathbuf[MAX_PATH+1];
	if(path == NULL)
		path = pathbuf;
272

273 274 275 276 277 278 279 280 281
	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);
282

283 284 285 286 287 288 289
	char prefix[MAX_PATH];
	if(isfullpath(code))
		SAFECOPY(prefix, code);
	else {
		backslash(menu_dir);
		SAFEPRINTF3(prefix, "%smenu/%s%s", cfg.text_dir, menu_dir, code);
	}
290
	safe_snprintf(path, MAX_PATH, "%s.%lucol.%s", prefix, cols, ext);
rswindell's avatar
rswindell committed
291 292
	if(fexistcase(path))
		return true;
293
	safe_snprintf(path, MAX_PATH, "%s.%s", prefix, ext);
deuce's avatar
deuce committed
294 295
	return fexistcase(path) ? true : false;
}