xd -- Non standard text editor inspired by ed, I promise to finish this...
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.

156 line
4.5KB

  1. #include <xolatile/xyntax.h>
  2. #include <xolatile/xyntax.c>
  3. #include <xolatile/xurses.h>
  4. #include <xolatile/xurses.c>
  5. static int active = 1;
  6. static char prompt [144] = "";
  7. static int command_count = 0;
  8. static int * command_copy = NULL;
  9. static char * * command_nick = NULL;
  10. static char * * command_name = NULL;
  11. static char * command_argument = NULL;
  12. static void (* * command_function) (void) = NULL;
  13. static void command_define (int copy, char * nick, char * name, void (* function) (void)) {
  14. command_copy = reallocate (command_copy, (command_count + 1) * (int) sizeof (* command_copy));
  15. command_nick = reallocate (command_nick, (command_count + 1) * (int) sizeof (* command_nick));
  16. command_name = reallocate (command_name, (command_count + 1) * (int) sizeof (* command_name));
  17. command_function = reallocate (command_function, (command_count + 1) * (int) sizeof (* command_function));
  18. command_copy [command_count] = copy;
  19. command_function [command_count] = function;
  20. command_nick [command_count] = allocate (string_length (nick) + 1);
  21. command_name [command_count] = allocate (string_length (name) + 1);
  22. string_copy (command_nick [command_count], nick);
  23. string_copy (command_name [command_count], name);
  24. ++command_count;
  25. }
  26. static void echo_help (void) {
  27. echo ("-h --help | Print help information to standard output.\n");
  28. echo ("-v --version | Print version information to standard output.\n");
  29. echo ("-l --license | Print license information to standard output.\n");
  30. }
  31. static void echo_version (void) {
  32. echo ("xd : Non-standard text editor (v6.6.6)\n");
  33. }
  34. static void echo_license (void) {
  35. echo ("xd : Non-standard text editor (GNU/GPLv3)\n");
  36. }
  37. static void quit (void) {
  38. active = 0;
  39. }
  40. static void echoes (void) {
  41. int offset = 0;
  42. int index = 0;
  43. int length = 0;
  44. if (file_list_active >= file_list_count) {
  45. echo ("Unknown active file (out of range)...\n");
  46. return;
  47. }
  48. if (file_list_data [file_list_active] == NULL) {
  49. echo ("Null pointer for file data...\n");
  50. return;
  51. }
  52. for (offset = 0; file_list_data [file_list_active] [offset] != '\0'; offset += length) {
  53. syntax_select (& file_list_data [file_list_active] [offset], & index, & length);
  54. if (index >= syntax_count) {
  55. curses_style (EFFECT_NORMAL, COLOUR_WHITE);
  56. } else {
  57. curses_style (syntax_effect [index], syntax_colour [index]);
  58. }
  59. out (& file_list_data [file_list_active] [offset], length);
  60. curses_style (-1, -1);
  61. }
  62. }
  63. static void import (void) {
  64. echo ("Importing file \"");
  65. echo (command_argument);
  66. echo ("\"...\n");
  67. file_list_import (command_argument);
  68. }
  69. int main (int argc, char * * argv) {
  70. int command_index;
  71. argument_define ("-h", "--help", echo_help);
  72. argument_define ("-v", "--version", echo_version);
  73. argument_define ("-l", "--license", echo_license);
  74. argument_select (argc, argv);
  75. command_define (0, "q", "quit", quit);
  76. command_define (0, "e", "echo", echoes);
  77. command_define (1, "o", "open", import);
  78. do {
  79. int index;
  80. command_index = -1;
  81. string_delete (prompt, (int) sizeof (prompt));
  82. in (prompt, (int) sizeof (prompt));
  83. if (prompt [0] == '`') {
  84. echo ("Executing shell command (this is so fucking unsafe I'm almost ashamed)...\n");
  85. (void) system (& prompt [1]);
  86. continue;
  87. }
  88. (void) string_split_space (prompt);
  89. for (index = 0; index != command_count; ++index) {
  90. /*if ((string_compare_limit (prompt, command_nick [index], string_length (command_nick [index])) != 0)
  91. || (string_compare_limit (prompt, command_name [index], string_length (command_name [index])) != 0)) {*/
  92. if ((string_compare (prompt, command_nick [index]) != 0)
  93. || (string_compare (prompt, command_name [index]) != 0)) {
  94. command_index = index;
  95. }
  96. }
  97. if (command_copy [command_index] != 0) {
  98. command_argument = & prompt [string_length (prompt) + 1];
  99. }
  100. if (command_index != -1) {
  101. command_function [command_index] ();
  102. } else {
  103. echo ("Unknown command!\n");
  104. }
  105. } while (active != 0);
  106. file_list_delete ();
  107. for (command_index = 0; command_index != command_count; ++command_index) {
  108. command_nick [command_index] = deallocate (command_nick [command_index]);
  109. command_name [command_index] = deallocate (command_name [command_index]);
  110. }
  111. command_copy = deallocate (command_copy);
  112. command_nick = deallocate (command_nick);
  113. command_name = deallocate (command_name);
  114. command_function = deallocate (command_function);
  115. argument_delete ();
  116. return (EXIT_SUCCESS);
  117. }