home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / util / mbq319 / cmdlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-28  |  9.2 KB  |  640 lines

  1. // cmdlib.c
  2.  
  3. #define __LITTLE_ENDIAN__
  4.  
  5. #include "cmdlib.h"
  6. #include "sys\stat.h"
  7. #include <fcntl.h>
  8.  
  9. #define PATHSEPERATOR   '/'
  10.  
  11. char    com_token[1024];
  12. int        com_eof;
  13.  
  14. /*
  15. ==============
  16. COM_Parse
  17.  
  18. Parse a token out of a string
  19. ==============
  20. */
  21. char *COM_Parse (char *data)
  22. {
  23.     int        c;
  24.     int        len;
  25.     
  26.     len = 0;
  27.     com_token[0] = 0;
  28.     
  29.     if (!data)
  30.         return NULL;
  31.         
  32. // skip whitespace
  33. skipwhite:
  34.     while ( (c = *data) <= ' ')
  35.     {
  36.         if (c == 0)
  37.         {
  38.             com_eof = true;
  39.             return NULL;            // end of file;
  40.         }
  41.         data++;
  42.     }
  43.     
  44. // skip // comments
  45.     if (c=='/' && data[1] == '/')
  46.     {
  47.         while (*data && *data != '\n')
  48.             data++;
  49.         goto skipwhite;
  50.     }
  51.     
  52.  
  53. // handle quoted strings specially
  54.     if (c == '\"')
  55.     {
  56.         data++;
  57.         do
  58.         {
  59.             c = *data++;
  60.             if (c=='\"')
  61.             {
  62.                 com_token[len] = 0;
  63.                 return data;
  64.             }
  65.             com_token[len] = c;
  66.             len++;
  67.         } while (1);
  68.     }
  69.  
  70. // parse single characters
  71.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  72.     {
  73.         com_token[len] = c;
  74.         len++;
  75.         com_token[len] = 0;
  76.         return data+1;
  77.     }
  78.  
  79. // parse a regular word
  80.     do
  81.     {
  82.         com_token[len] = c;
  83.         data++;
  84.         len++;
  85.         c = *data;
  86.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  87.             break;
  88.     } while (c>32);
  89.     
  90.     com_token[len] = 0;
  91.     return data;
  92. }
  93.  
  94.  
  95. /*
  96. ================
  97. =
  98. = filelength
  99. =
  100. ================
  101. */
  102.  
  103. int filelength (int handle)
  104. {
  105.     struct stat    fileinfo;
  106.     
  107.     if (fstat (handle,&fileinfo) == -1)
  108.     {
  109.         fprintf (stderr,"Error fstating");
  110.         exit (1);
  111.     }
  112.  
  113.     return fileinfo.st_size;
  114. }
  115.  
  116. int tell (int handle)
  117. {
  118.     return lseek (handle, 0, 0);
  119. }
  120.  
  121. char *strupr (char *start)
  122. {
  123.     char    *in;
  124.     in = start;
  125.     while (*in)
  126.     {
  127.         *in = toupper(*in);
  128.         in++;
  129.     }
  130.     return start;
  131. }
  132.  
  133. char *strlower (char *start)
  134. {
  135.     char    *in;
  136.     in = start;
  137.     while (*in)
  138.     {
  139.         *in = tolower(*in);
  140.         in++;
  141.     }
  142.     return start;
  143. }
  144.  
  145. #ifdef NeXT
  146. char *getcwd (char *path, int length)
  147. {
  148.     return getwd(path);
  149. }
  150. #endif
  151.  
  152. /* globals for command line args */
  153. extern int NXArgc;
  154. extern char **NXArgv;
  155. #define myargc    NXArgc
  156. #define myargv    NXArgv
  157.  
  158. #ifndef NeXT
  159. int NXArgc;
  160. char **NXArgv;
  161. #endif
  162.  
  163.  
  164. /*
  165. =============================================================================
  166.  
  167.                         MISC FUNCTIONS
  168.  
  169. =============================================================================
  170. */
  171.  
  172. /*
  173. =================
  174. =
  175. = Error
  176. =
  177. = For abnormal program terminations
  178. =
  179. =================
  180. */
  181.  
  182. void Error (char *error, ...)
  183. {
  184.     va_list argptr;
  185.  
  186.     printf ("\n\nError: ");
  187.     va_start (argptr,error);
  188.     vprintf (error,argptr);
  189.     va_end (argptr);
  190.     printf ("\n");
  191.     exit (1);
  192. }
  193.  
  194.  
  195. /*
  196. =================
  197. =
  198. = CheckParm
  199. =
  200. = Checks for the given parameter in the program's command line arguments
  201. =
  202. = Returns the argument number (1 to argc-1) or 0 if not present
  203. =
  204. =================
  205. */
  206.  
  207. int CheckParm (char *check)
  208. {
  209.     int             i;
  210.  
  211.     for (i = 1;i<myargc;i++)
  212.     {
  213.         if ( !stricmp(check, myargv[i]) )
  214.             return i;
  215.     }
  216.  
  217.     return 0;
  218. }
  219.  
  220.  
  221.  
  222.  
  223. int SafeOpenWrite (char *filename)
  224. {
  225.     int     handle;
  226.  
  227.     umask (0);
  228.     
  229.     handle = open(filename,O_RDWR | O_CREAT | O_TRUNC
  230.     , 0666);
  231.  
  232.     if (handle == -1)
  233.         Error ("Error opening %s: %s",filename,strerror(errno));
  234.  
  235.     return handle;
  236. }
  237.  
  238. int SafeOpenRead (char *filename)
  239. {
  240.     int     handle;
  241.  
  242.     handle = open(filename,O_RDONLY);
  243.  
  244.     if (handle == -1)
  245.         Error ("Error opening %s: %s",filename,strerror(errno));
  246.  
  247.     return handle;
  248. }
  249.  
  250.  
  251. void SafeRead (int handle, void *buffer, int count)
  252. {
  253.     int        iocount;
  254.  
  255.     iocount = read (handle,buffer,count);
  256.     if (iocount != count)
  257.         Error ("File read failure");
  258. }
  259.  
  260.  
  261. void SafeWrite (int handle, void *buffer, int count)
  262. {
  263.     int        iocount;
  264.  
  265.     iocount = write (handle,buffer,count);
  266.     if (iocount != count)
  267.         Error ("File write failure");
  268. }
  269.  
  270.  
  271. void *SafeMalloc (int size)
  272. {
  273.     void *ptr;
  274.  
  275.     ptr = malloc (size);
  276.  
  277.     if (!ptr)
  278.         Error ("Malloc failure for %lu bytes",size);
  279.  
  280.     return ptr;
  281. }
  282.  
  283.  
  284. /*
  285. ==============
  286. =
  287. = LoadFile
  288. =
  289. ==============
  290. */
  291.  
  292. int    LoadFile (char *filename, void **bufferptr)
  293. {
  294.     int             handle;
  295.     int    length;
  296.     void    *buffer;
  297.  
  298.     handle = SafeOpenRead (filename);
  299.     length = filelength (handle);
  300.     buffer = SafeMalloc (length+1);
  301.     SafeRead (handle, buffer, length);
  302.     close (handle);
  303.     ((byte *)buffer)[length] = 0;
  304.     
  305.     *bufferptr = buffer;
  306.     return length;
  307. }
  308.  
  309.  
  310. /*
  311. ==============
  312. =
  313. = SaveFile
  314. =
  315. ==============
  316. */
  317.  
  318. void    SaveFile (char *filename, void *buffer, int count)
  319. {
  320.     int             handle;
  321.  
  322.     handle = SafeOpenWrite (filename);
  323.     SafeWrite (handle, buffer, count);
  324.     close (handle);
  325. }
  326.  
  327.  
  328.  
  329. void DefaultExtension (char *path, char *extension)
  330. {
  331.     char    *src;
  332. //
  333. // if path doesn't have a .EXT, append extension
  334. // (extension should include the .)
  335. //
  336.     src = path + strlen(path) - 1;
  337.  
  338.     while (*src != PATHSEPERATOR && src != path)
  339.     {
  340.         if (*src == '.')
  341.             return;                 // it has an extension
  342.         src--;
  343.     }
  344.  
  345.     strcat (path, extension);
  346. }
  347.  
  348.  
  349. void DefaultPath (char *path, char *basepath)
  350. {
  351.     char    temp[128];
  352.  
  353.     if (path[0] == PATHSEPERATOR)
  354.         return;                   // absolute path location
  355.     strcpy (temp,path);
  356.     strcpy (path,basepath);
  357.     strcat (path,temp);
  358. }
  359.  
  360.  
  361. void    StripFilename (char *path)
  362. {
  363.     int             length;
  364.  
  365.     length = strlen(path)-1;
  366.     while (length > 0 && path[length] != PATHSEPERATOR)
  367.         length--;
  368.     path[length] = 0;
  369. }
  370.  
  371. void    StripExtension (char *path)
  372. {
  373.     int             length;
  374.  
  375. // FIXME: this fucks up on a file like ../gfz/aztec
  376.     length = strlen(path)-1;
  377.     while (length > 0 && path[length] != '.')
  378.         length--;
  379.     if (length)
  380.         path[length] = 0;
  381. }
  382.  
  383.  
  384. /*
  385. ====================
  386. =
  387. = Extract file parts
  388. =
  389. ====================
  390. */
  391.  
  392. void ExtractFilePath (char *path, char *dest)
  393. {
  394.     char    *src;
  395.  
  396.     src = path + strlen(path) - 1;
  397.  
  398. //
  399. // back up until a \ or the start
  400. //
  401.     while (src != path && *(src-1) != PATHSEPERATOR)
  402.         src--;
  403.  
  404.     memcpy (dest, path, src-path);
  405.     dest[src-path] = 0;
  406. }
  407.  
  408. void ExtractFileBase (char *path, char *dest)
  409. {
  410.     char    *src;
  411.  
  412.     src = path + strlen(path) - 1;
  413.  
  414. //
  415. // back up until a \ or the start
  416. //
  417.     while (src != path && *(src-1) != PATHSEPERATOR)
  418.         src--;
  419.  
  420.     while (*src && *src != '.')
  421.     {
  422.         *dest++ = *src++;
  423.     }
  424.     *dest = 0;
  425. }
  426.  
  427. void ExtractFileExtension (char *path, char *dest)
  428. {
  429.     char    *src;
  430.  
  431.     src = path + strlen(path) - 1;
  432.  
  433. //
  434. // back up until a . or the start
  435. //
  436.     while (src != path && *(src-1) != '.')
  437.         src--;
  438.     if (src == path)
  439.     {
  440.         *dest = 0;    // no extension
  441.         return;
  442.     }
  443.  
  444.     strcpy (dest,src);
  445. }
  446.  
  447.  
  448. /*
  449. ==============
  450. =
  451. = ParseNum / ParseHex
  452. =
  453. ==============
  454. */
  455.  
  456. int ParseHex (char *hex)
  457. {
  458.     char    *str;
  459.     int    num;
  460.  
  461.     num = 0;
  462.     str = hex;
  463.  
  464.     while (*str)
  465.     {
  466.         num <<= 4;
  467.         if (*str >= '0' && *str <= '9')
  468.             num += *str-'0';
  469.         else if (*str >= 'a' && *str <= 'f')
  470.             num += 10 + *str-'a';
  471.         else if (*str >= 'A' && *str <= 'F')
  472.             num += 10 + *str-'A';
  473.         else
  474.             Error ("Bad hex number: %s",hex);
  475.         str++;
  476.     }
  477.  
  478.     return num;
  479. }
  480.  
  481.  
  482. int ParseNum (char *str)
  483. {
  484.     if (str[0] == '$')
  485.         return ParseHex (str+1);
  486.     if (str[0] == '0' && str[1] == 'x')
  487.         return ParseHex (str+2);
  488.     return atol (str);
  489. }
  490.  
  491.  
  492. int GetKey (void)
  493. {
  494.     return getchar ();
  495. }
  496.  
  497.  
  498. /*
  499. ============================================================================
  500.  
  501.                     BYTE ORDER FUNCTIONS
  502.  
  503. ============================================================================
  504. */
  505.  
  506. #ifdef __alpha
  507. #define __LITTLE_ENDIAN__
  508. #endif
  509.  
  510.  
  511. #ifdef __BIG_ENDIAN__
  512.  
  513. short   LittleShort (short l)
  514. {
  515.     byte    b1,b2;
  516.  
  517.     b1 = l&255;
  518.     b2 = (l>>8)&255;
  519.  
  520.     return (b1<<8) + b2;
  521. }
  522.  
  523. short   BigShort (short l)
  524. {
  525.     return l;
  526. }
  527.  
  528.  
  529. int    LittleLong (int l)
  530. {
  531.     byte    b1,b2,b3,b4;
  532.  
  533.     b1 = l&255;
  534.     b2 = (l>>8)&255;
  535.     b3 = (l>>16)&255;
  536.     b4 = (l>>24)&255;
  537.  
  538.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  539. }
  540.  
  541. int    BigLong (int l)
  542. {
  543.     printf("HEY!!  THIS SHOULDN'T BE CALLED!!!\n");
  544.     return l;
  545. }
  546.  
  547.  
  548. float    LittleFloat (float l)
  549. {
  550.     union {byte b[4]; float f;} in, out;
  551.     
  552.     in.f = l;
  553.     out.b[0] = in.b[3];
  554.     out.b[1] = in.b[2];
  555.     out.b[2] = in.b[1];
  556.     out.b[3] = in.b[0];
  557.     
  558.     return out.f;
  559. }
  560.  
  561. float    BigFloat (float l)
  562. {
  563.     return l;
  564. }
  565.  
  566. #endif
  567.  
  568. #ifdef __LITTLE_ENDIAN__
  569.  
  570. short   BigShort (short l)
  571. {
  572.     byte    b1,b2;
  573.  
  574.     b1 = l&255;
  575.     b2 = (l>>8)&255;
  576.  
  577.     return (b1<<8) + b2;
  578. }
  579.  
  580. short   LittleShort (short l)
  581. {
  582.     return l;
  583. }
  584.  
  585. unsigned long BigLong(unsigned long val) {return ((val & 0x000000FF) << 24) | ((val & 0xFF00) << 8) | ((val & 0x00FF0000) >> 8) | (val >> 24);}
  586.  
  587. /* long BigLong(long a) {
  588.     long f, g, r;
  589.  
  590.     printf("---> BigLong() was called with a value of: %lu\n", a);
  591.     f = a >> 16;
  592.     g = a & 0x0000FFFF;
  593.     r = (g << 16) + f;
  594.     printf("---> BigLong() returned: %lu\n", r);
  595.     return r;
  596. } */
  597.  
  598. /*
  599. long    BigLong (int l)
  600. {
  601.     byte    b1,b2,b3,b4;
  602.     long duh;
  603.  
  604.     b1 = l&255;
  605.     b2 = (l>>8)&255;
  606.     b3 = (l>>16)&255;
  607.     b4 = (l>>24)&255;
  608.     duh = (long) (((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4);
  609.     return duh;
  610. }
  611. */
  612.  
  613. int    LittleLong (int l)
  614. {
  615.     return l;
  616. }
  617.  
  618. float    BigFloat (float l)
  619. {
  620.     union {byte b[4]; float f;} in, out;
  621.     
  622.     in.f = l;
  623.     out.b[0] = in.b[3];
  624.     out.b[1] = in.b[2];
  625.     out.b[2] = in.b[1];
  626.     out.b[3] = in.b[0];
  627.     
  628.     return out.f;
  629. }
  630.  
  631. float    LittleFloat (float l)
  632. {
  633.     return l;
  634. }
  635.  
  636.  
  637.  
  638. #endif
  639.  
  640.