🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: bit.listserv.simula
Article #394

Source-code + options-txtfile included [SIMED.C]!!

#394
From: ass. A.Rutges (I&O)
Date: Sun, 16 Jul 1995 00:00
258 lines
9547 bytes
Howdy,

Under my .sig, I include OPTIONS.TXT and SIMED.C for those ppl. that
were looking for it. 

DISCLAIMER: I have not found any copyrights legislative text in
both of the files, so I ASSUME that posting it is safe. I don't keep
myself responsible for any damage(s) resulting from using these
files.

Greetz.

Tijs Coumans
Computer Simulation + VAX/VMS Expert
Faculty of Management Studies, University of Groningen (The Netherlands)
Mail: RUTGES_ASS @ RUGB38.BDK.RUG.NL
------------------------------------------------------------------------
Disclaimer: All I say, is on my own behalf, not that of my employer's.

[OPTIONS.TXT] 

Simed - Simula source file formatter

Simed is a tool to format simula source code files. It features automatic
idention to reflect BEGIN/END blocks, formatting of simula reserved words
and simula-defined identifiers.

Synopsis:

Usage: simed [-notab] [-indent=#] [-nomagic] file [outputfile]

	-notab		Don't use tabs for indention, use spaces instead.
	-indent=#	indent # positions
	-nomagic	Don't do anything with the text, just indent
	file		Simula source file
	[outputfile]	Formatted file. The default is the same file

Tested on SunOS 4.1.1 (Berkeley Unix) and VAX/VMS.

[SIMED.C]

#include <stdio.h>
#include <ctype.h>
#define RECLENGTH 256
#define NKEYWRDS 63
#define NSYSWRDS 153

char record[RECLENGTH],uprecord[RECLENGTH],input[40],output[40];
char *upitem,*pos;
char saved, notab = 0, nomagic = 0, overwrite = 1;
int  indent = 8, level = 0, level_ = 0;

static char *keywords[NKEYWRDS] = {"ACTIVATE","AFTER","AND","ARRAY","AT",
"BEFORE","BEGIN","BOOLEAN","CHARACTER","CLASS","COMMENT","DELAY","DO","ELSE",
"END","EQV","EXTERNAL","FALSE","FOR","GE","GO","GOTO","GT","HIDDEN","IF","IMP",
"IN","INNER","INSPECT","INTEGER","IS","LABEL","LE","LONG","LT","NAME","NE",
"NEW","NONE","NOT","NOTEXT","OR","OTHERWISE","PRIOR","PROCEDURE",
"PROTECTED","QUA","REACTIVATE","REAL","REF","SHORT","STEP","SWITCH","TEXT",
"THEN","THIS","TO","TRUE","UNTIL","VALUE","VIRTUAL","WHEN","WHILE"};

static char *syswords[NSYSWRDS] = {"ABS","ACCUM","ADDEPSILON","ARCCOS",
"ARCSIN","ARCTAN","ARCTAN2","BLANKS","BREAKOUTIMAGE","BYTEFILE",
"CALL","CANCEL","CARDINAL","CHAR","CLEAR","CLOCKTIME","CLOSE",
"CONSTANT","COPY","COS","COSH","COTAN","CPUTIME","CURRENT",
"DATETIME","DECIMALMARK","DETACH","DIGIT","DIRECTBYTEFILE",
"DIRECTFILE","DISCRETE","DRAW","EJECT","EMPTY","ENDFILE","ENTIER",
"ERLANG","ERROR","EVTIME","EXP","FILENAME","FIRST","FOLLOW",
"GETCHAR","GETFRAC","GETINT","HEAD","HISTD","HISTO","HOLD","IDLE",
"IMAGE","IMAGEFILE","INBYTE","INBYTEFILE","INCHAR","INFILE","INFRAC",
"INIMAGE","ININT","INREAL","INRECORD","INTEXT","INTO","ISOCHAR","ISOPEN",
"ISORANK","LAST","LASTITEM","LASTLOC","LENGTH","LETTER","LINE","LINEAR",
"LINESPERPAGE","LINK","LINKAGE","LN","LOCATE","LOCATION","LOG10","LOWCASE",
"LOWERBOUND","LOWTEN","MAIN","MAX","MAXINT","MAXLONGREAL","MAXRANK",
"MAXREAL","MIN","MININT","MINLONGREAL","MINREAL","MOD","MORE","NEGEXP",
"NORMAL","OPEN","OUT","OUTBYTE","OUTBYTEFILE","OUTCHAR","OUTFILE","OUTFIX",
"OUTFRAC","OUTIMAGE","OUTINT","OUTREAL","OUTTEXT","PASSIVATE","POISSON",
"POS","PRECEDE","PRED","PREV","PRINTFILE","PROCESS","PUTCHAR","PUTFIX",
"PUTFRAC","PUTINT","PUTREAL","RANDINT","RANK","REM","RESUME","SETACCESS",
"SETPOS","SIGN","SIMSET","SIMULAID","SIMULATION","SIN","SINH","SOURCELINE",
"SPACING","SQRT","START","STRIP","SUB","SUBEPSILON","SUC","SYSIN","SYSOUT",
"TAN","TANH","TERMINATED","TIME","UNIFORM","UPCASE","UPPERBOUND","WAIT"};
	
print_usage() {
    printf("Usage: [-notab] [-indent=#] [-nomagic] file [outputfile]\n");
	exit(1);
}

strcompair(s1,s2) char *s1,*s2; { char c;
	while ( (c = *(s1++)) == *(s2++) );
	return(c!='\0');
}

simfile(file,file2) char *file,*file2; {
	strcpy(file2,file);
	while ( *file != '.' && *file != '\0' ) file++;
	if (*file == '\0') strcat(file2,".sim");
}

char *parse(argc,argv) int argc; char *argv[]; {
	int i=0;
	if (argc>1) {
		while (++i<argc && *argv[i] == '-') { 
			if ( !strcmp("-notab",argv[i]) ) notab = 1;
			else if ( !strcmp("-nomagic",argv[i]) ) nomagic = 1;
			else if ( !strcompair("-indent=",argv[i]) )
				sscanf(argv[i],"-indent=%d",&indent);
		    else {
				printf("%s: unrecognized option `%s'\n",argv[0],argv[i]);
				exit(1);
			}
		} 
		if (i<=argc-1) {
			simfile(argv[i],input); 
			if (++i==argc-1) { simfile(argv[i],output); overwrite = 0; }
		} else print_usage();
	} else print_usage();
}

filecopy(outf,inf) char *outf,*inf; {
    FILE *inp,*outp,*fopen(); char c;
    if ((inp = fopen(inf,"r")) != NULL && (outp = fopen(outf,"w")) != NULL ) {
	while ((c = getc(inp)) != EOF ) putc(c,outp);
    } else printf("error\n");
    fclose(inp); fclose(outp);
}

char *lowcase(text) char *text; { char *pos;
    pos = text;
    while (*pos != '\0') {
	    if (isupper(*pos)) *pos = tolower(*pos); pos++;
    } return(text);
}

char *upcase(text) char *text; { char *pos;
    pos = text;
    while (*pos != '\0') {
	    if (islower(*pos)) *pos = toupper(*pos); pos++;
    } return(text);
}

strcopy(t1,t2) char *t1,*t2; { /* this strcpy doesn't terminate t1 with \0 */
	while (*t2 != '\0') *(t1++) = *(t2++);
}

edit(upitem,mode) char *upitem; int mode; {
char *item;
	if ( mode>=0 ) {
		item = upitem - uprecord + record;
		if (mode==0) lowcase(upitem);
		else if (mode==1) {
			lowcase(upitem); 
			if (islower(*upitem)) *upitem = toupper(*upitem);
		} strcopy(item,upitem);
	}
}

iskeyword(item) char *item; {
	int cmp, low = 0, high = NKEYWRDS-1, mid;
	while (low <= high) {
		mid = low + (high-low)/2;
		if ((cmp = strcmp(item,keywords[mid])) < 0) high = mid - 1;
		else if (cmp>0) low = mid + 1;
		else return(1);
	} return(0);
}

issysword(item) char *item; {
    int cmp, low = 0, high = NSYSWRDS-1, mid;
    while (low <= high) {
        mid = low + (high-low)/2;
        if ((cmp = strcmp(item,syswords[mid])) < 0) high = mid - 1;
        else if (cmp>0) low = mid + 1;
        else return(1);
    } return(0);
}

getitem() { char c; 
    *pos = saved;
    while ( isspace((c = *(pos++))) ); upitem = pos-1;
    if (c != '\0') {
	if (!isalpha(c) ) {
		if (isdigit(c)) while ( isdigit((c = *pos)) ) pos++;
		if (c=='.') while ( isdigit((c = *pos)) ) pos++;
	} else while ( isalnum((c = *pos)) || c=='_' ) pos++;
	saved = *pos; *pos = '\0';
    } return(c != '\0');
}

format(record,fp,indention) char *record,indention; FILE *fp; {
    int i; char c;
    if (indention) {
        while ( *record == ' ' || *record == '\t' ) record++;
		if (!notab) {
			i = (level*indent)/8; while (i-- > 0) putc('\t',fp);
		    i = (level*indent)%8;
		} else i = level*indent;
      	while (i-- > 0) putc(' ',fp);
    } while ( (c = *(record++)) != '\0' ) putc(c,fp);
}

main(argc,argv) int argc; char *argv[]; {
    FILE *inf,*outf,*fopen();
    char insingle,indouble,incomment,incom,indention;
	static int mode[] = { 2, 1, -1};
	int keyword = 0, sysword = 1, identifier = 2;
	int linenr = 0;
    insingle = 0; indouble = 0; incomment = 0; incom = 0;

	parse(argc,argv);
    if (overwrite) sprintf(output,"/tmp/simed%d",getpid());
    if ((inf = fopen(input,"r"))     != NULL && 
	(outf = fopen(output,"w")) != NULL) {
        while (fgets(record,RECLENGTH,inf) != NULL) { linenr++;
	    if (*record != '%' && *record != '\n' ) {
		strcpy(uprecord,record); upcase(uprecord);
		pos = uprecord; saved = *pos;
		level = level_; indention = !incomment;
		if (insingle || indouble) {
			printf("simed: <CR><LF> found in quotes at line %d; ",linenr-1);
			printf("operation aborted\n");
			exit(1);
		}
		while (getitem() != '\0') { 
            if ( *upitem == ';') incomment = 0;
            else if ( *upitem == '"' && !incomment && !insingle )
                indouble = !indouble;
            else if ( *upitem == '\'' && !incomment && !indouble)
                insingle = !insingle;
            else if (*upitem == '!' && !insingle && !indouble) 
				incomment = 1;

			if (incom && (*upitem == ';' || 
				!strcmp(upitem,"END") || !strcmp(upitem,"ELSE"))) {
				incomment = 0; incom = 0;
			}

		    if (isalpha(*upitem) && !insingle && !indouble && !incomment) {
				if (!strcmp(upitem,"COMMENT")) incomment = 1;
				else if (!strcmp(upitem,"BEGIN")) level_++; 
				else if (!strcmp(upitem,"END")) {
					level = --level_; incomment = 1; incom = 1;
				}
				if (!nomagic)
					if (iskeyword(upitem)) edit(upitem,mode[keyword]);
					else if (issysword(upitem)) edit(upitem,mode[sysword]);
					else edit(upitem,mode[identifier]);
		    }
		} format(record,outf,indention);
	    } else fputs(record,outf);
	} fclose(inf); fclose(outf);
	if (overwrite) {
		filecopy(input,output);
	}
	if (level>0) printf("simed: too many BEGIN's found.\n");
	else if (level<0) printf("simed: too many END's found.\n");
	exit(level);
    } else printf("simed: can't open `%s'\n",input); exit(1);
}
Technical Details
Message-ID: <DBswJx.89q@rug.nl>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113dd9,78baa38add2773f5,start
X-Google-Attributes: gid113dd9,public
From: RUTGES_ASS@RUGB38.bdk.rug.nl (ass. A.Rutges (I&O))
Subject: Source-code + options-txtfile included [SIMED.C]!!
Date: 1995/07/16
Message-ID: <DBswJx.89q@rug.nl>
X-Deja-AN: 106302983
sender: root@rug.nl (Operator)
x-news-reader: VMS NEWS v1.25
organization: Faculty of Management Studies, University of Groningen (NL)
newsgroups: bit.listserv.simula