ed.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <string.h>
  11. #define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
  12. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  13. #define DESCRIPTION "Edit text."
  14. #define OPERANDS "[-p prompt]"
  15. #include "../common/common.h"
  16. void print_error();
  17. size_t c_append(char buffer[4096]);
  18. int main(int argc, char *argv[]) {
  19. int argument, i = 0, fildes;
  20. char buffer[4096], *edit_pathname, *prompt_string = "",
  21. command_string[4096], *error = "", *argv0 = strdup(argv[0]);
  22. int help_mode = 0;
  23. struct sigaction signal_action;
  24. while ((argument = getopt(argc, argv, "p:")) != -1) {
  25. if (argument == '?') {
  26. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  27. return 1;
  28. }
  29. else if (argument == 'p')
  30. prompt_string = optarg;
  31. }
  32. argc -= optind;
  33. argv += optind;
  34. signal_action.sa_handler = SIG_IGN;
  35. sigemptyset(&signal_action.sa_mask);
  36. sigaction(SIGINT, &signal_action, NULL);
  37. setvbuf(stdout, NULL, _IONBF, 0);
  38. for (;;) {
  39. write(STDOUT_FILENO, prompt_string, strlen(prompt_string));
  40. //if (fgets(command_string, 4096, stdin) == NULL) return errno;
  41. if (read(STDIN_FILENO, command_string, 4096) == -1)
  42. return errprint(argv0, "read <stdin>", errno);
  43. switch (command_string[i]) { /* Addresses */
  44. case '.':
  45. case '$':
  46. case '\'':
  47. case '+':
  48. case '-':
  49. /* FALLTHROUGH */
  50. ++i;
  51. print_error((error = "not implemented"), help_mode);
  52. continue;
  53. }
  54. switch (command_string[i]) { /* Commands */
  55. case 'e': /* Edit Command */
  56. if (i < 0) {
  57. print_error((error = "unexpected address"), help_mode);
  58. continue;
  59. }
  60. if (strlen(command_string) > 2 && i == 0) {
  61. edit_pathname = command_string + 2;
  62. edit_pathname[strlen(edit_pathname) - 1] = '\0';
  63. } else if (!edit_pathname) {
  64. print_error((error = "no pathname given"), help_mode);
  65. continue;
  66. }
  67. fildes = open(edit_pathname, O_RDONLY | O_CREAT);
  68. if (errno) return errprint(argv0, "open()", errno);
  69. printf("%ld\n", read(fildes, buffer, 4096));
  70. if (fildes) close(fildes);
  71. continue;
  72. case 'a':
  73. c_append(buffer);
  74. continue;
  75. case 'w':
  76. if (strtok(command_string, " ") != NULL && strtok(NULL, " ") != NULL
  77. && i == 0) {
  78. edit_pathname = command_string + 2;
  79. edit_pathname[strlen(edit_pathname) - 1] = '\0';
  80. fildes = open(edit_pathname, O_RDWR | O_CREAT);
  81. } else if (edit_pathname) {
  82. fildes = open(edit_pathname, O_RDWR);
  83. } else {
  84. if (i != 0)
  85. print_error((error = "unexpected address"), help_mode);
  86. else
  87. print_error((error = "no pathname given"), help_mode);
  88. continue;
  89. }
  90. if (errno) return errprint(argv0, "open()", errno);
  91. printf("%ld\n", write(fildes, buffer, strlen(buffer)));
  92. if (errno) return errprint(argv0, "write()", errno);
  93. if (fildes) close(fildes);
  94. continue;
  95. case 'q':
  96. if (fildes) close(fildes);
  97. return errprint(argv0, NULL, errno);
  98. case 'H': /* Help mode */
  99. if (i < 0) {
  100. print_error((error = "unexpected address"), help_mode);
  101. continue;
  102. }
  103. /* If help_mode was equal to 1, set it to 0 and vice-versa. */
  104. help_mode = !help_mode;
  105. /* FALLTHROUGH */
  106. case 'h': /* Help Command */
  107. /* FALLTHROUGH */
  108. print_error(error, help_mode + 2); /* Print the last encountered error. */
  109. continue;
  110. default: /* Invalid/Not implemented command */
  111. print_error((error = "invalid or not implemented"), help_mode);
  112. }
  113. }
  114. }
  115. size_t c_append(char buffer[4096]) {
  116. char *tempbuf = NULL;
  117. size_t bufsize = 0, bytes_read = 0;
  118. while ((bytes_read += getline(&tempbuf, &bufsize, stdin)) > 0) {
  119. if (!strcmp(tempbuf, ".\n")) break;
  120. strcat(buffer, tempbuf);
  121. }
  122. return bytes_read;
  123. }
  124. void printUsage() {
  125. printf("Ferass' Base System. (%s)\n\n"
  126. "Usage: ed [-p prompt_string]\n\n"
  127. "Edit text.\n\n"
  128. "\t-p prompt_string\tAppend a prompt string.\n", VERSION);
  129. }
  130. void print_error(const char *error, int help_mode) {
  131. if (help_mode <= 1) puts("?");
  132. if (help_mode) puts(error);
  133. }