CMDLIB.C 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) 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.
  10. See the 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
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // cmdlib.c
  16. #ifndef __NeXT__
  17. #include <process.h>
  18. #include <io.h>
  19. #include <dos.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <conio.h>
  28. #include "cmdlib.h"
  29. #define PATHSEPERATOR '\\'
  30. #endif
  31. #ifdef __NeXT__
  32. #define O_BINARY 0
  33. #define PATHSEPERATOR '/'
  34. #include <libc.h>
  35. #include <errno.h>
  36. #include <ctype.h>
  37. #include "cmdlib.h"
  38. int filelength (int handle)
  39. {
  40. struct stat fileinfo;
  41. if (fstat (handle,&fileinfo) == -1)
  42. Error ("Error fstating");
  43. return fileinfo.st_size;
  44. }
  45. #endif
  46. /* globals for command line args */
  47. #ifdef __NeXT__
  48. extern int NXargc;
  49. extern char **NXargv;
  50. #define myargc NXargc
  51. #define myargv NXargv
  52. #endif
  53. #ifdef __WATCOMC__
  54. extern int _argc;
  55. extern char **_argv;
  56. #define myargc _argc
  57. #define myargv _argv
  58. #endif
  59. /*
  60. =============================================================================
  61. MISC FUNCTIONS
  62. =============================================================================
  63. */
  64. /*
  65. =================
  66. =
  67. = Error
  68. =
  69. = For abnormal program terminations
  70. =
  71. =================
  72. */
  73. void Error (char *error, ...)
  74. {
  75. va_list argptr;
  76. va_start (argptr,error);
  77. vprintf (error,argptr);
  78. va_end (argptr);
  79. printf ("\n");
  80. exit (1);
  81. }
  82. /*
  83. =================
  84. =
  85. = CheckParm
  86. =
  87. = Checks for the given parameter in the program's command line arguments
  88. =
  89. = Returns the argument number (1 to argc-1) or 0 if not present
  90. =
  91. =================
  92. */
  93. int CheckParm (char *check)
  94. {
  95. int i;
  96. char *parm;
  97. for (i = 1;i<_argc;i++)
  98. {
  99. parm = _argv[i];
  100. if ( !isalpha(*parm) ) // skip - / \ etc.. in front of parm
  101. if (!*++parm)
  102. continue; // parm was only one char
  103. if ( !stricmp(check,parm) )
  104. return i;
  105. }
  106. return 0;
  107. }
  108. int SafeOpenWrite (char *filename)
  109. {
  110. int handle;
  111. handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_TRUNC
  112. , S_IREAD | S_IWRITE);
  113. if (handle == -1)
  114. Error ("Error opening %s: %s",filename,strerror(errno));
  115. return handle;
  116. }
  117. int SafeOpenRead (char *filename)
  118. {
  119. int handle;
  120. handle = open(filename,O_RDONLY | O_BINARY);
  121. if (handle == -1)
  122. Error ("Error opening %s: %s",filename,strerror(errno));
  123. return handle;
  124. }
  125. void SafeRead (int handle, void *buffer, long count)
  126. {
  127. unsigned iocount;
  128. while (count)
  129. {
  130. iocount = count > 0x8000 ? 0x8000 : count;
  131. if (read (handle,buffer,iocount) != iocount)
  132. Error ("File read failure");
  133. buffer = (void *)( (byte *)buffer + iocount );
  134. count -= iocount;
  135. }
  136. }
  137. void SafeWrite (int handle, void *buffer, long count)
  138. {
  139. unsigned iocount;
  140. while (count)
  141. {
  142. iocount = count > 0x8000 ? 0x8000 : count;
  143. if (write (handle,buffer,iocount) != iocount)
  144. Error ("File write failure");
  145. buffer = (void *)( (byte *)buffer + iocount );
  146. count -= iocount;
  147. }
  148. }
  149. void *SafeMalloc (long size)
  150. {
  151. void *ptr;
  152. ptr = malloc (size);
  153. if (!ptr)
  154. Error ("Malloc failure for %lu bytes",size);
  155. return ptr;
  156. }
  157. void SafeFree (void * ptr)
  158. {
  159. free (ptr);
  160. }
  161. /*
  162. ==============
  163. =
  164. = LoadFile
  165. =
  166. ==============
  167. */
  168. long LoadFile (char *filename, void **bufferptr)
  169. {
  170. int handle;
  171. long length;
  172. void *buffer;
  173. handle = SafeOpenRead (filename);
  174. length = filelength (handle);
  175. buffer = SafeMalloc (length);
  176. SafeRead (handle, buffer, length);
  177. close (handle);
  178. *bufferptr = buffer;
  179. return length;
  180. }
  181. /*
  182. ==============
  183. =
  184. = SaveFile
  185. =
  186. ==============
  187. */
  188. void SaveFile (char *filename, void *buffer, long count)
  189. {
  190. int handle;
  191. handle = SafeOpenWrite (filename);
  192. SafeWrite (handle, buffer, count);
  193. close (handle);
  194. }
  195. void DefaultExtension (char *path, char *extension)
  196. {
  197. char *src;
  198. //
  199. // if path doesn't have a .EXT, append extension
  200. // (extension should include the .)
  201. //
  202. src = path + strlen(path) - 1;
  203. while (*src != '\\' && src != path)
  204. {
  205. if (*src == '.')
  206. return; // it has an extension
  207. src--;
  208. }
  209. strcat (path, extension);
  210. }
  211. void DefaultPath (char *path, char *basepath)
  212. {
  213. char temp[128];
  214. if (path[0] == '\\')
  215. return; // absolute path location
  216. strcpy (temp,path);
  217. strcpy (path,basepath);
  218. strcat (path,temp);
  219. }
  220. void StripFilename (char *path)
  221. {
  222. int length;
  223. length = strlen(path)-1;
  224. while (length > 0 && path[length] != PATHSEPERATOR)
  225. length--;
  226. path[length] = 0;
  227. }
  228. void ExtractFileBase (char *path, char *dest)
  229. {
  230. char *src;
  231. int length;
  232. src = path + strlen(path) - 1;
  233. //
  234. // back up until a \ or the start
  235. //
  236. while (src != path && *(src-1) != '\\')
  237. src--;
  238. //
  239. // copy up to eight characters
  240. //
  241. memset (dest,0,8);
  242. length = 0;
  243. while (*src && *src != '.')
  244. {
  245. if (++length == 9)
  246. Error ("Filename base of %s >8 chars",path);
  247. *dest++ = toupper(*src++);
  248. }
  249. }
  250. /*
  251. ==============
  252. =
  253. = ParseNum / ParseHex
  254. =
  255. ==============
  256. */
  257. long ParseHex (char *hex)
  258. {
  259. char *str;
  260. long num;
  261. num = 0;
  262. str = hex;
  263. while (*str)
  264. {
  265. num <<= 4;
  266. if (*str >= '0' && *str <= '9')
  267. num += *str-'0';
  268. else if (*str >= 'a' && *str <= 'f')
  269. num += 10 + *str-'a';
  270. else if (*str >= 'A' && *str <= 'F')
  271. num += 10 + *str-'A';
  272. else
  273. Error ("Bad hex number: %s",hex);
  274. str++;
  275. }
  276. return num;
  277. }
  278. long ParseNum (char *str)
  279. {
  280. if (str[0] == '$')
  281. return ParseHex (str+1);
  282. if (str[0] == '0' && str[1] == 'x')
  283. return ParseHex (str+2);
  284. return atol (str);
  285. }
  286. /*
  287. ============================================================================
  288. BYTE ORDER FUNCTIONS
  289. ============================================================================
  290. */
  291. short BigShort (short l)
  292. {
  293. byte b1,b2;
  294. b1 = l&255;
  295. b2 = (l>>8)&255;
  296. return (b1<<8) + b2;
  297. }
  298. short LittleShort (short l)
  299. {
  300. return l;
  301. }
  302. long BigLong (long l)
  303. {
  304. byte b1,b2,b3,b4;
  305. b1 = l&255;
  306. b2 = (l>>8)&255;
  307. b3 = (l>>16)&255;
  308. b4 = (l>>24)&255;
  309. return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  310. }
  311. long LittleLong (long l)
  312. {
  313. return l;
  314. }
  315. /*
  316. ============================================================================
  317. BASIC GRAPHICS
  318. ============================================================================
  319. */
  320. #define PEL_WRITE_ADR 0x3c8
  321. #define PEL_READ_ADR 0x3c7
  322. #define PEL_DATA 0x3c9
  323. #define PEL_MASK 0x3c6
  324. /*
  325. ==============
  326. =
  327. = GetPalette
  328. =
  329. = Return an 8 bit / color palette
  330. =
  331. ==============
  332. */
  333. void GetPalette (byte *pal)
  334. {
  335. #ifndef __NeXT__
  336. int i;
  337. outp (PEL_READ_ADR,0);
  338. for (i=0 ; i<768 ; i++)
  339. pal[i] = inp (PEL_DATA)<<2;
  340. #endif
  341. }
  342. /*
  343. ==============
  344. =
  345. = SetPalette
  346. =
  347. = Sets an 8 bit / color palette
  348. =
  349. ==============
  350. */
  351. void SetPalette (byte *pal)
  352. {
  353. #ifndef __NeXT__
  354. int i;
  355. outp (PEL_WRITE_ADR,0);
  356. for (i=0 ; i<768 ; i++)
  357. outp (PEL_DATA, pal[i]>>2);
  358. #endif
  359. }