-
Notifications
You must be signed in to change notification settings - Fork 5
/
banner.c
709 lines (616 loc) · 16.5 KB
/
banner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* C E D A R
* S O L U T I O N S "Software done right."
* S O F T W A R E
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (c) 2000-2004.2007,2013,2014,2020,2024 Kenneth J. Pronovici.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* Version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Copies of the GNU General Public License are available from
* the Free Software Foundation website, http://www.gnu.org/.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Author : Kenneth J. Pronovici <[email protected]>
* Language : ANSI C
* Project : banner
* Purpose : Main routine and function definitions.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/********************
File documentation
********************/
/** @file
* @author Kenneth J. Pronovici
* @brief Main routine and function definitions.
*/
/***********************************
Doxygen "main page" documentation
***********************************/
/** @mainpage Banner Program
*
* I've used a lot of different UN*X systems, and all of them, as far as I can
* tell, seem to provide a "banner" program that prints some short string in
* large letters. Except Linux. When I wrote this, I couldn't seem to find
* something like this anywhere (well, there is @c /usr/games/banner, but that's
* different than a typical banner implementation). So, since it was such a
* simplistic thing to do, I wrote it myself.
*
* The 'banner' program prints a "banner" on the screen that corresponds to the
* first X characters of a string entered on the command line.
*
* Say we're printing "AB"... we make one call per character and build an array
* of strings from the top down. The first time through, we'll have this:
*
@verbatim
banner[0] = " # "
banner[1] = " # # "
banner[2] = " # # "
banner[3] = "# # "
banner[4] = "####### "
banner[5] = "# # "
banner[6] = "# # "
@endverbatim
*
* the second time through, we'll have this:
*
@verbatim
banner[0] = " # ###### "
banner[1] = " # # # # "
banner[2] = " # # # # "
banner[3] = "# # ###### "
banner[4] = "####### # # "
banner[5] = "# # # # "
banner[6] = "# # ###### "
@endverbatim
*
* i.e. the first line of the "B" array is concatenated onto the first line of
* the "A" array, the second onto the second, etc. to make the complete
* banner, line by line from top to bottom. The string is then printed top to
* bottom.
*
* It's pretty configurable on a compile-time basis. The letter definitions
* can be changed by changing the array definitions in "letters.h". The
* defined letter height, the number of spaces after each letter, the max
* width of a banner to be printed, etc. are all controlled from in there.
* The only real restriction is that all of the letters have to be the same
* height. Note, however, that there is no facility for printing lower-case
* letters - as far as I can remember, Solaris and AIX don't have that, and I
* didn't feel like putting it in. Adding it would be easy, if you want to
* come up with letter definitions for the lower-case letters.
*
* I could have made the add_to_banner() function prettier if I wanted to just
* key off of ASCII value rather than having hardcoded names for each
* letter/character. I decided that keying off ASCII values would not be
* flexible enough. If you want to optimize it, feel free, and then write me
* and let me know.
*
* @author Kenneth J. Pronovici
*/
/****************
Included files
****************/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#include <termios.h>
#include <sys/ioctl.h>
#include "letters.h"
/******************
Macro defintions
*******************/
/** Initial size of banner arrays */
#define INITIAL_SIZE (10)
/** Default print width (assumed terminal width) */
#define DEFAULT_PRINTWIDTH (80)
/** Normal exit status */
#define NORMAL_EXIT (0)
/** Error exit status */
#define ERROR_EXIT (-1)
/** URL of GNU's website */
#define GNU_URL ("http://www.gnu.org/")
/** Copyright year range */
#define COPYRIGHT_DATE ("2000-2004,2007,2013")
/** Name of author */
#define AUTHOR ("Kenneth J. Pronovici")
/** Email address of author */
#define AUTHOR_EMAIL ("[email protected]")
/************************
init_banner subroutine
************************/
/** Does initial memory allocation and clears banner buffer.
* Note: any memory previously allocated to this buffer will be lost (leaked).
* @param banner Banner buffer to be initialized.
*/
static void init_banner(char **banner)
{
int i = 0;
for(i=0; i<LETTER_HEIGHT; i++)
{
banner[i] = (char *)calloc(1, INITIAL_SIZE*sizeof(char));
}
}
/************************
free_banner subroutine
************************/
/** Frees memory allocated by init_banner().
* The array itself is not freed, only the allocations for the individual elements.
* @param banner Banner buffer to be freed.
*/
static void free_banner(char **banner)
{
int i = 0;
for(i=0; i<LETTER_HEIGHT; i++)
{
if(banner[i] != NULL)
{
free(banner[i]);
banner[i] = NULL;
}
}
}
/******************************
convert_to_upper subroutine
******************************/
/** Destructively converts a string to all UPPER-CASE letters.
* @param string String to be converted.
*/
static void convert_to_upper(char *string)
{
int i = 0;
for(i=0; i<strlen(string); i++)
{
string[i] = toupper(string[i]);
}
}
/*******************************
convert_whitespace subroutine
*******************************/
/** Destructively converts all whitespace characters to space characters.
* @param string String to be converted.
*/
static void convert_whitespace(char *string)
{
int i = 0;
for(i=0; i<strlen(string); i++)
{
if(isspace(string[i]))
{
string[i] = ' ';
}
}
}
/**************************
add_to_banner subroutine
**************************/
/** Tacks a letter into a banner buffer.
* @param banner Banner buffer
* @param height Height of letters
* @param space Width (in characters) of space between letters
* @param letter Letter to be added to the banner buffer
* @param maxwidth Maximum width of any one banner line
* @return Boolean true (1) if output was truncated, boolean false (0) otherwise.
*/
static int add_to_banner(char **banner, int height, int space, char letter, int maxwidth)
{
int i = 0;
int j = 0;
char *previous = NULL;
char **working_char = NULL;
int new_length = 0;
switch(letter)
{
case 'A':
working_char = char_a;
break;
case 'B':
working_char = char_b;
break;
case 'C':
working_char = char_c;
break;
case 'D':
working_char = char_d;
break;
case 'E':
working_char = char_e;
break;
case 'F':
working_char = char_f;
break;
case 'G':
working_char = char_g;
break;
case 'H':
working_char = char_h;
break;
case 'I':
working_char = char_i;
break;
case 'J':
working_char = char_j;
break;
case 'K':
working_char = char_k;
break;
case 'L':
working_char = char_l;
break;
case 'M':
working_char = char_m;
break;
case 'N':
working_char = char_n;
break;
case 'O':
working_char = char_o;
break;
case 'P':
working_char = char_p;
break;
case 'Q':
working_char = char_q;
break;
case 'R':
working_char = char_r;
break;
case 'S':
working_char = char_s;
break;
case 'T':
working_char = char_t;
break;
case 'U':
working_char = char_u;
break;
case 'V':
working_char = char_v;
break;
case 'W':
working_char = char_w;
break;
case 'X':
working_char = char_x;
break;
case 'Y':
working_char = char_y;
break;
case 'Z':
working_char = char_z;
break;
case '0':
working_char = char_0;
break;
case '1':
working_char = char_1;
break;
case '2':
working_char = char_2;
break;
case '3':
working_char = char_3;
break;
case '4':
working_char = char_4;
break;
case '5':
working_char = char_5;
break;
case '6':
working_char = char_6;
break;
case '7':
working_char = char_7;
break;
case '8':
working_char = char_8;
break;
case '9':
working_char = char_9;
break;
case '#':
working_char = char_hash;
break;
case '~':
working_char = char_tild;
break;
case '!':
working_char = char_excl;
break;
case '@':
working_char = char_atsg;
break;
case '$':
working_char = char_dolr;
break;
case '%':
working_char = char_prct;
break;
case '^':
working_char = char_crrt;
break;
case '&':
working_char = char_ampr;
break;
case '*':
working_char = char_star;
break;
case ')':
working_char = char_rpar;
break;
case '(':
working_char = char_lpar;
break;
case '_':
working_char = char_undr;
break;
case '+':
working_char = char_plus;
break;
case '=':
working_char = char_equl;
break;
case '}':
working_char = char_rbrc;
break;
case '{':
working_char = char_lbrc;
break;
case ']':
working_char = char_rbrk;
break;
case '[':
working_char = char_lbrk;
break;
case '|':
working_char = char_pipe;
break;
case '\\':
working_char = char_lsls;
break;
case ':':
working_char = char_coln;
break;
case ';':
working_char = char_scln;
break;
case '\"':
working_char = char_quot;
break;
case '\'':
working_char = char_squt;
break;
case '`':
working_char = char_btck;
break;
case '>':
working_char = char_rarw;
break;
case '<':
working_char = char_larw;
break;
case '.':
working_char = char_perd;
break;
case ',':
working_char = char_coma;
break;
case '?':
working_char = char_ques;
break;
case '/':
working_char = char_rsls;
break;
case '-':
working_char = char_dash;
break;
case ' ':
working_char = char_spac;
break;
default:
/* If we don't know about it, we'll just ignore it. */
return(0);
}
for(i=0; i<height; i++)
{
new_length = strlen(banner[i])*sizeof(char) + /* current banner */
strlen(working_char[i])*sizeof(char) + /* new character */
space*sizeof(char) + /* the spaces */
1; /* \0 character */
if(new_length > maxwidth)
{
return(1); /* banner was truncated */
}
previous = banner[i];
banner[i] = (char *)realloc(banner[i], new_length);
if(banner[i] == NULL)
{
banner[i] = previous;
return(1); /* act as if truncated */
}
strcat(banner[i], working_char[i]);
for(j=0; j<space; j++)
{
strcat(banner[i], " ");
}
}
return(0); /* banner was not truncated */
}
/************************
fill_banner subroutine
************************/
/** Fills a banner buffer in based on a string.
* @param banner Banner buffer
* @param string String to be filled into banner buffer
* @param maxwidth Maximum width of any one banner line
*/
static void fill_banner(char **banner, char *string, int maxwidth)
{
int j = 0;
int truncated = 0;
for(j=0; j<strlen(string); j++)
{
truncated = add_to_banner(banner, LETTER_HEIGHT, SPACE_WIDTH, string[j], maxwidth);
if(truncated)
{
break; /* stop looping; the string is too long */
}
}
}
/*************************
print_banner subroutine
*************************/
/** Prints a banner buffer.
* @param banner Banner buffer
*/
static void print_banner(char **banner)
{
int j = 0;
printf("\n");
for(j=0; j<LETTER_HEIGHT; j++)
{
printf("%s\n", banner[j]);
}
printf("\n");
}
/******************
usage subroutine
******************/
/** Provides usage information for the program.
* @param program Name program was invoked with.
*/
static void usage(char *program)
{
printf("Usage: %s string\n"
"\n"
"This is a classic-style banner program similar to the one found in Solaris or\n"
"AIX in the late 1990s. It prints a short string to the console in very large\n"
"letters.\n"
"\n"
"Banners that do not fit in the terminal will be truncated. If $COLUMNS is\n"
"exported in the environment, it is taken to be the width of the terminal. If\n"
"$COLUMNS is not exported, and TIOCGWINSZ is available on the platform, then its\n"
"idea of the terminal size is used. Otherwise, a terminal width of %d\n"
"characters is assumed.\n"
"\n"
"This is %s %s.\n"
"Copyright (c) %s %s <%s>.\n"
"\n"
"Distributed under the GNU General Public License.\n"
"See %s for details on the GNU GPL.\n",
program, DEFAULT_PRINTWIDTH,
PACKAGE, VERSION, COPYRIGHT_DATE,
AUTHOR, AUTHOR_EMAIL, GNU_URL);
}
/**************
Main routine
**************/
/** Program main routine.
*
* Command-line arguments are handled simplistically: if there are no
* arguments, then a usage statement is printed and an error code is returned.
* If the first argument is "-h" or "--help", then a usage statement is
* displayed. If the first argument is "--", then it is ignored, which
* provides a backwards-compatible way to print a banner containing "--help"
* or "-h". Finally, if we haven't errored out or printed the usage
* statement, anything else left on the command-line is considered a word to
* be printed as a banner.
*/
int main(int argc, char *argv[])
{
/*****************
Local variables
*****************/
int i = 0;
int j = 0;
int wordcount = 0;
char **wordlist = NULL;
char *columns = NULL;
int printwidth = 0;
int truncated = 0;
char *banner[LETTER_HEIGHT];
char *string = NULL;
/******************
Handle arguments
******************/
if(argc < 2)
{
usage(argv[0]);
exit(ERROR_EXIT);
}
if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
{
usage(argv[0]);
exit(NORMAL_EXIT); /* not an error */
}
if(strcmp(argv[1], "--") == 0)
{
wordcount = argc > 2 ? argc - 2 : 0;
wordlist = argc > 2 ? argv + 2 : NULL;
}
else
{
wordcount = argc > 1 ? argc - 1 : 0;
wordlist = argc > 1 ? argv + 1 : NULL;
}
if(wordcount < 1)
{
usage(argv[0]);
exit(ERROR_EXIT);
}
/***********************
Determine print width
***********************/
columns = getenv("COLUMNS");
if(columns == NULL)
{
#ifdef TIOCGWINSZ
struct winsize ws;
ws.ws_col = 0;
if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col > 0)
printwidth = ws.ws_col;
else
#endif
printwidth = DEFAULT_PRINTWIDTH;
}
else
{
printwidth = atoi(columns);
}
/******************************
Print a banner for each word
******************************/
for(i=0; i<wordcount; i++)
{
string = wordlist[i];
convert_to_upper(string);
convert_whitespace(string);
init_banner(banner);
fill_banner(banner, string, printwidth);
print_banner(banner);
free_banner(banner);
}
/*****************
Return normally
*****************/
return(0);
}