home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / file.lzh / help.c < prev    next >
C/C++ Source or Header  |  1995-04-27  |  12KB  |  334 lines

  1. /*************************************************************************
  2.  * help.c                                                                *
  3.  *************************************************************************
  4.  *  Changes Made                                            Date     Who *
  5.  *  ------------                                            ----     --- *
  6.  *  Created                                                 94/01/06 sam *
  7.  *  -help finished                                          94/01/21 sam *
  8.  *  edition number moved to file.c                          95/04/04 sam *
  9.  ************************************************************************/
  10.  
  11. #include <file.h>
  12.  
  13.  
  14. void main(argc,argv)
  15.   int argc;
  16.   char **argv;
  17. {
  18.     register char *p;
  19.     int arg_count = 0;
  20.     int i;
  21.  
  22. #if defined(_OSK) || defined(_OS9000)
  23.     progname = _prgname();
  24. #else
  25.     progname = strrchr(argv[0], '/');
  26.     if(progname == NULL)
  27.       progname = argv[0];
  28.     else
  29.       progname++;
  30. #endif
  31.  
  32.     if(argc == 1){
  33.         printUsage();
  34.         exit(0);
  35.     }
  36.  
  37.     magic_file = getenv("MAGIC_FILE");
  38.     if(magic_file == NULL)
  39.         magic_file = MAGIC_DEFAULT;
  40.  
  41.  
  42.     /* parse the options */
  43.     for(i=1;i<argc;i++){
  44.         if (*(p=argv[i]) == '-') {
  45.             optparse(p);
  46.             argv[i] = NULL;
  47.         }else        /* anything here is an arguement */
  48.             arg_count++;
  49.     }
  50.  
  51.     if((arg_count == 0) && (use_stdin == FALSE) && (file_path == NULL)){
  52.         printUsage();
  53.         exit(0);
  54.     }
  55.  
  56.     /* OK, time to read the magic file now */
  57.     if(magicList == NULL)
  58.       magicList = readMagic(magic_file);
  59.     
  60.     if(use_stdin == TRUE)           /* get file names from stdin */
  61.       getFileNames(NULL);
  62.     else if(file_path != NULL)      /* get file names from file_path */
  63.       getFileNames(file_path);
  64.     else                            /* get file names from command line */
  65.     for(i=1;i<argc;i++)
  66.         if(argv[i] != NULL)
  67.           readFile(argv[i]);
  68.  
  69.     exit(0);
  70. }
  71.  
  72. void optparse(ptr)
  73.   register char *ptr;
  74. {
  75.     while (*++ptr) {
  76.         switch (tolower(*ptr)) {
  77.             case 'f': if(*++ptr == '=')
  78.                           ptr++;
  79.                       file_path = ptr;
  80.                       return;
  81.             case 'h': if(strucmp(ptr, "help") == 0){
  82.                           printHelp();
  83.                           exit(0);
  84.                       }else{
  85. #if defined(_OSK) || defined(_OS9000)
  86.                           printHelp();
  87. #else
  88.                           printUsage();
  89. #endif
  90.                           exit(0);
  91.                       }
  92.             case 'm': if(*++ptr == '=')
  93.                           ptr++;
  94.                       magic_file = ptr;
  95.                       return;
  96.             case 'r': recursive = TRUE;
  97.                       exec_relative = FALSE;
  98.                       break;
  99. #if defined(_OSK) || defined(_OS9000)
  100.             case 'x': exec_relative = TRUE;
  101.                       recursive = FALSE;
  102.                       break;
  103. #endif
  104.             case 'z': if(*++ptr == '=')
  105.                           ptr++;
  106.                       if(*ptr != '\0')
  107.                           file_path = ptr;
  108.                       else
  109.                           use_stdin = TRUE;
  110.                       return;
  111.             case '?': printUsage();
  112.                       exit(0);
  113.             default:  printUsage();
  114.                       exit(_errmsg(1,"unknown option '%c'\n", *ptr));
  115.         }
  116.     }
  117. }
  118.  
  119. /* Help message */
  120. static char *cmds[] = {
  121.     "Function: identify file types\n",
  122.     "Options:\n",
  123.     "   -f[=]path  read files from path\n",
  124. #if !(defined(_OSK) || defined(_OS9000))
  125.     "   -h         displaly usage\n",
  126.     "   -help      display verbose help information\n",
  127. #else
  128.     "   -h[elp]    display verbose help information\n",
  129. #endif
  130.     "   -m[=]file  use file for magic file\n",
  131.     "   -r         recursively descend directories\n",
  132. #if defined(_OSK) || defined(_OS9000)
  133.     "   -x         execution directory relative\n",
  134. #endif
  135.     "   -z         read files from stdin\n",
  136.     "   -z[=]path  read files from path (same as -f)\n",
  137. #if defined(_OSK) || defined(_OS9000)
  138.     "\nNote: the -r and -x options are mutually exclusive, and the last\n",
  139.     "      one seen on the command line will turn the other off.\n",
  140.     "      Also, the options -f, -help, -m, and -z (since the may be\n",
  141.     "      longer than a single character, must be the final option in\n",
  142.     "      a group if grouped with other options. (ie -rz is OK, -zr is\n",
  143.     "      not)\n",
  144. #endif
  145.     "\nThe environment variable MAGIC_FILE (if set) changes\n",
  146.     "the default magic file (from ",
  147. #if defined(_OSK) || defined(_OS9000)
  148.     "/dd/SYS/magic",
  149. #else
  150.     "/etc/magic",
  151. #endif
  152.     ").\n",
  153.     "\nThis program is Copyright (c) 1995 by Scott McGee but may be\n",
  154.     "distributed at no cost in its original unaltered form. This program\n",
  155.     "comes with ABSOLUTELY NO WARRANTY!\n",
  156.     0
  157. };
  158.  
  159.  
  160.  
  161.  
  162. /* print the help message */
  163. void printUsage(){
  164.     register char **p=cmds;
  165.  
  166.     fprintf(stderr, "Syntax:   %s [<opts>] {<file>}\n", progname);
  167.     while (*p) fputs(*p++,stderr);
  168.     fputs("\n", stderr);
  169. }
  170.  
  171.  
  172.  
  173. /* long help message */
  174. static char *help[] = {
  175.     "\nThe ",
  176.     NULL,
  177.     " utility is used to identify filetypes. Syntax is:\n",
  178.     "    ",
  179.     NULL,
  180.     " [<opts>] {<file>}\n\n",
  181.     "Typical usage would be:\n",
  182.     "    ",
  183.     NULL,
  184.     " <path>        to identify the type of <path>\n",
  185.     "    ",
  186.     NULL,
  187.     " <dir>/*       to identify the type of all files in <dir>\n\n",
  188.     NULL,
  189.     " identifies OS-9 files by using knowledge of the internal\n",
  190.     "structure of the file. Other files are identified by matching\n",
  191.     "information in the file with data supplied in a 'magic' file.\n",
  192.     "The default location of the magic file is ",
  193. #if defined(_OSK) || defined(_OS9000)
  194.     "/dd/SYS/magic",
  195. #else
  196.     "/etc/magic",
  197. #endif
  198.     ".\n\n",
  199.     "The magic file specifies the following information that is\n",
  200.     "used in identifying file types.\n\n",
  201.     "OFFSET  This is the offset into the file where the matching\n",
  202.     "        information is found.\n\n",
  203.     "TYPE    This is the data type of the value to be matched. It\n",
  204.     "        may be one of the following types:\n",
  205.     "    byte       1 byte (char)\n",
  206.     "    short      2 bytes (short int) (byte order not specified)\n",
  207.     "    leshort    2 bytes (short int) (Little Endian byte order)\n",
  208.     "    beshort    2 bytes (short int) (Big Endian byte order)\n",
  209.     "    long       4 bytes (long int) (byte order not specified)\n",
  210.     "    lelong     4 bytes (long int) (Little Endian byte order)\n",
  211.     "    belong     4 bytes (long int) (Big Endian byte order)\n",
  212.     "    string     a string (array of char)\n\n",
  213.     "VALUE    This is the value that is tested for a match at the\n",
  214.     "         given offset. The value in the file is cast to the\n",
  215.     "         specified type before comparison. String types are\n",
  216.     "         the same length as the string in the magic file and\n",
  217.     "         need not be null terminated. The usual C style\n",
  218.     "         escapes are allowed. A space as the first character\n",
  219.     "         must be escaped, but a space elsewhere in the string\n",
  220.     "         may be typed explicitly. All tab characters must be\n",
  221.     "         escaped.\n\n",
  222.     "MESSAGE  This is the message that is printed out when a match\n",
  223.     "         is found. This string may contain any characters\n",
  224.     "         allowed in a C printf() style control string. It\n",
  225.     "         may also have one format specifier which will refer\n",
  226.     "         to the matched value if the TYPE is NOT string.\n\n",
  227.     "Each line in the magic file is checked until a match is found,\n",
  228.     "or the file is exhausted. An optional '>' character may precede\n",
  229.     "the offset entry indicating a continuation line. These lines\n",
  230.     "are ignored unless the preceding non-continuation line is\n",
  231.     "matched. In this case, each continuation line is checked, and\n",
  232.     "the message printed if it is a match. This continues until the\n",
  233.     "next non-continuation line is encountered.\n\n",
  234.     "A mask value may be ANDed with the value read from the file\n",
  235.     "at the given offset before any match is attempted. This mask\n",
  236.     "may be specified by appending an '&' character followed by the\n",
  237.     "mask value to the end of the value entry in the magic file.\n\n",
  238.     "The default match operation is 'equal' but any of the following\n",
  239.     "match comparisons may be specified by prepending the operator\n",
  240.     "character to the beginning of the value.\n",
  241.     "    Character   Operation\n",
  242.     "    =           EQUAL\n",
  243.     "    <           LESS THAN\n",
  244.     "    >           GREATER THAN\n",
  245.     "    &           AND (if a bit is set in the value entry, it\n",
  246.     "                     must be set in the file too)\n",
  247.     "    ^           OR  (at least one bit that is set in the value\n",
  248.     "                     entry must also be set in the file)\n",
  249.     "    x           ANY (any value will match)\n",
  250.     "\nA custom magic file may be specified in two ways. The environment\n",
  251.     "variable MAGIC_FILE will override the default (",
  252. #if defined(_OSK) || defined(_OS9000)
  253.     "/dd/SYS/magic",
  254. #else
  255.     "/etc/magic",
  256. #endif
  257.     ")\n",
  258.     "magic file. The -m option will also override the default magic\n",
  259.     "file, and will also override the MAGIC_FILE environment variable.\n",
  260.     "\nOptions:\n",
  261.     "\n   -f[=]path  The specified path is used to read filenames rather\n",
  262.     "              than the command line. \n",
  263. #if !(defined(_OSK) || defined(_OS9000))
  264.     "\n   -h         displaly usage.\n",
  265.     "\n   -help      display (this) verbose help information.\n",
  266. #else
  267.     "\n   -h[elp]    display (this) verbose help information.\n",
  268. #endif
  269.     "\n   -m[=]file  The specified file is read instead of the default\n",
  270.     "              magic file.\n",
  271.     "\n   -r         When this option is specified, and a directory is found,\n",
  272.     "              in addition to printing the fact that it is a directory,\n",
  273.     "              the program will recursively descend the directory. This\n",
  274.     "              allows the program to be used on an entire file tree.\n",
  275. #if defined(_OSK) || defined(_OS9000)
  276.     "              This option is not allowed in conjunction with the -x\n",
  277.     "              option. When both appear on the same command line, the\n",
  278.     "              last one is left active, and the other is disabled.\n",
  279.     "\n   -x         All paths are specified as relative to the execution\n",
  280.     "              directory. This can cause unexpected results if used\n",
  281.     "              with shell wild cards since the shell will expand the\n",
  282.     "              wild cards relative to the current data directory. The\n",
  283.     "              same applies to redirected input (-f or -z). Also, note\n",
  284.     "              the conflict with the -r option above.\n",
  285. #endif
  286.     "\n   -z         Read files from stdin instead of from the command line.\n",
  287.     "\n   -z[=]path  Read files from path (same as -f).\n",
  288.     "\nNote: The options -f, -help, -m, and -z (since they may be longer\n",
  289.     "      than a single character, must be the final option in a group\n",
  290.     "      if grouped with other options. (ie -rz is OK, -zr is not)\n",
  291.     0
  292. };
  293.  
  294.  
  295.  
  296. /* print the long help message */
  297. void printHelp(){
  298.     register char **p = help;
  299.  
  300.     p[1] = p[4] = p[8] = p[11] = p[13] = progname;
  301.  
  302.     while (*p) fputs(*p++,stderr);
  303.     fputs("\n", stderr);
  304. }
  305.  
  306.  
  307.  
  308. /* an error checking malloc */
  309. void *grab(size)
  310.   unsigned int size;
  311. {
  312.     void *ret;
  313.  
  314.     if ((ret = malloc(size)) == NULL)
  315.         exit(_errmsg(errno, "memory allocation error - "));
  316.     return ret;
  317. }
  318.  
  319.  
  320.  
  321. #if !(defined(_OSK) || defined(_OS9000))
  322. int _errmsg(nerr, msg, arg1, arg2, arg3, arg4, arg5, arg6)
  323.   int           nerr;
  324.   char          *msg;
  325.   unsigned int  arg1, arg2, arg3, arg4, arg5, arg6;
  326. {
  327.     extern char *progname;
  328.     
  329.     fprintf(stderr, "%s: ", progname);
  330.     fprintf(stderr, msg, arg1, arg2, arg3, arg4, arg5, arg6);
  331.     return nerr;
  332. }
  333. #endif
  334.