home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 345_01 / tlccmd.c < prev    next >
C/C++ Source or Header  |  1989-07-10  |  10KB  |  383 lines

  1. /* TLCCMD.C - "The Last Cross-referencer" - Command line routines        */
  2. /*    Last Modified:    07/07/89                                            */
  3.  
  4. /*
  5. ---------------------------------------------------------------------
  6. Copyright (c) 1987-1989, Eduard Schwan Programs [esp] - All rights reserved
  7. TLC (The Last C-Cross-Referencer) and TLP (same, but for Pascal) are
  8. Cross-Reference Generators crafted and shot into the Public Domain by
  9. Eduard Schwan.  The source code and executable program may be freely
  10. distributed as long as the copyright/author notices remain intact, and
  11. it is not used in part or whole as the basis of a commercial product.
  12. Any comments, bug-fixes, or enhancements are welcome.
  13. Also, if you find TLC and it's source code useful, a contribution of
  14. $20 (check/money order) is encouraged!  Hopefully we will all see more
  15. source code distributed!
  16.     Eduard Schwan, 1112 Oceanic Drive, Encinitas, Calif. 92024
  17. ---------------------------------------------------------------------
  18. */
  19.  
  20. /*
  21. HEADER:        The Last Cross-Referencer;
  22. TITLE:        TLC/TLP - The Last Cross-Referencer;
  23. VERSION:    1.01;
  24.  
  25. DESCRIPTION: "TLC/TLP.
  26.             Command line routines";
  27.  
  28. KEYWORDS:    Utility, Cross-reference, C, Pascal, Apple, Macintosh, APW, Aztec;
  29. SYSTEM:        Macintosh MPW, v3.0;
  30. FILENAME:    TLCCMD.C;
  31. WARNINGS:    "Has not yet been ported to MS-DOS.
  32.             Shareware, $20 Check/Money Order suggested.";
  33.  
  34. SEE-ALSO:    README.TLC,TLCHELP.DOC,TLPHELP.DOC;
  35. AUTHORS:    Eduard Schwan;
  36. COMPILERS:    AZTEC C65 v3.2b, APPLEIIGS APW C v1.0, APPLE MACINTOSH MPW C v3.0;
  37. */
  38.  
  39.  
  40. /*------------------------ include files ------------------------*/
  41.  
  42. #ifdef AppleIIgs
  43. #include    <shell.h>    /* for filename wildcards */
  44. #endif
  45. #include    <stdio.h>
  46. #include    <errno.h>
  47. #include    "tlc.h"
  48.  
  49.  
  50. /*--------------------- external declarations -------------------*/
  51.  
  52. #include    "tlc.ext"
  53. extern    FILE*    open_text_file();
  54. extern    VOID    close_text_file();
  55. extern    VOID    init_wildcard();
  56. extern    int     next_wildcard();
  57.  
  58.  
  59. /*----------------------- static variables ----------------------*/
  60.  
  61. static file_list_type* flist_tail = NULL;
  62.  
  63. #ifdef AppleIIgs
  64. static char   wild_fname[64];
  65. #endif
  66.  
  67. static char * help_array[] =
  68. {
  69.     "To run The Last CrossReferencer:",
  70.     "   TLC [-f FnameListFile] [-p ParmFile] [-r ResWordFile] [-v]",
  71.     "       [-d] FileName [FileNames..]",
  72.     "where:",
  73.     "   -d                ...Turns on debug messages sent to stderr.",
  74.     "   -f FnameListFile  ...Optional file containing list of source",
  75.     "                        file names to xref (one per line).",
  76.     "   -p ParmFile       ...Optional file containing parameters for",
  77.     "                        tailoring TLC output.",
  78.     "   -r ResWordFile    ...Optional file containing reserved words",
  79.     "                        not to xref.",
  80.     "   -v                ...Turns on progress messages sent to stderr,",
  81.     "                        two '-v's make it even more verbose.",
  82.     "   FileName(s)       ...Source files to xref.",
  83.     "",
  84.     "*** TLC/TLP are shareware utilities ($20 suggested), written by:",
  85.     "*** Eduard Schwan, 1112 Oceanic Drive, Encinitas, Calif. 92024",
  86.     NULL,
  87. };
  88.  
  89.  
  90. /*=================[ show_help ]===================*/
  91.  
  92. static VOID show_help()
  93.  
  94.     { /* show_help() */
  95.     char ** help_ptr;
  96.  
  97.     /*
  98.     start at first string in array of string pointers
  99.     and print each string until a null pointer is found.
  100.     */
  101.     help_ptr = help_array;
  102.     while (*help_ptr)
  103.         {
  104.         puts(*help_ptr++);
  105.         }
  106.     } /* show_help() */
  107.  
  108.  
  109. /*=================[ cmd_error ]===================*/
  110.  
  111. static VOID cmd_error(msg, argvp, error)
  112. char    *msg;
  113. char    *argvp;
  114. int     error;
  115.  
  116.     { /* cmd_error */
  117.     fprintf(stderr, "Error ");
  118.     if (error != 0)
  119.         fprintf(stderr, "# %d/$%x ", error, error);
  120.     fprintf(stderr, "handling CmdLineArg '%s'\n", argvp);
  121.     fprintf(stderr, "%s\n", msg);
  122.     } /* cmd_error */
  123.  
  124.  
  125. /*=================[ add_fname ]===================*/
  126.  
  127. static VOID add_fname(fname)
  128. char    *fname;
  129.  
  130.     { /* add_fname */
  131.  
  132.     if (verbose > 1)
  133.         fprintf(stderr,"add file # %d:'%s'\n",
  134.                 file_rec.num_in_fnames+1,fname);
  135.     if (file_rec.file_list==NULL)
  136.         { /* first on the list */
  137. /*debug(printf(" added first on list\n");)*/
  138.         file_rec.file_list = (file_list_type*)malloc(sizeof(file_list_type));
  139.         if (file_rec.file_list==NULL)
  140.             {
  141.             fprintf(stderr,"Out of memory adding file # %d:'%s'!\n",
  142.                     file_rec.num_in_fnames+1,fname);
  143.             exit(BAD_EXIT);
  144.             }
  145.         flist_tail                = file_rec.file_list;
  146.         flist_tail->file_num    = file_rec.num_in_fnames+1;
  147.         flist_tail->num_lines    = 0;
  148.         flist_tail->next        = NULL;
  149.         }
  150.     else
  151.         { /* add to end of existing list */
  152. /*debug(printf(" added next on list\n");)*/
  153.         flist_tail->next = (file_list_type*)malloc(sizeof(file_list_type));
  154.         if (flist_tail->next==NULL)
  155.             {
  156.             fprintf(stderr,"Out of memory adding file # %d:'%s'!\n",
  157.                     file_rec.num_in_fnames+1,fname);
  158.             exit(BAD_EXIT);
  159.             }
  160.         flist_tail                = flist_tail->next;
  161.         flist_tail->file_num    = file_rec.num_in_fnames+1;
  162.         flist_tail->num_lines    = 0;
  163.         flist_tail->next        = NULL;
  164.         }
  165.     flist_tail->file_name = malloc(strlen(fname)+1);
  166.     if (flist_tail->file_name==NULL)
  167.         {
  168.         fprintf(stderr,"Out of memory adding file # %d:'%s'!\n",
  169.                 file_rec.num_in_fnames+1,fname);
  170.         exit(BAD_EXIT);
  171.         }
  172.     strcpy(flist_tail->file_name, fname);
  173.     file_rec.num_in_fnames++;
  174.     } /* add_fname */
  175.  
  176.  
  177. /*=================[ read_in_fnames ]===================*/
  178.  
  179. static VOID read_in_fnames(fp, fname)
  180. FILE    *fp;
  181. char    *fname;
  182.  
  183.     { /* read_in_fnames() */
  184.     pos_int line_num;
  185.  
  186.     if (verbose)
  187.         fprintf(stderr,"Process filenames in '%s'\n",fname);
  188.     line_num = 0;
  189.     while (!feof(fp))
  190.         {
  191.         errno = 0;
  192.         fgets(curr_line, LINE_SIZE, fp);
  193.         line_num++;
  194. /*debug(printf("read_in_fnames:%2d:'%s'",line_num,curr_line);)*/
  195.         if (!feof(fp) && errno != 0)
  196.             {
  197.             cmd_error("Error reading line in -f file",
  198.                         fname, errno);
  199.             exit(BAD_EXIT);
  200.             }
  201.         if (strlen(curr_line) > 1 && !feof(fp))
  202.             if (curr_line[0]!=CH_SPACE && curr_line[0]!=CH_COMMENT)
  203.                {
  204.                curr_line[strlen(curr_line)-1] = 0; /* zap the <CR> */
  205.                if (strlen(curr_line) < FNAME_SIZE)
  206.                     {
  207. #ifdef AppleIIgs
  208.                     init_wildcard(curr_line);
  209.                     while (next_wildcard(wild_fname))
  210.                         add_fname(wild_fname);
  211. #else
  212.                     add_fname(curr_line);
  213. #endif
  214.                     }
  215.                else
  216.                    {
  217.                    fprintf(stderr,
  218.                         "FileName too long on line %d of file '%s'\n",
  219.                         line_num, fname);
  220.                    exit(BAD_EXIT);
  221.                    }
  222.                }
  223.         } /*while*/
  224.     } /* read_in_fnames() */
  225.  
  226.  
  227. /*=================[ handle_cmd_line ]===================*/
  228.  
  229. VOID handle_cmd_line(argc,argv)
  230. int     argc;
  231. char    **argv;
  232.  
  233.     { /* handle_cmd_line() */
  234.     FILE    *fp;
  235.     int     error;
  236.  
  237. debug(printf("handle_cmd_line:#args=%d\n",argc);)
  238.     /*
  239.     initialize file list tail pointer
  240.     */
  241.     flist_tail = NULL;
  242.  
  243.     /*
  244.     handle xref run with no arguments
  245.     */
  246.     if (argc == 1)
  247.         {
  248.         show_help();
  249.         exit(BAD_EXIT);
  250.         }
  251.  
  252.     /*
  253.     loop through & handle each command line argument
  254.     */
  255.     while (--argc)
  256.         {
  257.         /*
  258.         go to next command line parameter (the 1st is the progname)
  259.         */
  260.         argv++;
  261.         switch (tolower((*argv)[0]))
  262.             {
  263.             case '-':
  264.                 switch (tolower((*argv)[1]))
  265.                     {
  266.  
  267.                     case 'd': /* turn on debug messages */
  268.                         enable_debugs = TRUE;
  269.                         break;
  270.  
  271.                     case 'f': /* read file list file */
  272.                         if (argc < 2)
  273.                             {
  274.                             cmd_error("Expected filename after -f",
  275.                                         *argv, 0);
  276.                             exit(BAD_EXIT);
  277.                             }
  278.                         else
  279.                             { /* open & read fname list file */
  280.                             argv++;
  281.                             argc--;
  282.                             fp=open_text_file(*argv,"r",&error);
  283.                             if (error)
  284.                                 {
  285.                                 cmd_error("Cannot open -f file",
  286.                                             *argv, error);
  287.                                 exit(BAD_EXIT);
  288.                                 }
  289.                             else
  290.                                 {
  291.                                 read_in_fnames(fp, *argv);
  292.                                 close_text_file(fp, *argv);
  293.                                 }
  294.                             } /* else open & read fname list file */
  295.                         break;
  296.  
  297.                     case 'p': /* change PARMFILE name */
  298.                         if (argc < 2)
  299.                             {
  300.                             cmd_error("Expected filename after -p",
  301.                                         *argv, 0);
  302.                             exit(BAD_EXIT);
  303.                             }
  304.                         else
  305.                             {
  306.                             argv++;
  307.                             argc--;
  308.                             if (strlen(*argv)+1 > FNAME_SIZE)
  309.                                 {
  310.                                 cmd_error("-p filename too long",
  311.                                             *argv, 0);
  312.                                 exit(BAD_EXIT);
  313.                                 }
  314.                             else
  315.                                 {
  316.                                 strcpy(file_rec.parm_fname, *argv);
  317.                                 if (verbose > 1)
  318.                                     fprintf(stderr,
  319.                                     "set ParmFname to '%s'\n",
  320.                                     file_rec.parm_fname);
  321.                                 }
  322.                             }
  323.                         break;
  324.  
  325.                     case 'r': /* change RESWORDFILE name */
  326.                         if (argc < 2)
  327.                             {
  328.                             cmd_error("Expected filename after -r",
  329.                                         *argv, 0);
  330.                             exit(BAD_EXIT);
  331.                             }
  332.                         else
  333.                             {
  334.                             argv++;
  335.                             argc--;
  336.                             if (strlen(*argv)+1 > FNAME_SIZE)
  337.                                 {
  338.                                 cmd_error("-r filename too long",
  339.                                             *argv, 0);
  340.                                 exit(BAD_EXIT);
  341.                                 }
  342.                             else
  343.                                 {
  344.                                 strcpy(file_rec.resw_fname, *argv);
  345.                                 if (verbose > 1)
  346.                                     fprintf(stderr,
  347.                                     "Set ResWordFname to '%s'\n",
  348.                                     file_rec.resw_fname);
  349.                                 }
  350.                             }
  351.                         break;
  352.  
  353.                     case 'v': /* VERBOSE option on */
  354.                         verbose++;
  355.                         if (verbose)
  356.                             fprintf(stderr, "Verbose option on at level %d\n",verbose);
  357.                         break;
  358.  
  359.                     default:
  360.                         cmd_error("Unknown option, type TLC ? for help",
  361.                                     *argv, 0);
  362.                         break;
  363.                     }; /*switch*/
  364.                     break;
  365.  
  366.                 case '?': /* help for the neophyte */
  367.                     show_help();
  368.                     exit(GOOD_EXIT);
  369.                     break; /* never gets here (exit above).. */
  370.  
  371.                 default: /* assume file name */
  372. #ifdef AppleIIgs
  373.                     init_wildcard(*argv);
  374.                     while (next_wildcard(wild_fname))
  375.                         add_fname(wild_fname);
  376. #else
  377.                     add_fname(*argv);
  378. #endif
  379.                     break;
  380.             }; /*switch*/
  381.         } /*while*/
  382.     } /* handle_cmd_line() */
  383.