~rfc1149/adasockets

d1758900e60cfe5dcaa2c80833b3c35a8d80d8e8 — Samuel Tardieu 10 months ago 9e87aea
Generate all constants in one compilation
5 files changed, 49 insertions(+), 191 deletions(-)

M .gitignore
M src/Makefile.am
M src/constants.c
M src/constants.sh
D src/split.adb
M .gitignore => .gitignore +2 -0
@@ 105,3 105,5 @@ lib/
gnulib/
/INSTALL
/README
/src/constants
/src/constants-inc.c

M src/Makefile.am => src/Makefile.am +8 -11
@@ 27,7 27,7 @@ EXTRA_DIST = Makefile.RTEMS Makefile.VMS sockets-vms_link.ads \
	sockets-thin.adb.vms sockets-thin.adb.win32 \
	sockets-thin.ads.unix sockets-thin.ads.vms sockets-thin.ads.win32 \
	constants.c constants.sh \
	split.adb adasockets-config
	adasockets-config

ADA_OBJECTS = \
	sockets-constants.o \


@@ 47,19 47,16 @@ ADACOMPILER = $(top_srcdir)/support/adacompiler
ADAFLAGS = @ADAFLAGS@ -I. -I$(srcdir)

CLEANFILES = *.o *.ali b~*
DISTCLEANFILES = b_split.c split constants_nodef \
		sockets-thin.ads sockets-thin.adb \
		sockets-constants.ads
DISTCLEANFILES = sockets-thin.ads sockets-thin.adb sockets-constants.ads

constants_nodef: constants.c config.h
	$(CC) -I. -o constants_nodef $(srcdir)/constants.c $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
constants: $(srcdir)/constants.c constants-inc.c
	$(CC) -I. -o constants $(srcdir)/constants.c $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)

sockets-constants.ads: $(srcdir)/constants.sh split constants_nodef
	$(SHELL) $(srcdir)/constants.sh Sockets.Constants $(srcdir) \
	  sockets-constants.ads "$(CC)"
constants-inc.c: $(srcdir)/constants.sh
	$(SHELL) $(srcdir)/constants.sh

split: $(srcdir)/split.adb
	$(GNATMAKE) $(srcdir)/split.adb -cargs $(ADAFLAGS) -largs $(LDFLAGS)
sockets-constants.ads: constants
	./constants > sockets-constants.ads

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = adasockets.pc

M src/constants.c => src/constants.c +27 -35
@@ 7,12 7,12 @@
#include <config.h>

#ifdef HAVE_SYS_CONF_H
# ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
# endif
# ifdef HAVE_TIME_H
# include <time.h>
# endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <sys/conf.h>
#endif



@@ 114,21 114,19 @@
#undef ECONNREFUSED

/* Replace with Windows sockets declaration */
#define EINTR		WSAEINTR
#define EWOULDBLOCK	WSAEWOULDBLOCK
#define EINPROGRESS	WSAEINPROGRESS
#define EALREADY	WSAEALREADY
#define EISCONN		WSAEISCONN
#define ECONNREFUSED	WSAECONNREFUSED
#define EINTR WSAEINTR
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#define EALREADY WSAEALREADY
#define EISCONN WSAEISCONN
#define ECONNREFUSED WSAECONNREFUSED
#endif

#include <ctype.h>

