home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume11 / netdata / netdata.c < prev    next >
C/C++ Source or Header  |  1987-10-03  |  7KB  |  335 lines

  1. /* netdata.c:  convert text file to CMS    netdata    format      */
  2. /*           output is a stream of 80-byte card images  */
  3.  
  4. /* Author:   Gary Mills    <mills@cc.uofm.cdn>          */
  5. /*            <mills@uofmcc.bitnet>          */
  6.  
  7. /* Compilation Switches:                  */
  8. /*    MVS:    Waterloo C v1.3    for MVS              */
  9. /*    default:  Amdahl UTS Unix System V          */
  10.  
  11. /* #define MVS       1 */
  12. #include <stdio.h>
  13. #include <time.h>
  14. #ifdef    MVS
  15. #define    ATE(c)    (c)
  16. #else
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. extern long time();
  20. extern struct tm *gmtime();
  21. extern char atetab[128];    /* ASCII to EBCDIC translation table */
  22.  
  23. #define    ATE(c)    atetab[c]
  24. #endif
  25. #define    NUL    '\0'
  26. #define    TCAST    (long *)
  27. #define    SIZBUF    254
  28. #define    SIZREC    255
  29. #ifdef    MVS
  30. #define    ONODE    "UOFMCC"
  31. #else
  32. #define    ONODE    "UOFMCCX"
  33. #endif
  34.  
  35. /* control record identifiers */
  36. #define    INMR01    "INMR01"
  37. #define    INMR02    "INMR02"
  38. #define    INMR03    "INMR03"
  39. #define    INMR06    "INMR06"
  40.  
  41. /* text    unit keys */
  42. #define    INMLRECL 0x0042
  43. #define    INMFNODE 0x1011
  44. #define    INMFUID     0x1012
  45. #define    INMTNODE 0x1001
  46. #define    INMTUID     0x1002
  47. #define    INMFTIME 0x1024
  48. #define    INMNUMF     0x102f
  49. #define    INMSIZE     0x102c
  50. #define    INMDSORG 0x003c
  51. #define    INMUTILN 0x1028
  52. #define    INMRECFM 0x0049
  53. #define    INMTERM     0x0028
  54. #define    INMDSNAM 0x0002
  55. #define    INMFFM     0x102d
  56. #define    INMLCHG     0x1021
  57.  
  58. static char    inbuf[SIZBUF];
  59. static char    rec[SIZREC];
  60. static char    tvalue[21];
  61. #ifndef    MVS
  62. static struct    stat sbuf;
  63. #endif
  64. static int    count;    /* output char count */
  65. static FILE    *ofile;    /* output file */
  66.  
  67. char *acunit(),    *abunit(), *acfield(), *inirec(), *membin();
  68.  
  69. main(argc, argv) int argc; char    **argv;
  70.     {
  71.     int    n;
  72.     char *pend,    *pstar;
  73.     FILE *ifile;
  74.     char c;
  75.     char *ouser, *dnode, *duser;
  76.     char *tcurr, *tlast;
  77.     char *fname, *ftype, *pgm;
  78.     int    fsize, maxl;
  79.     long tbuf;
  80.     struct tm *tpt;
  81.  
  82. #ifdef MVS
  83.     tpt    = localtime();
  84. #else
  85.     tbuf = time( TCAST 0 );
  86.     tpt    = gmtime(&tbuf);
  87. #endif
  88.     sprintf(tvalue, "%04d%02d%02d%02d%02d%02d%06d",
  89.     tpt->tm_year+1900, tpt->tm_mon+1, tpt->tm_mday,
  90.     tpt->tm_hour, tpt->tm_min, tpt->tm_sec,    0);
  91.     tcurr = tlast = tvalue;
  92.     ifile = stdin;
  93.     ofile = stdout;
  94.     ouser = duser = fname = "MAILER";
  95.     dnode = ONODE;
  96.     ftype = "MAIL";
  97.     fsize = 0x0800;
  98.     maxl = SIZBUF-1;
  99.  
  100.     pgm    = *argv++;
  101.     --argc;
  102.     while ( argc )
  103.     {
  104.     if ( **argv == '-' )
  105.         {
  106.         c =    tolower( (*argv)[1] );
  107.         ++argv;
  108.         --argc;
  109.         if ( argc )
  110.         {
  111.         --argc;
  112.         if ( c == 'n' )
  113.             fname = *argv++;
  114.         else if    ( c == 't' )
  115.             ftype = *argv++;
  116.         else if    ( c == 'u' )
  117.             ouser = *argv++;
  118.         else if    ( c == 'd' )
  119.             dnode = *argv++;
  120.         else if    ( c == 'v' )
  121.             duser = *argv++;
  122.         else if    ( c == 's' )
  123.             fsize = atoi(*argv++);
  124.         else
  125.             {
  126.             ++argc;
  127.             break;
  128.             }
  129.         }
  130.         else
  131.         {
  132.         ++argc;
  133.         break;
  134.         }
  135.         }
  136.     else
  137.         {
  138.         if ( ( ifile = fopen(*argv,    "r") ) != NULL )
  139.         {
  140. #ifndef    MVS
  141.         if ( 0 == fstat( fileno(ifile),    &sbuf) )
  142.             fsize = sbuf.st_size;
  143. #endif
  144.         }
  145.         else
  146.         {
  147.         fprintf(stderr,    "%s: cannot open %s\n",    pgm, *argv);
  148.         return(1);
  149.         }
  150.         --argc;
  151.         break;
  152.         }
  153.     }
  154.     if ( argc )
  155.     {
  156.     fprintf(stderr,    "usage:    %s [-n name -t type -u orig_user\n", pgm);
  157.     fprintf(stderr,    "    -d dest_node -v    dest_user -s size file]\n");
  158.     return(1);
  159.     }
  160. #ifdef MVS
  161.     ofile = fopen("SYSUT2 ( bin", "w");
  162.     if ( ofile == NULL )
  163.     return 1;
  164. #endif
  165.  
  166.     pend = inirec(rec, INMR01, -1);    /* header */
  167.     pend = abunit(pend,    INMLRECL, 80, 2);
  168.     pend = acunit(pend,    INMFNODE, ONODE);
  169.     pend = acunit(pend,    INMFUID, ouser);
  170.     pend = acunit(pend,    INMTNODE, dnode);
  171.     pend = acunit(pend,    INMTUID, duser);
  172.     pend = acunit(pend,    INMFTIME, tcurr);
  173.     pend = abunit(pend,    INMNUMF, 1, 4);
  174.     finrec(rec,    pend);
  175.  
  176.     pend = inirec(rec, INMR02, 1);    /* output utility */
  177.     pend = abunit(pend,    INMSIZE, fsize,    8);
  178.     pend = abunit(pend,    INMDSORG, 0x4000, 2);
  179.     pend = abunit(pend,    INMLRECL, maxl,    4);
  180.     pend = acunit(pend,    INMUTILN, "INMCOPY");
  181.     pend = abunit(pend,    INMRECFM, 0x0002, 2);
  182.     pstar = pend;
  183.     pend = acunit(pend,    INMDSNAM, "A");
  184.     pend = acfield(pend, fname);
  185.     pend = acfield(pend, ftype);
  186.     pstar[3] = 3;
  187.     pend = acunit(pend,    INMFFM,    "1");
  188.     pend = acunit(pend,    INMLCHG, tlast);
  189.     finrec(rec,    pend);
  190.  
  191.     pend = inirec(rec, INMR03, -1);    /* input data */
  192.     pend = abunit(pend,    INMSIZE, fsize,    8);
  193.     pend = abunit(pend,    INMDSORG, 0x4000, 2);
  194.     pend = abunit(pend,    INMLRECL, 80, 2);
  195.     pend = abunit(pend,    INMRECFM, 0x0001, 2);
  196.     finrec(rec,    pend);
  197.  
  198.     while ( n =    igets(ifile) )        /* data    records    */
  199.     {
  200.     while (    n > 1 && inbuf[n-1] == ' ' )
  201.         --n;
  202.     inbuf[n] = NUL;
  203.     odata(n, inbuf);
  204.     }
  205.  
  206.     pend = inirec(rec, INMR06, -1);    /* trailer */
  207.     finrec(rec,    pend);
  208.  
  209.     finout();
  210.     return 0;
  211.     }
  212.  
  213. /* get input line, expanding tabs and padding null lines */
  214. igets(fp) FILE *fp;
  215.     {
  216.     int    c, col;
  217.     col    = 0;
  218.     while ( ( c    = getc(fp) ) !=    EOF )
  219.     {
  220.     if ( c == '\t' )
  221.         do
  222.         inbuf[col++] = ' ';
  223.         while ( col    < SIZBUF-1 && col % 8 );
  224.     else if    ( c == '\n' )
  225.         {
  226.         if ( col ==    0 )
  227.         inbuf[col++] = ' ';
  228.         break;
  229.         }
  230.     else
  231.         {
  232.         inbuf[col++] = c;
  233.         }
  234.     if ( !(    col < SIZBUF-1 ) )
  235.         break;
  236.     }
  237.     inbuf[col] = NUL;
  238.     return col;
  239.     }
  240.  
  241. /* initialize control record */
  242. char *inirec(pt, s, n) char *pt, *s; int n;
  243.     {
  244.     *pt++ = 0;        /* length bythe    */
  245.     *pt++ = 0xe0;    /* flag    byte */
  246.     while ( *s )    /* identifier */
  247.     *pt++ =    ATE( toupper(*s++) );
  248.     if ( n > 0 )    /* number of files */
  249.     pt = membin(pt,    n, 4);
  250.     return pt;
  251.     }
  252.  
  253. /* finalize control record */
  254. finrec(pt, pe) char *pt, *pe;
  255.     {
  256.     *pt    = pe - pt;    /* length byte */
  257.     while ( pt < pe )    /* record */
  258.     {
  259.     putc(*pt, ofile);
  260.     ++pt;
  261.     ++count;
  262.     }
  263.     }
  264.  
  265. /* add a binary    text unit */
  266. char *abunit(pt, k, v, n) char *pt; int    k, v, n;
  267.     {
  268.     pt = membin(pt, k, 2);    /* key */
  269.     pt = membin(pt, 1, 2);    /* count */
  270.     pt = membin(pt, n, 2);    /* length */
  271.     pt = membin(pt, v, n);    /* value */
  272.     return pt;
  273.     }
  274.  
  275. /* add a character text    unit */
  276. char *acunit(pt, k, s) char *pt; int k;    char *s;
  277.     {
  278.     pt = membin(pt, k, 2);    /* key */
  279.     pt = membin(pt, 1, 2);    /* count */
  280.     pt = membin(pt, strlen(s), 2);    /* length */
  281.     while ( *s )        /* value */
  282.     *pt++ =    ATE( toupper(*s++) );
  283.     return pt;
  284.     }
  285.  
  286. /* add a character field */
  287. char *acfield(pt, s) char *pt, *s;
  288.     {
  289.     pt = membin(pt, strlen(s), 2);    /* length */
  290.     while ( *s )        /* value */
  291.     *pt++ =    ATE( toupper(*s++) );
  292.     return pt;
  293.     }
  294.  
  295. /* add a binary    item */
  296. char *membin(pt, v, n) char *pt; int v,    n;
  297.     {
  298.     char *pe;
  299.     pe = pt + n;
  300.     while ( --pe >= pt )
  301.     {
  302.     *pe = v;
  303.     v = v /    256;
  304.     }
  305.     return pt +    n;
  306.     }
  307.  
  308. /* write a data    record */
  309. odata(n, pt) int n; char *pt;
  310.     {
  311.     char *pe;
  312.     pe = pt + n;
  313.     putc(n+2, ofile);  /* length byte */
  314.     putc(0xc0, ofile); /* flag byte */
  315.     count = count + 2;
  316.     while ( pt < pe )    /* data    */
  317.     {
  318.     putc(ATE(*pt), ofile);
  319.     ++pt;
  320.     ++count;
  321.     }
  322.     }
  323.  
  324. /* pad last record */
  325. finout()
  326.     {
  327.     while ( count % 80 )
  328.     {
  329.     putc(0,    ofile);
  330.     ++count;
  331.     }
  332.     }
  333.  
  334. /**/
  335.