prntfile.cpp 6.72 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 46

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

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

71
	if(mode&P_NOABORT || rip) {
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 && !rip) {
80
		CRLF;
81
	}
82

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

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

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

rswindell's avatar
rswindell committed
127
bool sbbs_t::printtail(const char* fname, int lines, long mode, long org_cols)
128
{
rswindell's avatar
rswindell committed
129
	char*	buf;
130
	char	fpath[MAX_PATH+1];
rswindell's avatar
rswindell committed
131 132 133
	char*	p;
	int		file,cur=0;
	long	length,l;
134

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

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

	sys_status&=~SS_ABORT;
	if(menu_file[0])
206
		SAFECOPY(path,menu_file);
207
	else {
rswindell's avatar
rswindell committed
208
		long term = term_supports();
209 210 211 212 213 214 215 216 217 218 219 220
		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
221
			}
222 223 224 225
			if(menu_exists(code, next, path))
				break;
			menu_exists(code, last, path);
		} while(0);
226
	}
227

228
	mode |= P_OPENCLOSE | P_CPM_EOF;
229 230
	if(column == 0)
		mode |= P_NOCRLF;
231
	return printfile(path, mode);
232 233
}

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

240 241 242 243 244 245 246 247 248
	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);
249

250 251 252 253 254 255 256
	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);
	}
257
	safe_snprintf(path, MAX_PATH, "%s.%lucol.%s", prefix, cols, ext);
rswindell's avatar
rswindell committed
258 259
	if(fexistcase(path))
		return true;
260
	safe_snprintf(path, MAX_PATH, "%s.%s", prefix, ext);
deuce's avatar
deuce committed
261 262
	return fexistcase(path) ? true : false;
}