static char *
capitalize (char *name)
{
  int  beginning = 1;
  char *result   = (char *) malloc (strlen (name) + 1);
static char *capitalize(char *name) {
  int beginning = 1;
  char *result = (char *)malloc(strlen(name) + 1);
  char *ptr;
  for (ptr = result; *name; ptr++, name++) {
    *ptr = *name;


@@ 144,24 142,18 @@ capitalize (char *name)
  return result;
}

static void
output (char *name, int value)
{
  char *capitalized = capitalize (name);
  if (value >= 0) {
    printf ("   %-20s : constant := 16#%04X#;\n", capitalized, value);
  } else {
    printf ("   %-20s : constant := %d;\n", capitalized, value);
#define OUTPUT(name, value)                                                    \
  if (value == -1) {                                                           \
    printf("   %-20s : constant := -1;\n", capitalize(name));                  \
  } else {                                                                     \
    printf("   %-20s : constant := 16#%04X#;\n", capitalize(name), value);     \
  }
}

int
main (int argc, char *argv[])
{
#ifdef CONSTANT_NAME
  output (argv[1], CONSTANT_NAME);
#else
  output (argv[1], -1);
#endif
  exit (0);
int main() {
  printf("--  This package has been generated automatically.\n");
  printf("--  Any change you make here is likely to be lost !\n");
  printf("package Sockets.Constants is\n");
#include "constants-inc.c"
  printf("end Sockets.Constants;\n");
  exit(0);
}

M src/constants.sh => src/constants.sh +12 -53
@@ 1,14 1,6 @@
#!/bin/sh
#
# This file generates any necessary constant for a given platform.
#

# Name of package

name=$1
srcdir=$2
fname=$3
CC="$4"
# This file generates the constants-inc.c file.

# List of constants we need to know



@@ 43,50 35,17 @@ constants="${constants} IPV6_JOIN_GROUP     IPV6_LEAVE_GROUP"
# ioctl
constants="${constants} SIOCINQ SIOCOUTQ"

# Debug

debug=$1

# Look for any header file found

tmpe=./tmpe$$
trap "rm -f ${tmpe}" 0 1 2 3 15

# Header of generated file

cat > ${fname} << EOF
--  This package has been generated automatically.
--  Any change you make here is likely to be lost !
package ${name} is
EOF

# For each constant, try to output its value or -1 if undefined

for c in ${constants}; do
    case ${c} in
	*-*)
            OIFS=${IFS}; IFS="-"; set ${c}; IFS=${OIFS}; \
	    echo "Checking value of $1 (or $2 as a substitute)"; \
            (${CC} -DCONSTANT_NAME=$1 -I. -o ${tmpe} ${srcdir}/constants.c \
	            2>/dev/null &&
	            ${tmpe} $1 >> ${fname}) ||
            (${CC} -DCONSTANT_NAME=$2 -I. -o ${tmpe} ${srcdir}/constants.c \
	            2>/dev/null &&
	            ${tmpe} $1 >> ${fname}) ||
            ./constants_nodef $1 >> ${fname}
    ;;
	*)
	    echo "Checking value of $c"; \
            (${CC} -DCONSTANT_NAME=${c} -I. -o ${tmpe} ${srcdir}/constants.c \
	            2>/dev/null && \
	            ${tmpe} ${c} >> ${fname}) || \
             ./constants_nodef ${c} >> ${fname}
    ;;
    esac
done

# Trailer of generated file

cat >> ${fname} << EOF
end ${name};
EOF
    OIFS=${IFS}; IFS="-"; set ${c}; IFS=${OIFS};
    echo "#if defined($1)"
    echo "    OUTPUT(\"$1\", $1);"
    if [ -n "$2" ]; then
        echo "#elif defined $2"
        echo "    OUTPUT(\"$1\", $1);"
    fi
    echo "#else"
    echo "OUTPUT(\"$1\", -1);"
    echo "#endif"
done > constants-inc.c

D src/split.adb => src/split.adb +0 -92
@@ 1,92 0,0 @@
-----------------------------------------------------------------------------
--                                                                         --
--                         ADASOCKETS COMPONENTS                           --
--                                                                         --
--                               S P L I T                                 --
--                                                                         --
--                                B o d y                                  --
--                                                                         --
--          Copyright (C) 1998-2023 Samuel Tardieu <sam@rfc1149.net>       --
--                   Copyright (C) 1999-2003 Télécom Paris                 --
--                                                                         --
--   AdaSockets is free software; you can  redistribute it and/or modify   --
--   it  under terms of the GNU  General  Public License as published by   --
--   the Free Software Foundation; either version 2, or (at your option)   --
--   any later version.   AdaSockets is distributed  in the hope that it   --
--   will be useful, but WITHOUT ANY  WARRANTY; without even the implied   --
--   warranty of MERCHANTABILITY   or FITNESS FOR  A PARTICULAR PURPOSE.   --
--   See the GNU General Public  License  for more details.  You  should   --
--   have received a copy of the  GNU General Public License distributed   --
--   with AdaSockets; see   file COPYING.  If  not,  write  to  the Free   --
--   Software  Foundation, 59   Temple Place -   Suite  330,  Boston, MA   --
--   02111-1307, USA.                                                      --
--                                                                         --
--   As a special exception, if  other  files instantiate generics  from   --
--   this unit, or  you link this  unit with other  files to produce  an   --
--   executable,  this  unit does  not  by  itself cause  the  resulting   --
--   executable to be  covered by the  GNU General Public License.  This   --
--   exception does  not  however invalidate any  other reasons  why the   --
--   executable file might be covered by the GNU Public License.           --
--                                                                         --
--   The main repository for this software is located at:                  --
--       http://www.rfc1149.net/devel/adasockets.html                      --
--                                                                         --
--   If you have any question, please use the issues tracker at:           --
--       https://github.com/samueltardieu/adasockets/issues                --
--                                                                         --
-----------------------------------------------------------------------------

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO;      use Ada.Text_IO;

procedure Split is

   Column          : Natural  := 0;
   Max_Column      : constant := 65;
   Hard_Max_Column : constant := 78;
   Was_Space       : Boolean  := False;

   procedure Print_Space_Maybe;

   procedure Print_Space_Maybe is
   begin
      if Was_Space and then Column /= 0 then
         Put (' ');
         Column := Column + 1;
         Was_Space := False;
      end if;
   end Print_Space_Maybe;

begin
   if Argument_Count /= 1 then
      Put_Line ("Error, usage: split ""text""");
      Set_Exit_Status (1);
   else
      for I in 1 .. Argument (1) 'Length loop
         if Argument (1) (I) = ' ' and then Column >= Max_Column then
            New_Line;
            Column := 0;
         elsif Argument (1) (I) = ' ' and then Column = 0 then
            null;
         elsif Column >= Hard_Max_Column then
            New_Line;
            Put ("--  " & Argument (1) (I));
            Column := 5;
         elsif Argument (1) (I) = ' ' then
            Was_Space := True;
         else
            if Column = 0 then
               Put ("--  ");
               Column := 4;
            else
               Print_Space_Maybe;
            end if;
            Put (Argument (1) (I));
            Column := Column + 1;
         end if;
      end loop;
      if Column > 0 then
         New_Line;
      end if;
   end if;
end Split;