xref: /csrg-svn/usr.bin/pascal/pdx/tree/misc.c (revision 5565)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)misc.c 1.2 01/19/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  * Send some nasty mail to the current pdx support person.
62  */
63 
64 gripe()
65 {
66 	char *maintainer = "csvax:linton";
67 
68 	puts("Type control-D to end your message.  Be sure to include");
69 	puts("your name and the name of the file you are debugging.");
70 	putchar('\n');
71 	call("Mail", stdin, stdout, maintainer, NIL);
72 	puts("Thank you.");
73 }
74 
75 /*
76  * Give the user some help.
77  */
78 
79 help()
80 {
81 	puts("pdx command subset summary:");
82 	putchar('\n');
83 	puts("run                    - begin execution of the program");
84 	puts("cont                   - continue execution");
85 	puts("step                   - single step one line");
86 	puts("next                   - step to next line (skip over calls)");
87 	puts("trace <line#>          - trace execution of the line");
88 	puts("trace <proc>           - trace calls to the procedure");
89 	puts("trace <var>            - trace changes to the variable");
90 	puts("trace <exp> at <line#> - print <exp> when <line> is reached");
91 	puts("stop at <line>         - suspend execution at the line");
92 	puts("stop in <proc>         - suspend execution when <proc> is called");
93 	puts("status                 - print trace/stop's in effect");
94 	puts("delete <number>        - remove trace or stop of given number");
95 	puts("call <proc>            - call the procedure");
96 	puts("where                  - print currently active procedures");
97 	puts("print <exp>            - print the value of the expression");
98 	puts("whatis <name>          - print the declaration of the name");
99 	puts("gripe                  - send mail to the person in charge of pdx");
100 	puts("quit                   - exit pdx");
101 }
102 
103 /*
104  * Divert output to the given file name.
105  * Cannot redirect to an existing file.
106  */
107 
108 LOCAL int so_fd;
109 LOCAL BOOLEAN notstdout;
110 
111 setout(filename)
112 char *filename;
113 {
114 	FILE *fp;
115 
116 	if ((fp = fopen(filename, "r")) != NIL) {
117 		fclose(fp);
118 		error("%s: file already exists", filename);
119 	} else {
120 		so_fd = dup(1);
121 		close(1);
122 		if (creat(filename, 0666) == NIL) {
123 			unsetout();
124 			error("can't create %s", filename);
125 		}
126 		notstdout = TRUE;
127 	}
128 }
129 
130 /*
131  * Revert output to standard output.
132  */
133 
134 unsetout()
135 {
136 	fflush(stdout);
137 	close(1);
138 	if (dup(so_fd) != 1) {
139 		panic("standard out dup failed");
140 	}
141 	close(so_fd);
142 	notstdout = FALSE;
143 }
144 
145 BOOLEAN isredirected()
146 {
147 	return(notstdout);
148 }
149