PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/textproc/nbsed/files/main.c

https://github.com/Morgawr/minix-pkgsrc
C | 413 lines | 275 code | 30 blank | 108 comment | 59 complexity | f807f30edabe9107cc51b85403a98bc6 MD5 | raw file
  1. /* $NetBSD: main.c,v 1.16 2004/07/13 12:11:06 wiz Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Diomidis Spinellis of Imperial College, University of London.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. /*-
  34. * Copyright (c) 1992 Diomidis Spinellis.
  35. *
  36. * This code is derived from software contributed to Berkeley by
  37. * Diomidis Spinellis of Imperial College, University of London.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. All advertising materials mentioning features or use of this software
  48. * must display the following acknowledgement:
  49. * This product includes software developed by the University of
  50. * California, Berkeley and its contributors.
  51. * 4. Neither the name of the University nor the names of its contributors
  52. * may be used to endorse or promote products derived from this software
  53. * without specific prior written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  56. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  59. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  61. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  62. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  65. * SUCH DAMAGE.
  66. */
  67. #if HAVE_CONFIG_H
  68. #include "config.h"
  69. #endif
  70. #include <nbcompat.h>
  71. #if HAVE_SYS_CDEFS_H
  72. #include <sys/cdefs.h>
  73. #endif
  74. #ifndef lint
  75. __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
  76. The Regents of the University of California. All rights reserved.\n");
  77. #endif /* not lint */
  78. #ifndef lint
  79. #if 0
  80. static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
  81. #else
  82. __RCSID("$NetBSD: main.c,v 1.16 2004/07/13 12:11:06 wiz Exp $");
  83. #endif
  84. #endif /* not lint */
  85. #if HAVE_SYS_TYPES_H
  86. #include <sys/types.h>
  87. #endif
  88. #if HAVE_CTYPE_H
  89. #include <ctype.h>
  90. #endif
  91. #if HAVE_ERRNO_H
  92. #include <errno.h>
  93. #endif
  94. #if HAVE_FCNTL_H
  95. #include <fcntl.h>
  96. #endif
  97. #if HAVE_REGEX_H
  98. #include <regex.h>
  99. #endif
  100. #if HAVE_STDDEF_H
  101. #include <stddef.h>
  102. #endif
  103. #if HAVE_STDIO_H
  104. #include <stdio.h>
  105. #endif
  106. #if HAVE_STDLIB_H
  107. #include <stdlib.h>
  108. #endif
  109. #if HAVE_STRING_H
  110. #include <string.h>
  111. #endif
  112. #if HAVE_UNISTD_H
  113. #include <unistd.h>
  114. #endif
  115. #include "defs.h"
  116. #include "extern.h"
  117. /*
  118. * Linked list of units (strings and files) to be compiled
  119. */
  120. struct s_compunit {
  121. struct s_compunit *next;
  122. enum e_cut {CU_FILE, CU_STRING} type;
  123. char *s; /* Pointer to string or fname */
  124. };
  125. /*
  126. * Linked list pointer to compilation units and pointer to current
  127. * next pointer.
  128. */
  129. static struct s_compunit *script, **cu_nextp = &script;
  130. /*
  131. * Linked list of files to be processed
  132. */
  133. struct s_flist {
  134. char *fname;
  135. struct s_flist *next;
  136. };
  137. /*
  138. * Linked list pointer to files and pointer to current
  139. * next pointer.
  140. */
  141. static struct s_flist *files, **fl_nextp = &files;
  142. int aflag, eflag, nflag, ere;
  143. /*
  144. * Current file and line number; line numbers restart across compilation
  145. * units, but span across input files.
  146. */
  147. char *fname; /* File name. */
  148. u_long linenum;
  149. int lastline; /* TRUE on the last line of the last file */
  150. static void add_compunit(enum e_cut, char *);
  151. static void add_file(char *);
  152. int main(int, char **);
  153. int
  154. main(int argc, char *argv[])
  155. {
  156. int c, fflag;
  157. setprogname(*argv);
  158. fflag = 0;
  159. while ((c = getopt(argc, argv, "ae:f:nE")) != -1)
  160. switch (c) {
  161. case 'a':
  162. aflag = 1;
  163. break;
  164. case 'e':
  165. eflag = 1;
  166. add_compunit(CU_STRING, optarg);
  167. break;
  168. case 'f':
  169. fflag = 1;
  170. add_compunit(CU_FILE, optarg);
  171. break;
  172. case 'n':
  173. nflag = 1;
  174. break;
  175. case 'E':
  176. ere = REG_EXTENDED;
  177. break;
  178. default:
  179. case '?':
  180. (void)fprintf(stderr,
  181. "usage:\t%s [-aEn] script [file ...]\n\t%s [-aEn] [-e script] ... [-f script_file] ... [file ...]\n",
  182. getprogname(), getprogname());
  183. exit(1);
  184. }
  185. argc -= optind;
  186. argv += optind;
  187. /* First usage case; script is the first arg */
  188. if (!eflag && !fflag && *argv) {
  189. add_compunit(CU_STRING, *argv);
  190. argv++;
  191. }
  192. compile();
  193. /* Continue with first and start second usage */
  194. if (*argv)
  195. for (; *argv; argv++)
  196. add_file(*argv);
  197. else
  198. add_file(NULL);
  199. process();
  200. cfclose(prog, NULL);
  201. if (fclose(stdout))
  202. err(FATAL, "stdout: %s", strerror(errno));
  203. exit (0);
  204. }
  205. /*
  206. * Like fgets, but go through the chain of compilation units chaining them
  207. * together. Empty strings and files are ignored.
  208. */
  209. char *
  210. cu_fgets(char *buf, int n)
  211. {
  212. static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
  213. static FILE *f; /* Current open file */
  214. static char *s; /* Current pointer inside string */
  215. static char string_ident[30];
  216. char *p;
  217. again:
  218. switch (state) {
  219. case ST_EOF:
  220. if (script == NULL)
  221. return (NULL);
  222. linenum = 0;
  223. switch (script->type) {
  224. case CU_FILE:
  225. if ((f = fopen(script->s, "r")) == NULL)
  226. err(FATAL,
  227. "%s: %s", script->s, strerror(errno));
  228. fname = script->s;
  229. state = ST_FILE;
  230. goto again;
  231. case CU_STRING:
  232. if ((snprintf(string_ident,
  233. sizeof(string_ident), "\"%s\"", script->s)) >=
  234. sizeof(string_ident) - 1)
  235. (void)strcpy(string_ident +
  236. sizeof(string_ident) - 6, " ...\"");
  237. fname = string_ident;
  238. s = script->s;
  239. state = ST_STRING;
  240. goto again;
  241. }
  242. case ST_FILE:
  243. if ((p = fgets(buf, n, f)) != NULL) {
  244. linenum++;
  245. if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
  246. nflag = 1;
  247. return (p);
  248. }
  249. script = script->next;
  250. (void)fclose(f);
  251. state = ST_EOF;
  252. goto again;
  253. case ST_STRING:
  254. if (linenum == 0 && s[0] == '#' && s[1] == 'n')
  255. nflag = 1;
  256. p = buf;
  257. for (;;) {
  258. if (n-- <= 1) {
  259. *p = '\0';
  260. linenum++;
  261. return (buf);
  262. }
  263. switch (*s) {
  264. case '\0':
  265. state = ST_EOF;
  266. if (s == script->s) {
  267. script = script->next;
  268. goto again;
  269. } else {
  270. script = script->next;
  271. *p = '\0';
  272. linenum++;
  273. return (buf);
  274. }
  275. case '\n':
  276. *p++ = '\n';
  277. *p = '\0';
  278. s++;
  279. linenum++;
  280. return (buf);
  281. default:
  282. *p++ = *s++;
  283. }
  284. }
  285. }
  286. /* NOTREACHED */
  287. return (NULL);
  288. }
  289. /*
  290. * Like fgets, but go through the list of files chaining them together.
  291. * Set len to the length of the line.
  292. */
  293. int
  294. mf_fgets(SPACE *sp, enum e_spflag spflag)
  295. {
  296. static FILE *f; /* Current open file */
  297. size_t len;
  298. char *p;
  299. int c;
  300. if (f == NULL)
  301. /* Advance to first non-empty file */
  302. for (;;) {
  303. if (files == NULL) {
  304. lastline = 1;
  305. return (0);
  306. }
  307. if (files->fname == NULL) {
  308. f = stdin;
  309. fname = "stdin";
  310. } else {
  311. fname = files->fname;
  312. if ((f = fopen(fname, "r")) == NULL)
  313. err(FATAL, "%s: %s",
  314. fname, strerror(errno));
  315. }
  316. if ((c = getc(f)) != EOF) {
  317. (void)ungetc(c, f);
  318. break;
  319. }
  320. (void)fclose(f);
  321. files = files->next;
  322. }
  323. if (lastline) {
  324. sp->len = 0;
  325. return (0);
  326. }
  327. /*
  328. * Use fgetln so that we can handle essentially infinite input data.
  329. * Can't use the pointer into the stdio buffer as the process space
  330. * because the ungetc() can cause it to move.
  331. */
  332. p = fgetln(f, &len);
  333. if (ferror(f))
  334. err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
  335. cspace(sp, p, len, spflag);
  336. linenum++;
  337. /* Advance to next non-empty file */
  338. while ((c = getc(f)) == EOF) {
  339. (void)fclose(f);
  340. files = files->next;
  341. if (files == NULL) {
  342. lastline = 1;
  343. return (1);
  344. }
  345. if (files->fname == NULL) {
  346. f = stdin;
  347. fname = "stdin";
  348. } else {
  349. fname = files->fname;
  350. if ((f = fopen(fname, "r")) == NULL)
  351. err(FATAL, "%s: %s", fname, strerror(errno));
  352. }
  353. }
  354. (void)ungetc(c, f);
  355. return (1);
  356. }
  357. /*
  358. * Add a compilation unit to the linked list
  359. */
  360. static void
  361. add_compunit(enum e_cut type, char *s)
  362. {
  363. struct s_compunit *cu;
  364. cu = xmalloc(sizeof(struct s_compunit));
  365. cu->type = type;
  366. cu->s = s;
  367. cu->next = NULL;
  368. *cu_nextp = cu;
  369. cu_nextp = &cu->next;
  370. }
  371. /*
  372. * Add a file to the linked list
  373. */
  374. static void
  375. add_file(char *s)
  376. {
  377. struct s_flist *fp;
  378. fp = xmalloc(sizeof(struct s_flist));
  379. fp->next = NULL;
  380. *fl_nextp = fp;
  381. fp->fname = s;
  382. fl_nextp = &fp->next;
  383. }