Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
6.9 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "extract.h"
  5. /*
  6. * Global file list holder
  7. */
  8. FileEntry *FilesToProcess = NULL;
  9. /*
  10. * File local procedures
  11. */
  12. void AddFileToProcess(PSTR pch, int type);
  13. FileEntry *AllocNewFile(FileEntry **start_file);
  14. PSTR FindExt(PSTR szPath);
  15. char errMissingFile[] = "Error: No file specified for '-%c' option.\n";
  16. char errIllegalOption[] = "Error: Illegal option flag '-%c'\n";
  17. /*
  18. * @doc EXTRACT
  19. * @api void | ParseArgs | Parse the command line arguments and build
  20. * the linked list of files to process.
  21. *
  22. * @parm int | argc | Number of elements of array <p argv>.
  23. * @parm char ** | argv | Array of strings each giving one word of the
  24. * command line arguments.
  25. *
  26. * @comm Processes the command line, and builds a linked list of
  27. * FileEntry structures specifying the source files to process. This
  28. * list is headed by the global variable FilesToProcess. If no source files
  29. * were specified on the command line, FilesToProcess will be NULL.
  30. *
  31. * Each element of FilesToProcess will contain the name of the source
  32. * file to be processed (buffer allocated using <f StringAlloc>) and the
  33. * source file as can be determined from the file extension or the
  34. * current file type as specified using command line options. This type
  35. * is set into the FileEntry structure for the file.
  36. *
  37. * The global variables fNoOutput and szOutputFile will be set according
  38. * to the output options specified. If no output is desired, fNoOutput
  39. * will be true. If stdout is to be used for output, szOutputFile will
  40. * be NULL. If a file is to be used for output, szOutputFile will
  41. * contain a string filename (allocated using <f StringAlloc>).
  42. *
  43. * Illegal options will cause the parameter usage display to be printed
  44. * and the program exited.
  45. *
  46. */
  47. void ParseArgs(argc,argv)
  48. int argc;
  49. char **argv;
  50. {
  51. int i,j;
  52. FileEntry *cur_file;
  53. PSTR sz;
  54. /* The file type for this set of files. Defaults to SRC_UNKNOWN */
  55. int wCurFileType = SRC_UNKNOWN;
  56. int wType;
  57. cur_file=NULL;
  58. i = 1;
  59. while( i < argc ) {
  60. /* Decide what to do with this command line argument
  61. */
  62. switch (*argv[i]) {
  63. /*
  64. * It is a flag
  65. */
  66. #ifdef MSDOS
  67. case '/' :
  68. #endif
  69. case '-' :
  70. ++argv[i];
  71. switch( *argv[i] ) {
  72. /* Setup the no output switch, only check syntax */
  73. case 'n':
  74. case 'N':
  75. fNoOutput = True;
  76. break;
  77. /* Output file option */
  78. case 'o':
  79. case 'O':
  80. ++argv[i];
  81. if(*argv[i]==':')
  82. ++argv[i];
  83. if(strlen(argv[i])==0) /* we have /l<space><file> */
  84. i++;
  85. if (*argv[i])
  86. szOutputFile = StringAlloc(argv[i]);
  87. else {
  88. fprintf(stderr, errMissingFile, 'o');
  89. Usage(argv[0]);
  90. }
  91. break;
  92. /* Treat following files a MASM source code */
  93. case 'A':
  94. case 'a':
  95. wCurFileType = SRC_MASM;
  96. break;
  97. /* Treat following files as C source code */
  98. case 'c':
  99. case 'C':
  100. wCurFileType = SRC_C;
  101. /* Treat following files as unknown source code */
  102. case 'd':
  103. case 'D':
  104. wCurFileType = SRC_UNKNOWN;
  105. break;
  106. default:
  107. fprintf(stderr, errIllegalOption, *argv[i]);
  108. /* FALL THROUGH */
  109. case '?':
  110. case 'H':
  111. case 'h':
  112. Usage(argv[0]);
  113. exit(1);
  114. } /* switch for case of '-' or '/' */
  115. break;
  116. /*
  117. * This isn't a flag, see type of filename
  118. */
  119. default:
  120. /* let's look to see what kind of file it is */
  121. wType = wCurFileType;
  122. sz = FindExt(argv[i]);
  123. if (sz) { // has an extension, figure it out
  124. if (!strcmpi(sz, "C"))
  125. wType = SRC_C;
  126. else if (!strcmpi(sz, "ASM"))
  127. wType = SRC_MASM;
  128. }
  129. /* Add this file as chosen file type */
  130. AddFileToProcess(argv[i], wType);
  131. break;
  132. } /* switch */
  133. i++;
  134. } /*while */
  135. }
  136. /*
  137. * @doc EXTRACT
  138. *
  139. * @func void | Usage | Prints usage information to 'stderr'.
  140. */
  141. void Usage(PSTR progName)
  142. {
  143. fprintf(stderr, "usage: %s [-o outputFile] [-n] [files]\n\n", progName);
  144. fprintf(stderr, "[-a] \t\t\tMASM source file.\n");
  145. fprintf(stderr, "[-n] \t\t\tNo output, error check only.\n");
  146. fprintf(stderr, "[-o outputFile] \tPlaces output in file outputFile,\n");
  147. fprintf(stderr, "\t\t\tOr uses standard output if not specified.\n");
  148. fprintf(stderr, "[files] \t\tList of files to be processed.");
  149. fprintf(stderr, "If none specified,\n\t\t\tuses standard input.\n");
  150. fprintf(stderr, "\nexample: %s file.c >file.doc\n", progName);
  151. }
  152. /*
  153. * @doc EXTRACT
  154. * @api void | AddFileToProcess | Adds filename <p pch> to the list of
  155. * files to be processed.
  156. *
  157. * @parm PSTR | pch | Identifies the filename to be processed.
  158. *
  159. * @parm int | type | Source code type of file.
  160. *
  161. * @comm Adds <p pch> to the linked list of files to be processed that
  162. * is pointed to by global FilesToProcess. This function is used by the
  163. * argument processing module, and should not be called by other
  164. * sections of the program.
  165. *
  166. */
  167. void AddFileToProcess(PSTR pch, int type)
  168. {
  169. FileEntry *cur_file;
  170. #ifdef FILEDEBUG
  171. dprintf("Adding input file to list: %s", pch);
  172. dprintf(" Type: ");
  173. switch (type) {
  174. case SRC_C:
  175. dprintf("C");
  176. break;
  177. case SRC_MASM:
  178. dprintf("MASM");
  179. break;
  180. case SRC_UNKNOWN:
  181. default:
  182. dprintf("UNKNOWN");
  183. break;
  184. }
  185. dprintf("\n");
  186. #endif
  187. cur_file = AllocNewFile(&FilesToProcess);
  188. cur_file->filename = StringAlloc(pch);
  189. cur_file->type = type;
  190. }
  191. /*
  192. * @doc EXTRACT
  193. * @api FileEntry * | AllocNewFile | Allocates a new file entry
  194. * structure, and appends this structure a the linked list of filenames
  195. * to process.
  196. *
  197. * @parm FileEntry ** | start_file | The address of the pointer to the
  198. * head of the linked list. If NULL, this pointer will be set to the
  199. * newly allocated FileEntry structure.
  200. *
  201. * @rdesc Returns a pointer to the newly allocated FileEntry structure.
  202. * This structure will have been placed into the linked list pointed to
  203. * by <p *start_file>.
  204. *
  205. * @comm This function used by <f ParseArgs> and should not be called
  206. * by other portions of the program.
  207. *
  208. */
  209. FileEntry *AllocNewFile(FileEntry **start_file)
  210. {
  211. FileEntry * cur_file;
  212. FileEntry * tmp_file;
  213. cur_file=(FileEntry *) NearMalloc(sizeof(FileEntry), True);
  214. if(!*start_file) {
  215. *start_file=cur_file;
  216. }
  217. else {
  218. tmp_file = *start_file;
  219. while(tmp_file->next)
  220. tmp_file = tmp_file->next;
  221. tmp_file->next = cur_file;
  222. }
  223. return(cur_file);
  224. }
  225. /*
  226. * @doc EXTRACT
  227. * @api PSTR | FileExt | Returns a pointer to the head of the file
  228. * extension of pathname <p szPath>.
  229. *
  230. * @parm PSTR | szPath | Path string.
  231. *
  232. * @rdesc Returns pointer to head of file extension within <p szPath>,
  233. * or NULL if no extension exists on szPath.
  234. *
  235. */
  236. #define SLASH(c) ((c) == '\\' || (c) == '/')
  237. PSTR FindExt(PSTR szPath)
  238. {
  239. PSTR sz;
  240. for (sz=szPath; *sz && *sz!=' '; sz++)
  241. ;
  242. for (; sz>=szPath && !SLASH(*sz) && *sz!='.'; sz--)
  243. ;
  244. return *sz=='.' ? ++sz : NULL;
  245. }