参考

{% collapse title=”prompt_unix.c” %}

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <editline/readline.h>
  4. #include <editline/history.h>
  5. int main(int argc, char** argv) {
  6. /* Print Version and Exit Information */
  7. puts("Lispy Version 0.0.0.0.1");
  8. puts("Press Ctrl+c to Exit\n");
  9. /* In a never ending loop */
  10. while (1) {
  11. /* Output our prompt and get input */
  12. char* input = readline("lispy> ");
  13. /* Add input to history */
  14. add_history(input);
  15. /* Echo input back to user */
  16. printf("No you're a %s\n", input);
  17. /* Free retrived input */
  18. free(input);
  19. }
  20. return 0;
  21. }

{% endcollapse %}

{% collapse title=”prompt_windows.c” %}

  1. #include <stdio.h>
  2. /* Declare a buffer for user input of size 2048 */
  3. static char input[2048];
  4. int main(int argc, char** argv) {
  5. /* Print Version and Exit Information */
  6. puts("Lispy Version 0.0.0.0.1");
  7. puts("Press Ctrl+c to Exit\n");
  8. /* In a never ending loop */
  9. while (1) {
  10. /* Output our prompt */
  11. fputs("lispy> ", stdout);
  12. /* Read a line of user input of maximum size 2048 */
  13. fgets(input, 2048, stdin);
  14. /* Echo input back to user */
  15. printf("No you're a %s", input);
  16. }
  17. return 0;
  18. }

{% endcollapse %}

{% collapse title=”prompt.c” %}

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* If we are compiling on Windows compile these functions */
  4. #ifdef _WIN32
  5. #include <string.h>
  6. static char buffer[2048];
  7. /* Fake readline function */
  8. char* readline(char* prompt) {
  9. fputs(prompt, stdout);
  10. fgets(buffer, 2048, stdin);
  11. char* cpy = malloc(strlen(buffer)+1);
  12. strcpy(cpy, buffer);
  13. cpy[strlen(cpy)-1] = '\0';
  14. return cpy;
  15. }
  16. /* Fake add_history function */
  17. void add_history(char* unused) {}
  18. /* Otherwise include the editline headers */
  19. #else
  20. #include <editline/readline.h>
  21. #include <editline/history.h>
  22. #endif
  23. int main(int argc, char** argv) {
  24. puts("Lispy Version 0.0.0.0.1");
  25. puts("Press Ctrl+c to Exit\n");
  26. while (1) {
  27. /* Now in either case readline will be correctly defined */
  28. char* input = readline("lispy> ");
  29. add_history(input);
  30. printf("No you're a %s\n", input);
  31. free(input);
  32. }
  33. return 0;
  34. }

{% endcollapse %}