Xtandard stuff...
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

701 satır
19KB

  1. /*
  2. * Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. *
  4. * Xtandard is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  5. * And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
  6. * It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
  7. */
  8. #ifndef XTANDARD_SOURCE
  9. #define XTANDARD_SOURCE
  10. #include "xtandard.h"
  11. char * program_name = NULL;
  12. char * program_mode = NULL;
  13. char * program_license = NULL;
  14. int argument_count = 0;
  15. char * * argument_nick = NULL;
  16. char * * argument_name = NULL;
  17. char * argument_input = NULL;
  18. char * argument_output = NULL;
  19. void (* * argument_function) (void) = NULL;
  20. int file_list_active = 0;
  21. int file_list_count = 0;
  22. int * file_list_mark = NULL;
  23. int * file_list_size = NULL;
  24. char * * file_list_name = NULL;
  25. char * * file_list_data = NULL;
  26. void in (void * data, int size) {
  27. fatal_failure (data == NULL, "in: Failed to read from standard input, data is null pointer.");
  28. fatal_failure (size == 0, "in: Failed to read from standard input, size is zero.");
  29. (void) read (STDIN_FILENO, data, (unsigned long int) size);
  30. }
  31. void out (void * data, int size) {
  32. fatal_failure (data == NULL, "out: Failed to write to standard output, data is null pointer.");
  33. fatal_failure (size == 0, "out: Failed to write to standard output, size is zero.");
  34. (void) write (STDOUT_FILENO, data, (unsigned long int) size);
  35. }
  36. void echo (char * data) {
  37. out (data, string_length (data));
  38. }
  39. void echo_new_line (void) {
  40. out ("\n", 1);
  41. }
  42. void echo_byte (int byte) {
  43. out ("0123456789ABCDEF" + (byte % 256) / 16, 1);
  44. out ("0123456789ABCDEF" + (byte % 256) % 16, 1);
  45. out (" ", 1);
  46. }
  47. void fatal_failure (int condition, char * message) {
  48. if (condition != 0) {
  49. echo ("\033[1;31m[!]\033[0m ");
  50. echo (message);
  51. echo ("\n");
  52. exit (EXIT_FAILURE);
  53. }
  54. }
  55. void * allocate (int size) {
  56. char * data = NULL;
  57. fatal_failure (size == 0, "allocate: Failed to allocate memory, size is zero.");
  58. data = calloc ((unsigned long int) size, sizeof (* data));
  59. fatal_failure (data == NULL, "allocate: Failed to allocate memory, function calloc returned null pointer.");
  60. return ((void *) data);
  61. }
  62. void * reallocate (void * data, int size) {
  63. fatal_failure (size == 0, "reallocate: Failed to reallocate memory, size is zero.");
  64. data = realloc (data, (unsigned long int) size);
  65. fatal_failure (data == NULL, "reallocate: Failed to reallocate memory, function recalloc returned null pointer.");
  66. /* Set new data to 0. */
  67. return (data);
  68. }
  69. void * deallocate (void * data) {
  70. fatal_failure (data == NULL, "deallocate: Failed to deallocate memory, data is null pointer.");
  71. free (data);
  72. return (NULL);
  73. }
  74. void * memorize (int size) { /* I broke this for testing something out... */
  75. static char * buffer = NULL;
  76. char * points = NULL;
  77. static int length = 0;
  78. static int chunks = 0;
  79. static int loling = 1024;
  80. if (size == 0) {
  81. free (buffer);
  82. buffer = NULL;
  83. length = 0;
  84. chunks = 0;
  85. return (NULL);
  86. }
  87. for (; length + size > chunks * loling; ) {
  88. int i;
  89. ++chunks;
  90. buffer = realloc (buffer, (unsigned long int) (chunks * loling));
  91. fatal_failure (buffer == NULL, "memorize: Oh no...");
  92. for (i = (chunks - 1) * loling; i != chunks * loling; ++i) {
  93. buffer [i] = '\0';
  94. }
  95. }
  96. points = & buffer [length];
  97. length += size;
  98. return ((void *) points);
  99. }
  100. void * record (void) {
  101. char * buffer = NULL;
  102. int offset = 0;
  103. int loling = 1024;
  104. buffer = reallocate (buffer, loling);
  105. do {
  106. if ((offset + 1) % loling == 0) {
  107. buffer = reallocate (buffer, ((offset + 1) / loling + 1) * loling);
  108. }
  109. buffer [offset] = '\0';
  110. in (& buffer [offset], sizeof (* buffer));
  111. ++offset;
  112. } while (buffer [offset - 1] != '\0');
  113. buffer [offset - 1] = '\0';
  114. return (buffer);
  115. }
  116. void argument_define (char * nick, char * name, void (* function) (void)) {
  117. fatal_failure (nick == NULL, "argument_define: Failed to define an argument, nick is null pointer.");
  118. fatal_failure (name == NULL, "argument_define: Failed to define an argument, name is null pointer.");
  119. fatal_failure (function == NULL, "argument_define: Failed to define an argument, function is null pointer.");
  120. ++argument_count;
  121. argument_nick = reallocate (argument_nick, argument_count * (int) sizeof (* argument_nick));
  122. argument_name = reallocate (argument_name, argument_count * (int) sizeof (* argument_name));
  123. argument_function = reallocate (argument_function, argument_count * (int) sizeof (* argument_function));
  124. argument_nick [argument_count - 1] = allocate (string_length (nick) + 1);
  125. argument_name [argument_count - 1] = allocate (string_length (name) + 1);
  126. string_copy (argument_nick [argument_count - 1], nick);
  127. string_copy (argument_name [argument_count - 1], name);
  128. argument_function [argument_count - 1] = function;
  129. }
  130. void argument_select (int count, char * * array) {
  131. int index_a = 0;
  132. int index_b = 0;
  133. if ((count == 1) || (array == NULL)) {
  134. return;
  135. }
  136. for (index_a = 1; index_a != count; ++index_a) {
  137. for (index_b = 0; index_b != argument_count; ++index_b) {
  138. if ((string_compare (array [index_a], "-h") != 0) || (string_compare (array [index_a], "--help") != 0)) {
  139. echo ("Printing help...\n");
  140. for (index_b = 0; index_b != argument_count; ++index_b) {
  141. echo ("\t"); echo (argument_nick [index_b]);
  142. echo (" "); echo (argument_name [index_b]);
  143. echo ("\n");
  144. }
  145. fatal_failure (1, "Help printed, terminating...");
  146. } else if ((string_compare (array [index_a], "-i") != 0) || (string_compare (array [index_a], "--input") != 0)) {
  147. ++index_a;
  148. argument_input = array [index_a];
  149. echo ("Selecting input: "); echo (argument_input); echo ("\n");
  150. break;
  151. } else if ((string_compare (array [index_a], "-o") != 0) || (string_compare (array [index_a], "--output") != 0)) {
  152. ++index_a;
  153. argument_output = array [index_a];
  154. echo ("Selecting output: "); echo (argument_output); echo ("\n");
  155. break;
  156. } else if ((string_compare (array [index_a], argument_nick [index_b]) != 0) || (string_compare (array [index_a], argument_name [index_b]) != 0)) {
  157. argument_function [index_b] ();
  158. echo ("Executing: "); echo (argument_name [index_b]); echo ("\n");
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. void argument_delete (void) {
  165. int index;
  166. for (index = 0; index != argument_count; ++index) {
  167. argument_nick [index] = deallocate (argument_nick [index]);
  168. argument_name [index] = deallocate (argument_name [index]);
  169. }
  170. argument_nick = deallocate (argument_nick);
  171. argument_name = deallocate (argument_name);
  172. argument_function = deallocate (argument_function);
  173. }
  174. int file_open (char * name, int mode) {
  175. int descriptor = -1;
  176. fatal_failure (name == NULL, "file_open: Failed to open file, name is null pointer.");
  177. descriptor = open (name, mode);
  178. fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");
  179. return (descriptor);
  180. }
  181. int file_close (int file) {
  182. fatal_failure (file == -1, "file_close: Failed to close file, invalid file descriptor.");
  183. fatal_failure (close (file) == -1, "file_close: Failed to close file, function close returned invalid code.");
  184. return (-1);
  185. }
  186. void file_read (int file, void * data, int size) {
  187. fatal_failure (file == -1, "file_read: Failed to read from file, invalid descriptor.");
  188. fatal_failure (data == NULL, "file_read: Failed to read from file, data is null pointer.");
  189. fatal_failure (size == 0, "file_read: Failed to read from file, size is zero.");
  190. (void) read (file, data, (unsigned long int) size);
  191. }
  192. void file_write (int file, void * data, int size) {
  193. fatal_failure (file == -1, "file_write: Failed to write to file, invalid descriptor.");
  194. fatal_failure (data == NULL, "file_write: Failed to write to file, data is null pointer.");
  195. fatal_failure (size == 0, "file_write: Failed to write to file, size is zero.");
  196. (void) write (file, data, (unsigned long int) size);
  197. }
  198. int file_seek (int file, int whence) {
  199. fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");
  200. return ((int) lseek (file, 0, whence));
  201. }
  202. int file_size (int file) {
  203. int size = 0;
  204. fatal_failure (file == -1, "file_size: Failed to get size of file, invalid descriptor.");
  205. size = lseek (file, 0, SEEK_END);
  206. (void) lseek (file, 0, SEEK_SET);
  207. fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");
  208. return (size);
  209. }
  210. int file_type (char * name) {
  211. char * file_type_data [FILE_TYPE_COUNT] = {
  212. ".txt", ".s", ".fasm", ".gasm", ".nasm", ".yasm", ".c", ".h",
  213. ".adb", ".ads", ".cpp", ".hpp"
  214. };
  215. int type = 0;
  216. while (* name != '.') {
  217. ++name;
  218. }
  219. for (type = 0; type != FILE_TYPE_COUNT; ++type) {
  220. if (string_compare (name, file_type_data [type]) != 0) {
  221. return (type);
  222. }
  223. }
  224. return (-1);
  225. }
  226. char * file_import (char * name) {
  227. int file = -1;
  228. int size = -1;
  229. char * data = NULL;
  230. fatal_failure (name == NULL, "file_import: Failed to import file, name is null pointer.");
  231. fatal_failure (data != NULL, "file_import: Failed to import file, data is not null pointer.");
  232. file = file_open (name, O_RDONLY);
  233. size = file_size (file) + 1;
  234. data = allocate (size);
  235. file_read (file, data, size - 1);
  236. data [size - 1] = '\0';
  237. file = file_close (file);
  238. return (data);
  239. }
  240. void file_export (char * name, void * data) {
  241. (void) name;
  242. (void) data;
  243. }
  244. void file_list_import (char * name) {
  245. fatal_failure (name == NULL, "file_list_import: Failed to import file, name is null pointer.");
  246. ++file_list_count;
  247. file_list_active = file_list_count - 1;
  248. file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count);
  249. file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count);
  250. file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count);
  251. file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count);
  252. file_list_mark [file_list_count - 1] = -1;
  253. file_list_size [file_list_count - 1] = -1;
  254. file_list_name [file_list_count - 1] = NULL;
  255. file_list_data [file_list_count - 1] = NULL;
  256. file_list_name [file_list_count - 1] = allocate (string_length (name) + 1);
  257. (void) string_copy_limit (file_list_name [file_list_count - 1], name, string_length (name) + 1);
  258. file_list_mark [file_list_count - 1] = open (name, O_RDONLY);
  259. fatal_failure (file_list_mark [file_list_count - 1] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");
  260. file_list_size [file_list_count - 1] = (int) lseek (file_list_mark [file_list_count - 1], 0, SEEK_END) + 1;
  261. (void) lseek (file_list_mark [file_list_count - 1], 0, SEEK_SET);
  262. file_list_data [file_list_count - 1] = allocate (file_list_size [file_list_count - 1]);
  263. (void) read (file_list_mark [file_list_count - 1], file_list_data [file_list_count - 1], (unsigned long int) (file_list_size [file_list_count - 1] - 1));
  264. close (file_list_mark [file_list_count - 1]);
  265. file_list_data [file_list_count - 1] [file_list_size [file_list_count - 1] - 1] = '\0';
  266. }
  267. void file_list_export (char * name) {
  268. fatal_failure (name == NULL, "file_list_export: Failed to export file, name is null pointer.");
  269. file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC);
  270. (void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]);
  271. close (file_list_mark [file_list_active]);
  272. }
  273. void file_list_delete (void) {
  274. int i;
  275. for (i = 0; i != file_list_count; ++i) {
  276. file_list_name [i] = deallocate (file_list_name [i]);
  277. file_list_data [i] = deallocate (file_list_data [i]);
  278. }
  279. file_list_mark = deallocate (file_list_mark);
  280. file_list_size = deallocate (file_list_size);
  281. file_list_name = deallocate (file_list_name);
  282. file_list_data = deallocate (file_list_data);
  283. }
  284. int character_is_uppercase (char character) {
  285. return ((int) ((character >= 'A') && (character <= 'Z')));
  286. }
  287. int character_is_lowercase (char character) {
  288. return ((int) ((character >= 'a') && (character <= 'z')));
  289. }
  290. int character_is_digit (char character) {
  291. return ((int) ((character >= '0') && (character <= '9')));
  292. }
  293. int character_is_blank (char character) {
  294. return ((int) ((character == ' ') || (character == '\t') || (character == '\r') || (character == '\n')));
  295. }
  296. int character_is_alpha (char character) {
  297. return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0));
  298. }
  299. int character_is_symbol (char character) {
  300. char * symbols = "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./";
  301. return (character_compare_array (character, symbols, string_length (symbols)));
  302. }
  303. int character_is_visible (char character) {
  304. return ((int) ((character >= ' ') && (character <= '~')));
  305. }
  306. int character_is_invisible (char character) {
  307. return (character_is_visible (character) == 0);
  308. }
  309. int character_is_escape (char character) {
  310. return ((int) (character == '\033'));
  311. }
  312. int character_is_underscore (char character) {
  313. return ((int) (character == '_'));
  314. }
  315. int character_is_hexadecimal (char character) {
  316. char * hexadecimals = "0123456789ABCDEF";
  317. return (character_compare_array (character, hexadecimals, string_length (hexadecimals)));
  318. }
  319. int character_compare_array (char character, char * character_array, int count) {
  320. int i = 0;
  321. do {
  322. if (character == character_array [i]) {
  323. return (1);
  324. }
  325. } while (++i != count);
  326. return (0);
  327. }
  328. int string_length (char * string) {
  329. int length = 0;
  330. fatal_failure (string == NULL, "string_length: String is null pointer.");
  331. for (length = 0; string [length] != '\0'; ++length);
  332. return (length);
  333. }
  334. void string_reverse (char * string) {
  335. int i = 0;
  336. int length = 0;
  337. fatal_failure (string == NULL, "string_reverse: String is null pointer.");
  338. length = string_length (string);
  339. for (i = 0; string [i] != '\0'; ++i) {
  340. string [i] = string [length - i];
  341. }
  342. }
  343. void string_delete (char * string, int length) {
  344. int i;
  345. fatal_failure (string == NULL, "string_delete: String is null pointer.");
  346. fatal_failure (length == 0, "string_delete: String length is zero.");
  347. for (i = 0; i != length; ++i) {
  348. string [i] = '\0';
  349. }
  350. }
  351. int string_compare (char * string_0, char * string_1) {
  352. int i = 0;
  353. fatal_failure (string_0 == NULL, "string_compare: Destination string is null pointer.");
  354. fatal_failure (string_1 == NULL, "string_compare: Source string is null pointer.");
  355. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
  356. if (string_0 [i] != string_1 [i]) {
  357. return (0);
  358. }
  359. }
  360. return (1);
  361. }
  362. void string_copy (char * string_0, char * string_1) {
  363. int i = 0;
  364. int length = 0;
  365. fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer.");
  366. fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer.");
  367. length = string_length (string_1);
  368. for (i = 0; i != length; ++i) {
  369. string_0 [i] = string_1 [i];
  370. }
  371. }
  372. void string_concatenate (char * string_0, char * string_1) {
  373. int i = 0;
  374. int length_0 = 0;
  375. int length_1 = 0;
  376. fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer.");
  377. fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer.");
  378. length_0 = string_length (string_0);
  379. length_1 = string_length (string_1);
  380. for (i = 0; i != length_1; ++i) {
  381. string_0 [length_0 + i] = string_1 [i];
  382. }
  383. }
  384. int string_compare_limit (char * string_0, char * string_1, int limit) {
  385. int i = 0;
  386. fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null pointer.");
  387. fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null pointer.");
  388. for (i = 0; i != limit; ++i) {
  389. if (string_0 [i] != string_1 [i]) {
  390. return (0);
  391. }
  392. }
  393. return (1);
  394. }
  395. void string_copy_limit (char * string_0, char * string_1, int limit) {
  396. int i = 0;
  397. fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null pointer.");
  398. fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null pointer.");
  399. if (limit <= 0) {
  400. return;
  401. }
  402. for (i = 0; i != limit; ++i) {
  403. string_0 [i] = string_1 [i];
  404. }
  405. }
  406. void string_concatenate_limit (char * string_0, char * string_1, int limit) {
  407. int i = 0;
  408. int length_0 = 0;
  409. int length_1 = 0;
  410. fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null pointer.");
  411. fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null pointer.");
  412. if (limit <= 0) {
  413. return;
  414. }
  415. length_0 = string_length (string_0);
  416. length_1 = string_length (string_1);
  417. for (i = 0; (i != length_1) && (i != limit); ++i) {
  418. string_0 [length_0 + i] = string_1 [i];
  419. }
  420. }
  421. int string_split_space (char * string) {
  422. int i = 0;
  423. int count = 0;
  424. fatal_failure (string == NULL, "string_split_space: Source string is null pointer.");
  425. for (i = 0; string [i] != '\0'; ++i) {
  426. if ((string [i] == ' ') || (string [i] == '\t') || (string [i] == '\n')) {
  427. string [i] = '\0';
  428. ++count;
  429. }
  430. }
  431. return (++count);
  432. }
  433. void memory_delete (void * memory, int length) {
  434. int i = 0;
  435. char * cast = (char *) memory;
  436. fatal_failure (memory == NULL, "memory_delete: Memory is null pointer.");
  437. if (length <= 0) {
  438. return;
  439. }
  440. for (i = 0; i != length; ++i) {
  441. cast [i] = '\0';
  442. }
  443. }
  444. int memory_compare (void * memory, void * source, int length) {
  445. int i = 0;
  446. char * cast_0 = (char *) memory;
  447. char * cast_1 = (char *) source;
  448. fatal_failure (memory == NULL, "memory_compare: Memory is null pointer.");
  449. fatal_failure (source == NULL, "memory_compare: Source is null pointer.");
  450. if (length <= 0) {
  451. return;
  452. }
  453. for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) {
  454. if (cast_0 [i] != cast_1 [i]) {
  455. return (0);
  456. }
  457. }
  458. return (1);
  459. }
  460. void memory_copy (void * memory, void * source, int length) {
  461. int i = 0;
  462. char * cast_0 = (char *) memory;
  463. char * cast_1 = (char *) source;
  464. fatal_failure (memory == NULL, "memory_copy: Memory is null pointer.");
  465. fatal_failure (source == NULL, "memory_copy: Source is null pointer.");
  466. if (length <= 0) {
  467. return;
  468. }
  469. for (i = 0; i != length; ++i) {
  470. cast_0 [i] = cast_1 [i];
  471. }
  472. }
  473. void terminal_clear (void) {
  474. echo ("\033[2J\033[H");
  475. }
  476. void terminal_colour (int colour, int effect) {
  477. char format [8] = "\033[ ;3 m";
  478. format [2] = (char) (effect % EFFECT_COUNT) + '0';
  479. format [5] = (char) (colour % COLOUR_COUNT) + '0';
  480. echo (format);
  481. }
  482. void terminal_cancel (void) {
  483. echo ("\033[0m");
  484. }
  485. void terminal_show_cursor (int show) {
  486. if (show != 0) {
  487. echo ("\033[?25h");
  488. } else {
  489. echo ("\033[?25l");
  490. }
  491. }
  492. int encode_byte (char * byte) {
  493. int encode = 0;
  494. fatal_failure (character_is_hexadecimal (byte [0]) == 0, "encode_byte: Upper byte character is not hexadecimal digit.");
  495. fatal_failure (character_is_hexadecimal (byte [1]) == 0, "encode_byte: Lower byte character is not hexadecimal digit.");
  496. encode = ((byte [0] - (character_is_digit (byte [0]) ? ('0') : ('A' - 10))) << 4) | (byte [1] - (character_is_digit (byte [1]) ? ('0') : ('A' - 10)));
  497. return (encode);
  498. }
  499. char * decode_byte (int byte) {
  500. static char * decode = " ";
  501. return (decode);
  502. }
  503. #endif