home *** CD-ROM | disk | FTP | other *** search
- /*
- Little Smalltalk
- misc lexer related routines
- timothy a. budd 12/84
- */
-
- /*
- The source code for the Little Smalltalk System may be freely
- copied provided that the source of all files is acknowledged
- and that this condition is copied with each file.
-
- The Little Smalltalk System is distributed without responsibility
- for the performance of the program and without any guarantee of
- maintenance.
-
- All questions concerning Little Smalltalk should be addressed to:
-
- Professor Tim Budd
- Department of Computer Science
- Oregon State University
- Corvallis, Oregon
- 97331
- US
- */
- # include <stdio.h>
- # include "env.h"
- # include <ctype.h>
-
- extern char toktext[];
-
- /* dolexcommand - read a ) type directive, and process it */
- dolexcommand(p)
- char *p;
- { char *q, buffer[100];
-
- /* replace trailing newline with end of string */
- for (q = p; *q && *q != '\n'; q++);
- if (*q == '\n') *q = '\0';
-
- switch( *++p) {
- case '!':
- # ifndef NOSYSTEM
- # ifdef ARM
- ssystem(++p);
- # else
- system(++p);
- # endif
- # endif
- break;
-
- case 'e': for (++p; isspace(*p); p++);
- # ifdef ARM /* return codes not used yet */
- lexedit(p);
- lexinclude(p);
- break;
- #else
-
- if (! lexedit(p)) lexinclude(p);
- break;
- # endif
- case 'g': for (++p; isspace(*p); p++);
- #ifdef ARM /* ARM uses '.' instead of '/' */
- sprintf(buffer,"%s.%s", LIBLOC, p);
- #else
- sprintf(buffer,"%s/%s", LIBLOC, p);
- #endif
- lexread(buffer);
- break;
-
- case 'i': for (++p; isspace(*p); p++);
- lexinclude(p);
- break;
-
- case 'r': for (++p; isspace(*p); p++);
- lexread(p);
- break;
-
- case 's': for(++p; isspace(*p); p++);
- dosave(p);
- break;
-
- case 'l': for(++p; isspace(*p); p++);
- doload(p);
- break;
-
- default: lexerr("unknown command %s", toktext);
- }
- }
-
- /* doload/dosave routines written by nick buchholz */
- /*
- doload and dosave routines make the following assumptions
- 1. version is the first global variable declared in main.
- 2. main is the first procedure seen by the loader
- 3. the loader allocates memory in the order it sees the procedures
- 4. memory is laid out as on the vax 780 under 4.2
-
- on other machines any or all of these might be false and the
- doload/dosave routines will not work
- */
- extern int version;
- #ifdef ARM
- /* replacement versions to warn of missing facility TPR */
- dosave(p)
- char *p;
- {
- printf("Sorry -- no can do at dosave\n");
- }
-
- doload(p)
- char *p;
- {
- printf("Sorry -- no can do at doload\n");
- }
- #else
-
- dosave(p) char *p;{
- int fd;
- char *start, *end, *sbrk();
- unsigned int length, len;
- int dlen;
-
- # ifdef OPEN3ARG
- if ((fd = open(p, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
- # endif
- # ifndef OPEN3ARG
- if ((fd = creat(p, 0666)) == -1)
- # endif
-
- ARMX(tprintf(,"can't open: %s\n",p), fprintf(stderr,"can't open: %s\n",p));
-
- start = (char *) &version;
- end = sbrk(0);
- length = end - start;
-
- write(fd, &version, sizeof(int));
- write(fd, &start, sizeof(char *));
- write(fd, &length, sizeof(unsigned int));
-
- for (len = 0; len < length; len += dlen) {
- dlen = ((length - len) > 512) ? 512 : (length - len);
- if (dlen != write(fd, start + len, dlen)) {
- cant_happen(23);
- }
- }
-
- ARMX(tprintf("%u bytes written\n",len), fprintf(stderr,"%u bytes written\n",len));
-
- close(fd);
- }
-
- # ifdef ENVSAVE
- extern char **environ;
- # endif
-
- doload(p) char *p;{
- int fd;
- char *start, *end, *brk();
- unsigned int length, len;
- int dlen;
- int test;
- # ifdef ENVSAVE
- char **evsave;
- # endif
-
- # ifdef OPEN3ARG
- if ((fd = open(p, O_RDONLY, 0)) == -1)
- # endif
- # ifndef OPEN3ARG
- if ((fd = open(p, 0 )) == -1)
- # endif
- ARMX(tprintf("no such context as: %s\n", p), fprintf(stderr,"no such context as: %s\n", p));
-
- else {
- read(fd, &test, sizeof(int));
- read(fd, &start, sizeof(char *));
- read(fd, &length, sizeof(unsigned int));
-
- if ((test != version) || (start != (char *) &version))
- ARMX(tprintf("%s: not a valid context file for version %d\n", p, version),
- fprintf(stderr,"%s: not a valid context file for version %d\n", p, version));
- else {
- start = (char *) &version;
- end = brk(start + length + 1);
- # ifdef ENVSAVE
- evsave = environ;
- # endif
-
- for (len = 0; len < length; len += dlen) {
- dlen = ((length - len) > 512) ? 512 : (length - len);
- if (dlen != read(fd, start + len, dlen)) {
- cant_happen(23);
- }
- }
- # ifdef ENVSAVE
- environ = evsave;
- # endif
- ARMX(tprintf("%u bytes read\n",len), fprintf(stderr,"%u bytes read\n",len));
- }
- close(fd);
- }
- }
- # endif
- /* lexread - read commands from a file */
- lexread(name)
- char *name;
- { FILE *fd;
-
- fd = fopen(name, "r");
- if (fd == NULL) {
- ARMX(tprintf("can't open %s\n", name), fprintf(stderr,"can't open %s\n", name));
- }
- else {
- set_file(fd);
- }
- }
-
- /* lexinclude - parse a class and include the class description */
- lexinclude(name)
- char *name;
- { char template[60], cmdbuf[120];
- int i;
-
- # ifndef NOSYSTEM
- gettemp(template);
- #ifdef ARM
- /* TPRs modified parser takes 2 files as args, since redirection is dodgy still */
- sprintf(cmdbuf, "*remove %s", template);
- ssystem(cmdbuf);
- sprintf(cmdbuf,"%s %s %s", PARSER, name, template);
- ssystem(cmdbuf);
- lexread(template); /* no return code at the mo */
- #else
- sprintf(cmdbuf,"%s %s >%s", PARSER, name, template);
- i = system(cmdbuf);
- if (i == 0)
- lexread(template);
- # endif
- # endif
- # ifdef NOSYSTEM
- fprintf(stderr,")i does not work on this system\n");
- # endif
- }
-
- /* lexedit - edit a class description */
- int lexedit(name)
- char *name;
- { char *e, buffer[100], *getenv();
-
- #ifdef ARM
- /* sprintf(buffer, "tw %s",name); ---- Changed by SLD to... */
- sprintf(buffer, "StEd %s", name); /* Alias StEd must exist! */
- return(ssystem(buffer));
- #else
- # ifndef NOSYSTEM
- e = getenv("EDITOR");
- # ifdef IBMPC
- if (!e) e = "edlin";
- #else
- if (!e) e = "ed";
- #endif
- sprintf(buffer,"%s %s", e, name);
- return(system(buffer));
- # endif
- # endif
- # ifdef NOSYSTEM
- fprintf(stderr,")e does not work on this system\n");
- return(1);
- # endif
- }
- /* TPR ssystem -- divert all system calls to oursystemcall */
-
- ssystem(s)
- char *s;
- {
- /* oursystemcall(s); */
- #ifndef ARM
- system(s);
- #else
- /* RiscOS system call added by MA - uses Wimp_StartTask */
- os_swi1(0x400DE, s);
- #endif
- }
-