Mercurial > hg > sidinfo
annotate sidinfo.c @ 116:d062312ea850
Fix th_strndup_trim() usage.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 15 Feb 2016 12:00:32 +0200 |
parents | 6843261b4cd6 |
children | 24583e5ca0b3 |
rev | line source |
---|---|
2 | 1 /* |
2 * SIDInfo - PSID/RSID information displayer | |
108 | 3 * Written by Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
73
2b2376f1b0cc
Update copyright years. Yay. It's 2016!
Matti Hamalainen <ccr@tnsp.org>
parents:
70
diff
changeset
|
4 * (C) Copyright 2014-2016 Tecnic Software productions (TNSP) |
2 | 5 */ |
0 | 6 #include "th_args.h" |
2 | 7 #include "th_string.h" |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
8 #include "th_file.h" |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
9 #include "sidlib.h" |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
10 |
0 | 11 |
28 | 12 // Some constants |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
13 #define SET_SLDB_FILENAME "Songlengths.txt" |
1 | 14 |
112
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
15 enum |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
16 { |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
17 OTYPE_OTHER = 0, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
18 OTYPE_STR = 1, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
19 OTYPE_INT = 2, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
20 }; |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
21 |
0 | 22 |
23 typedef struct | |
24 { | |
63 | 25 int cmd; |
26 char *str; | |
27 char chr; | |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
28 int flags; |
63 | 29 } PSFStackItem; |
30 | |
31 | |
32 typedef struct | |
33 { | |
34 int nitems, nallocated; | |
35 PSFStackItem *items; | |
36 } PSFStack; | |
37 | |
38 | |
39 typedef struct | |
40 { | |
2 | 41 char *name; |
42 char *lname; | |
112
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
43 int type; |
2 | 44 } PSFOption; |
45 | |
46 | |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
47 static const PSFOption optPSOptions[] = |
2 | 48 { |
112
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
49 { "Filename" , NULL , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
50 { "Type" , NULL , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
51 { "Version" , NULL , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
52 { "PlayerType" , "Player type" , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
53 { "PlayerCompat" , "Player compatibility" , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
54 { "VideoClock" , "Video clock speed" , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
55 { "SIDModel" , "SID model" , OTYPE_STR }, |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
56 |
112
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
57 { "DataOffs" , "Data offset" , OTYPE_INT }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
58 { "DataSize" , "Data size" , OTYPE_INT }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
59 { "LoadAddr" , "Load address" , OTYPE_INT }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
60 { "InitAddr" , "Init address" , OTYPE_INT }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
61 { "PlayAddr" , "Play address" , OTYPE_INT }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
62 { "Songs" , "Songs" , OTYPE_INT }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
63 { "StartSong" , "Start song" , OTYPE_INT }, |
28 | 64 |
112
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
65 { "Name" , NULL , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
66 { "Author" , NULL , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
67 { "Copyright" , NULL , OTYPE_STR }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
68 { "Hash" , NULL , OTYPE_OTHER }, |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
69 |
55d99dc7c35c
Prepare for some possible future enhancements by specifying a type for each data field.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
70 { "Songlengths" , "Song lengths" , OTYPE_OTHER }, |
2 | 71 }; |
72 | |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
73 static const int noptPSOptions = sizeof(optPSOptions) / sizeof(optPSOptions[0]); |
2 | 74 |
75 | |
28 | 76 // Option variables |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
77 char *setHVSCPath = NULL, |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
78 *setSLDBPath = NULL; |
2 | 79 BOOL optParsable = FALSE, |
7 | 80 optNoNamePrefix = FALSE, |
29 | 81 optHexadecimal = FALSE, |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
82 optOneLine = FALSE, |
111 | 83 optFieldOutput = TRUE; |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
84 char *optFieldSep = NULL; |
19 | 85 int optNFiles = 0; |
2 | 86 |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
87 PSFStack optFormat; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
88 |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
89 SIDLibSLDB *sidSLDB = NULL; |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
90 |
2 | 91 |
28 | 92 // Define option arguments |
44
7b137e4e817c
Fix to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
93 static const th_optarg_t optList[] = |
2 | 94 { |
95 { 0, '?', "help", "Show this help", OPT_NONE }, | |
24
16d6b5029543
Disable verbose option for now, it does not do anything at the moment.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
96 // { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
2 | 97 { 2, 'p', "parsable", "Output in script-parsable format", OPT_NONE }, |
7 | 98 { 5, 'n', "noprefix", "Output without field name prefix", OPT_NONE }, |
33
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
99 { 6, 'l', "line", "Output in one line format, -l <field separator>", OPT_ARGREQ }, |
2 | 100 { 3, 'f', "fields", "Show only specified field(s)", OPT_ARGREQ }, |
101 { 4, 'x', "hex", "Use hexadecimal values", OPT_NONE }, | |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
102 { 7, 'F', "format", "Use given format string (see below)", OPT_ARGREQ }, |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
103 { 8, 'H', "hvsc", "Specify path to HVSC documents directory", OPT_ARGREQ }, |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
104 { 9, 'S', "sldb", "Specify Songlengths.txt file (use -H if possible)", OPT_ARGREQ }, |
2 | 105 }; |
106 | |
44
7b137e4e817c
Fix to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
107 static const int optListN = sizeof(optList) / sizeof(optList[0]); |
2 | 108 |
109 | |
110 void argShowHelp(void) | |
111 { | |
75
92a4065c41d0
Improve listing of available fields in --help.
Matti Hamalainen <ccr@tnsp.org>
parents:
73
diff
changeset
|
112 int index, len; |
2 | 113 |
20 | 114 th_print_banner(stdout, th_prog_name, "[options] <sid filename> [sid filename #2 ..]"); |
44
7b137e4e817c
Fix to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
115 th_args_help(stdout, optList, optListN, 0); |
2 | 116 printf( |
117 "\n" | |
118 "Available fields:\n"); | |
3 | 119 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
120 for (len = index = 0; index < noptPSOptions; index++) |
2 | 121 { |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
122 const PSFOption *opt = &optPSOptions[index]; |
75
92a4065c41d0
Improve listing of available fields in --help.
Matti Hamalainen <ccr@tnsp.org>
parents:
73
diff
changeset
|
123 len += strlen(opt->name) + 3; |
92a4065c41d0
Improve listing of available fields in --help.
Matti Hamalainen <ccr@tnsp.org>
parents:
73
diff
changeset
|
124 if (len >= 72) |
2 | 125 { |
126 printf("\n"); | |
75
92a4065c41d0
Improve listing of available fields in --help.
Matti Hamalainen <ccr@tnsp.org>
parents:
73
diff
changeset
|
127 len = 0; |
2 | 128 } |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
129 printf("%s%s", opt->name, (index < noptPSOptions - 1) ? ", " : "\n\n"); |
2 | 130 } |
58 | 131 |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
132 printf( |
61 | 133 "Example: %s -x -p -f hash,copyright somesidfile.sid\n" |
134 "\n" | |
135 "Format strings for '-F' option are composed of @fields@ that\n" | |
136 "are expanded to their value. Also, escape sequences \\r, \\n and \\t\n" | |
137 "can be used: -F \"hash=@hash@\\ncopy=@copyright@\\n\"\n" | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
138 "\n" |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
139 "When specifying HVSC path, it is preferable to use -H/--hvsc option,\n" |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
140 "as STIL.txt and Songlengths.txt will be automatically used from there.\n" |
61 | 141 , th_prog_name); |
2 | 142 } |
143 | |
144 | |
78
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
145 int argMatchPSField(const char *field) |
2 | 146 { |
147 int index, found = -1; | |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
148 for (index = 0; index < noptPSOptions; index++) |
2 | 149 { |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
150 const PSFOption *opt = &optPSOptions[index]; |
78
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
151 if (th_strcasecmp(opt->name, field) == 0) |
2 | 152 { |
153 if (found >= 0) | |
154 return -2; | |
155 found = index; | |
156 } | |
157 } | |
158 | |
159 return found; | |
160 } | |
161 | |
162 | |
78
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
163 int argMatchPSFieldError(const char *field) |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
164 { |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
165 int found = argMatchPSField(field); |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
166 switch (found) |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
167 { |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
168 case -1: |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
169 THERR("No such field '%s'.\n", field); |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
170 break; |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
171 |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
172 case -2: |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
173 THERR("Field '%s' is ambiguous.\n", field); |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
174 break; |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
175 } |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
176 return found; |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
177 } |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
178 |
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
179 |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
180 BOOL siStackAddItem(PSFStack *stack, const PSFStackItem *item) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
181 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
182 if (stack->items == NULL || stack->nitems + 1 >= stack->nallocated) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
183 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
184 stack->nallocated += 16; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
185 if ((stack->items = th_realloc(stack->items, stack->nallocated * sizeof(PSFStackItem))) == NULL) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
186 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
187 THERR("Could not allocate memory for format item stack.\n"); |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
188 return FALSE; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
189 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
190 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
191 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
192 memcpy(stack->items + stack->nitems, item, sizeof(PSFStackItem)); |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
193 stack->nitems++; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
194 return TRUE; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
195 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
196 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
197 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
198 void siClearStack(PSFStack *stack) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
199 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
200 if (stack != NULL) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
201 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
202 if (stack->nitems > 0 && stack->items != NULL) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
203 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
204 int n; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
205 for (n = 0; n < stack->nitems; n++) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
206 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
207 if (stack->items[n].cmd == -1) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
208 th_free(stack->items[n].str); |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
209 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
210 th_free(stack->items); |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
211 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
212 memset(stack, 0, sizeof(PSFStack)); |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
213 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
214 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
215 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
216 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
217 BOOL argParsePSFields(PSFStack *stack, const char *fmt) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
218 { |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
219 const char *start = fmt; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
220 siClearStack(stack); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
221 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
222 while (*start) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
223 { |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
224 PSFStackItem item; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
225 const char *end = strchr(start, ','); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
226 char *field = (end != NULL) ? |
116
d062312ea850
Fix th_strndup_trim() usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
115
diff
changeset
|
227 th_strndup_trim(start, end - start, TH_TRIM_BOTH) : |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
228 th_strdup_trim(start, TH_TRIM_BOTH); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
229 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
230 int found = argMatchPSFieldError(field); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
231 th_free(field); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
232 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
233 if (found < 0) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
234 return FALSE; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
235 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
236 item.cmd = found; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
237 item.str = NULL; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
238 if (!siStackAddItem(stack, &item)) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
239 return FALSE; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
240 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
241 if (!end) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
242 break; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
243 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
244 start = end + 1; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
245 } |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
246 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
247 return TRUE; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
248 } |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
249 |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
250 |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
251 // |
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
252 // Parse a format string into a PSFStack structure |
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
253 // |
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
254 BOOL argParsePSFormatStr(PSFStack *stack, const char *fmt) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
255 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
256 PSFStackItem item; |
83 | 257 const char *start = NULL; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
258 int mode = 0; |
113
cd97e7517e21
Simplify error handling logic.
Matti Hamalainen <ccr@tnsp.org>
parents:
112
diff
changeset
|
259 BOOL rval = TRUE; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
260 |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
261 siClearStack(stack); |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
262 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
263 while (mode != -1) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
264 switch (mode) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
265 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
266 case 0: |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
267 if (*fmt == '@') |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
268 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
269 start = fmt + 1; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
270 mode = 1; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
271 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
272 else |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
273 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
274 start = fmt; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
275 mode = 2; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
276 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
277 fmt++; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
278 break; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
279 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
280 case 1: |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
281 if (*fmt == '@') |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
282 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
283 if (fmt - start == 0) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
284 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
285 item.cmd = -2; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
286 item.str = NULL; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
287 item.chr = '@'; |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
288 if (!siStackAddItem(stack, &item)) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
289 return FALSE; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
290 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
291 else |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
292 { |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
293 char *field = th_strndup_trim(start, fmt - start, TH_TRIM_BOTH); |
78
9fb70b7b34ff
Clean up error handling for field parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
294 int ret = argMatchPSFieldError(field); |
113
cd97e7517e21
Simplify error handling logic.
Matti Hamalainen <ccr@tnsp.org>
parents:
112
diff
changeset
|
295 th_free(field); |
81 | 296 if (ret >= 0) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
297 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
298 item.cmd = ret; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
299 item.str = NULL; |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
300 if (!siStackAddItem(stack, &item)) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
301 return FALSE; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
302 } |
113
cd97e7517e21
Simplify error handling logic.
Matti Hamalainen <ccr@tnsp.org>
parents:
112
diff
changeset
|
303 else |
cd97e7517e21
Simplify error handling logic.
Matti Hamalainen <ccr@tnsp.org>
parents:
112
diff
changeset
|
304 rval = FALSE; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
305 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
306 mode = 0; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
307 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
308 else |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
309 if (*fmt == 0) |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
310 mode = -1; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
311 fmt++; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
312 break; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
313 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
314 case 2: |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
315 if (*fmt == 0 || *fmt == '@') |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
316 { |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
317 item.cmd = -1; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
318 item.str = th_strndup(start, fmt - start); |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
319 if (!siStackAddItem(stack, &item)) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
320 return FALSE; |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
321 |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
322 mode = (*fmt == 0) ? -1 : 0; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
323 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
324 else |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
325 fmt++; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
326 break; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
327 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
328 |
113
cd97e7517e21
Simplify error handling logic.
Matti Hamalainen <ccr@tnsp.org>
parents:
112
diff
changeset
|
329 return rval; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
330 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
331 |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
332 |
2 | 333 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) |
334 { | |
335 switch (optN) | |
336 { | |
337 case 0: | |
338 argShowHelp(); | |
339 exit(0); | |
340 break; | |
341 | |
342 case 1: | |
343 th_verbosityLevel++; | |
344 break; | |
345 | |
346 case 2: | |
347 optParsable = TRUE; | |
348 break; | |
349 | |
350 case 3: | |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
351 if (!argParsePSFields(&optFormat, optArg)) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
352 return FALSE; |
2 | 353 break; |
354 | |
355 case 4: | |
356 optHexadecimal = TRUE; | |
357 break; | |
358 | |
7 | 359 case 5: |
360 optNoNamePrefix = TRUE; | |
361 break; | |
362 | |
29 | 363 case 6: |
364 optOneLine = TRUE; | |
33
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
365 optFieldSep = optArg; |
29 | 366 break; |
367 | |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
368 case 7: |
111 | 369 optFieldOutput = FALSE; |
79
5709d0beb394
Avoid using a global variable here.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
370 if (!argParsePSFormatStr(&optFormat, optArg)) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
371 return FALSE; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
372 break; |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
373 |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
374 case 8: |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
375 setHVSCPath = th_strdup(optArg); |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
376 break; |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
377 |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
378 case 9: |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
379 setSLDBPath = th_strdup(optArg); |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
380 break; |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
381 |
2 | 382 default: |
383 THERR("Unknown option '%s'.\n", currArg); | |
384 return FALSE; | |
385 } | |
386 | |
387 return TRUE; | |
388 } | |
389 | |
390 | |
62
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
391 static void siPrintStrEscapes(FILE *outFile, const char *str) |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
392 { |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
393 while (*str) |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
394 { |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
395 if (*str == '\\') |
82 | 396 switch (*(++str)) |
62
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
397 { |
82 | 398 case 'n': fputc('\n', outFile); break; |
399 case 'r': fputc('\r', outFile); break; | |
400 case 't': fputc('\r', outFile); break; | |
401 case '\\': fputc('\\', outFile); break; | |
402 default: fputc(*str, outFile); break; | |
62
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
403 } |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
404 else |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
405 fputc(*str, outFile); |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
406 |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
407 str++; |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
408 } |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
409 } |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
410 |
0d01efc3c803
Factor escape printing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
411 |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
412 static void siPrintFieldPrefix(FILE *outFile, const PSFOption *opt) |
2 | 413 { |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
414 const char *name = (optParsable || opt->lname == NULL) ? opt->name : opt->lname; |
111 | 415 if (!optNoNamePrefix && optFieldOutput) |
29 | 416 fprintf(outFile, optParsable ? "%s=" : "%-20s : ", name); |
2 | 417 } |
418 | |
419 | |
33
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
420 static void siPrintFieldSeparator(FILE *outFile) |
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
421 { |
111 | 422 if (optFieldOutput) |
83 | 423 fputs(optOneLine ? optFieldSep : "\n", outFile); |
33
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
424 } |
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
425 |
39d9df17c8b1
Add field separator argument to -l option.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
426 |
29 | 427 static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const int xindex, const char *xfmt, const char *xaltfmt, ...) |
2 | 428 { |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
429 const PSFOption *opt = &optPSOptions[xindex]; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
430 va_list ap; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
431 const char *fmt = optHexadecimal ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt; |
2 | 432 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
433 siPrintFieldPrefix(outFile, opt); |
2 | 434 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
435 va_start(ap, xaltfmt); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
436 vfprintf(outFile, fmt, ap); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
437 va_end(ap); |
3 | 438 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
439 siPrintFieldSeparator(outFile); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
440 *shown = TRUE; |
2 | 441 } |
442 | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
443 |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
444 #define PR(xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, shown, xindex, xfmt, xaltfmt, __VA_ARGS__ ) |
9
c1fba4abf56f
Make filename printing optional.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
445 |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
446 |
114 | 447 static void siPrintPSIDInformationField(FILE *outFile, const char *filename, const PSIDHeader *psid, BOOL *shown, const PSFStackItem *item) |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
448 { |
114 | 449 const PSFOption *opt = &optPSOptions[item->cmd]; |
450 switch (item->cmd) | |
35
79e6d08b473f
Add more information fields, including SID model, video clock speed, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
451 { |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
452 case 0: PR("%s", NULL, filename); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
453 case 1: PR("%s", NULL, psid->magic); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
454 case 2: PR("%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8)); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
455 case 3: PR("%s", NULL, (psid->flags & PSF_PLAYER_TYPE) ? "Compute! SIDPlayer MUS" : "Normal built-in"); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
456 case 4: |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
457 if (psid->version >= 2) |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
458 PR("%s", NULL, (psid->flags & PSF_PLAYSID_TUNE) ? "PlaySID" : "C64 compatible"); |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
459 break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
460 case 5: |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
461 if (psid->version >= 2) |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
462 PR("%s", NULL, si_get_sid_clock_str(psid->flags)); |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
463 break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
464 case 6: |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
465 if (psid->version >= 2) |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
466 PR("%s", NULL, si_get_sid_model_str(psid->flags)); |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
467 break; |
35
79e6d08b473f
Add more information fields, including SID model, video clock speed, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
468 |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
469 case 7: PR("%d", "$%08x", psid->dataOffset); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
470 case 8: PR("%d", "$%08x", psid->dataSize); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
471 case 9: PR("%d", "$%04x", psid->loadAddress); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
472 case 10: PR("%d", "$%04x", psid->initAddress); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
473 case 11: PR("%d", "$%04x", psid->playAddress); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
474 case 12: PR("%d", "$%04x", psid->nSongs); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
475 case 13: PR("%d", "$%04x", psid->startSong); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
476 case 14: PR("%s", NULL, psid->sidName); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
477 case 15: PR("%s", NULL, psid->sidAuthor); break; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
478 case 16: PR("%s", NULL, psid->sidCopyright); break; |
35
79e6d08b473f
Add more information fields, including SID model, video clock speed, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
479 |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
480 case 17: |
114 | 481 siPrintFieldPrefix(outFile, opt); |
482 th_md5_print(outFile, psid->hash); | |
483 siPrintFieldSeparator(outFile); | |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
484 break; |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
485 |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
486 case 18: |
114 | 487 siPrintFieldPrefix(outFile, opt); |
488 if (psid->lengths != NULL) | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
489 { |
114 | 490 int i; |
491 for (i = 0; i < psid->lengths->nlengths; i++) | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
492 { |
114 | 493 int len = psid->lengths->lengths[i]; |
494 fprintf(outFile, "%d:%d%s", len / 60, len % 60, | |
495 (i < psid->lengths->nlengths - 1) ? " " : ""); | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
496 } |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
497 } |
114 | 498 siPrintFieldSeparator(outFile); |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
499 break; |
29 | 500 } |
2 | 501 } |
502 | |
0 | 503 |
19 | 504 BOOL argHandleFile(char *filename) |
505 { | |
506 static PSIDHeader psid; | |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
507 th_ioctx *inFile = NULL; |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
508 FILE *outFile; |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
509 int index; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
510 BOOL shown = FALSE; |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
511 |
19 | 512 optNFiles++; |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
513 outFile = stdout; |
19 | 514 |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
515 if ((inFile = th_io_fopen(&th_stdio_io_ops, filename, "rb")) == NULL) |
19 | 516 { |
517 THERR("Could not open file '%s'.\n", filename); | |
27
4dd463eebb74
Fix argument handling when file does not exist.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
518 return TRUE; |
19 | 519 } |
520 | |
521 // Read PSID data | |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
522 if (si_read_sid_file(inFile, &psid) != 0) |
23 | 523 { |
524 THERR("Error reading %s\n", filename); | |
19 | 525 goto error; |
23 | 526 } |
19 | 527 |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
528 // Get songlength information, if any |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
529 if (sidSLDB != NULL) |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
530 psid.lengths = si_sldb_get_by_hash(sidSLDB, psid.hash); |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
531 |
19 | 532 // Output |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
533 for (index = 0; index < optFormat.nitems; index++) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
534 { |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
535 PSFStackItem *item = &optFormat.items[index]; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
536 switch (item->cmd) |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
537 { |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
538 case -1: |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
539 siPrintStrEscapes(outFile, item->str); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
540 break; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
541 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
542 case -2: |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
543 fputc(item->chr, outFile); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
544 break; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
545 |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
546 default: |
114 | 547 siPrintPSIDInformationField(outFile, filename, &psid, &shown, item); |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
548 break; |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
549 } |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
550 } |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
551 |
111 | 552 if (optFieldOutput) |
55
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
553 { |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
554 if (shown && optOneLine) |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
555 fprintf(outFile, "\n"); |
54b48086a1d0
Begin slight refactoring of how output is done.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
556 } |
19 | 557 |
558 // Shutdown | |
559 error: | |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
560 th_io_free(inFile); |
19 | 561 |
562 return TRUE; | |
563 } | |
564 | |
565 | |
0 | 566 int main(int argc, char *argv[]) |
567 { | |
568 // Initialize | |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
569 th_init("SIDInfo", "PSID/RSID information displayer", "0.6.1", NULL, NULL); |
0 | 570 th_verbosityLevel = 0; |
571 | |
59
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
572 memset(&optFormat, 0, sizeof(optFormat)); |
718cdb563531
Initial implementation of -F format string option.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
573 |
2 | 574 // Parse command line arguments |
575 if (!th_args_process(argc, argv, optList, optListN, | |
23 | 576 argHandleOpt, argHandleFile, OPTH_ONLY_OPTS)) |
2 | 577 return -1; |
578 | |
29 | 579 if (optOneLine) |
580 { | |
581 optParsable = FALSE; | |
582 optNoNamePrefix = TRUE; | |
583 } | |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
584 |
111 | 585 if (optFieldOutput && !optFormat.nitems) |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
586 { |
115 | 587 PSFStackItem item; |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
588 int i; |
115 | 589 memset(&item, 0, sizeof(item)); |
109
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
590 siClearStack(&optFormat); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
591 for (i = 0; i < noptPSOptions; i++) |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
592 { |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
593 item.cmd = i; |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
594 siStackAddItem(&optFormat, &item); |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
595 } |
59b5b99f4280
Reimplement '-f' option differently.
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
596 } |
29 | 597 |
90 | 598 // Check paths |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
599 if (setHVSCPath != NULL) |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
600 { |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
601 if (setSLDBPath == NULL) |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
602 setSLDBPath = th_strdup_printf("%s%c%s", setHVSCPath, TH_DIR_SEPARATOR, SET_SLDB_FILENAME); |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
603 } |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
604 |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
605 if (setSLDBPath != NULL) |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
606 { |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
607 // Initialize SLDB |
110 | 608 int ret = THERR_OK; |
609 th_ioctx *ctx = NULL; | |
91
5ec395268fd3
Fix to comply with th_ioctx API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
610 if ((ctx = th_io_fopen(&th_stdio_io_ops, setSLDBPath, "r")) == NULL) |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
611 { |
106 | 612 THERR("Could not open SLDB '%s'.\n", |
613 setSLDBPath); | |
110 | 614 goto err; |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
615 } |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
616 |
115 | 617 THMSG(1, "Reading SLDB.\n"); |
110 | 618 if ((sidSLDB = si_sldb_new()) == NULL) |
619 { | |
620 THERR("Could not allocate SLDB structure!\n"); | |
621 goto err; | |
622 } | |
623 | |
624 if ((ret = si_sldb_read(ctx, sidSLDB)) != THERR_OK) | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
625 { |
106 | 626 THERR("Error parsing SLDB: %d, %s\n", |
627 ret, th_error_str(ret)); | |
110 | 628 goto err; |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
629 } |
90 | 630 th_io_close(ctx); |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
631 |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
632 if ((ret = si_sldb_build_index(sidSLDB)) != THERR_OK) |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
633 { |
106 | 634 THERR("Error building SLDB index: %d, %s.\n", |
635 ret, th_error_str(ret)); | |
110 | 636 goto err; |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
637 } |
110 | 638 |
639 err: | |
640 th_io_close(ctx); | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
641 } |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
642 |
23 | 643 // Process files |
644 if (!th_args_process(argc, argv, optList, optListN, | |
645 argHandleOpt, argHandleFile, OPTH_ONLY_OTHER)) | |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
646 goto out; |
23 | 647 |
19 | 648 if (optNFiles == 0) |
2 | 649 { |
650 argShowHelp(); | |
19 | 651 THERR("No filename(s) specified.\n"); |
2 | 652 } |
1 | 653 |
89
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
654 out: |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
655 |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
656 th_free(setHVSCPath); |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
657 th_free(setSLDBPath); |
d9cb7c635e7b
Implement initial SLDB support.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
658 si_sldb_free(sidSLDB); |
19 | 659 return 0; |
0 | 660 } |