1 /*************************************************************************
2  *  TinyFugue - programmable mud client
3  *  Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2005, 2006-2007 Ken Keys
4  *
5  *  TinyFugue (aka "tf") is protected under the terms of the GNU
6  *  General Public License.  See the file "COPYING" for details.
7  ************************************************************************/
8 /* $Id: tfio.h,v 35004.66 2007/01/13 23:12:39 kkeys Exp $ */
9 
10 #ifndef TFIO_H
11 #define TFIO_H
12 
13 #ifdef _POSIX_VERSION
14 # include <sys/types.h>
15 # define MODE_T mode_t
16 #else
17 # define MODE_T unsigned long
18 #endif
19 
20 #include <stdarg.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #ifndef S_IROTH
24 # define S_IWUSR 00200
25 # define S_IRUSR 00400
26 # define S_IRGRP 00040
27 # define S_IROTH 00004
28 #endif
29 
30 /* TFILE types */
31 typedef enum { TF_NULL, TF_QUEUE, TF_FILE, TF_PIPE } TFILE_type_t;
32 
33 /* Sprintf flags */
34 #define SP_APPEND   1	/* don't truncate first, just append */
35 #define SP_CHECK    2	/* make sure char* args won't SIGSEGV or SIGBUS */
36 
37 typedef struct PhysLine {
38     conString *str;
39     int start;
40     short len;
41     short indent;
42     char visible; /* line passed screen_filter() */
43     char tmp;     /* should only be displayed once */
44 } PhysLine;
45 
46 typedef struct Queue {
47     List list;
48 } Queue;
49 
50 struct Screen {
51     int outcount;		/* lines remaining until pause */
52     struct List pline;		/* already displayed physical lines */
53 /* pline invariant: if one pline corresponding to an lline is in the list,
54  * all plines corresponding to that lline are in the list. */
55     int npline;			/* number of physical lines in pline */
56     int nlline;			/* number of logical lines in pline */
57     int maxlline;		/* max number of logical lines in pline */
58     int nback;			/* number of lines scrolled back */
59     int nnew;			/* number of new lines */
60     int nback_filtered;		/* number of filtered lines scrolled back */
61     int nnew_filtered;		/* number of filtered new lines */
62     ListEntry *top, *bot;	/* top and bottom of view in plines */
63     ListEntry *maxbot;		/* last line in plines that bot has reached */
64     int viewsize;		/* # of plines between top and bot, inclusive */
65     int scr_wrapflag;		/* wrapflag used to wrap plines */
66     int scr_wrapsize;		/* wrapsize used to wrap plines */
67     int scr_wrapspace;		/* wrapspace used to wrap plines */
68     int scr_wrappunct;		/* wrappunct used to wrap plines */
69     Pattern filter_pat;		/* filter pattern */
70     char filter_enabled;	/* is filter enabled? */
71     char filter_sense;		/* 0 = negative, 1 = positive */
72     char filter_attr;		/* filter by attributes? */
73     char selflush;		/* selective flushing flag */
74     char needs_refilter;	/* top and bot need to be recalculated */
75     char partialview;		/* do not expand viewsize to fit terminal */
76     char paused;		/* paused at a More prompt? */
77     char active;		/* has new lines without the A attribute? */
78 };
79 
80 /* TF's analogue of stdio's FILE */
81 typedef struct TFILE {
82     int id;
83     struct ListEntry *node;
84     TFILE_type_t type;
85     char *name;
86     union {
87         Queue *queue;
88         FILE *fp;
89     } u;
90     char buf[1024];
91     int off, len;
92     MODE_T mode;
93     char tfmode;
94     short warned;
95     short autoflush;
96 } TFILE;
97 
98 
99 #ifndef HAVE_DRIVES
100 # define is_absolute_path(path) \
101             ((path)[0] == '/' || (path)[0] == '~')
102 #else
103 # define is_absolute_path(path) \
104             ((path)[0] == '/' || (path)[0] == '~' || \
105             (is_alpha((path)[0]) && (path)[1] == ':'))
106 #endif
107 
108 
109 extern TFILE *loadfile;    /* currently /load'ing file */
110 extern int    loadline;    /* line number of /load'ing file */
111 extern int    loadstart;   /* line number of command start in /load'ing file */
112 extern TFILE *tfin;        /* tf input queue */
113 extern TFILE *tfout;       /* tf output queue */
114 extern TFILE *tferr;       /* tf error queue */
115 extern TFILE *tfalert;     /* tf alert file */
116 extern TFILE *tfkeyboard;  /* keyboard, where tfin usually points */
117 extern TFILE *tfscreen;    /* screen, where tfout & tferr usually point */
118 extern Screen*fg_screen;   /* current screen to which tf writes */
119 extern Screen*default_screen; /* default screen (unconnected or !virtscreen) */
120 extern int    read_depth;  /* depth of user kb reads */
121 extern int    readsafe;    /* safe to to a user kb read? */
122 extern PhysLine *plpool;   /* freelist of PhysLines */
123 
124 #define operror(str)    eprintf("%s: %s", str, strerror(errno))
125 #define oputline(line)  tfputline(line, tfout)
126 #define tfputs(str, f)  tfnputs(str, -1, f)
127 #define oputs(str)      tfputs(str, tfout)
128 #define eputs(str)      tfputs(str, tferr)
129 #define tfputc(c, file) fputc((c), (file)->u.fp)
130 #define tfflush(file) \
131     ((file->type==TF_FILE || file->type==TF_PIPE) ? fflush((file)->u.fp) : 0)
132 
133 extern void   init_tfio(void);
134 extern Screen*new_screen(long size);
135 extern void   free_screen_lines(Screen *screen);
136 extern void   free_screen(Screen *screen);
137 extern char  *tfname(const char *name, const char *macname);
138 extern char  *expand_filename(const char *str);
139 extern TFILE *tfopen(const char *name, const char *mode);
140 extern int    tfclose(TFILE *file);
141 extern void   tfnputs(const char *str, int n, TFILE *file);
142 extern attr_t tfputansi(const char *str, TFILE *file, attr_t attrs);
143 extern int    tfputp(const char *str, TFILE *file);
144 extern void   tfputline(struct conString *line, TFILE *file);
145 extern void   vSprintf(struct String *buf, int flags,
146                      const char *fmt, va_list ap);
147 extern void   Sprintf(struct String *buf, const char *fmt, ...)
148 		     format_printf(3, 4);
149 extern void   Sappendf(struct String *buf, const char *fmt, ...)
150 		     format_printf(2, 3);
151 extern void   oprintf(const char *fmt, ...) format_printf(1, 2);
152 extern void   tfprintf(TFILE *file, const char *fmt, ...)
153                      format_printf(2, 3);
154 extern void   eprefix(String *buffer);
155 extern void   eprintf(const char *fmt, ...) format_printf(1, 2);
156 extern void   wprintf(const char *fmt, ...) format_printf(1, 2);
157 extern char   igetchar(void);
158 extern int    handle_tfopen_func(const char *name, const char *mode);
159 extern TFILE *find_tfile(const char *handle);
160 extern TFILE *find_usable_tfile(const char *handle, int mode);
161 extern int    tfreadable(TFILE *file);
162 extern struct String *tfgetS(struct String *str, TFILE *file);
163 
164 extern void   hide_screen(Screen *screen);
165 extern void   unhide_screen(Screen *screen);
166 extern void   switch_screen(int quiet);
167 
168 #endif /* TFIO_H */
169