home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / m36gen.tar / m36gen_dir / m36gen.c < prev    next >
C/C++ Source or Header  |  1996-01-08  |  8KB  |  435 lines

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