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

  1. /* TLCPARM.C - "The Last Cross-referencer" - Parameter file routines    */
  2. /*    Last Modified:    02/10/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.             Parameter file routines";
  27.  
  28. KEYWORDS:    Utility, Cross-reference, C, Pascal, Apple, Macintosh, APW, Aztec;
  29. SYSTEM:        Macintosh MPW, v3.0;
  30. FILENAME:    TLCPARM.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. #include    <stdio.h>
  43. #include    <errno.h>
  44. #include    "tlc.h"
  45.  
  46.  
  47. /*------------------------- definitions -------------------------*/
  48.  
  49. #define     PARMN_SIZE            15
  50. #define     EXPECT_PARMNAME     1
  51. #define     EXPECT_EQUALS        2
  52. #define     EXPECT_VALUE        3
  53.  
  54.  
  55. /*--------------------- external declarations --------------------*/
  56.  
  57. #include    "tlc.ext"
  58.  
  59. extern    char*    fgets();
  60. extern    VOID    init_scanner();
  61. extern    int     get_token();
  62. extern    FILE    *open_text_file();
  63. extern    int     close_text_file();
  64. extern    VOID    set_parm();
  65.  
  66.  
  67. /*------------------------ static variables -----------------------*/
  68.  
  69. static pos_int    line_num, expecting;
  70. static char     parm_name[PARMN_SIZE+1];
  71.  
  72.  
  73. /*=================[ do_parmname ]===================*/
  74.  
  75. static VOID do_parmname()
  76.  
  77.     { /* do_parmname() */
  78. /*
  79. debug(printf("do_parmname:\n");)
  80. */
  81.     /*
  82.     save parm_name & advance to next state..
  83.     */
  84.     if (token.tok_type == TOK_ID)
  85.         {
  86.         /*
  87.         copy parm-name into buffer.  if the name is >= PARMN_SIZE,
  88.         then strncpy() won't copy the null byte at the end, so
  89.         we have to take care of that case.
  90.         */
  91.         strncpy(parm_name, token.tok_string, PARMN_SIZE);
  92.         parm_name[PARMN_SIZE] = 0;
  93.         expecting = EXPECT_EQUALS;
  94.         }
  95.     else
  96.         {
  97.         fprintf(stderr,
  98.             "expected a parm-name on line %d col %d of ParmFile '%s'\n",
  99.             line_num, token.tok_column, file_rec.parm_fname);
  100.         exit(BAD_EXIT);
  101.         }
  102.     } /* do_parmname() */
  103.  
  104.  
  105. /*=================[ do_equals ]===================*/
  106.  
  107. static VOID do_equals()
  108.  
  109.     { /* do_equals() */
  110. /*
  111. debug(printf("do_equals:\n");)
  112. */
  113.     /*
  114.     just advance to next state..
  115.     */
  116.     if (token.tok_type == TOK_EQUALS)
  117.         expecting = EXPECT_VALUE;
  118.     else
  119.         {
  120.         fprintf(stderr,
  121.             "expected an '=' on line %d col %d of ParmFile '%s'\n",
  122.             line_num, token.tok_column, file_rec.parm_fname);
  123.         exit(BAD_EXIT);
  124.         }
  125.     } /* do_equals() */
  126.  
  127.  
  128. /*=================[ do_value ]===================*/
  129.  
  130. static VOID do_value()
  131.  
  132.     { /* do_value() */
  133. /*
  134. debug(printf("do_value:\n");)
  135. */
  136.  
  137.     /*
  138.     set parm_name to value & advance to first state..
  139.     */
  140.     if (token.tok_type == TOK_SCONST ||
  141.         token.tok_type == TOK_NCONST)
  142.         {
  143.         set_parm(line_num, parm_name);
  144.         expecting = EXPECT_PARMNAME;
  145.         }
  146.     else
  147.         {
  148.         fprintf(stderr,
  149.             "expected a parm-value on line %d col %d of ParmFile '%s'\n",
  150.             line_num, token.tok_column, file_rec.parm_fname);
  151.         exit(BAD_EXIT);
  152.         }
  153.     } /* do_value() */
  154.  
  155.  
  156. /*=================[ handle_parm_file ]===================*/
  157.  
  158. VOID handle_parm_file()
  159.  
  160.     { /* handle_parm_file() */
  161.     int     error;
  162.     boolean end_of_file;
  163.     FILE    *fp;
  164.  
  165.     if (verbose)    
  166.         fprintf(stderr, "Reading ParmFile '%s'\n", file_rec.parm_fname);
  167.  
  168.     fp = open_text_file(file_rec.parm_fname, "r", &error);
  169.     if (error)
  170.         {
  171.         fprintf(stderr, "Error #%d/$%x opening ParmFile '%s'\n",
  172.                 error, error, file_rec.parm_fname);
  173.         exit(BAD_EXIT);
  174.         }
  175.  
  176.     init_scanner(FALSE, FALSE, TRUE); /* CaseSens=F UseUndersc=F DelQuotes=T */
  177.     line_num = 0;
  178.     end_of_file = feof(fp);
  179.     while (!end_of_file && !error)
  180.         { /* do each line in the file */
  181.         /*
  182.         read next line in file
  183.         */
  184.         errno = 0;
  185.         end_of_file = (fgets(curr_line, LINE_SIZE, fp) == NULL);
  186. debug(printf("%dparm line #%02d:%s",end_of_file,line_num,curr_line);)
  187.         line_num++;
  188.         error = errno;
  189.         if (end_of_file)
  190.             error = 0;
  191.         if (error)
  192.             {
  193.             fprintf(stderr,
  194.             "error # %d/$%x reading line %d of ParmFile '%s'\n",
  195.             error, error, line_num, file_rec.parm_fname);
  196.             exit(BAD_EXIT);
  197.             }
  198.  
  199.         /*
  200.         handle tokens on this line, note that each line resets the
  201.         state variable, so "parmname = value" cannot wrap across lines.
  202.         */
  203.         expecting = EXPECT_PARMNAME;
  204.         while (!end_of_file && !error)
  205.             { /* get parms on a line */
  206.             error = get_token(curr_line);
  207.             if (!error)
  208.                 {
  209.                 switch (expecting)
  210.                     {
  211.                     case EXPECT_PARMNAME:
  212.                         do_parmname();
  213.                         break;
  214.                     case EXPECT_EQUALS:
  215.                         do_equals();
  216.                         break;
  217.                     case EXPECT_VALUE:
  218.                         do_value();
  219.                         break;
  220.                     } /*switch*/
  221.                 } /* if !error */
  222.             } /*while in line*/
  223.  
  224.         /*
  225.         if a new line is needed, not really an error
  226.         */
  227.         if (error)
  228.             {
  229.             if (error == TERR_EOLN)
  230.                 error = 0;
  231.             else
  232.                 {
  233.                 fprintf(stderr,
  234.             "error # %d happened parsing line %d, col %d of ParmFile '%s'\n",
  235.                 error, line_num, token.tok_column, file_rec.parm_fname);
  236.                 exit(BAD_EXIT);
  237.                 }
  238.             }
  239.         } /*while !eof*/
  240.  
  241.     /*
  242.     handle any fatal errors
  243.     */
  244.     if (error)
  245.         {
  246.         fprintf(stderr,
  247.             "error # %d/$%x reading line %d of ParmFile '%s'\n",
  248.             error, error, line_num, file_rec.parm_fname);
  249.         exit(BAD_EXIT);
  250.         }
  251.     error = close_text_file(fp, file_rec.parm_fname);
  252.     } /* handle_parm_file() */
  253.