11656Smckusick /* Copyright (c) 1979 Regents of the University of California */
21656Smckusick 
3*7969Smckusick static char sccsid[] = "@(#)GETNAME.c 1.9 08/29/82";
41656Smckusick 
51656Smckusick #include "h00vars.h"
6*7969Smckusick #include "libpc.h"
71656Smckusick 
81656Smckusick /*
91656Smckusick  * GETNAME - activate a file
101656Smckusick  *
111656Smckusick  * takes a name, name length, element size, and variable
121656Smckusick  * level and returns a pointer to a file structure.
131656Smckusick  *
141656Smckusick  * a new file structure is initialized if necessary.
151656Smckusick  * temporary names are generated, and given
161656Smckusick  * names are blank trimmed.
171656Smckusick  */
181656Smckusick 
195058Smckusic static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
205058Smckusic 
211656Smckusick struct iorec *
223006Smckusic GETNAME(filep, name, namlim, datasize)
231656Smckusick 
241656Smckusick 	register struct iorec	*filep;
251656Smckusick 	char			*name;
263006Smckusic 	long			namlim;
273006Smckusic 	long			datasize;
281656Smckusick {
293006Smckusic 	int		maxnamlen = namlim;
301656Smckusick 	struct iorec	*prev;
311656Smckusick 	struct iorec	*next;
321656Smckusick 	register int	cnt;
331656Smckusick 	struct iorec	locvar;
341656Smckusick 
35*7969Smckusick 	if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) {
36*7969Smckusick 		/*
37*7969Smckusick 		 * Close and immediately reactivate the file.
38*7969Smckusick 		 */
39*7969Smckusick 		PFCLOSE(filep);
40*7969Smckusick 		_actfile[filep->fblk] = filep;
41*7969Smckusick 		filep->funit &= (TEMP | FTEXT);
42*7969Smckusick 	} else {
431656Smckusick 		/*
44*7969Smckusick 		 * Initialize a new file record.
451656Smckusick 		 */
461656Smckusick 		filep->funit = 0;
471656Smckusick 		if (datasize == 0) {
481656Smckusick 			filep->funit |= FTEXT;
491656Smckusick 			datasize = 1;
501656Smckusick 		}
511656Smckusick 		filep->fsize = datasize;
521656Smckusick 		filep->fbuf = 0;
531656Smckusick 		filep->lcount = 0;
541656Smckusick 		filep->llimit = 0x7fffffff;
551656Smckusick 		filep->fileptr = &filep->window[0];
561656Smckusick 		/*
57*7969Smckusick 		 * Check to see if file is global, or allocated in
581656Smckusick 		 * the stack by checking its address against the
591656Smckusick 		 * address of one of our routine's local variables.
601656Smckusick 		 */
611656Smckusick 		if (filep < &locvar)
621656Smckusick 			filep->flev = GLVL;
631656Smckusick 		else
641656Smckusick 			filep->flev = filep;
655058Smckusic 		for (_filefre++; _filefre < MAXFILES; _filefre++)
665058Smckusic 			if (_actfile[_filefre] == FILNIL)
675058Smckusic 				goto gotone;
685058Smckusic 		for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++)
695058Smckusic 			if (_actfile[_filefre] == FILNIL)
705058Smckusic 				goto gotone;
715058Smckusic 		ERROR("File table overflow\n");
725058Smckusic 		return;
735058Smckusic gotone:
741656Smckusick 		filep->fblk = _filefre;
751656Smckusick 		_actfile[_filefre] = filep;
761656Smckusick 		/*
77*7969Smckusick 		 * Link the newrecord into the file chain.
781656Smckusick 		 */
791656Smckusick 		prev = (struct iorec *)&_fchain;
801656Smckusick 		next = _fchain.fchain;
811656Smckusick 		while (filep->flev > next->flev) {
821656Smckusick 			prev = next;
831656Smckusick 			next = next->fchain;
841656Smckusick 		}
85*7969Smckusick 		if (filep->flev == GLVL)
861656Smckusick 			/*
87*7969Smckusick 			 * Must order global files so that all dynamic files
88*7969Smckusick 			 * within a record are grouped together.
891656Smckusick 			 */
90*7969Smckusick 			while (next != FILNIL && (struct iorec *)filep > next) {
91*7969Smckusick 				prev = next;
92*7969Smckusick 				next = next->fchain;
932107Smckusic 			}
94*7969Smckusick 		filep->fchain = next;
95*7969Smckusick 		prev->fchain = filep;
961656Smckusick 	}
971656Smckusick 	/*
98*7969Smckusick 	 * Get the filename associated with the buffer.
991656Smckusick 	 */
1001656Smckusick 	if (name == NULL) {
1011656Smckusick 		if (*filep->fname != NULL) {
1021656Smckusick 			return(filep);
1031656Smckusick 		}
1041656Smckusick 		/*
105*7969Smckusick 		 * No name given and no previous name, so generate
106*7969Smckusick 		 * a new one of the form #tmp.xxxxxx.
1071656Smckusick 		 */
1081656Smckusick 		filep->funit |= TEMP;
1095058Smckusic 		sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk],
1105023Smckusic 		    getpid());
1113662Smckusic 		filep->pfname = &filep->fname[0];
1123662Smckusic 		return(filep);
1131656Smckusick 	}
1141656Smckusick 	/*
115*7969Smckusick 	 * Trim trailing blanks, and insure that the name
116*7969Smckusick 	 * will fit into the file structure.
1173662Smckusic 	 */
1183662Smckusic 	for (cnt = 0; cnt < maxnamlen; cnt++)
1193662Smckusic 		if (name[cnt] == '\0' || name[cnt] == ' ')
1203662Smckusic 			break;
1213662Smckusic 	if (cnt >= NAMSIZ) {
1223867Smckusic 		ERROR("%s: File name too long\n", name);
1233662Smckusic 		return;
1243662Smckusic 	}
1253662Smckusic 	maxnamlen = cnt;
1263662Smckusic 	filep->funit &= ~TEMP;
1273662Smckusic 	/*
128*7969Smckusick 	 * Put the new name into the structure.
1291656Smckusick 	 */
1301656Smckusick 	for (cnt = 0; cnt < maxnamlen; cnt++)
1311656Smckusick 		filep->fname[cnt] = name[cnt];
1321656Smckusick 	filep->fname[cnt] = '\0';
1331656Smckusick 	filep->pfname = &filep->fname[0];
1341656Smckusick 	return(filep);
1351656Smckusick }
136