Debian Codegraph @

utils/autosp/autosp-src/utils.c

raw
1 /* Copyright (C) 2014-15 R. D. Tennent School of Computing,
2 * Queen's University, [email protected]
3 *
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General
6 * Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will
11 * be useful, but WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General
17 * Public License along with this program; if not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 /* utils.c - utilities for autosp, fixmsxpart.c and msxlint.c
23 */
24
25 #ifdef HAVE_CONFIG_H /* for TeXLive */
26 #include <config.h>
27 #endif
28
29 # define PRIVATE static
30 # include <stdlib.h>
31 # include <string.h>
32 # include <stdio.h>
33 # include <ctype.h>
34
35 # ifdef KPATHSEA
36 # include <kpathsea/getopt.h>
37 # else
38 # include <getopt.h>
39 # endif
40 # include <time.h>
41
42 # define LINE_LEN 1024
43
44 # ifdef HAVE_STDBOOL_H
45 # include <stdbool.h>
46 # else
47 # ifndef HAVE__BOOL
48 # define _Bool signed char
49 # endif
50 # define bool _Bool
51 # define true 1
52 # define false 0
53 # endif
54
55 extern size_t append (char *dst, char **offset, const char *src, size_t n);
56 /* Copies src to *offset and updates *offset accordingly (if possible).
57 * Assumes *offset is dst if offset == NULL.
58 * The src string must be null-terminated.
59 * Execution aborts unless **offset == '\0'.
60 * Returns (original offset - dst) + strlen(src); if >= n, the string was truncated.
61 */
62
63 extern bool prefix (const char *cs, const char *ct); /* is string cs[] a prefix of ct[]? */
64 extern bool suffix (const char *cs, const char *ct); /* is string cs[] a suffix of ct[]? */
65
66 int lineno;
67
68 extern void error (const char msg[]); /* abort with stderr message msg */
69 extern void warning (const char msg[]); /* output warning message msg to stderr */
70
71 void
72 warning (const char msg[]) /* output warning message msg to stderr */
73 {
74 fprintf (stderr, "Warning: %s\n", msg);
75 return;
76 }
77
78 void
79 error (const char msg[]) /* abort with stderr message msg */
80 {
81 if (lineno > 0)
82 fprintf (stderr, "Error on line %d: %s\n", lineno, msg);
83 else
84 fprintf (stderr, "Error: %s\n", msg);
85 exit (EXIT_FAILURE);
86 }
87
88 bool
89 prefix (const char *cs, const char *ct)
90 /*
91 is string cs[] a prefix of ct[]?
92 */
93 {
94 const char *pcs = cs;
95 const char *pct = ct;
96 while (*pcs != '\0')
97 {
98 if (*pcs != *pct) return false;
99 pcs++; pct++;
100 }
101 return true;
102 }
103
104 bool
105 suffix (const char *cs, const char *ct)
106 /* is string cs[] a suffix of ct[]? */
107 {
108 const char *pcs = cs;
109 const char *pct = ct + strlen (ct) - strlen (cs);
110 while (*pcs != '\0')
111 {
112 if (*pct != *pcs) return false;
113 pcs++; pct++;
114 }
115 return true;
116 }
117
118 size_t
119 append (char *dst, char **offset, const char *src, size_t n)
120 /* Copies src to *offset and updates *offset accordingly (if possible).
121 * Assumes *offset is dst if offset == NULL.
122 * Execution aborts if **offset != '\0'. The src string must be null-terminated.
123 * Returns (original offset - dst) + strlen(src); if >= n, the string was truncated.
124 */
125 {
126 const char *s = src;
127 char *d;
128 char *off_orig = ( offset != NULL ? *offset : dst );
129 d = off_orig;
130 if (*d != '\0')
131 {
132 char msg[LINE_LEN + 24] = {'\0'};
133 char *msg_n = msg;
134 append (msg, &msg_n, "\nNull character expected at offset specified for string ", sizeof (msg));
135 append (msg, &msg_n, src, sizeof (msg));
136 error (msg);
137 }
138 n = n - 1 - (off_orig - dst); /* number of available slots (leaving room for \0) */
139 while (n > 0 && *s != '\0')
140 {
141 *d = *s; /* copy non-null character */
142 d++; s++; n--;
143 }
144 *d = '\0'; /* null-terminate */
145 if (offset != NULL)
146 *offset = d; /* update *offset (if possible) */
147 while (*s != '\0') /* traverse rest of src */
148 s++;
149 return (off_orig - dst) + (s - src); /* length of the untruncated string */
150 }