xref: /csrg-svn/usr.bin/pascal/pdx/tree/misc.c (revision 5544)
1*5544Slinton /* Copyright (c) 1982 Regents of the University of California */
2*5544Slinton 
3*5544Slinton static char sccsid[] = "@(#)misc.c 1.1 01/18/82";
4*5544Slinton 
5*5544Slinton /*
6*5544Slinton  * Miscellaneous commands "edit" and "help".
7*5544Slinton  * Also, output redirection routine "setout" and "unsetout".
8*5544Slinton  */
9*5544Slinton 
10*5544Slinton #include "defs.h"
11*5544Slinton #include "tree.h"
12*5544Slinton #include "command.h"
13*5544Slinton #include "object.h"
14*5544Slinton #include "mappings.h"
15*5544Slinton #include "sym.h"
16*5544Slinton #include "symtab.h"
17*5544Slinton 
18*5544Slinton extern char *getenv();
19*5544Slinton 
20*5544Slinton #define DEF_EDITOR	"vi"
21*5544Slinton 
22*5544Slinton /*
23*5544Slinton  * Invoke an editor on the given file.  Which editor to use might change
24*5544Slinton  * installation to installation.  For now, we use "vi".  In any event,
25*5544Slinton  * the environment variable "EDITOR" overrides any default.
26*5544Slinton  */
27*5544Slinton 
28*5544Slinton edit(filename)
29*5544Slinton char *filename;
30*5544Slinton {
31*5544Slinton 	char *ed;
32*5544Slinton 	FILE *fp;
33*5544Slinton 	SYM *s;
34*5544Slinton 	ADDRESS addr;
35*5544Slinton 	char buff[10];
36*5544Slinton 
37*5544Slinton 	if ((ed = getenv("EDITOR")) == NIL) {
38*5544Slinton 		ed = DEF_EDITOR;
39*5544Slinton 	}
40*5544Slinton 	fp = fopen(filename, "r");
41*5544Slinton 	if (fp == NIL) {
42*5544Slinton 		s = st_lookup(symtab, filename);
43*5544Slinton 		if (s == NIL) {
44*5544Slinton 			error("can't read \"%s\"", filename);
45*5544Slinton 		}
46*5544Slinton 		s = which(s);
47*5544Slinton 		if (!isblock(s)) {
48*5544Slinton 			error("can't read \"%s\"", filename);
49*5544Slinton 		}
50*5544Slinton 		addr = firstline(s);
51*5544Slinton 		filename = srcfilename(addr);
52*5544Slinton 		sprintf(buff, "+%d", srcline(addr));
53*5544Slinton 		call(ed, stdin, stdout, buff, filename, NIL);
54*5544Slinton 	} else {
55*5544Slinton 		fclose(fp);
56*5544Slinton 		call(ed, stdin, stdout, filename, NIL);
57*5544Slinton 	}
58*5544Slinton }
59*5544Slinton 
60*5544Slinton /*
61*5544Slinton  * Give the user some help.
62*5544Slinton  */
63*5544Slinton 
64*5544Slinton help()
65*5544Slinton {
66*5544Slinton 	puts("pdx command subset summary:");
67*5544Slinton 	putchar('\n');
68*5544Slinton 	puts("run                    - begin execution of the program");
69*5544Slinton 	puts("cont                   - continue execution");
70*5544Slinton 	puts("step                   - single step one line");
71*5544Slinton 	puts("next                   - step to next line (skip over calls)");
72*5544Slinton 	puts("trace <line#>          - trace execution of the line");
73*5544Slinton 	puts("trace <proc>           - trace calls to the procedure");
74*5544Slinton 	puts("trace <var>            - trace changes to the variable");
75*5544Slinton 	puts("trace <exp> at <line#> - print <exp> when <line> is reached");
76*5544Slinton 	puts("stop at <line>         - suspend execution at the line");
77*5544Slinton 	puts("stop in <proc>         - suspend execution when <proc> is called");
78*5544Slinton 	puts("status                 - print trace/stop's in effect");
79*5544Slinton 	puts("delete <number>        - remove trace or stop of given number");
80*5544Slinton 	puts("call <proc>            - call the procedure");
81*5544Slinton 	puts("where                  - print currently active procedures");
82*5544Slinton 	puts("print <exp>            - print the value of the expression");
83*5544Slinton 	puts("whatis <name>          - print the declaration of the name");
84*5544Slinton 	puts("quit                   - exit pdx");
85*5544Slinton }
86*5544Slinton 
87*5544Slinton /*
88*5544Slinton  * Divert output to the given file name.
89*5544Slinton  * Cannot redirect to an existing file.
90*5544Slinton  */
91*5544Slinton 
92*5544Slinton LOCAL int so_fd;
93*5544Slinton LOCAL BOOLEAN notstdout;
94*5544Slinton 
95*5544Slinton setout(filename)
96*5544Slinton char *filename;
97*5544Slinton {
98*5544Slinton 	FILE *fp;
99*5544Slinton 
100*5544Slinton 	if ((fp = fopen(filename, "r")) != NIL) {
101*5544Slinton 		fclose(fp);
102*5544Slinton 		error("%s: file already exists", filename);
103*5544Slinton 	} else {
104*5544Slinton 		so_fd = dup(1);
105*5544Slinton 		close(1);
106*5544Slinton 		if (creat(filename, 0666) == NIL) {
107*5544Slinton 			unsetout();
108*5544Slinton 			error("can't create %s", filename);
109*5544Slinton 		}
110*5544Slinton 		notstdout = TRUE;
111*5544Slinton 	}
112*5544Slinton }
113*5544Slinton 
114*5544Slinton /*
115*5544Slinton  * Revert output to standard output.
116*5544Slinton  */
117*5544Slinton 
118*5544Slinton unsetout()
119*5544Slinton {
120*5544Slinton 	fflush(stdout);
121*5544Slinton 	close(1);
122*5544Slinton 	if (dup(so_fd) != 1) {
123*5544Slinton 		panic("standard out dup failed");
124*5544Slinton 	}
125*5544Slinton 	close(so_fd);
126*5544Slinton 	notstdout = FALSE;
127*5544Slinton }
128*5544Slinton 
129*5544Slinton BOOLEAN isredirected()
130*5544Slinton {
131*5544Slinton 	return(notstdout);
132*5544Slinton }
133