main_opt.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* main_opt.inc: Option parser for ld-tigcc command-line program
  2. Copyright (C) 2002-2003 Sebastian Reichelt
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. // This file is included by main.c.
  15. {
  16. const char *Arg;
  17. BOOLEAN ArgMatches (const char *Name)
  18. {
  19. return (!(strcmp (Arg, Name)));
  20. }
  21. // For each argument...
  22. for (CurArg = 1; CurArg < ArgCount; CurArg++)
  23. {
  24. // Get pointer to arg.
  25. Arg = Args [CurArg];
  26. // Check whether it is empty.
  27. if (Arg && *Arg)
  28. {
  29. // Check whether it is an option.
  30. if (Arg [0] == '-')
  31. {
  32. Arg++;
  33. if (Arg [0] == '-')
  34. Arg++;
  35. // Handle the option.
  36. #ifdef ENABLE_HELP
  37. if (ArgMatches ("version"))
  38. {
  39. printf ("ar-tigcc Version " PROGRAM_VERSION_STRING "\n"
  40. COPYRIGHT_NOTICE_STRING "\n"
  41. "ar-tigcc is free software; see the source code for details.\n");
  42. goto Cleanup;
  43. }
  44. else if (ArgMatches ("help") || ArgMatches ("-h"))
  45. {
  46. printf ("Usage: ar-tigcc [options] <file> [<file> ...]\n"
  47. "Options:\n"
  48. " -h --help Display this message\n"
  49. " --version Display version number\n"
  50. #ifdef ENABLE_DUMP
  51. " --dump Display small dump of the archive contents\n"
  52. #endif /* ENABLE_DUMP */
  53. " -o --output <file> Write output to <file>\n"
  54. " -rc/-qc <file> Same as `-o' for compatibility with GNU ar\n"
  55. " --no-names Do not include object file names in archive\n");
  56. goto Cleanup;
  57. }
  58. else
  59. #endif /* ENABLE_HELP */
  60. #ifdef ENABLE_DUMP
  61. if (ArgMatches ("dump"))
  62. Dump = TRUE;
  63. else
  64. #endif /* ENABLE_DUMP */
  65. if (ArgMatches ("no-names"))
  66. NoNames = TRUE;
  67. else if (ArgMatches ("o") || ArgMatches ("output") || ArgMatches ("rc") || ArgMatches ("-qc"))
  68. {
  69. if ((++CurArg) < ArgCount)
  70. DestFile = Args [CurArg];
  71. else
  72. Error (NULL, "`--output' option must be followed by a name.");
  73. }
  74. else
  75. Error (NULL, "Unrecognized option `%s'.", Args [CurArg]);
  76. }
  77. else
  78. {
  79. // Treat it as a file name.
  80. // Open the file and load it into memory.
  81. FILE *File = fopen (Arg, "rb");
  82. if (File)
  83. {
  84. SIZE Size;
  85. fseek (File, 0, SEEK_END);
  86. Size = ftell (File);
  87. rewind (File);
  88. {
  89. I1 *Data = malloc (Size);
  90. if (Data)
  91. {
  92. if (fread (Data, Size, 1, File) == 1)
  93. {
  94. // Create a new object file inside the current archive.
  95. OBJECT_FILE *ObjectFile = calloc (1, sizeof (OBJECT_FILE));
  96. if (!ObjectFile)
  97. {
  98. Error (Arg, "Out of memory.");
  99. break;
  100. }
  101. ObjectFile->Parent = &Archive;
  102. ObjectFile->Data = Data;
  103. ObjectFile->Size = Size;
  104. ObjectFile->FileName = Arg;
  105. GetFileStats (Arg, ObjectFile->FileStats);
  106. Append (Archive.ObjectFiles, ObjectFile);
  107. // Update the statistics of the archive accordingly.
  108. if (StatFileIsNewer (ObjectFile->FileStats, Archive.FileStats))
  109. StatCopyAttributes (Archive.FileStats, ObjectFile->FileStats);
  110. // Try to import the object file's contents.
  111. ArImportObjectFile (ObjectFile);
  112. }
  113. else
  114. Error (Arg, "Unable to read file.");
  115. }
  116. else
  117. Error (Arg, "Not enough memory to load file.");
  118. }
  119. fclose (File);
  120. }
  121. else
  122. Error (Arg, "Unable to open file.");
  123. }
  124. }
  125. }
  126. }