grep2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* file grep2.c */
  2. /* Signature: 733176dd 27-Jan-1997 */
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include "machine.h"
  7. #include "tags.h"
  8. #include "cslerror.h"
  9. #include "externs.h"
  10. #include "entries.h"
  11. #include "arith.h"
  12. /* Defined in grep.c */
  13. extern void zsfinit_(int32 *mode);
  14. extern void zscpat_(char *pat, int32 *cse, int32 *cpat, int32 *maxpat,
  15. int32 *ifail, int32 len);
  16. extern int32 zsfind_(char *line, int32 *start, int32 *finish, int32 *cpat,
  17. int32 len);
  18. /*
  19. * I really do need to be able to read arbitrary sized lines because the
  20. * AXIOM databases contain some quite long ones!
  21. *
  22. */
  23. /* The number of characters we will malloc at a time */
  24. #define LINE_CHUNK_LENGTH 127
  25. typedef struct LineChunk
  26. {
  27. char s[LINE_CHUNK_LENGTH+1];
  28. struct LineChunk *next;
  29. } LineChunk;
  30. LineChunk *newChunk()
  31. {
  32. LineChunk *new;
  33. new = (LineChunk *)malloc(sizeof(LineChunk));
  34. new->next = NULL;
  35. return(new);
  36. }
  37. int getChunk(char *buffer, FILE *fp)
  38. {
  39. int32 i=0;
  40. char c;
  41. c = getc(fp);
  42. /* In theory could compare c to EOF but this probably isn't very safe */
  43. while (c != '\n' && !feof(fp) && i < LINE_CHUNK_LENGTH) {
  44. buffer[i] = c;
  45. ++i;
  46. c = getc(fp);
  47. }
  48. buffer[i] = '\0';
  49. if (i == LINE_CHUNK_LENGTH) {
  50. ungetc(c,fp);
  51. return (0);
  52. }
  53. else
  54. return (1);
  55. }
  56. char *concatenateChunks(LineChunk *l, int32 len)
  57. {
  58. char *s;
  59. LineChunk *prev;
  60. if (len <= LINE_CHUNK_LENGTH)
  61. return(l->s);
  62. else {
  63. s = (char *)malloc((size_t) len);
  64. s[0] = '\0'; /* Otherwise strcat might get confused! */
  65. while (l != NULL) {
  66. strcat(s,l->s);
  67. prev = l;
  68. l = l->next;
  69. free(prev);
  70. }
  71. return(s);
  72. }
  73. }
  74. char *read_a_line(FILE *fp)
  75. {
  76. LineChunk *start, *place;
  77. int32 chunk_count=1;
  78. char *str;
  79. start = newChunk();
  80. place = start;
  81. while (!getChunk(place->s, fp) ) {
  82. place->next = newChunk();
  83. place = place->next;
  84. ++chunk_count;
  85. }
  86. /* The +1 in the size is for the terminator */
  87. str = concatenateChunks(start,
  88. (chunk_count-1)*LINE_CHUNK_LENGTH+strlen(place->s)+1);
  89. return(str);
  90. }
  91. #define MAXIMUM_PATTERN_SIZE 512
  92. /*
  93. * This function takes a filename or a list of strings and returns
  94. * lines which match pattern
  95. * which uses egrep syntax. The flag casep says whether to be
  96. * case sensitive in matching, and the flag matchp says whether to return
  97. * a line or not if the pattern matches (i.e. it behaves like the "-v"
  98. * option to grep et al).
  99. */
  100. Lisp_Object MS_CDECL Lgrep(Lisp_Object nil, int nargs, ...)
  101. {
  102. va_list args;
  103. Lisp_Object name, pattern, casep, matchp, result;
  104. FILE *fp;
  105. char filename[LONGEST_LEGAL_FILENAME], *pat, *line;
  106. int32 len, case_sensitive, match, mode, ifail=0, max_patternsize,
  107. compiled_pattern[MAXIMUM_PATTERN_SIZE], result_is_null=1;
  108. Header h;
  109. extern char *get_string_data(Lisp_Object name, char *why, int32 *l);
  110. extern int32 zsinit_(int32 *mode);
  111. /* Set up arguments as Lisp Objects */
  112. argcheck(nargs,4,"Lgrep");
  113. va_start(args,nargs);
  114. name = va_arg(args, Lisp_Object);
  115. pattern = va_arg(args, Lisp_Object);
  116. casep = va_arg(args, Lisp_Object);
  117. matchp = va_arg(args, Lisp_Object);
  118. va_end(args);
  119. /* Translate arguments into C objects */
  120. if (!is_vector(pattern) || !(type_of_header(vechdr(pattern)) == TYPE_STRING) )
  121. return aerror("grep");
  122. pat = get_string_data(pattern,"grep",&len);
  123. if (casep == lisp_true)
  124. case_sensitive = 1;
  125. else
  126. case_sensitive = 0;
  127. if (matchp == lisp_true)
  128. match = 1;
  129. else
  130. match = 0;
  131. /* Initialise the matcher to egrep mode */
  132. mode = 2;
  133. zsinit_(&mode);
  134. /* Compile the pattern */
  135. max_patternsize=MAXIMUM_PATTERN_SIZE;
  136. zscpat_(pat,&case_sensitive,compiled_pattern,&max_patternsize,
  137. &ifail,len);
  138. if (ifail != 0) return aerror1("Invalid regular expression", pattern);
  139. /* Test to see whether we are dealing with a list of strings or a file */
  140. if (is_cons(name))
  141. return grepList(nil, name, case_sensitive, match, compiled_pattern);
  142. /* Sort out the filename */
  143. #ifdef COMMON
  144. if (complex_stringp(name))
  145. { name = simplify_string(name);
  146. errexit();
  147. h = vechdr(name);
  148. }
  149. else
  150. #endif
  151. if (symbolp(name))
  152. { name = get_pname(name);
  153. errexit();
  154. h = vechdr(name);
  155. }
  156. else if (!(is_vector(name))) return aerror("grep");
  157. else if (type_of_header(h = vechdr(name)) != TYPE_STRING)
  158. return aerror("grep");
  159. len = length_of_header(h) - 4;
  160. if (len >= sizeof(filename)) len = sizeof(filename);
  161. /* Check whether file is readable using method desfined in SYS.c */
  162. if (!file_readable(filename,
  163. (char *)name + (4 - TAG_VECTOR),
  164. (size_t)len) ) return aerror1("Cannot read",name);
  165. /* Open file */
  166. fp = fopen(filename,"r");
  167. if (!fp) return aerror("Error opening file in grep - should not happen!");
  168. while (!feof(fp)) {
  169. line = read_a_line(fp);
  170. if (match == zsfind_(line,&ifail,&ifail,compiled_pattern,strlen(line))){
  171. if (result_is_null) {
  172. result = Lncons(nil,make_string(line));
  173. result_is_null = 0;
  174. }
  175. else
  176. result = Lcons(nil,make_string(line),result);
  177. }
  178. else
  179. free((void *)line);
  180. }
  181. fclose(fp);
  182. if (result_is_null)
  183. return(nil);
  184. else
  185. return(Lnreverse(nil,result));
  186. }
  187. Lisp_Object grepList(Lisp_Object nil, Lisp_Object string_list,
  188. int32 case_sensitive, int32 match,
  189. int32 *compiled_pattern)
  190. {
  191. Lisp_Object lstring, result;
  192. char *str;
  193. int32 ifail=0, result_is_null=1;
  194. while (string_list != nil) {
  195. lstring = Lcar(nil,string_list);
  196. if (!is_vector(lstring) || type_of_header(vechdr(lstring)) != TYPE_STRING)
  197. aerror1("error in grep, non-string in list",lstring);
  198. str=&celt(lstring,0);
  199. if (match == zsfind_(str,&ifail,&ifail,compiled_pattern,strlen(str)))
  200. if (result_is_null) {
  201. result = Lncons(nil,lstring);
  202. result_is_null = 0;
  203. }
  204. else
  205. result = Lcons(nil,lstring,result);
  206. string_list = Lcdr(nil,string_list);
  207. }
  208. if (result_is_null)
  209. return(nil);
  210. else
  211. return(Lnreverse(nil,result));
  212. }
  213. setup_type const grep_setup[] =
  214. {
  215. {"grep", wrong_no_na, wrong_no_nb, Lgrep},
  216. {NULL, 0, 0, 0}
  217. };
  218. /* end of grep2.c */