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