xref: /csrg-svn/usr.bin/pascal/pdx/tree/misc.c (revision 22560)
1*22560Sdist /*
2*22560Sdist  * Copyright (c) 1980 Regents of the University of California.
3*22560Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22560Sdist  * specifies the terms and conditions for redistribution.
5*22560Sdist  */
65544Slinton 
7*22560Sdist #ifndef lint
8*22560Sdist static char sccsid[] = "@(#)misc.c	5.1 (Berkeley) 06/06/85";
9*22560Sdist #endif not lint
105544Slinton 
115544Slinton /*
125544Slinton  * Miscellaneous commands "edit" and "help".
135544Slinton  * Also, output redirection routine "setout" and "unsetout".
145544Slinton  */
155544Slinton 
165544Slinton #include "defs.h"
175544Slinton #include "tree.h"
185544Slinton #include "command.h"
195544Slinton #include "object.h"
205544Slinton #include "mappings.h"
215544Slinton #include "sym.h"
225544Slinton #include "symtab.h"
235544Slinton 
245544Slinton extern char *getenv();
255544Slinton 
265544Slinton #define DEF_EDITOR	"vi"
275544Slinton 
285544Slinton /*
295544Slinton  * Invoke an editor on the given file.  Which editor to use might change
305544Slinton  * installation to installation.  For now, we use "vi".  In any event,
315544Slinton  * the environment variable "EDITOR" overrides any default.
325544Slinton  */
335544Slinton 
345544Slinton edit(filename)
355544Slinton char *filename;
365544Slinton {
375544Slinton 	char *ed;
385544Slinton 	FILE *fp;
395544Slinton 	SYM *s;
405544Slinton 	ADDRESS addr;
415544Slinton 	char buff[10];
425544Slinton 
435544Slinton 	if ((ed = getenv("EDITOR")) == NIL) {
445544Slinton 		ed = DEF_EDITOR;
455544Slinton 	}
465544Slinton 	fp = fopen(filename, "r");
475544Slinton 	if (fp == NIL) {
485544Slinton 		s = st_lookup(symtab, filename);
495544Slinton 		if (s == NIL) {
505544Slinton 			error("can't read \"%s\"", filename);
515544Slinton 		}
525544Slinton 		s = which(s);
535544Slinton 		if (!isblock(s)) {
545544Slinton 			error("can't read \"%s\"", filename);
555544Slinton 		}
565544Slinton 		addr = firstline(s);
575544Slinton 		filename = srcfilename(addr);
585544Slinton 		sprintf(buff, "+%d", srcline(addr));
595544Slinton 		call(ed, stdin, stdout, buff, filename, NIL);
605544Slinton 	} else {
615544Slinton 		fclose(fp);
625544Slinton 		call(ed, stdin, stdout, filename, NIL);
635544Slinton 	}
645544Slinton }
655544Slinton 
665544Slinton /*
675565Slinton  * Send some nasty mail to the current pdx support person.
685565Slinton  */
695565Slinton 
705565Slinton gripe()
715565Slinton {
7212736Slinton 	char *maintainer = "linton@ucbvax";
735565Slinton 
745565Slinton 	puts("Type control-D to end your message.  Be sure to include");
755565Slinton 	puts("your name and the name of the file you are debugging.");
765565Slinton 	putchar('\n');
775565Slinton 	call("Mail", stdin, stdout, maintainer, NIL);
785565Slinton 	puts("Thank you.");
795565Slinton }
805565Slinton 
815565Slinton /*
825544Slinton  * Give the user some help.
835544Slinton  */
845544Slinton 
855544Slinton help()
865544Slinton {
875544Slinton 	puts("pdx command subset summary:");
885544Slinton 	putchar('\n');
895544Slinton 	puts("run                    - begin execution of the program");
905544Slinton 	puts("cont                   - continue execution");
915544Slinton 	puts("step                   - single step one line");
925544Slinton 	puts("next                   - step to next line (skip over calls)");
935544Slinton 	puts("trace <line#>          - trace execution of the line");
945544Slinton 	puts("trace <proc>           - trace calls to the procedure");
955544Slinton 	puts("trace <var>            - trace changes to the variable");
965544Slinton 	puts("trace <exp> at <line#> - print <exp> when <line> is reached");
975544Slinton 	puts("stop at <line>         - suspend execution at the line");
985544Slinton 	puts("stop in <proc>         - suspend execution when <proc> is called");
995544Slinton 	puts("status                 - print trace/stop's in effect");
1005544Slinton 	puts("delete <number>        - remove trace or stop of given number");
1015544Slinton 	puts("call <proc>            - call the procedure");
1025544Slinton 	puts("where                  - print currently active procedures");
1035544Slinton 	puts("print <exp>            - print the value of the expression");
1045544Slinton 	puts("whatis <name>          - print the declaration of the name");
1055591Slinton 	puts("list <line>, <line>    - list source lines");
1065591Slinton 	puts("edit <proc>            - edit file containing <proc>");
1075565Slinton 	puts("gripe                  - send mail to the person in charge of pdx");
1085544Slinton 	puts("quit                   - exit pdx");
1095544Slinton }
1105544Slinton 
1115544Slinton /*
1125544Slinton  * Divert output to the given file name.
1135544Slinton  * Cannot redirect to an existing file.
1145544Slinton  */
1155544Slinton 
1165544Slinton LOCAL int so_fd;
1175544Slinton LOCAL BOOLEAN notstdout;
1185544Slinton 
1195544Slinton setout(filename)
1205544Slinton char *filename;
1215544Slinton {
1225544Slinton 	FILE *fp;
1235544Slinton 
1245544Slinton 	if ((fp = fopen(filename, "r")) != NIL) {
1255544Slinton 		fclose(fp);
1265544Slinton 		error("%s: file already exists", filename);
1275544Slinton 	} else {
1285544Slinton 		so_fd = dup(1);
1295544Slinton 		close(1);
1305544Slinton 		if (creat(filename, 0666) == NIL) {
1315544Slinton 			unsetout();
1325544Slinton 			error("can't create %s", filename);
1335544Slinton 		}
1345544Slinton 		notstdout = TRUE;
1355544Slinton 	}
1365544Slinton }
1375544Slinton 
1385544Slinton /*
1395544Slinton  * Revert output to standard output.
1405544Slinton  */
1415544Slinton 
1425544Slinton unsetout()
1435544Slinton {
1445544Slinton 	fflush(stdout);
1455544Slinton 	close(1);
1465544Slinton 	if (dup(so_fd) != 1) {
1475544Slinton 		panic("standard out dup failed");
1485544Slinton 	}
1495544Slinton 	close(so_fd);
1505544Slinton 	notstdout = FALSE;
1515544Slinton }
1525544Slinton 
1535544Slinton BOOLEAN isredirected()
1545544Slinton {
1555544Slinton 	return(notstdout);
1565544Slinton }
157