shprint.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "mk.h"
  10. static char *vexpand(char*, Envy*, Bufblock*);
  11. static char *shquote(char*, Rune, Bufblock*);
  12. static char *shbquote(char*, Bufblock*);
  13. void
  14. shprint(char *s, Envy *env, Bufblock *buf)
  15. {
  16. int n;
  17. Rune r;
  18. while(*s) {
  19. n = chartorune(&r, s);
  20. if (r == '$')
  21. s = vexpand(s, env, buf);
  22. else {
  23. rinsert(buf, r);
  24. s += n;
  25. s = copyq(s, r, buf); /*handle quoted strings*/
  26. }
  27. }
  28. insert(buf, 0);
  29. }
  30. static char *
  31. mygetenv(char *name, Envy *env)
  32. {
  33. if (!env)
  34. return 0;
  35. if (symlook(name, S_WESET, 0) == 0 && symlook(name, S_INTERNAL, 0) == 0)
  36. return 0;
  37. /* only resolve internal variables and variables we've set */
  38. for(; env->name; env++){
  39. if (strcmp(env->name, name) == 0)
  40. return wtos(env->values, ' ');
  41. }
  42. return 0;
  43. }
  44. static char *
  45. vexpand(char *w, Envy *env, Bufblock *buf)
  46. {
  47. char *s, carry, *p, *q;
  48. assert(/*vexpand no $*/ *w == '$');
  49. p = w+1; /* skip dollar sign */
  50. if(*p == '{') {
  51. p++;
  52. q = utfrune(p, '}');
  53. if (!q)
  54. q = strchr(p, 0);
  55. } else
  56. q = shname(p);
  57. carry = *q;
  58. *q = 0;
  59. s = mygetenv(p, env);
  60. *q = carry;
  61. if (carry == '}')
  62. q++;
  63. if (s) {
  64. bufcpy(buf, s, strlen(s));
  65. free(s);
  66. } else /* copy name intact*/
  67. bufcpy(buf, w, q-w);
  68. return(q);
  69. }
  70. void
  71. front(char *s)
  72. {
  73. char *t, *q;
  74. int i, j;
  75. char *flds[512];
  76. q = strdup(s);
  77. i = getfields(q, flds, nelem(flds), 0, " \t\n");
  78. if(i > 5){
  79. flds[4] = flds[i-1];
  80. flds[3] = "...";
  81. i = 5;
  82. }
  83. t = s;
  84. for(j = 0; j < i; j++){
  85. for(s = flds[j]; *s; *t++ = *s++);
  86. *t++ = ' ';
  87. }
  88. *t = 0;
  89. free(q);
  90. }