home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / utility / sed.arc / sajnote.sed < prev    next >
Text File  |  1989-03-06  |  2KB  |  49 lines

  1. After putting an unreasonable amount of effort into debugging this beast, I
  2. feel entitled to add a few words to what's already in the .man file.
  3.  
  4. The main reason I wanted sed was to help distinguish defining and referencing 
  5. declarations of external variables in C.  Andy Tanenbaum got me started on
  6. this in his Operating Systems, Design and Implementation (the MINIX book).
  7. Anyway, I adopted his idea of a pre-processor macro that expands to a 
  8. gramatically proper referencing declaration everywhere except in one file,
  9. where it is redefined to expand to a proper defining declaration.  This does
  10. not serve for initialized external variables, however.  My solution follows:
  11.  
  12. The macros EXTERN add Initialize are defined as
  13.  
  14. #define EXTERN extern
  15. #define INITIALIZE(type, name, value) extern type name
  16.  
  17. except in one file, which says
  18.  
  19. #undef EXTERN
  20. #undef INITIALIZE
  21. #define EXTERN
  22. #define INITIALIZE(type, name, value) type name = (value)
  23.  
  24. This allows things like
  25.  
  26. INITIALIZE(double, imagintvl, -.0125);
  27. EXTERN double reintmp, imintmp;
  28. EXTERN struct complex upleft, center;
  29. INITIALIZE(int, drawarray[4], ({0, 9, 0, 0}));
  30.  
  31. Fine so far.  But that initializer {0. 9, 0, 0} really does need to be 
  32. enclosed in parentheses, because only parentheses can protect a comma from
  33. being taken as a token delimiter by the C pre-processor.  Similarly,
  34. the INITIALIZE macro really should put the initializer in parentheses
  35. to avoid unpleasant surprises.  Imagine my surprise when C compilers 
  36. rejected "int drawarray[4] = (({0, 9, 0, 0}));".  It seems that the syntax
  37. of a multi-value initializer requires " = <optional whitespace> {".  Damn.
  38.  
  39. Sed to the rescue!! My makefile for a complex program now includes
  40.  
  41.  
  42. expanded.c:  defines.c
  43.     cc -E defines.c > $(TMPDIR)\expanded
  44.     sed -e '/=/s/(\([{"].*\));/\1;/' $(TMPDIR)\expanded > $(TMPDIR)\filtered
  45.     sed -e '/=/s/(\([{"].*\));/\1;/' $(TMPDIR)\filtered > expanded.c
  46.  
  47. Ta daaa!
  48.                 Stephen Jacobs (saj@chinet.chi.il.us)
  49.