private-utils.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <config.h>
  2. #include <apt-pkg/configuration.h>
  3. #include <apt-pkg/fileutl.h>
  4. #include <apt-private/private-utils.h>
  5. #include <cstdlib>
  6. #include <unistd.h>
  7. // DisplayFileInPager - Display File with pager /*{{{*/
  8. void DisplayFileInPager(std::string const &filename)
  9. {
  10. pid_t Process = ExecFork();
  11. if (Process == 0)
  12. {
  13. const char *Args[3];
  14. Args[1] = filename.c_str();
  15. Args[2] = NULL;
  16. if (isatty(STDOUT_FILENO) == 1)
  17. {
  18. // likely installed, provided by sensible-utils
  19. std::string const pager = _config->Find("Dir::Bin::Pager",
  20. "sensible-pager");
  21. Args[0] = pager.c_str();
  22. execvp(Args[0],(char **)Args);
  23. // lets try some obvious alternatives
  24. Args[0] = getenv("PAGER");
  25. if (Args[0] != NULL)
  26. execvp(Args[0],(char **)Args);
  27. Args[0] = "pager";
  28. execvp(Args[0],(char **)Args);
  29. }
  30. // we could read the file ourselves, but… meh
  31. Args[0] = "cat";
  32. execvp(Args[0],(char **)Args);
  33. exit(100);
  34. }
  35. // Wait for the subprocess
  36. ExecWait(Process, "pager", false);
  37. }
  38. /*}}}*/
  39. // EditFileInSensibleEditor - Edit File with editor /*{{{*/
  40. void EditFileInSensibleEditor(std::string const &filename)
  41. {
  42. pid_t Process = ExecFork();
  43. if (Process == 0)
  44. {
  45. // likely installed, provided by sensible-utils
  46. std::string const editor = _config->Find("Dir::Bin::Editor",
  47. "sensible-editor");
  48. const char *Args[3];
  49. Args[0] = editor.c_str();
  50. Args[1] = filename.c_str();
  51. Args[2] = NULL;
  52. execvp(Args[0],(char **)Args);
  53. // the usual suspects we can try as an alternative
  54. Args[0] = getenv("VISUAL");
  55. if (Args[0] != NULL)
  56. execvp(Args[0],(char **)Args);
  57. Args[0] = getenv("EDITOR");
  58. if (Args[0] != NULL)
  59. execvp(Args[0],(char **)Args);
  60. Args[0] = "editor";
  61. execvp(Args[0],(char **)Args);
  62. exit(100);
  63. }
  64. // Wait for the subprocess
  65. ExecWait(Process, "editor", false);
  66. }
  67. /*}}}*/