Skip to content

Commit 1bd1066

Browse files
committed
[sfntedit] Fix exit codes
Fixes #348
1 parent 166fd3c commit 1bd1066

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

c/sfntedit/source/main.c

+20-24
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void cleanup(int code) {
185185

186186
/* Print usage information */
187187
static void printUsage(void) {
188-
fprintf(stderr,
188+
fprintf(stdout,
189189
"Usage:\n"
190190
" %s [options] <srcfile> [<dstfile>]\n"
191191
"OR: %s -X <scriptfile>\n\n"
@@ -209,63 +209,62 @@ static void printUsage(void) {
209209
/* Show usage information */
210210
static void showUsage(void) {
211211
printUsage();
212-
quit(0);
213212
}
214213

215214
/* Show usage and help information */
216215
static void showHelp(void) {
217216
printUsage();
218-
fprintf(stderr,
217+
fprintf(stdout,
219218
"Notes:\n"
220219
" This program supports table-editing, listing, and checksumming options\n"
221220
"on sfnt-formatted files such as OpenType Format (OTF) or TrueType. The\n"
222221
"mandatory source file is specified as an argument to the program. An\n"
223222
"optional destination file may also be specified which receives the edited\n"
224223
"data otherwise the source data is edited in-place thus modifying the source\n");
225-
fprintf(stderr,
224+
fprintf(stdout,
226225
"file. In-place editing is achieved by the use of a temporary file called\n"
227226
"%s that is created in the directory of execution (requiring you\n"
228227
"to have write permission to that directory).\n"
229228
" The target table of an editing option (-x, -d, and -a) is specified\n"
230229
"with a table tag argument that is nominally 4 characters long. If fewer\n"
231230
"than 4 characters are specified the tag is padded with spaces (more than 4\n",
232231
tmpname);
233-
fprintf(stderr,
232+
fprintf(stdout,
234233
"characters is a fatal error). Multiple tables may be specified as a single\n"
235234
"argument composed from a comma-separated list of tags.\n"
236235
" The extract option (-x) copies the table data into a file whose default\n"
237236
"name is the concatenation of the source filename (less its .otf or .ttf\n"
238237
"extension), a period character (.), and the table tag. If the tag contains\n"
239238
"non-alphanumeric characters they are replaced by underscore characters (_)\n");
240-
fprintf(stderr,
239+
fprintf(stdout,
241240
"and finally trailing underscores are removed. The default filename may be\n"
242241
"overridden by appending an equals character (=) followed by an alternate\n"
243242
"filename to the table tag argument. The delete option (-d) deletes a table.\n"
244243
"Unlike the -x option no files may be specified in the table tag list. The\n"
245244
"add option (-a) adds a table or replaces one if the table already exists.\n"
246245
"The source file containing the table data is specified by appending an\n");
247-
fprintf(stderr,
246+
fprintf(stdout,
248247
"equals character (=) followed by a filename to the table tag.\n"
249248
" The 3 editing options may be specified together as acting on the same\n"
250249
"table. In such cases the -x option is applied before the -d option which is\n"
251250
"applied before the -a option. (The -d option applied to the same table as a\n"
252251
"subsequent -a option is permitted but redundant.) The -d and -a options\n"
253252
"change the contents of the sfnt and cause the table checksums and the head\n");
254-
fprintf(stderr,
253+
fprintf(stdout,
255254
"table's checksum adjustment field to be recomputed.\n"
256255
" The list option (-l) simply lists the contents of the sfnt table\n"
257256
"directory. This is the default action if no other options are specified.\n"
258257
"The check checksum option (-c) performs a check of all the table checksums\n"
259258
"and the head table's checksum adjustment field and reports any errors. The\n"
260259
"fix checksum option (-f) fixes any checksum errors.\n");
261-
fprintf(stderr,
260+
fprintf(stdout,
262261
" The -d, -a, and -f options create a new sfnt file by copying tables\n"
263262
"from the source file to the destination file. The tables are copied in the\n"
264263
"order recommended in the OpenType specification. A side effect of copying\n"
265264
"is that all table information including checksums and sfnt search fields\n"
266265
"is recalculated.\n"
267266
"Examples:\n");
268-
fprintf(stderr,
267+
fprintf(stdout,
269268
"o Extract GPOS and GSUB tables to files minion.GPOS and minion.GSUB.\n"
270269
" sfntedit -x GPOS,GSUB minion.otf\n"
271270
"o Add tables extracted previously to different font.\n"
@@ -274,7 +273,6 @@ static void showHelp(void) {
274273
" sfntedit -d TR01,TR02,TR03 pala.ttf\n"
275274
"o Copy font to new file fixing checksums and reordering tables.\n"
276275
" sfntedit -f helv.ttf newhelv.ttf\n");
277-
quit(0);
278276
}
279277

280278
static void makeArgs(char *filename) {
@@ -525,10 +523,10 @@ static int parseArgs(int argc, char *argv[]) {
525523
break;
526524
case 'u':
527525
showUsage();
528-
break;
526+
exit(0);
529527
case 'h':
530528
showHelp();
531-
break;
529+
exit(0);
532530
default:
533531
fatal(SFED_MSG_UNRECOGOPT, arg);
534532
}
@@ -635,21 +633,21 @@ static void sfntReadHdr(void) {
635633
static void sfntDumpHdr(void) {
636634
int i;
637635

638-
fprintf(stderr, "--- sfnt header [%s]\n", srcfile.name);
636+
fprintf(stdout, "--- sfnt header [%s]\n", srcfile.name);
639637
if (sfnt.version == 0x00010000)
640-
fprintf(stderr, "version =1.0 (00010000)\n");
638+
fprintf(stdout, "version =1.0 (00010000)\n");
641639
else
642-
fprintf(stderr, "version =%c%c%c%c (%08x)\n",
640+
fprintf(stdout, "version =%c%c%c%c (%08x)\n",
643641
TAG_ARG(sfnt.version), sfnt.version);
644-
fprintf(stderr, "numTables =%hu\n", sfnt.numTables);
645-
fprintf(stderr, "searchRange =%hu\n", sfnt.searchRange);
646-
fprintf(stderr, "entrySelector=%hu\n", sfnt.entrySelector);
647-
fprintf(stderr, "rangeShift =%hu\n", sfnt.rangeShift);
642+
fprintf(stdout, "numTables =%hu\n", sfnt.numTables);
643+
fprintf(stdout, "searchRange =%hu\n", sfnt.searchRange);
644+
fprintf(stdout, "entrySelector=%hu\n", sfnt.entrySelector);
645+
fprintf(stdout, "rangeShift =%hu\n", sfnt.rangeShift);
648646

649-
fprintf(stderr, "--- table directory [index]={tag,checksum,offset,length}\n");
647+
fprintf(stdout, "--- table directory [index]={tag,checksum,offset,length}\n");
650648
for (i = 0; i < sfnt.numTables; i++) {
651649
Table *tbl = &sfnt.directory[i];
652-
fprintf(stderr, "[%2d]={%c%c%c%c,%08x,%08x,%08x}\n", i,
650+
fprintf(stdout, "[%2d]={%c%c%c%c,%08x,%08x,%08x}\n", i,
653651
TAG_ARG(tbl->tag), tbl->checksum, tbl->offset, tbl->length);
654652
}
655653
}
@@ -1245,7 +1243,5 @@ int main(int argc, char *argv[]) {
12451243

12461244
doingScripting = 0;
12471245
}
1248-
1249-
fprintf(stdout, "\nDone.\n");
12501246
return 0;
12511247
}

tests/sfntedit_test.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import print_function, division, absolute_import
22

33
import os
4+
import pytest
5+
import subprocess32 as subprocess
46
import tempfile
57

68
from fontTools.ttLib import TTFont
@@ -47,11 +49,25 @@ def _font_has_table(font_path, table_tag):
4749
# Tests
4850
# -----
4951

50-
# This test is commented because sfntedit does not exit with 0
51-
# def test_no_options():
52-
# actual_path = runner(CMD + ['-r', '-f', LIGHT])
53-
# expected_path = _get_expected_path('list_sfnt.txt')
54-
# assert differ([expected_path, actual_path, '-l', '1'])
52+
def test_exit_no_option():
53+
# When ran by itself, 'sfntedit' prints the usage
54+
assert subprocess.call([TOOL]) == 1
55+
56+
57+
@pytest.mark.parametrize('arg', ['-h', '-u'])
58+
def test_exit_known_option(arg):
59+
assert subprocess.call([TOOL, arg]) == 0
60+
61+
62+
@pytest.mark.parametrize('arg', ['-j', '--bogus'])
63+
def test_exit_unknown_option(arg):
64+
assert subprocess.call([TOOL, arg]) == 1
65+
66+
67+
def test_no_options():
68+
actual_path = runner(CMD + ['-s', '-f', LIGHT])
69+
expected_path = _get_expected_path('list_sfnt.txt')
70+
assert differ([expected_path, actual_path, '-l', '1'])
5571

5672

5773
def test_extract_table():

0 commit comments

Comments
 (0)