summaryrefslogtreecommitdiff
path: root/dviware/dvitty/sys.c
blob: f2532c21b16b374ba56e12ee7fd6e53b7b2483c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

#define NAMSIZ		76
#define	SYNC	0x004	/* 1 => window is out of sync */

typedef enum {FALSE, TRUE} bool;

struct iorec {
	char		*fileptr;	/* ptr to file window */
	long		lcount;		/* number of lines printed */
	long		llimit;		/* maximum number of text lines */
	FILE		*fbuf;		/* FILE ptr */
	struct iorec	*fchain;	/* chain to next file */
	struct iorec	*flev;		/* ptr to associated file variable */
	char		*pfname;	/* ptr to name of file */
	short		funit;		/* file status flags */
	unsigned short	fblk;		/* index into active file table */
	long		fsize;		/* size of elements in the file */
	char		fname[NAMSIZ];	/* name of associated UNIX file */
	char		buf[BUFSIZ];	/* I/O buffer */
	char		window[1];	/* file window element */
};

setpos(f, p, m)				/* seek on a pascal file	   */
struct iorec *f;
int p, m;
{
	f->funit |= SYNC;
	(void) fseek (f->fbuf, (long) p, m);
}

int
sizef(f)				/* return size of file		   */
struct iorec *f;
{
	struct stat st;

	(void) fstat (fileno(f->fbuf), &st);
	return (st.st_size);
}

delete(f)				/* remove a link to a file	   */
struct iorec *f;
{
	(void) unlink (f->pfname);
}

bool
readp(filename)				/* check if a file may be read 	   */
char *filename;
{
	register char *p;
        register bool ok;

	for(p=filename; *p!=' '; p++)	/* pascal strings are space-padded */
		;
	*p='\0';			/* make it a C-string		   */
	if (access(filename, 04) == 0)	/* check if we can read this file  */
		ok=TRUE;
	else
		ok=FALSE;
	*p=' ';				/* "re-pasclify" the string	   */
	return(ok);
}

bool
writep(filename)			/* check if a file may be written   */
char *filename;
{
	register char *p;
        register bool ok;
	int f;

	for(p=filename; *p!=' '; p++)	/* pascal strings are space-padded */
		;
	*p='\0';			/* make it a C-string		   */
	if ((f=creat(filename, 0666)) >= 0) {	/* can we create this file?*/
		(void) close(f);
		ok = TRUE;
	} else
		ok = FALSE;
	*p=' ';				/* "re-pasclify" the string	   */
	return(ok);
}

tostdout(f)				/* make file appear on stdout	   */
struct iorec *f;
{
	(void) fflush (f->fbuf);
	f->fbuf = stdout;
}

tostderr(f)				/* make file appear on stderr	   */
struct iorec *f;
{
	(void) fflush (f->fbuf);
	f->fbuf = stderr;
}

bool
ttyp(f)					/* is file associated with a tty?  */
struct iorec *f;
{
	if (isatty(fileno(f->fbuf))==1)
		return(TRUE);
	else
		return(FALSE);
}

bool
popenp(f, path, pthpgm)			/* can this file be piped into a   */
struct iorec  *f;			/*   pager (like more)?		   */
char *path;
bool pthpgm;
{
	char *getenv(), *p;
	FILE *popen(), *g;

	if (pthpgm==FALSE) {			/* -Fpath option used	   */
		for(p=path; *p !=' ' && *p != '\0'; p++)
			;
		*p = '\0';
	} else if ((p=getenv("PAGER"))==NULL) { /* find prefered pager     */
		for(p=path; *p !=' ' && *p != '\0'; p++)
			;			/* didn't have one, use    */
		*p = '\0';			/* the default one for pgm */
	} else
		path = p;			/* default pager from env  */
	if ((g=popen(path, "w"))!=NULL) {	/* get a pipe to the pager */
		(void) fflush (f->fbuf);	/* make output to f go to  */
		f->fbuf = g;			/*   the pager		   */
		return(TRUE);
	} else
		return(FALSE);			/* couldn't do it	   */
}

pcloseit(f)					/* popend stream has to be */
struct iorec *f;				/*   pclosed		   */
{
	(void) pclose (f->fbuf);
}

bool
envargs(optch, str)
char *optch, *str;
{
	char *getenv(), *q;
	static char *p;
	static int foo = 0;

	switch (foo) {
	case 0:	if ((p=getenv("DVITTY"))==NULL)	/* 1st time only, get var  */
			return(FALSE);		/* sorry, no var	   */
		foo++;		/* remember we've been here to next call   */
				/* fall through 			   */
	default:
		if (*p!=' ' && *p!='\0')	/* anything here? 	   */
			*optch = *p++;		/* yes, it's our optchar   */
		else
			return(FALSE);		/* sorry, no more	   */
		q=str;				/* get args or whatever    */
		while (*p!=' ' && *p!='\0')
			*q++ = *p++;
		*q=' ';			/* the pascal program wants ' '    */
		while (*p==' ')		/* step to next or end             */
			p++;
		return(TRUE);
	}
}