PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.sbin/lpr/chkprintcap/skimprintcap.c

https://bitbucket.org/freebsd/freebsd-head/
C | 260 lines | 156 code | 26 blank | 78 comment | 47 complexity | d2a7333ad20f6419927067a3785cf781 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * ------+---------+---------+---------+---------+---------+---------+---------*
  3. * Copyright (c) 2001 - Garance Alistair Drosehn <gad@FreeBSD.org>.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * The views and conclusions contained in the software and documentation
  28. * are those of the authors and should not be interpreted as representing
  29. * official policies, either expressed or implied, of the FreeBSD Project.
  30. *
  31. * ------+---------+---------+---------+---------+---------+---------+---------*
  32. */
  33. #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
  34. __FBSDID("$FreeBSD$");
  35. #include <sys/types.h>
  36. #include <ctype.h>
  37. #include <err.h>
  38. #include <errno.h>
  39. #include <grp.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <sys/param.h> /* needed for lp.h but not used here */
  45. #include <dirent.h> /* ditto */
  46. #include "lp.h"
  47. #include "lp.local.h"
  48. #include "skimprintcap.h"
  49. /*
  50. * Save the canonical queue name of the entry that is currently being
  51. * scanned, in case a warning message is printed for the current queue.
  52. * Only the first 'QENTRY_MAXLEN' characters will be saved, since this
  53. * is only for warning messages. The variable includes space for the
  54. * string " (entry " and a trailing ")", when the scanner is in the
  55. * middle of an entry. When the scanner is not in a specific entry,
  56. * the variable will be the a null string.
  57. */
  58. #define QENTRY_MAXLEN 30
  59. #define QENTRY_PREFIX " (entry "
  60. static char skim_entryname[sizeof(QENTRY_PREFIX) + QENTRY_MAXLEN + 2];
  61. /*
  62. * isgraph is defined to work on an 'int', in the range 0 to 255, plus EOF.
  63. * Define a wrapper which can take 'char', either signed or unsigned.
  64. */
  65. #define isgraphch(Anychar) isgraph(((int) Anychar) & 255)
  66. struct skiminfo *
  67. skim_printcap(const char *pcap_fname, int verbosity)
  68. {
  69. struct skiminfo *skinf;
  70. char buff[BUFSIZ];
  71. char *ch, *curline, *endfield, *lastchar;
  72. FILE *pc_file;
  73. int missing_nl;
  74. enum {NO_CONTINUE, WILL_CONTINUE, BAD_CONTINUE} is_cont, had_cont;
  75. enum {CMNT_LINE, ENTRY_LINE, TAB_LINE, TABERR_LINE} is_type, had_type;
  76. skinf = malloc(sizeof(struct skiminfo));
  77. memset(skinf, 0, sizeof(struct skiminfo));
  78. pc_file = fopen(pcap_fname, "r");
  79. if (pc_file == NULL) {
  80. warn("fopen(%s)", pcap_fname);
  81. skinf->fatalerr++;
  82. return (skinf); /* fatal error */
  83. }
  84. skim_entryname[0] = '0';
  85. is_cont = NO_CONTINUE;
  86. is_type = CMNT_LINE;
  87. errno = 0;
  88. curline = fgets(buff, sizeof(buff), pc_file);
  89. while (curline != NULL) {
  90. skinf->lines++;
  91. /* Check for the expected newline char, and remove it */
  92. missing_nl = 0;
  93. lastchar = strchr(curline, '\n');
  94. if (lastchar != NULL)
  95. *lastchar = '\0';
  96. else {
  97. lastchar = strchr(curline, '\0');
  98. missing_nl = 1;
  99. }
  100. if (curline < lastchar)
  101. lastchar--;
  102. /*
  103. * Check for `\' (continuation-character) at end of line.
  104. * If there is none, then trim off spaces and check again.
  105. * This would be a bad line because it looks like it is
  106. * continued, but it will not be treated that way.
  107. */
  108. had_cont = is_cont;
  109. is_cont = NO_CONTINUE;
  110. if (*lastchar == '\\') {
  111. is_cont = WILL_CONTINUE;
  112. lastchar--;
  113. } else {
  114. while ((curline < lastchar) && !isgraphch(*lastchar))
  115. lastchar--;
  116. if (*lastchar == '\\')
  117. is_cont = BAD_CONTINUE;
  118. }
  119. had_type = is_type;
  120. is_type = CMNT_LINE;
  121. switch (*curline) {
  122. case '\0': /* treat zero-length line as comment */
  123. case '#':
  124. skinf->comments++;
  125. break;
  126. case ' ':
  127. case '\t':
  128. is_type = TAB_LINE;
  129. break;
  130. default:
  131. is_type = ENTRY_LINE;
  132. skinf->entries++;
  133. /* pick up the queue name, to use in warning messages */
  134. ch = curline;
  135. while ((ch <= lastchar) && (*ch != ':') && (*ch != '|'))
  136. ch++;
  137. ch--; /* last char of queue name */
  138. strcpy(skim_entryname, QENTRY_PREFIX);
  139. if ((ch - curline) > QENTRY_MAXLEN) {
  140. strncat(skim_entryname, curline, QENTRY_MAXLEN
  141. - 1);
  142. strcat(skim_entryname, "+");
  143. } else {
  144. strncat(skim_entryname, curline, (ch - curline
  145. + 1));
  146. }
  147. strlcat(skim_entryname, ")", sizeof(skim_entryname));
  148. break;
  149. }
  150. /*
  151. * Check to see if the previous line was a bad contination
  152. * line. The check is delayed until now so a warning message
  153. * is not printed when a "bad continuation" is on a comment
  154. * line, and it just "continues" into another comment line.
  155. */
  156. if (had_cont == BAD_CONTINUE) {
  157. if ((had_type != CMNT_LINE) || (is_type != CMNT_LINE) ||
  158. (verbosity > 1)) {
  159. skinf->warnings++;
  160. warnx("Warning: blanks after trailing '\\',"
  161. " at line %d%s", skinf->lines - 1,
  162. skim_entryname);
  163. }
  164. }
  165. /* If we are no longer in an entry, then forget the name */
  166. if ((had_cont != WILL_CONTINUE) && (is_type != ENTRY_LINE)) {
  167. skim_entryname[0] = '\0';
  168. }
  169. /*
  170. * Print out warning for missing newline, done down here
  171. * so we are sure to have the right entry-name for it.
  172. */
  173. if (missing_nl) {
  174. skinf->warnings++;
  175. warnx("Warning: No newline at end of line %d%s",
  176. skinf->lines, skim_entryname);
  177. }
  178. /*
  179. * Check for start-of-entry lines which do not include a
  180. * ":" character (to indicate the end of the name field).
  181. * This can cause standard printcap processing to ignore
  182. * ALL of the following lines.
  183. * XXXXX - May need to allow for the list-of-names to
  184. * continue on to the following line...
  185. */
  186. if (is_type == ENTRY_LINE) {
  187. endfield = strchr(curline, ':');
  188. if (endfield == NULL) {
  189. skinf->warnings++;
  190. warnx("Warning: No ':' to terminate name-field"
  191. " at line %d%s", skinf->lines,
  192. skim_entryname);
  193. }
  194. }
  195. /*
  196. * Now check for cases where this line is (or is-not) a
  197. * continuation of the previous line, and a person skimming
  198. * the file would assume it is not (or is) a continuation.
  199. */
  200. switch (had_cont) {
  201. case NO_CONTINUE:
  202. case BAD_CONTINUE:
  203. if (is_type == TAB_LINE) {
  204. skinf->warnings++;
  205. warnx("Warning: values-line after line with"
  206. " NO trailing '\\', at line %d%s",
  207. skinf->lines, skim_entryname);
  208. }
  209. break;
  210. case WILL_CONTINUE:
  211. if (is_type == ENTRY_LINE) {
  212. skinf->warnings++;
  213. warnx("Warning: new entry starts after line"
  214. " with trailing '\\', at line %d%s",
  215. skinf->lines, skim_entryname);
  216. }
  217. break;
  218. }
  219. /* get another line from printcap and repeat loop */
  220. curline = fgets(buff, sizeof(buff), pc_file);
  221. }
  222. if (errno != 0) {
  223. warn("fgets(%s)", pcap_fname);
  224. skinf->fatalerr++; /* fatal error */
  225. }
  226. if (skinf->warnings > 0)
  227. warnx("%4d warnings from skimming %s", skinf->warnings,
  228. pcap_fname);
  229. if (verbosity)
  230. warnx("%4d lines (%d comments), %d entries for %s",
  231. skinf->lines, skinf->comments, skinf->entries, pcap_fname);
  232. fclose(pc_file);
  233. return (skinf);
  234. }