home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / ciftomann.tar / pggen_dir / pggen.c < prev    next >
C/C++ Source or Header  |  1988-01-28  |  7KB  |  410 lines

  1. #include <stdio.h>
  2. #include "getbox.c"
  3.  
  4. #define E_NL '\012'
  5.  
  6. #define NIL (layer_desc *) 0
  7.  
  8. typedef struct layer_desc {
  9.     char name[5];
  10.     int flashs;
  11.     int file_no;
  12.     struct layer_desc *next;
  13. } layer_desc;
  14.  
  15. layer_desc *layer_list, *read_dir();
  16.  
  17. extern char *bin_lib_name;
  18.  
  19. char *getlogin();
  20.  
  21. int x_trans = 0,    /* the amount the masks are to be translated in
  22.              * in the x direction
  23.              */
  24.     y_trans = 0;    /* the amount the masks are to be translated in
  25.              * in the y direction
  26.              */
  27.  
  28. char *dir_file;
  29.  
  30. int x_center,y_center;
  31.  
  32. int left, right, top, bottom;    /* the bounding box for the masks */
  33.  
  34.  
  35. int silent = 0;
  36. int debug = 0;
  37. int center = 0;
  38. int one_pass = 0;
  39.  
  40. char *name;
  41. int command_number = 1;
  42.  
  43. FILE *null_file, *tty, *infile;
  44.  
  45.     /*
  46.      * pggen reads the output of ciftomann, creates a directory
  47.      * of the various masks and converts it to David Mann 3000 format.
  48.      *
  49.      *        In addition, pggen will center the masks at 
  50.      * any location the user chooses.
  51.      */
  52.  
  53. main(argc,argv)
  54. int argc;
  55. char **argv;
  56. {
  57.     char layer_name[5];
  58.     char line_buffer[500];
  59.     layer_desc *current;
  60.     int finished;
  61.     int file_no;
  62.     int first, first_flash;
  63.     int o_x, o_y, o_w, o_l, o_a;
  64.     int i;
  65.     char *ptr;
  66.  
  67.     tty = fopen("/dev/tty", "r");
  68.  
  69.     name = getlogin();
  70.  
  71.     /* input the command line arguments */
  72.  
  73.     get_args(argc,argv);
  74.  
  75.     for (ptr=name; *ptr != '\0'; ptr++) {
  76.     if (*ptr >= 'a' && *ptr <= 'z') {
  77.         *ptr += 'A' - 'a';
  78.     }
  79.     }
  80.  
  81.     
  82.     if (!one_pass) {
  83.  
  84.     finished = 0;
  85.     first = 1;
  86.  
  87.     /*
  88.      *  Make one pass through to build up the directory
  89.      */
  90.  
  91.     do {
  92.         register int c;
  93.  
  94.         flush_space(infile,c);
  95.  
  96.         switch(c) {
  97.         
  98.         case '(' :
  99.             
  100.             flush_comment();
  101.             break;
  102.  
  103.         case 'L' :
  104.             
  105.             if (first == 1) {
  106.             layer_list = (layer_desc *) malloc(sizeof(layer_desc));
  107.             current = layer_list;
  108.             first = 0;
  109.             } else {
  110.             current->next = (layer_desc *) malloc(sizeof(layer_desc));
  111.             current = current->next;
  112.             }
  113.  
  114.             if (fscanf(infile," %5[^ ;]",current->name) != 1) {
  115.             error("Bad Layer name in code");
  116.             }
  117.  
  118.             current->flashs = 0;
  119.             current->next = NIL;
  120.  
  121.             flush_to_semi();
  122.             break;
  123.         
  124.         case 'B' :
  125.  
  126.  
  127.             if ( center ) {
  128.  
  129.             int params;
  130.             int l,w,x,y,x_dir,y_dir;
  131.  
  132.             params = get_box(infile, &l, &w, &x, &y, &x_dir,
  133.                     &y_dir);
  134.  
  135.             if ( params != 4 && params != 6 ) {
  136.                 error("Bad box command");
  137.             }
  138.  
  139.             update_bb(l,w,x,y);
  140.  
  141.             } else {
  142.             flush_to_semi();
  143.             }
  144.  
  145.             current->flashs++;
  146.             break;
  147.  
  148.         case 'E' :
  149.             
  150.             finished = 1;
  151.             break;
  152.  
  153.         default :
  154.             
  155.             error("Unrecognizable command in input");
  156.         }
  157.  
  158.         command_number++;
  159.  
  160.     } while ( !finished );
  161.     rewind(infile);
  162.     } else {
  163.     layer_list = read_dir(dir_file);
  164.     }
  165.  
  166.     /* 
  167.      *    Write the directory onto the tape
  168.      */
  169.  
  170.     fprintf(stderr,"Hit return when the tape is ready : ");
  171.     getc(tty);
  172.  
  173.     if ( !silent ) {
  174.     fprintf(stderr,"\nWriting the directory\n");
  175.     }
  176.  
  177.     start_write();
  178.  
  179.     file_no = 1;
  180.  
  181.     for ( current = layer_list; current != NIL; current = current->next){
  182.     current->file_no = file_no;
  183.     sprintf(line_buffer,"%-8.8s%4s%04d0000",name,current->name,
  184.             file_no);
  185.     put_string(line_buffer);
  186.     file_no++;
  187.     if (file_no%25 == 1) {
  188.         put_string("            "); /* start a new record */
  189.     }
  190.     }
  191.  
  192.     put_string("$");
  193.     
  194.     stop_write();
  195.  
  196.     if ( !silent ) {
  197.     fprintf(stderr, "\n");
  198.     }
  199.  
  200.  
  201.     if ( center ) {
  202.     x_trans = x_center - ( right + left )/2;
  203.     y_trans = y_center - ( top + bottom )/2;
  204.     }
  205.  
  206.     finished = 0;
  207.     first = 1;
  208.     command_number = 1;
  209.     current = layer_list;
  210.  
  211.     /*
  212.         now process the file layer by layer, producing a seperate
  213.         file for each layer
  214.      */
  215.  
  216.     do {
  217.     register int c;
  218.  
  219.     flush_space(infile, c);
  220.  
  221.     switch(c) {
  222.         
  223.         case '(' :
  224.         
  225.         flush_comment();
  226.         break;
  227.  
  228.         case 'L' :
  229.         
  230.         if (fscanf(infile," %5[^ ;]",layer_name) != 1) {
  231.             error("Bad Layer name in code");
  232.         }
  233.  
  234.         if (first == 1) {
  235.             first = 0;
  236.             current = layer_list;
  237.         } else {
  238.             current = current->next;
  239.             put_string("$"); /* mark the end of the mask */
  240.             stop_write();    /* end the previous mask */
  241.         }
  242.  
  243.         if (current == (layer_desc *) 0 || 
  244.             strcmp(current->name, layer_name) != 0) {
  245.             fprintf(stderr, "Layer %s is out of order or not in the directory.\nNo more layers will be written\n", layer_name);
  246.             exit(-1);
  247.         }
  248.  
  249.                 
  250.             
  251.         if ( !silent ) {
  252.             fprintf(stderr,"Starting layer %s\n",layer_name);
  253.             fprintf(stderr,"This layer contains %d flashes\n\n",
  254.                 current->flashs);
  255.         }
  256.  
  257.         start_write(); /* start the mask for this
  258.                         layer */
  259.  
  260.         first_flash = 1;
  261.         flush_to_semi();
  262.         break;
  263.         
  264.         case 'B' :
  265.         {
  266.             int params, tmp, angle;
  267.             int l,w,x,y,x_dir,y_dir;
  268.  
  269.             params = get_box(infile, &l, &w, &x, &y, &x_dir,
  270.                 &y_dir);
  271.             if ( params != 4 && params != 6 ) {
  272.             error("Bad box command");
  273.             }
  274.  
  275.             x += x_trans;
  276.             y += y_trans;
  277.  
  278.             if (params == 6) {
  279.             angle = dir_to_angle(x_dir, y_dir);
  280.  
  281.             if (angle > 900) {
  282.                 /* swap width for length */
  283.                 angle -= 900;
  284.                 tmp = w; w = l; l = tmp;
  285.             } 
  286.             } else {
  287.             angle = 0;
  288.             }
  289.  
  290.             if (first_flash) {
  291.             first_flash = 0;
  292.             o_x = x; o_y = y, o_l = l; o_w = w; o_a = angle;
  293.             put_string("X");
  294.             put_int(x);
  295.             put_string("Y");
  296.             put_int(y);
  297.             put_string("H");
  298.             put_int(w);
  299.             put_string("W");
  300.             put_int(l);
  301.             put_string("A");
  302.             put_int(angle);
  303.             } else {
  304.             if (o_x != x) {
  305.                 put_string("X");
  306.                 put_int(x);
  307.                 o_x = x;
  308.             }
  309.             if (o_y != y) {
  310.                 put_string("Y");
  311.                 put_int(y);
  312.                 o_y = y;
  313.             }
  314.             if (o_w != w) {
  315.                 put_string("H");
  316.                 put_int(w);
  317.                 o_w = w;
  318.             }
  319.             if (o_l != l) {
  320.                 put_string("W");
  321.                 put_int(l);
  322.                 o_l = l;
  323.             }
  324.             if (o_a != angle) {
  325.                 put_string("A");
  326.                 put_int(angle);
  327.                 o_a = angle;
  328.             }
  329.             }
  330.             put_string(";");
  331.             break;
  332.         }
  333.             
  334.  
  335.         case 'E' :
  336.         
  337.         finished = 1;
  338.         break;
  339.  
  340.         default :
  341.         
  342.         error("Unrecognizable command in input");
  343.     }
  344.  
  345.     command_number++;
  346.  
  347.     } while ( !finished );
  348.  
  349.     put_string("$");    /* mark the end of the mask */
  350.     
  351.     stop_write();
  352.  
  353.     if ( !silent ) {
  354.     fprintf(stderr,"Done !\n");
  355.     }
  356. }
  357.  
  358. layer_desc *read_dir(dir_file)
  359. char *dir_file;
  360. {
  361.     FILE *dir;
  362.     int retcode, flashes;
  363.     char name[100];
  364.     layer_desc *current, *list;
  365.  
  366.     dir = fopen(dir_file, "r");
  367.  
  368.     if (dir == NULL) {
  369.     perror(dir_file);
  370.     exit(-1);
  371.     }
  372.  
  373.     retcode = fscanf(dir, "%d %d %d %d", &left, &bottom, &right, &top);
  374.  
  375.     if (retcode != 4) {
  376.     fprintf(stderr, "%s : Bad format for a directory file\n",
  377.             dir_file);
  378.     exit(-1);
  379.     }
  380.  
  381.     current = (layer_desc *) 0;
  382.  
  383.     while ( (retcode = fscanf(dir, "%s %d", name, &flashes)) != EOF) {
  384.  
  385.     if (current == (layer_desc *) 0) {
  386.         list = current = (layer_desc *) malloc(sizeof(layer_desc));
  387.     } else {
  388.         current->next = (layer_desc *) malloc(sizeof(layer_desc));
  389.         current = current->next;
  390.     }
  391.  
  392.     if (retcode != 2) {
  393.         fprintf(stderr, "%s : Bad format for a directory file\n",
  394.             dir_file);
  395.         exit(-1);
  396.     }
  397.  
  398.     strncpy(current->name, name, 5);
  399.     current->flashs = flashes;
  400.     current->next = (layer_desc *) 0;
  401.     }
  402.  
  403.     if (current == (layer_desc *) 0) {
  404.     fprintf(stderr, "%s: No layers in directory file\n", dir_file);
  405.     exit(-1);
  406.     }
  407.  
  408.     return(list);
  409. }
  410.