@@ -87,6 +87,12 @@ int *updates = NULL; /* What does this do again? */
87
87
volatile sig_atomic_t signal_status = 0 ; /* Indicates a caught signal */
88
88
#endif
89
89
90
+ #ifdef _WIN32
91
+ char * DELIMITER = "\r\n" ;
92
+ #else
93
+ char * DELIMITER = "\n" ;
94
+ #endif
95
+
90
96
int va_system (char * str , ...) {
91
97
92
98
va_list ap ;
@@ -115,6 +121,7 @@ void finish(void) {
115
121
exit (0 );
116
122
}
117
123
124
+
118
125
/* What we do when we're all set to exit */
119
126
void c_die (char * msg , ...) {
120
127
@@ -140,6 +147,32 @@ void c_die(char *msg, ...) {
140
147
exit (0 );
141
148
}
142
149
150
+ /* Reads the content of a give path into a char pointer */
151
+ char * read_file (char * path ) {
152
+ FILE * file ;
153
+ char * buf ;
154
+ long fs ;
155
+
156
+ file = fopen (path , "r" );
157
+ if (!file ){
158
+ c_die ("Could not read message file.\n" );
159
+ }
160
+
161
+ fseek (file , 0L , SEEK_END );
162
+ fs = ftell (file );
163
+ fseek (file , 0L , SEEK_SET );
164
+
165
+ buf = (char * )calloc (fs , sizeof (char ));
166
+ if (!buf ) {
167
+ c_die ("Could not allocate memory.\n" );
168
+ }
169
+
170
+ fread (buf , sizeof (char ), fs , file );
171
+ fclose (file );
172
+
173
+ return buf ;
174
+ }
175
+
143
176
void usage (void ) {
144
177
printf (" Usage: cmatrix -[abBcfhlsmVxk] [-u delay] [-C color] [-t tty] [-M message]\n" );
145
178
printf (" -a: Asynchronous scroll\n" );
@@ -155,7 +188,7 @@ void usage(void) {
155
188
printf (" -s: \"Screensaver\" mode, exits on first keystroke\n" );
156
189
printf (" -x: X window mode, use if your xterm is using mtx.pcf\n" );
157
190
printf (" -V: Print version information and exit\n" );
158
- printf (" -M [message]: Prints your message in the center of the screen. Overrides -L's default message.\n" );
191
+ printf (" -M [message]: Prints your message in the center of the screen. Overrides -L's default message. Accepts readable files. \n" );
159
192
printf (" -u delay (0 - 10, default 4): Screen update delay\n" );
160
193
printf (" -C [color]: Use this color for matrix (default green)\n" );
161
194
printf (" -r: rainbow mode\n" );
@@ -326,6 +359,7 @@ int main(int argc, char *argv[]) {
326
359
int classic = 0 ;
327
360
int changes = 0 ;
328
361
char * msg = "" ;
362
+ char * mfile = "" ;
329
363
char * tty = NULL ;
330
364
331
365
srand ((unsigned ) time (NULL ));
@@ -390,6 +424,11 @@ int main(int argc, char *argv[]) {
390
424
break ;
391
425
case 'M' :
392
426
msg = strdup (optarg );
427
+
428
+ // check if msg is a readable file
429
+ if (access (msg , F_OK ) == 0 ) {
430
+ mfile = msg ;
431
+ }
393
432
break ;
394
433
case 'n' :
395
434
bold = -1 ;
@@ -845,30 +884,56 @@ if (console) {
845
884
}
846
885
}
847
886
887
+ // check if -M was used with a file
888
+ // if yes, read the msg from the file
889
+ if (mfile [0 ] != '\0' ) {
890
+ msg = read_file (mfile );
891
+ }
892
+
848
893
//check if -M and/or -L was used
849
894
if (msg [0 ] != '\0' ) {
850
- //Add our message to the screen
851
- int msg_x = LINES /2 ;
852
- int msg_y = COLS /2 - strlen (msg )/2 ;
895
+ // Add our message to the screen
896
+ int multiline = 0 ;
853
897
int i = 0 ;
854
898
899
+ // count the newlines
900
+ for (i = 0 ; i < strlen (msg ); i ++ )
901
+ if (msg [i ] == '\n' )
902
+ multiline ++ ;
903
+
904
+ char * line = strtok (msg , DELIMITER );
905
+ if (line == NULL )
906
+ line = msg ;
907
+
908
+ int x_offset = -1 * multiline /2 ;
909
+ int msg_x = LINES /2 + x_offset ;
910
+ int msg_y = COLS /2 - strlen (line )/2 ;
911
+
855
912
//Add space before message
856
913
move (msg_x - 1 , msg_y - 2 );
857
- for (i = 0 ; i < strlen (msg )+ 4 ; i ++ )
914
+ for (i = 0 ; i < strlen (line )+ 4 ; i ++ )
858
915
addch (' ' );
859
916
860
- //Write message
861
- move (msg_x , msg_y - 2 );
862
- addch (' ' );
863
- addch (' ' );
864
- addstr (msg );
865
- addch (' ' );
866
- addch (' ' );
867
-
868
- //Add space after message
869
- move (msg_x + 1 , msg_y - 2 );
870
- for (i = 0 ; i < strlen (msg )+ 4 ; i ++ )
917
+ while (line != NULL ){
918
+ msg_x = LINES /2 + x_offset ;
919
+ msg_y = COLS /2 - strlen (line )/2 ;
920
+
921
+ //Write message
922
+ move (msg_x , msg_y - 2 );
923
+ addch (' ' );
924
+ addch (' ' );
925
+ addstr (line );
926
+ addch (' ' );
871
927
addch (' ' );
928
+
929
+ //Add space after message
930
+ move (msg_x + 1 , msg_y - 2 );
931
+ for (i = 0 ; i < strlen (line )+ 4 ; i ++ )
932
+ addch (' ' );
933
+
934
+ line = strtok (NULL , DELIMITER );
935
+ x_offset ++ ;
936
+ }
872
937
}
873
938
874
939
napms (update * 10 );
0 commit comments