1##########################################################
2#  MG: A UNIX Tool to generate new MAKEFILES from
3#      shell commands
4#        A. Kumar, ATT-BL, CB-2454, 3C-291
5#  THIS TOOL IS NOT SUPPORTED at this time
6#           Sept. 1, 1988
7#  However, please send any suggestions/bugs/enhancements
8#  to me (cblph!ajy) for future generics.
9##########################################################
10###############################################################
11#          AT&T BELL LABS - PROPRIETARY                  #
12#        Use pursuant to company Instructions                 #
13#      This source code is a proprietary information of       #
14#  AT&T and it is not for use or disclosure    #
15#  outside company except written agreement.                  #
16###############################################################
17BEGIN {
18#YOU COULD ADD to these lists for command recognition
19#do not give full path names, only the name of the command
20
21	CC_cmds="cc m32cc m15cc m32ld ld ceccc cecc m4"
22	AR_cmds="ar m32ar"
23	CP_cmds="cp mv ln"
24	CPMV_cmds="cpmv move"
25	CHMOD_cmds="chmod"
26	STRIP_cmds="strip m32strip"
27	MKLN_cmds="mkln 3b2mkln"
28	PAT_cmds="ppmkpat nepat pat"
29
30	if (dl == "")
31	{
32		dl="_"
33		debug_level=0
34	}
35	else
36	{
37		debug_level=dl
38	}
39	#changing this debug level will print more stuff.
40	#this is for future debugging...
41	if (oOPT == 0)
42	{
43		format_slash=0
44		MAXPERLINE=50
45	}
46	else
47	{
48		format_slash=1
49		MAXPERLINE=oOPT
50	}
51	#change this variable to =1 for backslash output...
52	DSTYLE="ajy"
53	Style_new = 0
54	if (sOPT == 0) Style=DSTYLE
55	else if (sOPT == 1)
56	{
57		print "new style"
58		Style_new = 1
59		Style = sOPT
60	}
61	else
62	{
63		print "using default version! Bad Style=" sOPT
64		Style=DSTYLE
65	}
66	########################
67	if (mOPT <= 0) FUZZYMATCH=2
68	else
69	{
70		printf("FUZZYMATCH=%d", mOPT)
71		FUZZYMATCH=mOPT
72	}
73	#FUZZYMATCH=4
74	#how far in the line could an executable exist on $0
75	# for example
76	# + x cc  will not be recoginized with FUZZYMATCH=2
77	# but
78	# x cc will be recoginixzed...
79	debug(1, "%s \n", "pass 1: begin", 0);
80	if (varfile == "") varfile="_"
81	if (invarfile == "" ) invarfile="_"
82	if (premakefile == "" ) premakefile="_"
83	#value TO define name/variable function
84	if (fv2n == "" ) fv2n="_"
85	NULL=""
86	#used by var_i,val_i arrays.
87	iIN=0
88	iCMD=0;
89	CMD = 0
90	var[CMD] = "COMMANDS"
91	Vmake="M"
92	iMAKE = 0
93	#edge list for include files (i,j)
94	# where i and j are include directories.
95	iEDGES = 0
96	# used by lib directory lists
97	iaEDGES = 0
98	resth["1"]=""
99	resta["1"]=""
100	resto["1"]=""
101	restc["1"]=""
102	iINCDIR=0
103	iLIBDIR=0
104	iCMD=0  #noof commands in the install script.
105	kCMD=0  #noof comamnds in the install script. insorder[1..kCMD]
106	CONINVAR="INSTDIR"
107	## if you have an initial list of vars, put in invar array.
108	#shell variables are input here,
109	XCMDNAME="CMD"
110	VXCMDNAME="VCMD"
111	resto["1"]=""
112	restc["1"]=""
113
114	iVAR++
115	#assert iVAR=1
116	PAT="PATTERNS" #hardcoded at .ALLlist #MAJOR KLUDGE
117	var[1]=PAT
118	val[1]=""
119	shenv();
120	varfill();
121	invarfill();
122
123	#lint files in LINTFILES
124	iVAR++
125	iLINT=iVAR
126	var[iLINT]="LINTFILES"
127	val[iLINT]=""
128
129	###test
130}
131END {
132	debug(1, "%s \n", "pass 2: begin", 0);
133	ts_makefile();
134	# build the variable list, typically install dirs INSTDIR1, ...and others
135	bld_invar();
136	#builds Sdep lines for SOURCE.a..etc. and also variables.
137	bld_SOURCE();
138	# build COMMAND and LIBxxx and LIBxxxFILES variables
139	# var is only concerned with variables and not with any install dirs
140	bld_var();
141	# build the dependency lines, such as .ALL:, a:: rules etc
142	bld_dep();
143	# print the above list
144	# print_invar MUST follow all bld's because all install directories not
145	# used are not printed by default
146	print_invar();
147	# build and print the SOURCE, .a lists
148	print_SOURCE();
149	# print the above
150	print_var();
151	print_dep();
152	# print the above dep lines
153	print_install2(); #print the install directory, after gathering common installs
154		 # also, as a sideeffect generate .CMD options
155print_CMD(); #print the .CMD command set
156}
157
158#chmod xxx tar  tar tar ...
159#  j    j+1   j+2
160#strip tar  tar tar ...
161#  j   J+1
162#mkln tar tar tar ...
163#  j  j+1
164{ #BEGIN OF MAIN PROGRAM
165lmatch=0
166debug(9, "L:%s\n", $0)
167
168for (idi=1; ((idi<=FUZZYMATCH) && (idi <= NF)) ; idi++){
169	#global variable cmdtype is set by get_cmds.
170	debug(1, "idi=%d $idi=%s\n", idi, $idi);
171	cmd=get_cmds($idi)
172	if (cmd == "")
173		continue
174	#reaching here means command is recognized, get index
175	debug(1, "cmd=%s type=%s idi=%d\n", cmd, cmdtype, idi)
176	iindex=idi
177	if (cmdtype == "CC_cmds") {
178		call_CC_cmds($0, NF, cmd, cmdtype, iindex)
179		lmatch=1
180		break
181	}
182	else if (cmdtype == "AR_cmds") {
183		call_AR_cmds($0, NF, cmd, cmdtype, iindex)
184		lmatch=1
185		break
186	}
187	else if ((cmdtype == "CP_cmds") || (cmdtype=="CPMV_cmds")) {
188		call_CP_cmds($0, NF, cmd, cmdtype, iindex)
189		lmatch=1
190		break
191	}
192	else if ((cmdtype == "CHMOD_cmds") || (cmdtype == "STRIP_cmds") || (cmdtype == "MKLN_cmds") ) {
193		call_CH_cmds($0, NF, cmd, cmdtype, iindex)
194		lmatch=1
195		break
196	}
197	else if (cmdtype == "PAT_cmds" ) {
198		call_PAT_cmds($0, NF, cmd, cmdtype, iindex)
199		lmatch=1
200		break
201	}
202	else {
203		print "[internal error]" 1023394830498
204		debug(1, "internal error\n");
205	}
206}#end of for loop for checking args..
207	if (lmatch == 0) {
208		#print "ignored:\n" $0
209		ignore($0)
210	}
211} #END OF MAIN PROGRAM
212
213#returns the actual command matched
214#cmd from *_cmds arrays defined earlier
215#GLOBALS:also cmdtype is set
216function get_cmds(str,	i,j,k,jj,ii,kk,arr,tmp)
217{
218debug(9, "get_cmds %s\n", str)
219basename(str); bstr=base
220dirname(str); dstr=dir
221if (isin(CC_cmds, base) > 0 ) {
222	cmdtype="CC_cmds"
223	cmd=base
224	return(cmd)
225}
226if (isin(CP_cmds,base) > 0 ) {
227	cmdtype="CP_cmds"
228	cmd=base
229	return(cmd)
230}
231if (isin(CPMV_cmds, base) > 0 ) {
232	cmdtype="CPMV_cmds"
233	cmd=base
234	return(cmd)
235}
236if (isin(AR_cmds, base) > 0 ) {
237	cmdtype="AR_cmds"
238	cmd=base
239	return(cmd)
240}
241if (isin(CH_cmds, base) > 0 ) {
242	cmdtype="CH_cmds"
243	cmd=base
244	return(cmd)
245}
246if (isin(CHMOD_cmds, base) > 0 ) {
247	cmdtype="CHMOD_cmds"
248	cmd=base
249	return(cmd)
250}
251if (isin(STRIP_cmds, base) > 0 ) {
252	cmdtype="STRIP_cmds"
253	cmd=base
254	return(cmd)
255}
256if (isin(MKLN_cmds, base) > 0 ) {
257	cmdtype="MKLN_cmds"
258	cmd=base
259	return(cmd)
260}
261if (isin(PAT_cmds, base) > 0 ) {
262	cmdtype="PAT_cmds"
263	cmd=base
264	return(cmd)
265}
266cmd=""
267cmdtype=""
268return(cmd);
269}#end of function 1
270
271#/chmod / || /strip / m32strip or mkln
272function call_CH_cmds(lin, nlin, cmd, cmdtype, iindex,  i,j,k,arr,tmp,opt)
273{
274	if (cmdtype == "CHMOD_cmds") {
275		opt=$(iindex+1)
276		starti=iindex+2
277	}
278	else if (cmdtype== "STRIP_cmds") {
279		opt=""
280		for (k=iindex+1; k<=NF; k++)
281		 	if ($(k) ~ /^\-.*/ )
282				opt=opt " " $(k)
283			else {
284				starti=k
285				break
286			}
287	}
288	else if (cmdtype == "MKLN_cmds") {
289		opt=$(iindex+1)
290		starti=iindex+1
291	}
292	debug(2, "cmd=%s opt=%s starti=%d\n", cmd, opt, starti)
293	for (k=starti; k <= NF; k++) {
294		tar = $(k)
295			#find if the target already exists?
296			#loop over targets :
297		if  (tar in target)	{
298				#this is a change on that target...
299				#must have an installation ?
300			basename(tar); src=base
301				#src=  src is the ???
302			for(i=1; icmd[src,i]!=""; i++);
303				#KLUDGE : $(<) or $(>) find it from target.
304			if (src == tar)
305				direct = "$(>)"
306			else
307				direct = "$(<)"
308			icmd[src, i]=sprintf("\t %s %s %s", vcommand(cmd), opt, direct);
309			icmd[src, i+1] = "" #mark the end of list!
310			debug(1, "mod on target=%s icmd[%s,%d]\n", tar, src,i);
311		}
312		else if ((ic=destins(tar)) > 0 ) {
313			for(i=1; kcmd[ic, i]!=""; i++);
314				kcmd[ic, i] =sprintf("\t %s %s $(<)", vcommand(cmd), opt);
315			debug(1, "destins\n");
316
317		} else {
318#now the object is neither in target list, nor in install list, so must
319#be something not dependent on anything, need a ins[] line here?
320		if (cmdtype == "MKLN_cmds" ) {
321			val[iLINT]=val[iLINT] " " nbasename(any2ln(tar))
322			var_cnt[iLINT]++
323			spath(tar)
324		 } else {
325			print "[NOTFOUND] confused at line:" $0 "  target=? " tar
326			print "no dependency why have it in this makefile"
327			ignore($0)
328		 } #mkln chk.
329	} #the last else
330       } # for all targets
331} #e_o_f:call_CH_cmds
332
333
334function call_CC_cmds(lin, nlin, cmd, cmdtype, iindex,  i,j,k,arr,tmp,opt)
335{
336	j=smatch($0, "-o")
337	if (j > 0) {
338		#this is a cc line with -o
339		debug(5, "cc -o line%s \n", $0)
340		tar=$(j+1)
341		if (tar ~ /^\.\/.*/ )
342			sub(/^\.\//,"",tar)
343		tar_in_ins(tar)
344		debug(5, "checked tarinins\n");
345		if (tar in target) {
346			print "ERROR: multiple target definition: " tar
347			ignore($0)
348			next
349		}
350		else {
351			target[tar] = tar
352			tlist = tlist " " tar
353		}
354		#convert all .o to .c
355		#also any .c's are put in the _cli list.
356		#side effects are to build .SOURCE.c,.o in future versions
357		dot_o2c($0)
358		cdep[tar] = _cli
359			#process any libraries
360			#side effects are to build .SOURCE.a (ok)
361		dot_as($0)
362		adep[tar] = _ali
363			#process any header file dependency
364			#side effects are to build .SOURCE (ok)
365		dot_Is($0)
366		dflags($0)
367		if (_Dli != "")  {
368			print "(FLAG) rule"
369			#new dependency is here
370			Ddep[tar] = _Dli
371		}
372	} #end of -o processing
373	else { # this is a normal compilation, NO a.outs please
374		# usually -c option
375		dot_o2c($0)
376		dot_Is($0)
377		dflags($0)
378		if (_Dli != "")  {
379#gsf#			print ".o : rule"
380			j1=smatch($0, "-c")
381			if (j1 > 0) {
382				#this is a cc line with -c
383				#may want to do a check if more .c on line
384				for(ii=1; ii<=NF; ii++) {
385					if (isc($ii) > 0 ) {
386						basename($ii); tar=base
387						gsub(/\.c$/,"",tar)
388						tar=tar ".o"
389#gsf#						print ".o " tar _Dli
390						target[tar] = tar
391						cdep[tar] = ""
392						adep[tar] = ""
393						Ddep[tar] = _Dli
394					}
395				}
396				#tar=
397			} else {
398#gsf#				print "what is =" $0
399			}
400			#new dependency is here
401		}
402	} #end of -c processing
403debug(5, "exit:call_CC_cmds()\n");
404}#e_o_f:CC_cmds
405
406#if ($0 ~ /mv / || /cp / || /ln / || /move / || /cpmv / )
407function call_CP_cmds(lin, nlin, cmd, cmdtype, iindex,  i,j,k,arr,tmp,opt, kloop, command, src, dest, starti, ddest, dsrc, bdest, bsrc, opt, u)
408{
409	command=cmd
410	j=iindex
411	if (cmdtype == "CP_cmds") {
412				src=$(j+1)
413				dest=$(j+2)
414				starti=j+2
415	}
416	if (cmdtype == "CPMV_cmds") {
417					src=$(j+1)
418					dest=$(j+5)
419					starti=j+5
420	}
421	opt=""
422            for(kloop=starti;kloop <= NF; kloop++) {
423		dest=$(kloop)
424		if (dest ~ /^\-.*/ ) {
425			opt=opt " " dest
426			continue
427		}
428		debug(1,"%s -> %s\n", src, dest);
429		basename(src);bsrc=base
430		dirname(src); dsrc=dir
431		#print bsrc "\ " dsrc
432		if (dsrc== "./") dsrc=""
433		if (tOPT == 1) { #test the destination direcotry
434			debug(7,"1: %s %s\n", dsrc, ddest);
435			if (isfile(dest) > 0)  {
436				basename(dest); bdest= base
437				dirname(dest); ddest=dir
438			debug(7,"2: %s %s\n", dsrc, ddest);
439			} else {
440				bdest=bsrc
441				ddest=dest
442				print "WARNING: destination is a dir=" dest
443			}
444		} else { #donot test any directories, assume fullpath name
445				basename(dest); bdest= base
446				dirname(dest); ddest=dir
447			debug(7,"3: %s %s\n", dsrc, ddest);
448		}
449			if (ddest== "./") ddest=""
450			if (ddest ~ /^\.\/.*/) {
451				sub(/^\.\//,"", ddest)
452			}
453			if ((bdest== ".") || (bdest == "") || (bdest == " ") ) bdest=bsrc
454			debug(7,"8: %s %s\n", dsrc, ddest);
455		kCMD++;
456#check to see if bsrc is built here, and any precommands exist.
457#such as a m32strip, chmod etc.
458		if (icmd[bsrc,1] != "" ) {
459			print "bsrc was modified"
460			for(u=1; icmd[bsrc, u]!=""; u++) {
461				for(k=1; kcmd[kCMD,k]!=""; k++);
462				kcmd[kCMD,k] = icmd[bsrc,u];
463				#print "putting:" icmd[bsrc,u] " TO " k
464			}
465			for(u=1; icmd[bsrc,u]!=""; u++)
466				icmd[bsrc,u]="" #empty it out
467		}
468		#call subinvar simply to increment the count, to make sure these
469		# variables are used...
470			debug(7,"sub: %s %s\n", dsrc, ddest);
471			subinvar(dsrc); subinvar(ddest);
472			ins[dsrc, bsrc, ddest, bdest] = kCMD
473			insorder[kCMD]=dsrc SUBSEP bsrc SUBSEP ddest SUBSEP bdest
474			instype[kCMD]="install"
475			#print "insorder" kCMD " {}-"insorder[kCMD]
476
477			for(k=1; kcmd[kCMD,k]!=""; k++);
478			if (cmdtype == "CPMV_cmds")
479			kcmd[kCMD,k] = sprintf("\t %s $(>) %s %s %s $(<)",
480				vcommand(command), $(j+2), $(j+3), $(j+4));
481			else if (cmdtype == "CP_cmds" )
482			kcmd[kCMD,k] = sprintf("\t %s  $(>) $(<)",
483				vcommand(command) )
484			else  {
485				plogic("arrr!WHATCMD");
486				debug(1, "failed terribly %s\n", cmdtype);
487			}
488
489		instdir[ddest]=""
490           } #for loop for each destination
491} #e_o_f:CP_cmds
492#/ar /
493function call_AR_cmds(lin, nlin, cmd, cmdtype, iindex,  i,j,k,arr,tmp,opt, kloop, command, src, dest, starti, ddest, dsrc, bdest, bsrc, opt, u,jj)
494{
495		j=iindex
496		tar=$(j+2)
497		if (tar in target) {
498			#print "Warning:multiple archive (ar) target: " tar
499			dot_o2c($0)
500			chklist = _cli
501			x=split(chklist,  arr, " ")
502			if (x > 0 ) {
503				for (jj=1; jj<=x; jj++)
504					if (isin(cdep[tar], arr[jj]) <  0) {
505						newstr=cdep[tar] " " arr[jj]
506						cdep[tar] = newstr
507					}
508			}
509		} else {
510			target[tar]=tar
511		#AR command processing, build the list
512		dot_o2c($0)
513		cdep[tar] = _cli
514		}
515}#e_o_f:AR_cmds
516
517
518function call_PAT_cmds(lin, nlin, cmd, cmdtype, iindex,  i,j,k,arr,tmp,opt)
519{
520			cin="";
521			cout="";
522			find_in_out($0)
523			debug(1, "in=%s out=%s\n",  cin , cout)
524			rule_pat($0, cmd, iindex)
525}
526
527
528function ignore(thisline)
529{
530	print "note " thisline
531}
532function print_CMD(s, ar, cmd, ic)
533{
534	for (s in cmdl) {
535		printf("\n\n")
536		cmd=sprintf(".CMD.%d",cmdl[s])
537		printf("%s : .USE\n", cmd)
538		split(s, ar, "/")
539		printf("\tcpmv $(>) %s %s %s $(<)", ar["1"], ar["2"], ar["3"])
540		printf("\n")
541	}
542
543	for (s in ccmdl) {
544		printf("\n\n")
545		printf(".CMD.%d : .USE\n", s)
546		ic = ccmdl[s] # the iindex for some icmd arrary
547			for(k=1; icmd[ic,k]!=""; k++)
548					printf("%s\n", icmd[ic,k]) ;
549
550
551	}
552	for (s in kcmdl) {
553		ic = kcmdl[s] # the index for some icmd arrary
554		#print "s="s "ic="ic
555	        if (s in normal_kkcmd) {
556		printf("\n\n")
557		printf(".%s.%d : .USE\n",XCMDNAME, s)
558			for(k=1; kcmd[ic,k]!=""; k++)
559					printf("%s\n", kcmd[ic,k]) ;
560		}else;
561			#print "not used"
562	}
563	for (s in kcmdl) {
564			ic = kcmdl[s] # the index for some icmd arrary
565		#print "s="s "ic="ic
566		if (s in virtual_kkcmd) {
567			printf("\n\n")
568			printf(".%s.%d : .USE\n",VXCMDNAME, s)
569			for(k=1; kcmd[ic,k]!=""; k++)
570					printf("%s\n", vircnv(kcmd[ic,k]) ) ;
571		}else ;
572			#print "not used"
573	}
574}
575#prints the SOURCE.h, .a lists
576function bld_SOURCE()
577{
578top_sort("Check: INCLUDE directory list in SOURCE.h", snode, enode, iEDGES);
579hdrlist = _SOURCEh
580notonlist(hdrlist, incdir, iINCDIR, resth)
581if ((hdrlist == "" ) && (resth["1"] == "" ) )
582	#dont print SOURCE.h
583	#print "empty SOURCE.h"
584	#noop();
585	Sdeph=""
586else {
587#	printf(".SOURCE.h:%s %s\n\n", v2(hdrlist), v2(resth["1"]))
588	Sdeph=sprintf(".SOURCE.h:%s %s\n\n", v2(hdrlist), v2(resth["1"]))
589}
590top_sort("Check: LIB directory in SOURCE.a", sanode, eanode, iaEDGES);
591hdrlist = _SOURCEh
592debug(1,"hdr=%s", hdrlist);
593notonlist(hdrlist, libdir, iLIBDIR, resta)
594if ((hdrlist == "" ) && (resta["1"] == "" ) )
595	#dont print SOURCE.h
596	#print "empty SOURCE.a"
597	#noop();
598	Sdepa=""
599else
600	#printf(".SOURCE.a:%s %s\n\n", v2(hdrlist), v2(resta["1"]))
601	Sdepa=sprintf(".SOURCE.a:%s %s\n\n", v2(hdrlist), v2(resta["1"]))
602top_sort("Check: OBJ directory in SOURCE.o", sonode, eonode, ioEDGES);
603hdrlist = _SOURCEh
604notonlist(hdrlist, objdir, iOBJDIR, resto)
605if ((hdrlist == "" ) && (resto["1"] == "" ) )
606	#dont print SOURCE.h
607	#print "empty SOURCE.a"
608	#noop();
609	Sdepo=""
610else
611	Sdepo=sprintf(".SOURCE.o:%s %s\n\n", v2(hdrlist), v2(resto["1"]))
612	#printf(".SOURCE.o:%s %s\n\n", v2(hdrlist), v2(resta["1"]))
613top_sort("Check: .c directory list in SOURCE.c", sonode, eonode, ioEDGES);
614hdrlist = _SOURCEh
615notonlist(hdrlist, dotcdir, iDOTCDIR, restc)
616if ((hdrlist == "" ) && (restc["1"] == "" ) )
617	#dont print SOURCE.h
618	#print "empty SOURCE.a"
619	#noop();
620	Sdepc=""
621else
622	Sdepc=sprintf(".SOURCE.c:%s %s\n\n", v2(hdrlist), v2(restc["1"]))
623	#printf(".SOURCE.c:%s %s\n\n", v2(hdrlist), v2(restc["1"]))
624}
625function print_SOURCE()
626{
627if (Sdeph != "")
628	printf("%s", Sdeph)
629if (Sdepa != "")
630	printf("%s", Sdepa)
631if (Sdepc != "")
632	printf("%s", Sdepc)
633if (Sdepo != "")
634	printf("%s", Sdepo)
635}
636
637#print install directory variable names
638function print_invar(vardir)
639{
640for (vardir in invar ) {
641	if (vardir in noprintvar) { #if on this list of donotprint
642		debug(5,"noprint: %s\n", vardir)
643		continue
644	}
645	if (invar_cnt[vardir] <= 0 ){
646		debug(5, "nouse: %s\n", vardir)
647		continue
648	}
649	if (invar[vardir] == "" )
650		printf("%s=%s\n", vardir, "./") ;
651	else
652		printf("%s=%s\n", vardir, invar[vardir]) ;
653}
654printf("\n") ;
655}
656
657function bld_invar(i,j,dir, found)
658{
659#instdir[dir(path] = VAR
660#invar[VAR] = val
661#generate a define, and assign it
662#longest common substring, number of times used etc.
663
664for (dir in instdir ) {
665	xdir=dir
666	found=0
667	for (vardir in invar)
668		if (dir == invar[vardir] ){
669			#print "1"
670			found=1
671		}
672		else if (dir"/" == invar[vardir] ) {
673			#print "2"
674			found=1
675		}
676		else if (compress(xdir) == invar[vardir] ) {
677			#print "3"
678			found=1
679		}
680		else if (dir !=  subinvar(dir) ) {
681			#print "v sub match" dir "->" subinvar(dir)
682			found=1
683		}
684	#either found or not found
685	if (found == 0 ) { #not found, build a new name
686		iINVAR++
687		newvar = sprintf("%s%s",CONINVAR, iINVAR);
688		invar[newvar] = compress(dir)
689		invar_cnt[newvar]++
690	#	instdir[dir] = compress(newvar)
691		debug(6, " new var %s%s=%s\n", CONINVAR, iINVAR, dir);
692	} else
693		invar_cnt[vardir]++
694		#instdir[dir] = vardir
695	} # for
696}
697
698#add a new invar[xx] = dir type variable ...
699function add_invar(dir, i,j,dir, found)
700{
701	found=0
702	for (vardir in invar)
703		if (dir == invar[vardir] )
704			found=1
705	#either found or not found
706	if (found == 0 ) { #not found, build a new name
707		iINVAR++
708		newvar = sprintf("%s%s",CONINVAR, iINVAR);
709		invar[newvar] = compress(dir)
710	#	instdir[dir] = newvar
711		debug(6, "new var %s%s=%s\n", CONINVAR, iINVAR, dir);
712	} else
713		#instdir[dir] = vardir
714}
715
716#version 2.0 nmake install uses an explicit install: dependency rule
717# print_install2(	i,j, dir, found[A#version 2.0 nmake install uses an explicit install: dependency r
718function print_install2(	i,j, dir, found, vardir, ic, nm)
719{
720debug(5, "install2: enter\n");
721#first build install: dependency line
722#next print each specific dependency line as a .CMD command
723#may be in future combine all installdirectories as one variable.
724#in ins, all extra commands.
725install_list = install_list " " v_install_list
726	for (iorder=1; iorder<=kCMD ; iorder++) {
727		jj=insorder[iorder]  #jj is the unique install option
728		itype = instype[iorder]
729		jjn = split(jj, arr, SUBSEP);
730		debug(9, "jj=%s\n", jj);
731		dir = arr["3"]
732			#AJYBLANK printf("\n");
733			nm=subinvar(dir);
734			if (dir != nm ) {
735				#print "sub: " dir " -> " nm
736				found=1
737				myvardir=compress(nm) #removes "//" etc.
738			}
739			else
740			{
741			found=0
742			for (vardir in invar)
743				if  (invar[vardir] == dir)
744#KLUDGE		             (compress(invar[vardir]) == compress(dir))
745					{
746                                   print "FOUND" vardir ":"dir":" invar[vardir]
747					if ((dir == "") || (dir== "./")  )
748						myvardir=""
749					else
750						myvardir="$("vardir ")"
751					found = 1
752					invar_cnt[vardir]++
753						break;
754				}
755			if (found ==1)
756				print ".." myvardir
757			}
758			if ((myvardir == "") || (myvardir == "./") )
759				pmyvardir=""
760			else
761				pmyvardir= compress(myvardir) "/"
762			debug(9, "install2:pmy=%s b=%s\n",  pmyvardir, arr["4"])
763			if ((found == 1) && (itype == "install" ) ) {
764				if ((Style_new==1) &&(virtual_ins[iorder]==1) )
765					install_list=install_list " " virtual_n(pmyvardir,arr["4"], arr["2"])
766				else
767			   	 install_list=install_list " " pmyvardir arr["4"]
768			   	 #install_list=install_list " $("tayvardir")/"arr["4"]
769                            #printf("\nINSTALLDIR=$(%s)\n", myvardir)
770			} else {
771				;
772				#print "WARNING: empty install: rule"
773			}
774				#print "LOGIC ERROR:call author immediately"
775				#printf("\nINSTALLDIR=%s\n", dir)
776		# now we have the install line
777		# now find the actual load line
778		if  (itype=="pattern") { #KLUDGE =i="" {
779			if ((arr["1"] == "") || (arr["1"] == "./") || (arr["1"] == "." ) ) #no sub
780	 		printf("%s : %s", arr["4"], arr["2"]) ;
781			else
782	 		printf("%s : %s", arr["4"], subinvar(arr["1"] "/" arr["2"]) ) ;
783		}
784		if ((itype == "install") && (Style_new == 1) && (virtual_ins[iorder]==1) ) {
785			if ((arr["1"] == "") || (arr["1"] == "./") ) #no sub
786	 		printf("%s : %s%s", virtual_g(pmyvardir, arr["4"], arr["2"]), pmyvardir, arr["4"] ) ;
787			else
788			printf("%s : %s", virtual_g(pmyvardir, arr["4"], arr["2"]), compress(subinvar(arr["1"]) "/" arr["2"]) ) ;
789		}
790
791		if (((itype == "install") && (Style_new != 1) ) || ((Style_new==1) && (virtual_ins[iorder] != 1) && (itype== "install"  )) ) {
792		if ((arr["1"] == "") || (arr["1"] == "./") ) #no sub
793	 		printf("%s%s : %s",pmyvardir, arr["4"], arr["2"] ) ;
794			else
795			printf("%s%s : %s", pmyvardir, arr["4"], compress(subinvar(arr["1"]) "/" arr["2"]) ) ;
796		}
797		kkcmd = ins[jj]
798		ic = getkcmdi(kkcmd);
799		if  ((Style_new==1) && (virtual_ins[iorder] == 1) ) {
800			printf(" .%s.%d",VXCMDNAME, ic) ;
801			printf(" .VIRTUAL\n") ;
802			virtual_kkcmd[ic]=1
803		} else {
804			normal_kkcmd[ic]=1
805			printf(" .%s.%d",XCMDNAME, ic) ;
806			printf("\n") ;
807		}
808	}
809#now the install line:
810if (format_slash == 0) { #this is
811	printf("\ninstall: %s\n", install_list) ;
812} else {
813	printf("\ninstall: \\\n")  ;
814		nl(install_list) ;
815}
816}
817function print_install(	i,j, dir, found, vardir, ic)
818{
819	print "&*^%$#FATAL ERROR : nosuchfunction exists"
820}
821
822function print_dep(tar, i)
823{
824for (i=0; i<=iDEP; i++)
825	if ((ldep[i] != "") && (COMD[ldep[i]] != 1) && (iso(ldep[i]) <= 0) )
826		ALLlist = ALLlist " " ldep[i]
827#gsf#	if ((val[CMD] != "") && (COMD[ldep[i]] != 1))
828#gsf#		ALLlist = ALLlist " $(COMMANDS) "
829	#MAJOR KLUDGE::
830	if ((var[1] == PAT) && (val[1] != "") ) {
831		ALLlist = ALLlist " $(PATTERNS) "
832	}
833	for (i=0; i<=iDEP; i++)
834		if (ldep[i] != "" && rdep[i] !~ "(.*)") {
835			#distinguish between :: and : rules
836				if (iso(ldep[i]) > 0 )
837					operator=":"
838				else
839					operator="::"
840			if (format_slash==0) { #hello
841					printf("%s %s %s %s\n\n", ldep[i],
842						f_ldep_more(i),
843						operator, rdep[i])
844			} else {
845				#use "backslash for formatting.
846				printf("%s %s %s \\\n",  ldep[i],
847					f_ldep_more(i), operator )
848				nl(rdep[i])
849			}
850		}
851
852
853	if (val[iLINT]!="")  {
854		printf("slint: %s\n\n", "$(" var[iLINT] ")" ) ;
855		print "WARNING: following rule be defined:"
856		print "				*.ln : *.c  "
857		print "					mkln $(>) "
858	}
859	for (i=0; i<=viDEP; i++)
860		if (vldep[i] != "" ) {
861			printf("%s : %s .VIRTUAL\n", vldep[i], vrdep[i]) ;
862			basename(vrdep[i])
863			for (j=1; icmd[base, j] != ""; j++)
864				printf("%s\n", vircnv(icmd[base,j]) );
865		}
866
867
868}
869#builds dep lines
870#uses global lists
871function bld_dep(	i, tar, tmp, blib, jj, jjn, arr, tmp,iorder)
872{
873for (tar in target)
874	if (isa(tar) > 0) {
875		blib  = tar
876		sub(/\.a/, "", blib)
877		sub("^lib","", blib)
878		i = ivar[tar]
879		iDEP++
880		ldep[iDEP] = "$(" var[i] ")"
881		rdep[iDEP] = val[i+1] #KLUDGE : i+1 is libfiles.
882	} else {
883		# a command from .c,.o
884		#i = ivar[tar]
885		iDEP++
886		ldep[iDEP] = subinvar(tar)
887		rdep[iDEP] = cdep[tar] " " adep[tar] " " Ddep[tar]
888		COMD[subinvar(tar)]=1
889		basename(tar)
890		#XAJY print tar "**" base "***" icmd[tar,1]
891		if (icmd[base,1] != "") {
892			viDEP++
893			print "NEED A VIRTUAL=%s", base
894			vldep[viDEP] = "VV_"base
895			vrdep[viDEP] = ldep[iDEP]
896			v_install_list = v_install_list " " vldep[viDEP]
897		}
898	}
899
900#XAJY for(i=0;i<=iDEP; i++) print i"="ldep[i]
901#go thru all targets...if new style
902if (Style_new==1) {
903	for (iorder=1; iorder<=kCMD ; iorder++) {
904		jj=insorder[iorder]  #jj is the unique install option
905		itype = instype[iorder]
906		jjn = split(jj, arr, SUBSEP);
907			##ins[dsrc, bsrc, ddest, bdest] = kCMD
908		dsrc=arr["1"]
909		bsrc=arr["2"]
910		ddest=arr["3"]
911		bdest=arr["4"]
912				#print "NEW STYLE:" iDEP
913				for(jjj=0;jjj<=iDEP;jjj++) {
914				#print "LDEP=" ldep[jjj]
915				basename(ldep[jjj])
916				dirname(ldep[jjj])
917				if ((dir=="") || (dir=="./")) dir=""
918				if (!(dir ~ /^\$.*/) 	&& (dir != "") )
919					dir=compress(subinvar(dir))
920				if ((dsrc=="") || (dsrc=="./")) dirins=dsrc
921				if (!(dirins ~ /^\$.*/) && (dirins != "") )
922					dirins=compress(subinvar(dir))
923				debug(1, "CK:dsrc=%s bsrc=%s dir=%s base=%s\n",
924					dirins, bsrc, dir, base)
925				if ((dirins == dir) && (bsrc == base) ) {
926					print "virtual:" dsrc "-" bsrc
927					virtual_ins[iorder]=1
928				destdir=ddest
929				if ((destdir=="") || (destdir=="./")) destdir=""
930				if (!(destdir ~ /^\$.*/) 	&& (destdir != "") )
931					destdir=compress(subinvar(destdir))
932
933					if (destdir != "")
934						ldep_more[jjj]=ldep_more[jjj] ", " compress(destdir "/" bdest)
935					else
936						ldep_more[jjj]=ldep_more[jjj] ", " compress(bdest)
937
938				}#if match!
939				}#forloop
940		}#fo all install tars
941	}
942}
943
944function print_var(i)
945{
946for (i=2; i<=iVAR; i++) {
947	if (var[i] in noprintvar) { #if on this list of donotprint
948		debug(5, "noprint: %s\n", var[i])
949		continue
950	}
951	if (var_cnt[i] <= 0 ) { #if not used
952		debug(5, "nouse: %s\n", var[i])
953		continue
954	}
955	if (var[i] != "" ) #if no variable oops
956			if (val[i] != "" ) # print nonnull fields
957				printf("setv %s %s\n", var[i], val[i])
958}
959}
960
961#builds var list
962#uses global lists...
963function bld_var(	i, tar, tmp, blib)
964{
965for (tar in target  ) {
966	tar_in_ins(tar);
967	if (isa(tar) > 0) {
968	    #print tar "is lib"
969	    #is a library!
970	     if (tar in ivar)
971		printf "multiple target definition: " tar
972	      else {
973		blib  = tar
974		basename(blib); blib=base;
975		sub(/\.a/, "", blib)
976		sub("^lib","", blib)
977		iVAR++
978		ivar[tar]  = iVAR
979		var[iVAR]  = "LIB" blib
980		val[iVAR]  = "lib" blib ".a"
981		var_cnt[iVAR]++
982		iVAR++
983		#ivar[tar]  = iVAR
984		var[iVAR] = "LIB" blib "FILES"
985		val[iVAR] = cdep[tar]
986		var_cnt[iVAR]++
987		noprintvar[var[iVAR]]=""
988	     }
989	}else  if (iso(tar) > 0 ) {
990		; #donot put in commands line, nouse
991	} else {
992		#if command from .c,.osas usual
993		# var[CMD] = "CMDS" as defined
994		#substitute with subinvar, if tar has a dir?
995		dirname(tar);
996		basename(tar);
997		nm=subinvar(dir)
998		if (dir == nm )
999			val[CMD] = val[CMD] " " tar
1000		else {
1001			print "dir match by (sub)invar"
1002			val[CMD] = val[CMD] " " compress(nm "/"base)
1003		}
1004		var_cnt[CMD]++
1005		COMD[tar] = 1
1006		#iVAR++
1007		#ivar[tar]  = iVAR
1008		#var[iVAR] = "CMD" tar "FILES"
1009		#val[iVAR] = cdep[tar]
1010	}
1011  } #for
1012
1013}
1014function chkcmd(s, n, t, 	i, bas)
1015{
1016	for(i=1; i<=n; i++) {
1017		basename($i); bas=base;
1018		#print "bas=" bas " test=" t
1019		if(bas == t)
1020	 		return i
1021	}
1022	return -1
1023
1024}
1025
1026function getcmdi(ar1 ,	ti) {
1027	if (ar1  in cmdl ) {
1028		return (cmdl[ar1 ])
1029	}
1030	else {
1031		cmdl[ar1 ]=iCMD++;
1032		return (cmdl[ar1 ])
1033	}
1034}
1035
1036function getccmdi(ic, i, iass,k,  xmatch) {
1037for (i in ccmdl )  {
1038	iass = ccmdl[i]
1039#	print "checking " iass
1040	xmatch=1
1041	for (k=1; icmd[ic,k]!=""; k++)
1042		if (icmd[ic,k] != icmd[iass,k])
1043			xmatch=0
1044	if (xmatch == 1 )
1045		if (icmd[iass,k] != "" )
1046			xmatch =0
1047	#check if matched, or else go over next one
1048	if (xmatch == 1) {
1049		#print "match" ic i
1050		return(i)
1051	}
1052}
1053#if we reach here, means we found no match.
1054iCCMD++
1055ccmdl[iCCMD] = ic
1056return(iCCMD)
1057}
1058
1059function getkcmdi(ic, i, iass,k,  xmatch) {
1060for (i in kcmdl )  {
1061	iass = kcmdl[i]
1062#	print "checking " iass
1063	xmatch=1
1064	for (k=1; kcmd[ic,k]!=""; k++)
1065		if (kcmd[ic,k] != kcmd[iass,k])
1066			xmatch=0
1067	if (xmatch == 1 )
1068		if (kcmd[iass,k] != "" )
1069			xmatch =0
1070	#check if matched, or else go over next one
1071	if (xmatch == 1) {
1072		#print "match" ic i
1073		return(i)
1074	}
1075}
1076#if we reach here, means we found no match.
1077iKCMD++
1078kcmdl[iKCMD] = ic
1079return(iKCMD)
1080}
1081
1082function smatch(s, t, i)
1083{
1084for(i=1; i<=NF; i++)
1085	if($i == t)
1086	 	return i
1087return -1
1088}
1089
1090function print_Is(i)
1091{
1092for (i=0; i<=iEDGES; i++)
1093	print "("snode[i] "," enode[i]	")"
1094}
1095
1096function dot_Is(s, bincl, k, prev)
1097{
1098debug(6, "enter:dot_Is()\n");
1099prev=""
1100_Ili=""
1101for(k=1; k<=NF; k++)
1102	if (isI($k) > 0) {
1103		bincl = $k
1104		sub(/\-I/,"", bincl)
1105		if ((bincl == "") || (bincl == "-") || (bincl == ".") )
1106			continue;
1107		_Ili = _Ili " " bincl
1108		if (!(bincl in incdir_a) ) {
1109			incdir_a[bincl]="y"
1110			iINCDIR++
1111			incdir[iINCDIR]=bincl
1112		}
1113		if (prev == "")
1114			prev=bincl
1115		else {
1116			iEDGES++
1117			snode[iEDGES] = prev
1118			enode[iEDGES] = bincl
1119			prev = bincl
1120		}
1121	}
1122debug(6, "exit:dot_Ts()\n");
1123}
1124
1125
1126function dot_as(s, blib,k,prev)
1127{
1128debug(6, "entered dot_as\n");
1129prev=""
1130_ali=""
1131for(k=1; k<=NF; k++) {
1132	if (isa($k) > 0) {
1133		basename($k)
1134		blib=base
1135		dirname($k)
1136		dlib=dir
1137		sub(/\.a/, "", blib)
1138		sub("^lib","", blib)
1139		_ali=_ali " " "-l"blib
1140		if (!(dir in libdir_a) ) {
1141			libdir_a[dir] = "y"
1142			iLIBDIR++
1143			libdir[iLIBDIR]=dir
1144		}
1145		if (prev == "")
1146			prev=dlib
1147		else {
1148			iaEDGES++
1149			sanode[iaEDGES] = prev
1150			eanode[iaEDGES] = dlib
1151			prev = bincl
1152		}
1153	}
1154	if (isl($k) > 0) {
1155		basename($k)
1156		blib=base
1157		dirname($k)
1158		dlib=dir
1159		sub(/^\-l/,"", blib)
1160		_ali=_ali " " "-l"blib
1161		if (dlib == "./") dlib=""
1162		if (dlib != "" ) {
1163		 if (!(dir in libdir_a) ) {
1164			libdir_a[dir] = "y"
1165			iLIBDIR++
1166			libdir[iLIBDIR]=dir
1167		 }
1168		 if (prev == "")
1169			prev=dlib
1170		 else {
1171			iaEDGES++
1172			sanode[iaEDGES] = prev
1173			eanode[iaEDGES] = dlib
1174			prev = bincl
1175		 }
1176		}#if dlib is not= ""
1177	}
1178   } #for all members in the line
1179debug(6, "exit: dot_as()\n");
1180}
1181
1182#_cli is a global variable.
1183function dot_o2c(s, ss, arr, k, ssdir)
1184{
1185debug(6, "enter:dot_o2c()\n")
1186#print $0
1187_cli=""
1188prev_o=""
1189prev_c=""
1190for(k=1; k<=NF; k++) {
1191    tar_in_ins($k);
1192    if (iso($k) > 0) {  #if a .o file name
1193	ss = arr[split($k, arr, "/")]
1194	if (sub(/\.o/,"", ss) == 1 ) {
1195		_cli=_cli " " ss".c"
1196	} #if
1197        dirname($k); ssdir=dir;
1198	sub(/\/\//, "", ssdir);
1199	if ((ssdir == "") || (ssdir == ".") || (ssdir == "./") )
1200		noop();
1201	else {
1202		if (!(ssdir in objdir_a) ) {
1203			objdir_a[ssdir]="y"
1204			iOBJDIR++
1205			objdir[iOBJDIR]=ssdir
1206		}
1207		if (prev_o == "")
1208			prev_o=ssdir
1209		else {
1210			ioEDGES++
1211			sonode[ioEDGES] = prev_o
1212			eonode[ioEDGES] = ssdir
1213			prev_o = ssdir
1214		}
1215	}
1216
1217     } #if a .o
1218     if (isc($k) > 0) { #if a .c file name
1219	basename($k);
1220	_cli = _cli " " base
1221        dirname($k); ssdir=dir;
1222	sub(/\/\//, "", ssdir);
1223	if ((ssdir == "") || (ssdir == ".") || (ssdir == "./") )
1224		noop();
1225	else {
1226		if (!(ssdir in dotcdir_a) ) {
1227			dotcdir_a[ssdir]="y"
1228			iDOTCDIR++
1229			dotcdir[iDOTCDIR]=ssdir
1230		}
1231		if (prev_c == "")
1232			prev_c=ssdir
1233		else {
1234			icEDGES++
1235			scnode[icEDGES] = prev_c
1236			ecnode[icEDGES] = ssdir
1237			prev_c = ssdir
1238		}
1239	}
1240     }
1241} # for loop
1242debug(6, "exit:dot_o2c()\n");
1243}
1244
1245#base is a global variable, need to learn how
1246# to pass/return string values.
1247function basename(path, 	arr)
1248{
1249base=arr[split(path, arr, "/")]
1250}
1251function nbasename(path, 	arr)
1252{
1253return(arr[split(path, arr, "/")])
1254}
1255
1256#dir is a global;
1257function dirname(path, 	b,arr)
1258{
1259dir=""
1260nfi=split(path, arr, "/");
1261b=arr[split(path, arr, "/")]
1262for (i=1; i<nfi; i++) {
1263	#print arr[i] "/"
1264	dir=dir arr[i] "/"
1265}
1266if (dir == "./") dir=""
1267debug(9, "path=%s dir=%s\n", path, dir)
1268return
1269debug(9, "\npath=%s arr:1=%s 2=%s", path, arr["1"], arr["2"]);
1270debug(9, "b=%d path=%s\n", b, path)
1271if (b == "" )
1272	printf("internal error: empty string to dirname()\n");
1273else
1274	sub(b,"",path)
1275if ((path == "./") || (path == ".//") ) # KLUDGE need a regular expr ./*
1276	path=""
1277debug(9, "b=%s path=%s\n", b, path)
1278dir=path
1279}
1280
1281
1282
1283
1284function iso(is)
1285{
1286	sss = arr1[split(is, arr1, "/")]
1287	if (sub(/\.o/,"", sss) == 1 )
1288		return 1
1289	else
1290		return -1
1291}
1292
1293function isI(s)
1294{
1295	if (sub(/\-I/,"", s) == 1 ) {
1296		return 1
1297	} else {
1298		return 0
1299	}
1300}
1301
1302function isa(s)
1303{
1304	ss = arr[split(s, arr, "/")]
1305	if (sub(/\.a/,"", ss) == 1 )
1306		return 1
1307	else
1308		return -1
1309}
1310
1311function isl(s, arr, ss)
1312{
1313	if (s ~ /^\-l/ )
1314		return 1
1315	else
1316		return -1
1317}
1318
1319function isc(s)
1320{
1321	ss = arr[split(s, arr, "/")]
1322	if (sub(/\.c/,"", ss) == 1 )
1323		return 1
1324	else
1325		return -1
1326}
1327
1328#top_sort
1329#globals: snode, enode, _SOURCEh
1330function top_sort(tmsg, snode, enode, nc, a,b, node, nodecnt, i,front,back)
1331{
1332_SOURCEh=""
1333for (node in pcnt)
1334	delete pcnt[node]
1335for (i in slist)
1336	delete slist[i]
1337for (i in scnt)
1338	delete scnt[i]
1339for (i in q)
1340	delete q[i]
1341nodecnt = 0
1342
1343for (j=1; j<=nc; j++) { #kludge, start=1 0r 0
1344	a=snode[j]
1345	if (!(a in pcnt) )
1346		pcnt[a] = 0
1347	b=enode[j]
1348	pcnt[b]++
1349	slist[a, ++scnt[a] ] = b
1350}
1351	back = 0
1352	for (node in pcnt) {
1353		nodecnt++
1354		if (pcnt[node] == 0) {
1355			#print "no back, pred:" back node
1356			q[++back] = node
1357		}
1358	}
1359	#print "n=" nodecnt
1360	for (front = 1; front <= back; front++) {
1361		node = q[front]
1362		#printf(" %s", node = q[front])
1363		#printf(" %s", node )
1364		_SOURCEh = _SOURCEh " " node
1365		for (i=1; i<=scnt[node]; i++)
1366			if (--pcnt[slist[node, i]] == 0)
1367				q[++back] = slist[node, i]
1368	}
1369	if (back != nodecnt) {
1370		print "warning: TOPSORT failed, cycle found, check-> " tmsg
1371	}
1372
1373}
1374
1375# a sample call : notonlist(hdrlist, dotcdir, iDOTCDIR, restc)
1376function notonlist(s, list, nlist, al, arr, 	i,n)
1377{
1378n = split(s, arr, " ");
1379#arr["1"] ["2"],...["i"]
1380for (i=1; i<=nlist; i++)
1381	if (list[i] == "")
1382		continue;
1383	else {
1384		found=0
1385		for (j=1; j<=n; j++)
1386			if (list[i] == arr[j])
1387				found=1
1388		if (found == 0) {
1389			al["1"] = al["1"] " " list[i]
1390			#list[i] does not belong to arr/s
1391		}
1392
1393	}
1394
1395}
1396
1397#check ins to see if tar == ddest/bdest
1398function destins(tar, k, i, tmptar, tmptar1, x)
1399{
1400tmptar = tar
1401#next piece of code removes all multiple occurences of ///->to one /
1402#KLUDGE : use a for loop with a # of selections made counter
1403gsub(/\/\//,"/", tmptar)
1404gsub(/\/\//,"/", tmptar)
1405gsub(/\/\//,"/", tmptar)
1406gsub(/\/\//,"/", tmptar)
1407for (jj in ins) {
1408	i =  split(jj, arr, SUBSEP);
1409	tmptar1 = arr["3"] arr["4"]
1410	gsub(/\/\//,"/", tmptar1)
1411	gsub(/\/\//,"/", tmptar1)
1412	gsub(/\/\//,"/", tmptar1)
1413	gsub(/\/\//,"/", tmptar1)
1414	# now compare tmptar1 and tmptar
1415	if (tmptar == tmptar1) {
1416		x=ins[jj]
1417		return(x)
1418	}
1419}
1420return(-2)
1421}
1422
1423#takes in a ccl, a list of defines,
1424# sideeffects add to a list of cc flags.
1425# shcc[] changed
1426function bld_shcc(ccl,	 arr, i,j,k)
1427{
1428print "CC flags(predefined outside makefile): "
1429k=split(ccl, arr, " ")
1430for(i=1; i<=k; i++) {
1431	if (arr[i] ~ /^-D/ ) {
1432		gsub(/^-D/, "", arr[i])
1433		print arr[i] " "
1434		shcc[arr[i]] = ""
1435	} else
1436		print "ignoring " arr[i]
1437}
1438}
1439
1440#determines if l has any -D type commands, and returns them
1441#in  a global list _Dli
1442#other side effects is to generate a define var list.
1443# to be printed at the begining. we will use var[], val[] arrays.
1444function dflags(l, i,k,j, arr, def)
1445{
1446_Dli=""
1447#assume $1, $2, ... etc. are valid arguments
1448for(k=1; k<=NF; k++) {
1449	if ($k ~ /^-D/ ) #yes a -D type var
1450	{
1451		def = $k
1452		gsub(/^-D/, "", def)
1453		gsub(/\=.*/,"",def)
1454		defval=$k
1455		if (gsub(/^-D.*\=/, "", defval) <= 0 )
1456			defval=""
1457		if (defval == "") 	#just -DXXX
1458			defval=$k
1459		#print "a define: " def ": " defval
1460		if (def in shcc) {
1461			#print "already in shcc"
1462			#ok, do nothing
1463			;
1464		}
1465		else {
1466			#aha, this define should go into _Dli
1467			_Dli = _Dli " " "("def")"
1468			#also put it in var/val list so that it gets
1469			# printed when those variables get printed
1470			found=0
1471			for(i=1;i<=iVAR; i++)
1472				if (var[i] == def )
1473					found=1
1474			if (found == 0) {
1475				#print "in array"
1476				iVAR++
1477				var[iVAR] = def
1478				val[iVAR] = defval
1479				var_cnt[iVAR]++
1480			}
1481		}
1482	} #if $k is ^-D
1483
1484} #for loop over all items in l
1485
1486#print "_Dli at return=", _Dli
1487}
1488
1489function noop(){}
1490
1491function isin(listy, ele, i, j,k,x ,tmp, arr)
1492{
1493if (listy == "")
1494	return(-1)
1495tmp=listy
1496#print listy
1497x=split(tmp, arr, " ")
1498for (j=1; j<=x; j++)
1499	if(arr[j] == ele ) {
1500		#print listy
1501		return(1)
1502	}
1503
1504#print listy
1505return(-1)
1506}
1507function shenv(sname, svalue,x) {
1508	system("env >.ajyENV")
1509	while (getline x <".ajyENV" > 0 ){
1510		iSHVAR++
1511		sname = x
1512		svalue = x
1513		gsub(/\=.*/,"",sname)
1514		gsub(/.*\=/,"",svalue)
1515		isshvar[sname] = iSHVAR # a fast search array
1516		shvar[iSHVAR] = sname
1517		shval[iSHVAR] = svalue
1518		#print "< " sname " " svalue " >"
1519		}
1520	#this is the default for cc flags, call bld_shcc multiple times
1521	# if you want more flags as defaulted outside makefile
1522	if ("NOPRINTVAR" in isshvar) {
1523		print "NOPRINTVAR:these variables will not be printed"
1524		print " 	in the Makefile(s) generated"
1525		t=shval[isshvar["NOPRINTVAR"]]
1526		i8=split(t,arr8, " "); print "i=" i8 "t="t;
1527		for(j=1; j<=i8; j++) {
1528			noprintvar[arr8[j]]=""
1529		}
1530	}
1531	CC_SHVAR = "ENVCCF"
1532	if (CC_SHVAR in isshvar) {
1533		#print "ok"
1534		bld_shcc(shval[isshvar[CC_SHVAR]] ); #builds list of cc flags
1535	}
1536	else;
1537	# note that this is used in determining what flags to set
1538	# during the makefile generation since some flags on CC
1539	# might be simply a property within that makefile.
1540	NMAKE_SHVAR = "NMAKE_VER"
1541	if (NMAKE_SHVAR in isshvar ) {
1542		if (shval[NMAKE_SHVAR] == "1.4" )
1543			nmake_ver="1.4"
1544		else if (shval[NMAKE_SHVAR] == "2.0" )
1545			nmake_ver="2.0"
1546		else {
1547			print "1.4 or 2.0 in NMAKE_VER var"
1548		}
1549	} else {
1550			; #print "using 2.0 nmake version"
1551	}
1552#nmake_var is unused currently
1553}
1554
1555function plogic(s)
1556{
1557	printf("[%s]logic error: call support, results could be incorrect", s);
1558}
1559
1560#$0
1561function find_in_out(s, arr, n,i )
1562{
1563n=split(s, arr, " ")
1564cin="";cout="";
1565for(i=1;i<=n;i++) {
1566	if (arr[i] == ">")
1567		cout=arr[i+1]
1568	else if (arr[i] == "<" )
1569		cin=arr[i+1]
1570	else if (arr[i] ~ /^\>/ ) {
1571		gsub(/\>/, "", arr[i] )
1572		cout=arr[i]
1573	} else if (arr[i] ~ /^\</ ) {
1574		gsub(/\</, "", arr[i] )
1575		cin=arr[i]
1576	}
1577}
1578}
1579
1580function tar_in_ins(tar, btar, dtar , i, jj)
1581{
1582basename(tar);btar=base
1583dirname(tar); dtar=dir
1584#print btar " - " dtar
1585	for (jj in ins) {
1586		i =  split(jj, arr, SUBSEP);
1587		gsub(/\/\//, "/", arr["3"]);
1588		gsub(/\/\//, "/", arr["3"]);
1589		gsub(/\/\//, "/", dtar)
1590		gsub(/\/\//, "/", dtar)
1591		if (dtar == "") dtar="./"
1592		if (arr["3"] == "") arr["3"]="./"
1593		#print arr["3"] " - " arr["4"]
1594
1595		if ((btar == arr["4"] ) && (dtar == arr["3"] ) )
1596		{
1597			print tar " in install????"
1598			print arr["1"] arr["2"] "->" dtar btar
1599			print "WARNING: CHECK Makefile.0 "
1600		}
1601	} #for
1602
1603}
1604			#ins[dsrc, bsrc, ddest, bdest] = kCMD
1605
1606#rule for patterns
1607# syntax :
1608# <cmd> <options> <name> < <fname>
1609# cat <fname> | <cmd> <options> <name> - this is not implemented.
1610# .p : .pat .CMD.xx
1611function rule_pat(s, myi, jmy, i,j,k,jj, jmy)
1612{
1613cin="";cout=""
1614find_in_out(s); #use s instead later
1615fname=cin
1616opts=""
1617	for (k=jmy+1; k<=NF; k++){
1618		if ($k ~ /\-/) {
1619			print "Warning: options on pattern could be ignored"
1620			opts=opt " " $k
1621			#options, use them
1622		} else if ($k ~ /</ ) {
1623			#break out of loop
1624			break;
1625		} else {
1626			name=$k
1627			break;
1628		}
1629	} #for loop
1630	#print "name=" name " fname=" fname
1631	#KLUDGE 1 is for patterns PATTERNS
1632	#KLUDGE 0 is for COMMANDS variables.
1633	val[1] = val[1] " " name".p"
1634	var_cnt[1]++
1635
1636	dirname(fname);basename(fname);
1637	if (dir == "") dir = ""
1638	dsrc=dir; bsrc=base; ddest="./"; bdest=name".p" ;
1639	kCMD++
1640	ins[dsrc, bsrc, ddest, bdest] = kCMD
1641	insorder[kCMD]=dsrc SUBSEP bsrc SUBSEP ddest SUBSEP bdest
1642	instype[kCMD]="pattern"
1643#	print "insorder" kCMD " {}-"insorder[kCMD]
1644
1645	for(k=1; kcmd[kCMD,k]!=""; k++);
1646	kcmd[kCMD,k] = sprintf("\t %s %s %s < $(<)", vcommand(myi), opts, name)
1647	instdir[ddest]=""
1648}
1649
1650function isfile(path, tmp)
1651{
1652#use if this is set
1653tmp=sprintf("test -f %s", path)
1654if (system(tmp) == 0 )
1655	return 1
1656else
1657	return 0
1658}
1659
1660function rm_vpath(path, vpath, i, varr, parr,iv, j, k, arr, tmp)
1661{
1662	#KLUDGE AND INEFFICIENT!
1663	for (i=0;i<=15; i++)
1664		gsub(/\/\//, "", path)
1665	for (i=0;i<=15; i++)
1666		gsub(/\/\//, "", vpath)
1667	iv=split(vpath, varr, ":")
1668	ip=split(path, parr,  "/");
1669	for (i=1; i<=iv;  i++) { #for each vpath path
1670
1671
1672	}
1673}
1674
1675function abs2relpath(path, i, j)
1676{
1677
1678}
1679
1680function vcommand(command, i, j, k)
1681{
1682for(i=1; i<=iVAR; i++)
1683	if (val[i] == command) {
1684		var_cnt[i]++
1685		return("$("var[i]")")
1686	}
1687return(command);
1688}
1689
1690function varfill(sname, svalue,x) {
1691	if (varfile == "_")
1692		return
1693	if (isfile(varfile) <= 0 ) {
1694		print "ERROR: cannot find file: " varfile
1695		return
1696	}
1697	debug(7, "varfile:%s\n" varfile);
1698	while (getline x <varfile > 0 ){
1699		iVAR++
1700		sname = x
1701		svalue = x
1702		gsub(/\=.*/,"",sname)
1703		gsub(/.*\=/,"",svalue)
1704		isvar[sname] = iVAR # a fast search array
1705		var[iVAR] = sname
1706		val[iVAR] = svalue
1707		var_cnt[iVAR]=0 #increment number of times used.
1708		debug(7, "%s %s\n", sname ,svalue);
1709		}
1710}
1711function invarfill(sname, svalue,x) {
1712	if (invarfile == "_" )
1713		return
1714	if (isfile(invarfile) <= 0 ) {
1715		print "ERROR: cannot find: " invarfile
1716		return
1717	}
1718	debug(7, "invarfile:%s\n" invarfile);
1719	while (getline x <invarfile > 0 ){
1720		#iINVAR++
1721		sname = x
1722		svalue = x
1723		gsub(/\=.*/,"",sname)
1724		gsub(/.*\=/,"",svalue)
1725		svalue=compress(svalue)
1726		invar_cnt[sname]=0  #counts how many times sname used!
1727		### added for X=xxx:xx:yyy:zzz kind of assignments.
1728		ia=split(svalue, arr,  ":" );
1729		debug(9, "ia=%d", ia)
1730		if (ia > 1) {
1731			debug(5, ": define found! %s\n", sname);
1732			isinvar[sname] = iVAR # a fast search array
1733			if (arr["1"] ~ /^\+.*/){
1734				tmpa=arr["1"];
1735				sub(/^\+/,"", tmpa);
1736				invar[sname]=tmpa #use first define...as default
1737			} else
1738				invar[sname]=arr["1"] #use first define...as default
1739			debug(9, "invar[%s]=%s\n", sname , invar[sname])
1740			invar_more[sname]=1#this indicates if we need
1741				#to search the var_i/val_i arrays...
1742			for (i=1; i<=ia; i++) {
1743				debug(9, ":%s", arr[i]);
1744				if (arr[i] ~ /^\+.*/ ) {
1745					debug(9, "+VPATH");
1746					sub(/^\+/,"", arr[i]);
1747					print "VPATH +"arr[i]
1748					itill=iIN;
1749					for(iti=0; iti<=itill; iti++){
1750						if (var_i[iti] == "VPATH"){
1751							iIN++;
1752							var_i[iIN]=sname
1753				val_i[iIN]=compress(val_i[iti] "/" arr[i])
1754				debug(7, ":+(%s=%s)\n", sname ,val_i[iIN]);
1755						}
1756					}
1757				} else {
1758				iIN++
1759				var_i[iIN]=sname
1760				val_i[iIN]=compress(arr[i])
1761				debug(7, ":%s %s\n", sname ,val_i[i]);
1762				}
1763			}
1764		} else  { #if a : separated define exists ...
1765			isinvar[sname] = iVAR # a fast search array
1766			invar[sname]=svalue
1767			invar_cnt[sname]=0  #counts how many times sname used!
1768			debug(7, "%s %s\n", sname ,svalue);
1769			if (svalue ~ /^\.\/.*/ ) {
1770				invar_more[sname]=1
1771				iIN++
1772				var_i[iIN]=sname
1773				val_i[iIN]=svalue
1774				sub(/^\.\//, "", val_i[iIN]);
1775			}
1776
1777		} #regular define with no colons:...
1778		}#while allinvars in invarfile...
1779}
1780
1781#puts time stamp in the makefile generated.
1782function ts_makefile(d, x)
1783{
1784	printf("note # # make abstract machine file generated by mg # #\n");
1785	if (isfile(premakefile) >= 0 ) {
1786		while (getline x <premakefile >0)
1787			printf("%s\n", x)
1788		printf("\n\n") ;
1789	}
1790}
1791
1792#returns a variable or NULL string depending on if it is
1793#defined.
1794function v2n(val, pwd, e, i, j, k, tmp, arr, val1)
1795{
1796e=""
1797nm=subinvar(val)
1798if (val != nm ) {
1799	#print "found a sub " val ":" nm
1800	return(nm);
1801}
1802#print "v2n: " "val=" val"<" " pwd=" pwd " fv2n=" fv2n"<"
1803if (fv2n != "_" ) {
1804	print "shell: " fv2n " "val
1805	fv2n " "val " "KLU++ | getline e
1806		#print "e=" e
1807	if (e == "" ) {
1808		#try another variation
1809		val1 = val"/"
1810		print "shell: " fv2n " "val1
1811		fv2n " "val1 " "KLU++ | getline e
1812	}
1813	return e
1814} else
1815	return NULL
1816}
1817
1818#in:a list "/eald ../sdlfk /./..sdf" etc.
1819#out: a list with variables substitutes.
1820#typiclaly used in .SOURCE.h, .a...etc. lines
1821function v2(ilist, ia, i, vval, vvar, jlist, vfdir)
1822{
1823	for (i in ztmp)
1824		delete ztmp[i]
1825	jlist=""
1826	ia=split(ilist, arr,  " ");
1827	for (i=1; i<=ia; i++) {
1828		vval=arr[i]
1829		vval=compress(vval)
1830		#print "vval=" vval
1831		#vvar=v2n(vval);
1832		vvar=compress(subinvar(vval)) ;
1833		debug(1, "S: %s %s\n", vval, vvar);
1834		if (vvar in ztmp)
1835			debug(9, "already in S:%s\n", vvar)
1836		else {
1837			ztmp[vvar]=1
1838			jlist = jlist " " vvar
1839		}
1840	}
1841return(jlist);
1842
1843#this piece of code is extinct below:::
1844		if (vvar == NULL ) {
1845		#check the INSTDIR list...
1846		found=0
1847		for (vdir in invar) {
1848			vali = invar[vdir]
1849			vali1=compress(vali)
1850			if (vval == vali1) {
1851				vfdir=vdir
1852				debug(6, "ffvdir=%s vval=%s\n" vfdir , vali1)
1853				found=1
1854				invar_cnt[vdir]++
1855			break;
1856			}
1857		}
1858		if (found == 1)
1859			jlist = jlist " " "$("vfdir")"
1860		        else
1861				jlist = jlist " " arr[i]
1862	}
1863}
1864
1865function compress(xval, i, val)
1866{
1867if (xval ~/^\.\/.*/ )
1868	debug(9, "WARNING: ./ matching will fail %s", xval);
1869val=xval
1870#no doble slashes; KLUDGE
1871	for (i=0;i<=5; i++)
1872		gsub(/\/\//, "/", val)
1873#no trailing single / eg. /usr/include/ => /usr/include
1874	if (val == "/" )  {
1875		#cannot remove this single slash! so return...
1876		debug(3, "compress: single slash\n");
1877		return(val);
1878	}
1879	gsub(/\/$/, "", val);
1880return(val);
1881}
1882
1883#substiture the s with invar[VAR]=VAL, using first match!!!
1884#bug removal1: ./ or "" matching prohibited...
1885function subinvar(s1, myvar, i, j, k, match_length, match_val, match_var)
1886{
1887s=compress(s1)
1888debug(8,"subinvar:%s -compress> %s\n", s1, s)
1889match_length=0
1890match_val=""
1891match_var=""
1892for(yvar in invar) {
1893	yval=invar[yvar]
1894	debug(8, "subinvar: check %s=%s\n", yvar, yval);
1895	if (length(yval) < match_length) #we do not want a smaller match!
1896			continue
1897
1898	if ((yval == "") || (yval == "./") ) {
1899		#cant match this noops.
1900		continue
1901	}
1902	r=""
1903	i=index(s, yval);
1904	#print i "-" s "/" length(s) "-" yval "/"length(yval)
1905	r=substr(s, length(yval)+1 )
1906	debug(8, "index: i=%d, s=%s yval=%s r=%s\n", i, s, yval, r);
1907	if ((r ~ /\/.*/) || (r == "") )
1908		debug(8, "ok\n");
1909	else {
1910		debug(8, "no match %s %s i=%d\n",s, r, i);
1911		continue
1912		}
1913	if (i== 1) {
1914		#print yvar " val=" yval "len=" length(yval)
1915		r=substr(s, length(yval)+1 )
1916	if ((r ~ /\/.*/) || (r == "") )
1917		debug(8, "ok\n");
1918	else {
1919		debug(8, "no match %s %s i=%d\n",s, r, i);
1920		continue
1921	}
1922		#print "r=" r
1923		debug(8, "yvar=%s %d SUB=%s\n", yvar, invar_cnt[yvar], s)
1924		match_var = yvar
1925		match_val = "$(" yvar ")/" r;
1926		match_length=length(yval)
1927	}
1928} #for match
1929debug(8, ":check var_i/val_i arrays for maxmatch \n");
1930############new codebelow for checking val_i/var_i lists.
1931for(j=0;j<=iIN; j++) {
1932	r=""
1933	yvar=var_i[j]
1934	yval=val_i[j]
1935	debug(8, ":%s %s \n", yvar, yval)
1936	if (length(yval) < match_length) #we do not want a smaller match!
1937			continue
1938
1939	if ((yval == "") || (yval == "./") ) {
1940		#cant match this noops.
1941		continue
1942	}
1943	i=index(s, yval);
1944	if (i== 1) {
1945		debug(8, "ahh! var_i/val_i found\n")
1946		#print yvar " val=" yval "len=" length(yval)
1947		r=substr(s, length(yval)+1 )
1948	if ((r ~ /\/.*/) || (r == "") )
1949		noop();#ok
1950	else {
1951		debug(8, "no match %s %s i=%d\n",s, r, i);
1952		continue
1953	}
1954		#print "r=" r
1955		debug(8, ":yvar=%s %d SUB=%s\n", yvar, invar_cnt[yvar], s)
1956		match_var = yvar
1957		match_val = "$(" yvar ")/" r;
1958		match_length=length(yval)
1959	}
1960}#loop thru all var_i
1961
1962if (match_length > 0) {
1963	#something matched!
1964	invar_cnt[match_var]++
1965	debug(4, "\tmatch:%s %s\n", match_val, s)
1966	return(match_val)
1967}
1968#else return same old string...
1969debug(4, "NO MATCH:%s\n", s1);
1970return(s1)
1971
1972}
1973
1974		#return ("$(" yvar ")/" r);
1975
1976#given a string s, determines any .SOURCE.* lines for
1977#modification...
1978function spath(s)
1979{
1980	if (isc(s) > 0 ) {
1981		dirname(s);
1982		if (!(dir in dotcdir_a) ) {
1983			dotcdir_a[dir]="y"
1984			iDOTCDIR++
1985			dotcdir[iDOTCDIR]=dir
1986		}
1987	} else {
1988		dirname(s);
1989		if (dir != "")
1990			print "add .SOURCE: line manually" dir " for " s
1991	}
1992}
1993
1994#converts a any file to a .ln file
1995function any2ln(s, tmp)
1996{
1997	if (isc(s) > 0) {
1998		#is a .c so get base name and convert...
1999		basename(s);
2000		tmp=s
2001		sub(/\.c$/, "", tmp)
2002		return(tmp".ln")
2003	} else {
2004		return(s".ln")
2005	}
2006}
2007function nl(Plist,Llist,Larr,Lc,Li, thisline)
2008{
2009	Llist=Plist;
2010	Lc=split(Llist,Larr," ");
2011	thisline=0
2012	maxline=MAXPERLINE
2013	if (maxline <= 0)
2014		maxline=1
2015	#print maxline
2016	for (Li=1;Li<=Lc;Li++)
2017	{
2018	#print thisline maxline Larr[Li]
2019		if (thisline < maxline){
2020			printf("\t%s ",Larr[Li])
2021			thisline++
2022		}else {
2023			#here we have printed max on this line, start newline
2024			printf("\\\n") ;
2025			printf("\t%s ",Larr[Li])
2026			thisline=1
2027		}
2028	}
2029	#printf("\t\t%s\n\n",Larr[Li])
2030	printf("\n\n")
2031}
2032
2033function debug(level, con_str, a,b,c,d,e,f,g,h,i,j,k,l)
2034{
2035if (level > debug_level )
2036	return
2037printf(con_str, a,b,c,d,e,f,g,h,i,j,k,l)
2038}
2039
2040function virtual_g(a,b,c, 	tmp)
2041{
2042return(avu[a,b,c]);
2043}
2044
2045function virtual_n(a,b,c,	i,j,k,arr,tmp)
2046{
2047tmp=a SUBSEP b SUBSEP c
2048if (tmp in avu) {
2049	print "Virtual error:duplicate" a "--" b "--"  c
2050} else {
2051	usage[c]++
2052	if (usage[c] == 1)
2053		avu[a,b,c]="V_" c
2054	else
2055		avu[a,b,c]="V_" c usage[c]
2056}
2057return(avu[a,b,c]);
2058}
2059
2060function print_dep_new()
2061{
2062}
2063#returns "" or some more dependencies...
2064function f_ldep_more(ind, iorder, i,j,k,tmp,arr, jj, itype, jjn, dir, nm)
2065{
2066return(ldep_more[ind]);
2067if (ind in ldep_more) {
2068	debug(9,"ok");
2069}else {
2070	debug(1, "nomore for %s", ldep[i]);
2071	return("");
2072}
2073#here there are more targets, ....
2074#following code from install dire...
2075	iorder=ldep_more[ind] #k is the index into ins array
2076		jj=insorder[iorder]  #jj is the unique install option
2077		itype = instype[iorder]
2078		jjn = split(jj, arr, SUBSEP);
2079		dir = arr["3"]
2080		nm=subinvar(dir);
2081		debug(1,"more returns: %s\n", compress(nm) );
2082		return(compress(nm))
2083}
2084
2085function vircnv(s, tmp)
2086{
2087	gsub(/\</, ">", s)
2088	return(s);
2089}
2090