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