ul.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* ul utility, but fails to support -i. */
  2. #include <stdio.h>
  3. extern char **argvec;
  4. char *getenv();
  5. char *tgetstr();
  6. int do_os;
  7. char *start;
  8. char *stop;
  9. char *termtype;
  10. char tcp_buf[1024];
  11. char cap_buf[1024];
  12. main (ac, av)
  13. int ac;
  14. char **av;
  15. {
  16. char c;
  17. char d;
  18. int in_under;
  19. int is_under;
  20. if (strncmp (av[1], "-t", 2) == 0)
  21. {
  22. termtype = av[1] + 2;
  23. ac --;
  24. av ++;
  25. }
  26. else
  27. {
  28. termtype = getenv ("TERM");
  29. if (termtype == 0)
  30. {
  31. termtype = "dumb";
  32. }
  33. }
  34. switch (tgetent (tcp_buf, termtype))
  35. {
  36. case -1:
  37. fprintf (stderr, "%s: cannot open termcap\n", argvec[0]);
  38. exit (1);
  39. break;
  40. case 0:
  41. strcpy (tcp_buf, "dumb:os:");
  42. break;
  43. }
  44. if (tgetflag ("os") || tgetflag ("ul"))
  45. {
  46. do_os = 1;
  47. }
  48. else
  49. {
  50. char *cp;
  51. do_os = 0;
  52. cp = cap_buf;
  53. start = tgetstr ("us", &cp);
  54. stop = tgetstr ("ue", &cp);
  55. if ((start == 0) || (stop == 0))
  56. {
  57. start = tgetstr ("so", &cp);
  58. stop = tgetstr ("se", &cp);
  59. if ((start == 0) || (stop == 0))
  60. {
  61. start = "";
  62. stop = "";
  63. }
  64. }
  65. }
  66. in_under = 0;
  67. while (1)
  68. {
  69. c = getchar ();
  70. if (feof (stdin))
  71. {
  72. break;
  73. }
  74. is_under = 0;
  75. if (c == '_')
  76. {
  77. d = getchar ();
  78. if (d == '\b')
  79. {
  80. c = getchar ();
  81. is_under = 1;
  82. }
  83. else
  84. {
  85. ungetc (d, stdin);
  86. }
  87. }
  88. if (is_under != in_under)
  89. {
  90. printf ("%s", is_under?start:stop);
  91. in_under = is_under;
  92. }
  93. putchar (c);
  94. }
  95. if (in_under)
  96. {
  97. printf ("%s", stop);
  98. }
  99. }