home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part10 / rpc / rpcgen / rpc_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.3 KB  |  267 lines

  1. #ifndef lint 
  2. static char sccsid[] = "@(#)rpc_main.c 1.2 86/04/30 (C) 1986 SMI";
  3. #endif
  4.  
  5. /*
  6.  * rpc_main.c, Top level of the RPC protocol compiler.
  7.  *
  8.  * Copyright (C) 1986, Sun Microsystems, Inc.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <strings.h>
  13. #include <sys/file.h>
  14. #include "rpc_util.h"
  15. #include "rpc_parse.h"
  16. #include "rpc_scan.h"
  17.  
  18. static cflag;
  19. static hflag;
  20. static sflag;
  21. static char *cmdname;
  22.  
  23. main(argc,argv)
  24.     int argc;
  25.     char *argv[];
  26. {
  27.  
  28.     cmdname = argv[0];
  29.     if (! parseargs(argc,argv)) {
  30.         fprintf(stderr,
  31.             "usage: %s infile\n",cmdname);
  32.         fprintf(stderr,
  33.             "       %s [-c | -h] [-o outfile] [infile]\n",cmdname);
  34.         fprintf(stderr,
  35.             "       %s [-s udp|tcp]* [-o outfile] [infile]\n",
  36.             cmdname);
  37.         exit(1);
  38.     }
  39.     if (cflag) {
  40.         open_input(infile);
  41.         open_output(outfile,(char*)NULL);
  42.         c_output();
  43.     } else if (hflag) {
  44.         open_input(infile);
  45.         open_output(outfile,(char*)NULL);
  46.         h_output();
  47.     } else if (sflag) {
  48.         open_input(infile);
  49.         open_output(outfile,(char*)NULL);
  50.         s_output(argc,argv);
  51.     } else {
  52.         open_input(infile);
  53.         open_output(infile,".c");
  54.         c_output();    
  55.         outfile2 = outfile;
  56.         reinitialize();
  57.         open_input(infile);    
  58.         open_output(infile,".h");
  59.         h_output();    
  60.     }
  61. }
  62.  
  63. char *
  64. extend(file,ext)
  65.     char *file;
  66.     char *ext;
  67. {
  68.     char *res;
  69.     char *p;
  70.  
  71.     res = alloc(strlen(file) + strlen(ext) + 1);
  72.     if (res == NULL) {
  73.         abort();    
  74.     }
  75.     p = rindex(file,'.');
  76.     if (p == NULL) {
  77.         return(NULL);
  78.     }
  79.     strcpy(res,file);
  80.     strcpy(res + (p - file),ext);
  81.     return(res);
  82. }
  83.  
  84.     
  85. open_output(file,ext)
  86.     char *file;
  87.     char *ext;
  88. {
  89.  
  90.     if (file == NULL) {
  91.         fout = stdout;
  92.         return;
  93.     }
  94.     if (ext != NULL) {
  95.         if (! (outfile = extend(file, ext))) {
  96.             fprintf(stderr,"%s: %s has no extension\n",cmdname,file);
  97.             crash();    
  98.         }
  99.     } else {
  100.         outfile = file;
  101.     }
  102.     if (infile != NULL && streq(outfile,infile)) {
  103.         fprintf(stderr,"%s: output would overwrite %s\n",cmdname,infile);
  104.         outfile = NULL;
  105.         crash();
  106.     }
  107.     fout = fopen(outfile,"w");    
  108.     if (fout == NULL) {
  109.         fprintf(stderr,"%s: unable to open ",cmdname);
  110.         perror(outfile);
  111.         crash();    
  112.     }
  113. }
  114.  
  115. open_input(file)
  116.     char *file;
  117. {
  118.     if (file == NULL) {
  119.         fin = stdin;
  120.         return;
  121.     }
  122.     infile = file;
  123.     fin = fopen(infile,"r");
  124.     if (fin == NULL) {
  125.         fprintf(stderr,"%s: unable to open ",cmdname);
  126.         perror(infile);
  127.         crash();    
  128.     }
  129. }
  130.  
  131.  
  132. c_output()
  133. {
  134.     definition *def;
  135.     char *include;    
  136.  
  137.     fprintf(fout,"#include <rpc/rpc.h>\n");
  138.     if (infile && (include = extend(infile,".h"))) {
  139.         fprintf(fout,"#include \"%s\"\n",include);
  140.         free(include);
  141.     }
  142.     scanprint(OFF);
  143.     while (def = get_definition()) {
  144.         emit(def);
  145.     }
  146. }
  147.  
  148. h_output()
  149. {
  150.     definition *def;
  151.  
  152.     scanprint(ON);    
  153.     while (def = get_definition()) {
  154.         print_datadef(def);
  155.     }
  156.     print_funcdefs();
  157. }
  158.  
  159. s_output(argc,argv)
  160.     int argc;
  161.     char *argv[];
  162. {
  163.     char *include;    
  164.  
  165.     scanprint(OFF);
  166.     fprintf(fout,"#include <stdio.h>\n");
  167.     fprintf(fout,"#include <rpc/rpc.h>\n");
  168.     if (infile && (include = extend(infile,".h"))) {
  169.         fprintf(fout,"#include \"%s\"\n",include);
  170.         free(include);
  171.     }
  172.     while (get_definition()) 
  173.         ;
  174.     write_most();
  175.     do_registers(argc,argv);    
  176.     write_rest();
  177. }
  178.  
  179.  
  180. do_registers(argc,argv)
  181.     int argc;
  182.     char *argv[];
  183. {
  184.     int i;
  185.  
  186.     for (i = 1; i < argc; i++) {
  187.         if (streq(argv[i],"-s")) {
  188.             write_register(argv[i+1]);
  189.             i++;
  190.         }
  191.     }
  192. }
  193.  
  194. static
  195. parseargs(argc,argv)
  196.     int argc;
  197.     char *argv[];
  198. {
  199.     int i;
  200.     int j;
  201.     char c;
  202.     char flag[(1 << 8*sizeof(char))];
  203.  
  204.     if (argc < 2) {
  205.         return(0);
  206.     }
  207.  
  208.     flag['c'] = 0;
  209.     flag['h'] = 0;
  210.     flag['s'] = 0;
  211.     flag['o'] = 0;
  212.     for (i = 1; i < argc; i++) {
  213.         if (argv[i][0] != '-') {
  214.             if (infile) {
  215.                 return(0);
  216.             }
  217.             infile = argv[i];
  218.         } else {
  219.             for (j = 1; argv[i][j] != 0; j++) {
  220.                 switch (c = argv[i][j]) {
  221.                 case 'c':    
  222.                 case 'h':
  223.                     if (flag[c]) {
  224.                         return(0);
  225.                     }
  226.                     flag[c] = 1;    
  227.                     break;
  228.                 case 'o':    
  229.                 case 's':
  230.                     if (argv[i][j-1] != '-' || argv[i][j+1] != 0) {
  231.                         return(0);
  232.                     }
  233.                     flag[c] = 1;
  234.                     if (++i == argc) {
  235.                         return(0);
  236.                     }
  237.                     if (c == 's') {
  238.                         if (! streq(argv[i],"udp") &&
  239.                                 ! streq(argv[i],"tcp")) {
  240.                             return(0);
  241.                         }
  242.                     } else if (c == 'o') {
  243.                         if (outfile) {
  244.                             return(0);
  245.                         }
  246.                         outfile = argv[i];
  247.                     }
  248.                     goto nextarg;    
  249.     
  250.                 default:
  251.                     return(0);
  252.                 }
  253.             }
  254.         nextarg:
  255.             ;
  256.         }
  257.     }
  258.     cflag = flag['c'];
  259.     hflag = flag['h'];
  260.     sflag = flag['s'];
  261.     if (! cflag && ! hflag && !sflag && (infile == NULL || outfile != NULL)) {
  262.         return(0);
  263.     }
  264.     return(1);
  265. }
  266.         
  267.