From abe9823ee2f5682ec2a30aae1d36603970504d68 Mon Sep 17 00:00:00 2001
From: Lev Kujawski <int21h@mailbox.org>
Date: Sat, 6 Feb 2021 20:21:45 -0700
Subject: [PATCH 4/5] Teach Imake how to autoconfigure using AT&T's iffe

config/cf/Imake.rules:
    - Create an IffeConfigure rule to run iffe and generate configure files
    - Clean iffe output
config/cf/Imake.tmpl:
    - Define IFFE, IFFE_CMD, and IffeCmd
    - Create a make rule to run iffe
programs/dtprintinfo/objects/PrintObj/ParseJobs.C:
    - Use iffe to detect rresvport()
---
 cde/config/cf/Imake.rules                              | 10 +++++++++-
 cde/config/cf/Imake.tmpl                               |  8 ++++++++
 cde/programs/dtprintinfo/objects/PrintObj/ParseJobs.C  |  9 ++-------
 .../objects/PrintObj/features/rresvport.iffe           |  2 ++
 4 files changed, 21 insertions(+), 8 deletions(-)
 create mode 100644 cde/programs/dtprintinfo/objects/PrintObj/features/rresvport.iffe

diff --git a/cde/config/cf/Imake.rules b/cde/config/cf/Imake.rules
index 42323f74..ab803552 100644
--- a/cde/config/cf/Imake.rules
+++ b/cde/config/cf/Imake.rules
@@ -1418,6 +1418,7 @@ depend::								@@\
 #define	CleanTarget()							@@\
 clean::									@@\
 	$(RM) FilesToClean ExtraFilesToClean "#"*			@@\
+	if [ -d FEATURE ]; then $(RM) -r FEATURE; fi			@@\
 									@@\
 ProofCleanTarget()
 #endif /* CleanTarget */
@@ -1585,6 +1586,14 @@ LintLibReferences(varname,libname,libsource)
 	$(CC) -E $(CFLAGS) options src > $@
 #endif
 
+/*
+ * IffeConfigure - run AT&T's iffe (IF Feature Exist)
+ */
+#ifndef IffeConfigure
+#define IffeConfigure(src) RemoveFile($@)				@@\
+	ClearmakeOSName \
+	$(IFFE_CMD) src
+#endif
 /*
  * ObjectCompile - compile fragment for a normal object file
  */
@@ -1798,7 +1807,6 @@ clean::									@@\
 	RemoveFiles(y.tab.c file.c)
 #endif /* YaccFileNoFlags */
 
-
 #ifndef MakeLibrary
 #define MakeLibrary(libname,objlist) $(AR) libname objlist
 #endif
diff --git a/cde/config/cf/Imake.tmpl b/cde/config/cf/Imake.tmpl
index 1874d0b1..024ddb41 100644
--- a/cde/config/cf/Imake.tmpl
+++ b/cde/config/cf/Imake.tmpl
@@ -613,6 +613,9 @@ XCOMM the platform-specific parameters - edit site.def to change
 #ifndef YaccCmd
 #define YaccCmd yacc
 #endif
+#ifndef IffeCmd
+#define IffeCmd sh $(TOP)/programs/dtksh/ksh93/src/cmd/INIT/iffe.sh
+#endif
 #ifndef CplusplusYaccCmd
 #define CplusplusYaccCmd YaccCmd
 #endif
@@ -969,6 +972,7 @@ CXXEXTRA_INCLUDES = CplusplusExtraIncludes
            LEXLIB = LexLib
              YACC = YaccCmd
            CCYACC = CplusplusYaccCmd
+             IFFE = IffeCmd
              LINT = LintCmd
       LINTLIBFLAG = LintLibFlag
          LINTOPTS = LintOpts
@@ -1159,6 +1163,7 @@ CXXEXTRA_INCLUDES = CplusplusExtraIncludes
 
         MACROFILE = MacroFile
            RM_CMD = $(RM)
+         IFFE_CMD = $(IFFE)
 
     IMAKE_DEFINES = /* leave blank, for command line use only */
 #ifdef UseInstalled
@@ -1231,6 +1236,9 @@ XCOMM common rules for all Makefiles - do not edit
 
 .c.i:
 	CPPOnlyCompile($*.c,$(_NOOP_))
+
+FEATURE/%: features/%.iffe
+	IffeConfigure($<)
 /*
  * These need to be here so that rules in Imakefile occur first;  the blank
  * emptyrule is to make sure that an empty Imakefile does not default to make
diff --git a/cde/programs/dtprintinfo/objects/PrintObj/ParseJobs.C b/cde/programs/dtprintinfo/objects/PrintObj/ParseJobs.C
index 2c532c8a..ef80830c 100644
--- a/cde/programs/dtprintinfo/objects/PrintObj/ParseJobs.C
+++ b/cde/programs/dtprintinfo/objects/PrintObj/ParseJobs.C
@@ -79,6 +79,7 @@ extern "C" { int rresvport(int *); }
 
 #include "ParseJobs.h"
 #include "Invoke.h"
+#include "FEATURE/rresvport"
 
 static int G_AIX_LOCAL = 0;
 
@@ -196,13 +197,7 @@ int ConnectToPrintServer(const char *rhost, int timeout)
    SETEUID(0);
 
    // Try connecting to the server from a privileged port.
-   /* FIXME: Test for the presence of rresvport and provide a substitute
-    *        if necessary. Linux C library support:
-    * dietlibc: no rresvport, defines __dietlibc__
-    * musl: no rresvport
-    * uclibc: rresvport, defines __GLIBC__
-    */
-#if !defined(__linux__) || defined(__GLIBC__)
+#ifdef _lib_rresvport
    s = rresvport(&lport);
 #endif
 
diff --git a/cde/programs/dtprintinfo/objects/PrintObj/features/rresvport.iffe b/cde/programs/dtprintinfo/objects/PrintObj/features/rresvport.iffe
new file mode 100644
index 00000000..6d91d86d
--- /dev/null
+++ b/cde/programs/dtprintinfo/objects/PrintObj/features/rresvport.iffe
@@ -0,0 +1,2 @@
+hdr unistd,netdb
+lib rresvport
-- 
2.30.0

