Subversion Repositories pentevo

Rev

Blame | Last modification | View Log | Download | RSS feed | ?url?

  1. /* plist.c */
  2. /*****************************************************************************/
  3. /* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only                     */
  4. /*                                                                           */
  5. /* AS-Portierung                                                             */
  6. /*                                                                           */
  7. /* Anzeige des Inhalts einer Code-Datei                                      */
  8. /*                                                                           */
  9. /*****************************************************************************/
  10.  
  11. #include "stdinc.h"
  12. #include <string.h>
  13.  
  14. #include "version.h"
  15. #include "be_le.h"
  16. #include "bpemu.h"
  17. #include "stringlists.h"
  18. #include "cmdarg.h"
  19. #include "msg_level.h"
  20. #include "nls.h"
  21. #include "nlmessages.h"
  22. #include "plist.rsc"
  23. #ifdef _USE_MSH
  24. # include "plist.msh"
  25. #endif
  26. #include "ioerrs.h"
  27. #include "strutil.h"
  28. #include "toolutils.h"
  29. #include "headids.h"
  30.  
  31. /* --------------------------------------------------------------- */
  32.  
  33. static unsigned num_files;
  34. LongWord Sums[SegCount];
  35.  
  36. /* --------------------------------------------------------------- */
  37.  
  38. static void ProcessSingle(const char *pFileName)
  39. {
  40.   FILE *ProgFile;
  41.   Byte Header, Segment, Gran, CPU;
  42.   const TFamilyDescr *FoundId;
  43.   int Ch;
  44.   Word Len, ID;
  45.   LongWord StartAdr;
  46.   Boolean HeadFnd;
  47.  
  48.   ProgFile = fopen(pFileName, OPENRDMODE);
  49.   if (!ProgFile)
  50.     ChkIO(pFileName);
  51.  
  52.   if (!Read2(ProgFile, &ID))
  53.     chk_wr_read_error(pFileName);
  54.   if (ID != FileMagic)
  55.     FormatError(pFileName, getmessage(Num_FormatInvHeaderMsg));
  56.  
  57.   if (num_files > 1)
  58.     printf("%s\n", pFileName);
  59.  
  60.   do
  61.   {
  62.     ReadRecordHeader(&Header, &CPU, &Segment, &Gran, pFileName, ProgFile);
  63.  
  64.     HeadFnd = False;
  65.  
  66.     if (num_files > 1)
  67.       printf("%s", Blanks(strlen(getmessage(Num_MessHeaderLine1F))));
  68.     if (Header == FileHeaderEnd)
  69.     {
  70.       errno = 0; fputs(getmessage(Num_MessGenerator), stdout); ChkIO(OutName);
  71.       do
  72.       {
  73.         errno = 0; Ch = fgetc(ProgFile); ChkIO(pFileName);
  74.         if (Ch != EOF)
  75.         {
  76.           errno = 0; putchar(Ch); ChkIO(OutName);
  77.         }
  78.       }
  79.       while (Ch != EOF);
  80.       errno = 0; printf("\n"); ChkIO(OutName);
  81.       HeadFnd = True;
  82.     }
  83.  
  84.     else if (Header == FileHeaderStartAdr)
  85.     {
  86.       if (!Read4(ProgFile, &StartAdr))
  87.         chk_wr_read_error(pFileName);
  88.       errno = 0;
  89.       printf("%s%08lX\n", getmessage(Num_MessEntryPoint), LoDWord(StartAdr));
  90.       ChkIO(OutName);
  91.     }
  92.  
  93.     else if (Header == FileHeaderRelocInfo)
  94.     {
  95.       PRelocInfo RelocInfo;
  96.       PRelocEntry PEntry;
  97.       PExportEntry PExp;
  98.       int z;
  99.  
  100.       RelocInfo = ReadRelocInfo(ProgFile);
  101.       for (z = 0,  PEntry = RelocInfo->RelocEntries; z < RelocInfo->RelocCount; z++, PEntry++)
  102.         printf("%s  %08lX        %3d:%d(%c)     %c%s\n",
  103.                getmessage(Num_MessRelocInfo),
  104.                LoDWord(PEntry->Addr), RelocBitCnt(PEntry->Type) >> 3,
  105.                RelocBitCnt(PEntry->Type) & 7,
  106.                (PEntry->Type & RelocFlagBig) ? 'B' : 'L',
  107.                (PEntry->Type & RelocFlagSUB) ? '-' : '+', PEntry->Name);
  108.  
  109.       for (z = 0,  PExp = RelocInfo->ExportEntries; z < RelocInfo->ExportCount; z++, PExp++)
  110.         printf("%s  %08lX          %c          %s\n",
  111.                getmessage(Num_MessExportInfo),
  112.                LoDWord(PExp->Value),
  113.                (PExp->Flags & RelFlag_Relative) ? 'R' : ' ',
  114.                PExp->Name);
  115.  
  116.       DestroyRelocInfo(RelocInfo);
  117.     }
  118.  
  119.     else if ((Header == FileHeaderDataRec) || (Header == FileHeaderRDataRec)
  120.           || (Header == FileHeaderRelocRec) || (Header == FileHeaderRRelocRec))
  121.     {
  122.       errno = 0;
  123.       if (Magic != 0)
  124.         FoundId = NULL;
  125.       else
  126.         FoundId = FindFamilyById(CPU);
  127.       if (!FoundId)
  128.         printf("\?\?\?=%02x        ", Header);
  129.       else
  130.         printf("%-13s ", FoundId->Name);
  131.       ChkIO(OutName);
  132.  
  133.       errno = 0; printf("%-7s   ", SegNames[Segment]); ChkIO(OutName);
  134.  
  135.       if (!Read4(ProgFile, &StartAdr))
  136.         chk_wr_read_error(pFileName);
  137.       errno = 0; printf("%08lX          ", LoDWord(StartAdr)); ChkIO(OutName);
  138.  
  139.       if (!Read2(ProgFile, &Len))
  140.         chk_wr_read_error(pFileName);
  141.       errno = 0; printf("%04X       ", LoWord(Len));  ChkIO(OutName);
  142.  
  143.       if (Len != 0)
  144.         StartAdr += (Len / Gran) - 1;
  145.       else
  146.         StartAdr--;
  147.       errno = 0; printf("%08lX\n", LoDWord(StartAdr));  ChkIO(OutName);
  148.  
  149.       Sums[Segment] += Len;
  150.  
  151.       if (ftell(ProgFile) + Len >= FileSize(ProgFile))
  152.         FormatError(pFileName, getmessage(Num_FormatInvRecordLenMsg));
  153.       else if (fseek(ProgFile, Len, SEEK_CUR) != 0)
  154.         ChkIO(pFileName);
  155.     }
  156.     else
  157.      SkipRecord(Header, pFileName, ProgFile);
  158.   }
  159.   while (Header != 0);
  160.  
  161.   errno = 0; fclose(ProgFile); ChkIO(pFileName);
  162.  
  163.   (void)HeadFnd;
  164. }
  165.  
  166. int main(int argc, char **argv)
  167. {
  168.   Word z;
  169.   Boolean FirstSeg;
  170.   as_cmd_results_t cmd_results;
  171.   char *p_file_name;
  172.  
  173.   nls_init();
  174.   if (!NLS_Initialize(&argc, argv))
  175.     exit(4);
  176.  
  177.   be_le_init();
  178.   bpemu_init();
  179.   strutil_init();
  180. #ifdef _USE_MSH
  181.   nlmessages_init_buffer(plist_msh_data, sizeof(plist_msh_data), MsgId1, MsgId2);
  182. #else
  183.   nlmessages_init_file("plist.msg", *argv, MsgId1, MsgId2); ioerrs_init(*argv);
  184. #endif
  185.   as_cmdarg_init(*argv);
  186.   msg_level_init();
  187.   toolutils_init(*argv);
  188.  
  189.   if (e_cmd_err == as_cmd_process(argc, argv, "PLISTCMD", &cmd_results))
  190.   {
  191.     fprintf(stderr, "%s%s\n", getmessage(cmd_results.error_arg_in_env ? Num_ErrMsgInvEnvParam : Num_ErrMsgInvParam), cmd_results.error_arg);
  192.     fprintf(stderr, "%s\n", getmessage(Num_ErrMsgProgTerm));
  193.     exit(1);
  194.   }
  195.  
  196.   if ((msg_level >= e_msg_level_verbose) || cmd_results.write_version_exit)
  197.   {
  198.     String Ver;
  199.  
  200.     as_snprintf(Ver, sizeof(Ver), "PLIST V%s", Version);
  201.     WrCopyRight(Ver);
  202.     errno = 0; printf("\n"); ChkIO(OutName);
  203.   }
  204.  
  205.   if (cmd_results.write_help_exit)
  206.   {
  207.     char *ph1, *ph2;
  208.  
  209.     errno = 0;
  210.     printf("%s%s%s\n", getmessage(Num_InfoMessHead1), as_cmdarg_get_executable_name(), getmessage(Num_InfoMessHead2));
  211.     ChkIO(OutName);
  212.     for (ph1 = getmessage(Num_InfoMessHelp), ph2 = strchr(ph1, '\n'); ph2; ph1 = ph2 + 1, ph2 = strchr(ph1, '\n'))
  213.     {
  214.       *ph2 = '\0';
  215.       printf("%s\n", ph1);
  216.       *ph2 = '\n';
  217.     }
  218.   }
  219.  
  220.   if (cmd_results.write_version_exit || cmd_results.write_help_exit)
  221.     exit(0);
  222.  
  223.   num_files = StringListCount(cmd_results.file_arg_list);
  224.   if (!num_files)
  225.   {
  226.     fprintf(stderr, "%s: %s\n", as_cmdarg_get_executable_name(), getmessage(Num_ErrMessNoInputFiles));
  227.     exit(1);
  228.   }
  229.  
  230.   errno = 0; printf("%s%s\n", (num_files > 1) ? getmessage(Num_MessHeaderLine1F) : "", getmessage(Num_MessHeaderLine1)); ChkIO(OutName);
  231.   errno = 0; printf("%s%s\n", (num_files > 1) ? getmessage(Num_MessHeaderLine2F) : "", getmessage(Num_MessHeaderLine2)); ChkIO(OutName);
  232.  
  233.   for (z = 0; z < SegCount; Sums[z++] = 0);
  234.  
  235.   while (True)
  236.   {
  237.     p_file_name = MoveAndCutStringListFirst(&cmd_results.file_arg_list);
  238.     if (!p_file_name)
  239.       break;
  240.     if (*p_file_name)
  241.     {
  242.       String exp_file_name;
  243.  
  244.       strmaxcpy(exp_file_name, p_file_name, sizeof(exp_file_name));
  245.       AddSuffix(exp_file_name, sizeof(exp_file_name), getmessage(Num_Suffix));
  246.       ProcessSingle(exp_file_name);
  247.     }
  248.     free(p_file_name);
  249.   }
  250.  
  251.   errno = 0; printf("\n"); ChkIO(OutName);
  252.   FirstSeg = True;
  253.   for (z = 0; z < SegCount; z++)
  254.     if ((z == SegCode) || Sums[z])
  255.     {
  256.       errno = 0;
  257.       printf("%s", FirstSeg ? getmessage(Num_MessSum1) : Blanks(strlen(getmessage(Num_MessSum1))));
  258.       printf(LongIntFormat, Sums[z]);
  259.       printf("%s%s\n",
  260.              getmessage((Sums[z] == 1) ? Num_MessSumSing : Num_MessSumPlur),
  261.              SegNames[z]);
  262.       FirstSeg = False;
  263.     }
  264.  
  265.   return 0;
  266. }
  267.