>I need to interact with some binary files that are basically stored C
>structures.

I can suggest two distinct tools.

The older one from 1999 is:
http://juggle.gaertner.de/bnp/c2j.html

This article was originally published in Vector v16n2.  This approach
is self-contained in J but assumes that you most probably manually
describe structure layouts, ending up with non-portable code.

I expect this article will give you a viable approach to dissect
your datafiles.

Second tool:

For the J 4.x Release whose Unix port I took care of in 2000, I was
faced with the problem that both the socket and the regexp functions
where killed off as foreigns 16!:n and 17!:n implemented in C.  The
task was to replace them by 15!:0-based implementations in J.
(Two bad moves, in my opinion.)   I devised "sym2ijs", a portable tool
which allows you to specify various items "in the C world" you want
to interface to and get the proper values **on the the target
system** where your code is supposed to run.

(My original setup (which I still use and prefer) is based
on a makefile rule (whose purpose was obviously never understood
by JSoftware), a mostly awk-based shell script, and a man-page.
JSoftware transformed all that into a J script so it is usable
on Windows, too, fair enough.

I can't be bothered to hunt for its current place or documentation
beyond a naive but unsuccessful search on jsoftware.com, so here
is my old man page.

                                                        Martin

neitzel 27 > man sym2ijs

sym2ijs(1)                                             sym2ijs(1)

NAME
       sym2ijs  - translate C type and constant declarations into
       J definitions

SYNOPSIS
       sym2ijs [ -k | -c  |  -cc  |  -C  compiler-command  ]  ...
       [filename.sym]

DESCRIPTION
       Sym2ijs  converts header definitions given in C (and the C
       preprocessor) into equivalent J definitions.  It expects a
       compact  input  format listing the symbols of interest and
       generates the corresponding J script defining the same (or
       closely related) symbols.

       Input  is expected either in a file with a .sym suffix, in
       which case the output is written to a corresponding  file-
       name.ijs  file,  or on stdin, generating the J definitions
       to stdout.

       A short example for the expected  input  format  which  is
       detailled further below is this:

              <sys/types.h>
              <sys/stat.h>

              i S_IFIFO S_IFCHR S_IFDIR S_IFBLK S_IFREG
              i S_IROTH S_IWOTH S_IXOTH
              t struct stat
              f    st_mode
              f    st_uid

       Filtered through sym2ijs, this will generate the following
       J script on a Sun system:

              NB. do not edit -- created by sym2ijs from -
              S_IFIFO =: 4096
              S_IFCHR =: 8192
              S_IFDIR =: 16384
              S_IFBLK =: 24576
              S_IFREG =: 32768
              S_IROTH =: 4
              S_IWOTH =: 2
              S_IXOTH =: 1
              stat_sz =: 136
              st_mode_off =: 20
              st_mode_sz =: 4
              st_uid_off =: 28
              st_uid_sz =: 4

       The above compiles integer constants and some  information
       about  the  stat  structure  type  and  two of its fields.
       Equipped with all these values, a J programmer can  access
       stat  structures  with memr and memw.  There is no need to
       figure out C header values or offsets manually or to hard-
       wire those (usually system-dependent) values anywhere.

       The  purpose of this tool is portable J code sticking to C
       APIs as published in library manual pages.   Sym2ijs  cre-
       ates  an intermediate C language program and runs the sys-
       tem's C compiler on that.  This  intermediate  program  in
       turn  will  be  run  once and print all the J assignments.
       Something like make(1) is perfect to re-create the J defi-
       nitions whenever necessary.

OPTIONS
       Options  must  be listed separately, i.e., they may not be
       aggregated under a common hyphen.

       -C compiler-call
              Sym2ijs will search automatically for some standard
              compiler  names:  cc,  gcc, c89, acc, lcc, xlc.  If
              that should not fit the bill, you can set the  com-
              piler name explicitly.  You can also add some flags
              if you take care of the proper quoting.  For  exam-
              ple,

                 sym2ijs -C "gcc -I../inc -DFOO='has some blanks'"  foo.sym

       -k     keep  any  intermediate files.  If the file foo.ijs
              is processed, there will be the intermediate  files
              foo.c  and  foo in order to create the final output
              file foo.ijs.  If stdin  is  processed,  the  files
              will  be  named  sym2ijsPID.c and sym2ijsPID in the
              /tmp directory, with .I PID being a unique  process
              id.

       -c     Stop  after generation of the C input file (implies
              -k).

       -cc    Stop after compilation of the C input file (implies
              -k).

INPUT FORMAT
       Input will most likely start with the listing of some pre-
       requiste #include files.  The  first  empty  line  in  the
       input  marks the transition from the header section to the
       section with various directives to translate symbols.

       Every input line is analyzed according to the character or
       word  at  the  very beginning of the line.  No line may be
       indented.  The rest of the line may employ arbitrary forms
       of white space.  The following input line formats are rec-
       ognized and processed:

       <filename>
              Include the specified system header file.

       "filename"
              Include the specified "local" header file.

       first emtpy line
              Switch from global section in  the  intermediary  C
              program  to  the  main()  function  issuing all the
              print statements for the values.  No further header
              files may be listed after this line.

       other empty lines
              ...  will  be simply preserved in the J script out-
              put.

       c symbol ...

       i symbol ...

       s symbol ...
              Typed symbols (of type char, integer,  or  "string"
              resp.)    defined   as  C  header  constants  or  C
              variables.  Corresponding assignments to J  symbols
              will be generated.

       t typename
              Define the (J) symbol typename_sz to have the value
              of (C) sizeof(typename).

       st typename

       st struct structtag
              A structure type is introduced, leading to a  type-
              name_sz  or  structtag_sz  definition as above.  In
              addition, any following f  lines  for  fields  will
              refer  to this structure.  The optional struct key-
              word has to be used where only this type of  defin-
              tion  is  used  in  the  header  file  (and the man
              pages).

       f fieldname
              Define fieldname_sz and  the  offset  fieldname_off
              for  a member of the structure type mentioned last.

       ; comment words ...
              A comment line which will be completely ignored.

       C literal C code

       J literal J code
              The lines will be passed as-is to the  intermediate
              C  or  final  J  program.  The initial C or J along
              with the first  (optional)  white  space  character
              will be removed, the rest is passed literally.

       # C preprocessor directive
              These,  too,  will  be passed to the intermediate C
              program.  They allow for easy conditional  code  as
              in this example:

                 i MAP_SHARED MAP_PRIVATE MAP_FIXED
                 #ifdef sun
                 i MAP_NOPRESERVE
                 #endif

SEE ALSO
       cc(1), j(1), make(1).

DIAGNOSTICS
       Error  messages can be a bit puzzling.  They might be from
       any stage of the entire translation process:  failures  to
       read  the  input,  failures  to compile the intermediate C
       program, failures to run it.

RESTRICTIONS
       Perhaps there should be more support for different  types.
       Unsigned  integers should get their own code; perhaps they
       should better be translated into J extended integers.

AUTHOR
       Martin Neitzel, Gaertner Datensysteme, [email protected]


                                                       sym2ijs(1)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to