home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / edit / jade / src / misc.c < prev    next >
C/C++ Source or Header  |  1994-09-18  |  12KB  |  466 lines

  1. /* misc.c -- Miscellaneous functions
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.    If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22. #include "revision.h"
  23.  
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28.  
  29. _PR VALUE concat2(u_char *, u_char *);
  30. _PR VALUE concat3(u_char *, u_char *, u_char *);
  31. _PR void misc_init(void);
  32.  
  33. #ifndef HAVE_STPCPY
  34. /*
  35.  * copy src to dst, returning pointer to terminating '\0' of dst.
  36.  * Although this has a prototype in my <string.h> it doesn't seem to be
  37.  * in the actual library??
  38.  */
  39. char *
  40. stpcpy(register char *dst, register const char *src)
  41. {
  42.     while((*dst++ = *src++) != 0)
  43.     ;
  44.     return(dst - 1);
  45. }
  46. #endif /* !HAVE_STPCPY */
  47.  
  48. #ifndef HAVE_MEMCHR
  49. void *
  50. memchr(const void *mem, int c, size_t len)
  51. {
  52.     register char *tmp = (char *)mem;
  53.     while(len-- > 0)
  54.     {
  55.     if(*tmp++ != c)
  56.         continue;
  57.     return((void *)(tmp - 1));
  58.     }
  59.     return(NULL);
  60. }
  61. #endif /* !HAVE_MEMCHR */
  62.  
  63. VALUE
  64. concat2(u_char *s1, u_char *s2)
  65. {
  66.     int len = strlen(s1) + strlen(s2);
  67.     VALUE res = make_string(len + 1);
  68.     stpcpy(stpcpy(VSTR(res), s1), s2);
  69.     return(res);
  70. }
  71. VALUE
  72. concat3(u_char *s1, u_char *s2, u_char *s3)
  73. {
  74.     int len = strlen(s1) + strlen(s2) + strlen(s3);
  75.     VALUE res = make_string(len + 1);
  76.     stpcpy(stpcpy(stpcpy(VSTR(res), s1), s2), s3);
  77.     return(res);
  78. }
  79.  
  80. _PR VALUE cmd_file_name_concat(VALUE args);
  81. DEFUN("file-name-concat", cmd_file_name_concat, subr_file_name_concat, (VALUE args), V_SubrN, DOC_file_name_concat) /*
  82. ::doc:file_name_concat::
  83. file-name-concat PARTS...
  84.  
  85. Returns a string made from all the PARTS, each of which is one component of
  86. a file name. Add's `/' characters between each PART if necessary.
  87. ::end:: */
  88. {
  89.     if(CONSP(args) && STRINGP(VCAR(args)))
  90.     {
  91.     u_char buf[512];
  92.     strcpy(buf, VSTR(VCAR(args)));
  93.     args = VCDR(args);
  94.     while(CONSP(args) && STRINGP(VCAR(args)))
  95.     {
  96.         if(add_file_part(buf, VSTR(VCAR(args)), 512) == 0)
  97.         return(NULL);
  98.         args = VCDR(args);
  99.     }
  100.     return(string_dup(buf));
  101.     }
  102.     return(null_string);
  103. }
  104.  
  105. _PR VALUE cmd_expand_file_name(VALUE name, VALUE full);
  106. DEFUN("expand-file-name", cmd_expand_file_name, subr_expand_file_name, (VALUE name, VALUE full), V_Subr2, DOC_expand_file_name) /*
  107. ::doc:expand_file_name::
  108. expand-file-name FILENAME [FULLY-QUALIFY]
  109.  
  110. Returns a valid file name from the string FILENAME. Currently this just expands
  111. any `~' characters it finds (if we're running on Unix) to the user's home
  112. directory.
  113.  
  114. If FULLY-QUALIFY is t then the FILENAME is made absolute
  115. ::end:: */
  116. {
  117.     DECLARE1(name, STRINGP);
  118.     name = sys_expand_file_name(name);
  119.     if(!NILP(full))
  120.     name = sys_fully_qualify_file_name(name);
  121.     return(name);
  122. }
  123.  
  124. _PR VALUE cmd_system(VALUE command);
  125. DEFUN_INT("system", cmd_system, subr_system, (VALUE command), V_Subr1, DOC_system, "sShell command:") /*
  126. ::doc:system::
  127. system SHELL-COMMAND
  128.  
  129. Tells the operating-system to execute SHELL-COMMAND, returns the exit code
  130. of that command.
  131. ::end:: */
  132. {
  133.     DECLARE1(command, STRINGP);
  134.     return(make_number(system(VSTR(command))));
  135. }
  136.  
  137. _PR VALUE cmd_substring(VALUE string, VALUE start, VALUE end);
  138. DEFUN("substring", cmd_substring, subr_substring, (VALUE string, VALUE start, VALUE end), V_Subr3, DOC_substring) /*
  139. ::doc:substring::
  140. substring STRING START [END]
  141.  
  142. Returns the portion of STRING starting at character number START and ending
  143. at the character before END (or the end of the string is END is not given).
  144. All indices start at zero.
  145. ::end:: */
  146. {
  147.     int slen;
  148.     DECLARE1(string, STRINGP);
  149.     DECLARE2(start, NUMBERP);
  150.     slen = STRING_LEN(string);
  151.     if(VNUM(start) > slen)
  152.     return(signal_arg_error(start, 2));
  153.     if(NUMBERP(end))
  154.     {
  155.     if((VNUM(end) > slen) || (VNUM(end) < VNUM(start)))
  156.         return(signal_arg_error(end, 3));
  157.     return(string_dupn(VSTR(string) + VNUM(start), VNUM(end) - VNUM(start)));
  158.     }
  159.     else
  160.     return(string_dupn(VSTR(string) + VNUM(start), slen - VNUM(start)));
  161. }
  162.  
  163. _PR VALUE cmd_beep(void);
  164. DEFUN_INT("beep", cmd_beep, subr_beep, (void), V_Subr0, DOC_beep, "") /*
  165. ::doc:beep::
  166. beep
  167.  
  168. Rings a bell.
  169. ::end:: */
  170. {
  171.     beep(curr_vw);
  172.     return(sym_t);
  173. }
  174.  
  175. _PR VALUE cmd_file_name_nondirectory(VALUE file);
  176. DEFUN("file-name-nondirectory", cmd_file_name_nondirectory, subr_file_name_nondirectory, (VALUE file), V_Subr1, DOC_file_name_nondirectory) /*
  177. ::doc:file_name_nondirectory::
  178. file-name-nondirectory FILE-NAME
  179.  
  180. Returns the nondirectory part of FILE-NAME.
  181. ::end:: */
  182. {
  183.     DECLARE1(file, STRINGP);
  184.     return(string_dup(file_part(VSTR(file))));
  185. }
  186.  
  187. _PR VALUE cmd_file_name_directory(VALUE file);
  188. DEFUN("file-name-directory", cmd_file_name_directory, subr_file_name_directory, (VALUE file), V_Subr1, DOC_file_name_directory) /*
  189. ::doc:file_name_directory::
  190. file-name-directory FILE-NAME
  191.  
  192. Returns the directory part of FILE-NAME.
  193. ::end:: */
  194. {
  195.     int len;
  196.     DECLARE1(file, STRINGP);
  197.     len = file_part(VSTR(file)) - VSTR(file);
  198.     return(string_dupn(VSTR(file), len));
  199. }
  200.  
  201. _PR VALUE cmd_balance_brackets(VALUE open, VALUE close, VALUE string);
  202. DEFUN("balance-brackets", cmd_balance_brackets, subr_balance_brackets, (VALUE open, VALUE close, VALUE string), V_Subr3, DOC_balance_brackets) /*
  203. ::doc:balance_brackets::
  204. balance-brackets OPEN-STRING CLOSE-STRING STRING
  205. ::end:: */
  206. {
  207.     int cnt = 0;
  208.     u_char *s;
  209.     DECLARE1(open, STRINGP);
  210.     DECLARE2(close, STRINGP);
  211.     DECLARE3(string, STRINGP);
  212.     s = VSTR(string) - 1;
  213.     while((s = strpbrk(s + 1, VSTR(open))))
  214.     cnt++;
  215.     s = VSTR(string) - 1;
  216.     while((s = strpbrk(s + 1, VSTR(close))))
  217.     cnt--;
  218.     return(make_number(cnt));
  219. }
  220.  
  221. _PR VALUE cmd_strtoc(VALUE string);
  222. DEFUN("strtoc", cmd_strtoc, subr_strtoc, (VALUE string), V_Subr1, DOC_strtoc) /*
  223. ::doc:strtoc::
  224. strtoc STRING
  225.  
  226. Returns the first character of STRING.
  227. ::end:: */
  228. {
  229.     DECLARE1(string, STRINGP);
  230.     return(make_number((long)*VSTR(string)));
  231. }
  232.  
  233. _PR VALUE cmd_ctostr(VALUE ch);
  234. DEFUN("ctostr", cmd_ctostr, subr_ctostr, (VALUE ch), V_Subr1, DOC_ctostr) /*
  235. ::doc:ctostr::
  236. ctostr CHAR
  237.  
  238. Returns a one-character string containing CHAR.
  239. ::end:: */
  240. {
  241.     u_char tmp[2];
  242.     DECLARE1(ch, CHARP);
  243.     tmp[0] = (u_char)VCHAR(ch);
  244.     tmp[1] = 0;
  245.     return(string_dup(tmp));
  246. }
  247.  
  248. _PR VALUE cmd_amiga_p(void);
  249. DEFUN("amiga-p", cmd_amiga_p, subr_amiga_p, (void), V_Subr0, DOC_amiga_p) /*
  250. ::doc:amiga_p::
  251. amiga-p
  252.  
  253. t if running on an Amiga.
  254. ::end:: */
  255. {
  256. #ifdef HAVE_AMIGA
  257.     return(sym_t);
  258. #else
  259.     return(sym_nil);
  260. #endif
  261. }
  262. _PR VALUE cmd_x11_p(void);
  263. DEFUN("x11-p", cmd_x11_p, subr_x11_p, (void), V_Subr0, DOC_x11_p) /*
  264. ::doc:x11_p::
  265. x11-p
  266.  
  267. t if running on the X Window System V11.
  268. ::end:: */
  269. {
  270. #ifdef HAVE_X11
  271.     return(sym_t);
  272. #else
  273.     return(sym_nil);
  274. #endif
  275. }
  276. _PR VALUE cmd_unix_p(void);
  277. DEFUN("unix-p", cmd_unix_p, subr_unix_p, (void), V_Subr0, DOC_unix_p) /*
  278. ::doc:unix_p::
  279. unix-p
  280.  
  281. t if running under some flavour of unix.
  282. ::end:: */
  283. {
  284. #ifdef HAVE_UNIX
  285.     return(sym_t);
  286. #else
  287.     return(sym_nil);
  288. #endif
  289. }
  290.  
  291. _PR VALUE cmd_tmp_file_name(void);
  292. DEFUN("tmp-file-name", cmd_tmp_file_name, subr_tmp_file_name, (void), V_Subr0, DOC_tmp_file_name) /*
  293. ::doc:tmp_file_name::
  294. tmp-file-name
  295.  
  296. Returns the name of a unique file.
  297. ::end:: */
  298. {
  299.     return(string_dup(tmpnam(NULL)));
  300. }
  301.  
  302. _PR VALUE cmd_make_completion_string(VALUE args);
  303. DEFUN("make-completion-string", cmd_make_completion_string, subr_make_completion_string, (VALUE args), V_SubrN, DOC_make_completion_string) /*
  304. ::doc:make_completion_string::
  305. make-completion-string EXISTING [POSSIBLE | POSIIBLE...]
  306. ::end:: */
  307. {
  308.     u_char *orig, *match = NULL;
  309.     int matchlen = 0, origlen;
  310.