extra.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: extra.c /main/4 1995/11/01 15:54:55 rswiston $ */
  24. /* Copyright (c) 1991, 1992 UNIX System Laboratories, Inc. */
  25. /* All Rights Reserved */
  26. /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
  27. /* UNIX System Laboratories, Inc. */
  28. /* The copyright notice above does not evidence any */
  29. /* actual or intended publication of such source code. */
  30. #include "defs.h"
  31. #include "shell.h"
  32. #include "name.h"
  33. #include "stdio.h"
  34. #include "msgs.h"
  35. void
  36. env_set(
  37. char *var )
  38. {
  39. (void)nv_open(var, sh.var_tree, NV_ASSIGN);
  40. }
  41. void
  42. env_set_gbl(
  43. char *vareqval )
  44. {
  45. env_set(vareqval);
  46. }
  47. char *
  48. env_get(
  49. char *var )
  50. {
  51. Namval_t *np;
  52. char *val;
  53. np = nv_search(var,sh.var_tree,0);
  54. if (np && (val = nv_getval(np)))
  55. return(val);
  56. return(NULL);
  57. }
  58. void *
  59. xkhash_init(
  60. int num )
  61. {
  62. return((void *) hashalloc(NULL,0));
  63. }
  64. void
  65. xkhash_override(
  66. Hash_table_t *tbl,
  67. const char *name,
  68. void *val )
  69. {
  70. hashput(tbl, name, val);
  71. }
  72. void *
  73. xkhash_find(
  74. Hash_table_t *tbl,
  75. const char *name )
  76. {
  77. return(hashget(tbl, name));
  78. }
  79. void
  80. xkhash_add(
  81. Hash_table_t *tbl,
  82. const char *name,
  83. char *val )
  84. {
  85. hashput(tbl, name, val);
  86. }
  87. int
  88. ksh_eval(
  89. char *cmd )
  90. {
  91. sh_eval(sfopen(NIL(Sfile_t*),cmd,"s"),0);
  92. sfsync(sh.outpool);
  93. return(sh.exitval);
  94. }
  95. void
  96. env_set_var(
  97. char *var,
  98. char *val )
  99. {
  100. int len;
  101. char tmp[512];
  102. char *set = &tmp[0];
  103. if ((len = strlen(var) + strlen(val?val:"") + 2) > sizeof(tmp)) /* 11/06 CHANGED */
  104. set = malloc(len);
  105. strcpy(set, var);
  106. strcat(set, "=");
  107. strcat(set, val?val:""); /* 11/06 CHANGED */
  108. env_set(set);
  109. if (set != &tmp[0])
  110. free(set);
  111. }
  112. void
  113. env_blank(
  114. char *var )
  115. {
  116. env_set_var(var, "");
  117. }
  118. void
  119. printerr(
  120. char *cmd,
  121. char *msg1,
  122. char *msg2 )
  123. {
  124. if (msg1 == NULL)
  125. msg1 = "";
  126. if (msg2 == NULL)
  127. msg2 = "";
  128. if (cmd && (strlen(cmd) > 0))
  129. printf( "%s: %s %s\n", cmd, msg1, msg2);
  130. else
  131. printf( "%s %s\n", msg1, msg2);
  132. }
  133. void
  134. printerrf(
  135. char *cmd,
  136. char *fmt,
  137. char *arg0,
  138. char *arg1,
  139. char *arg2,
  140. char *arg3,
  141. char *arg4,
  142. char *arg5,
  143. char *arg6,
  144. char *arg7 )
  145. {
  146. char buf[2048];
  147. if (arg0 == NULL)
  148. arg0 = "";
  149. if (arg1 == NULL)
  150. arg1 = "";
  151. if (arg2 == NULL)
  152. arg2 = "";
  153. if (arg3 == NULL)
  154. arg3 = "";
  155. if (arg4 == NULL)
  156. arg4 = "";
  157. if (arg5 == NULL)
  158. arg5 = "";
  159. if (arg6 == NULL)
  160. arg6 = "";
  161. if (arg7 == NULL)
  162. arg7 = "";
  163. sprintf(buf, fmt, arg0, arg1, arg2, arg3,arg4, arg5, arg6, arg7);
  164. if (cmd && (strlen(cmd) > 0))
  165. printf("%s: %s\n", cmd, buf);
  166. else
  167. printf("%s\n", buf);
  168. }