]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/banner/banner.c
Change license for banner to GPLv3+
[cygwin-apps/cygutils.git] / src / banner / banner.c
1 /*
2 * banner - make posters
3 * Copyright (C) 1999-2001 Joerg Schaible
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See the COPYING file for full license information.
19 */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "common.h"
26
27 static const char versionID[] = PACKAGE_VERSION;
28 static const char revID[] =
29 "$Id$";
30 static const char copyrightID[] =
31 "Copyright (c) 1999-2001\nJoerg Schaible. All rights reserved.\nLicensed under GPLv3+\n";
32
33 static void printTopDescription (FILE * f, char *name);
34 static void printBottomDescription (FILE * f, char *name);
35 static const char *getVersion (void);
36 static void usage (FILE * f, char *name);
37 static void help (FILE * f, char *name);
38 static void version (FILE * f, char *name);
39 static void license (FILE * f, char *name);
40
41 static char *program_name;
42 static poptContext optCon;
43
44 static char *c = "X";
45 static int w = 80;
46 static TEXTMETRIC tm;
47 static HFONT hFont;
48 static HBITMAP hBmp;
49 static HBITMAP hBmpOrg;
50 static HDC hMem;
51 static RECT rt;
52 static BITMAP bm;
53 static BITMAPINFO bmi;
54 static unsigned char *buffer;
55
56 void
57 initialize ()
58 {
59 HDC hScreen;
60 hFont = GetStockObject (ANSI_FIXED_FONT);
61 hScreen = GetDC (0);
62 hMem = CreateCompatibleDC (hScreen);
63 ReleaseDC (0, hScreen);
64
65 SelectObject (hMem, hFont);
66 GetTextMetrics (hMem, &tm);
67
68 hBmp = CreateBitmap (w, tm.tmHeight, 1, 1, 0);
69 GetObject (hBmp, sizeof (bm), &bm);
70 memset (&bmi.bmiHeader, 0, sizeof (BITMAPINFOHEADER));
71 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
72 bmi.bmiHeader.biWidth = bm.bmWidth;
73 bmi.bmiHeader.biHeight = bm.bmHeight;
74 bmi.bmiHeader.biPlanes = 1;
75 bmi.bmiHeader.biCompression = BI_RGB;
76 bmi.bmiHeader.biBitCount = 1;
77 bmi.bmiHeader.biSizeImage =
78 ((((unsigned int) bm.bmWidth + 31) & ~31) >> 3) * bm.bmHeight;
79 bmi.bmiHeader.biClrUsed = 2;
80 buffer = (unsigned char *) malloc (bmi.bmiHeader.biSizeImage);
81
82 hBmpOrg = SelectObject (hMem, hBmp);
83 SelectObject (hMem, GetStockObject (WHITE_BRUSH));
84 SelectObject (hMem, GetStockObject (WHITE_PEN));
85
86 SetBkColor (hMem, RGB (0, 0, 0));
87 SetTextColor (hMem, RGB (255, 255, 255));
88 SetBkMode (hMem, OPAQUE);
89
90 rt.top = 0;
91 rt.left = 0;
92 rt.right = w;
93 rt.bottom = tm.tmHeight;
94 }
95
96 void
97 deinitialize ()
98 {
99 SelectObject (hMem, hBmpOrg);
100 DeleteObject (hBmp);
101 DeleteDC (hMem);
102 free (buffer);
103 }
104
105 int
106 print_line (const char *line)
107 {
108 int x, y;
109 int len = strlen (line);
110 if (len > (w / tm.tmMaxCharWidth))
111 len = (w / tm.tmMaxCharWidth);
112
113 Rectangle (hMem, 0, 0, rt.right, rt.bottom);
114 DrawText (hMem, line, len, &rt, DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE);
115
116 SelectObject (hMem, hBmpOrg);
117 GetDIBits (hMem, hBmp, 0, bmi.bmiHeader.biHeight, buffer,
118 (LPBITMAPINFO) & bmi.bmiHeader, DIB_RGB_COLORS);
119 SelectObject (hMem, hBmp);
120
121 for (y = tm.tmHeight; y--;)
122 {
123 unsigned char *line =
124 buffer + y * (bmi.bmiHeader.biSizeImage / bmi.bmiHeader.biHeight);
125
126 for (x = 0; x < len * tm.tmMaxCharWidth; ++x)
127 {
128 unsigned char byte = *(line + (x / 8));
129 putchar ((byte & (1 << (7 - ((int) x % 8)))) ? *c : ' ');
130 }
131
132 putchar ('\n');
133 }
134 return 0;
135 }
136
137 int
138 main (int argc, const char **argv)
139 {
140 const char **rest;
141 int rc;
142 int ec = 0;
143 int i;
144
145 struct poptOption generalOptionsTable[] = {
146 {"char", 'c', POPT_ARG_STRING, &c, 'c',
147 "use character <X>", "X"},
148 {"width", 'w', POPT_ARG_INT, &w, 'w',
149 "set display width to <80> ", "80"},
150 {NULL, '\0', 0, NULL, 0, NULL, NULL}
151 };
152
153 struct poptOption helpOptionsTable[] = {
154 {"help", '?', POPT_ARG_NONE, NULL, '?',
155 "Show this help message", NULL},
156 {"usage", '\0', POPT_ARG_NONE, NULL, 'u',
157 "Display brief usage message", NULL},
158 {"version", '\0', POPT_ARG_NONE, NULL, 'v',
159 "Display version information", NULL},
160 {"license", '\0', POPT_ARG_NONE, NULL, 'l',
161 "Display licensing information", NULL},
162 {NULL, '\0', 0, NULL, 0, NULL, NULL}
163 };
164
165 struct poptOption opt[] = {
166 {NULL, '\0', POPT_ARG_INCLUDE_TABLE, generalOptionsTable, 0,
167 "General options", NULL},
168 {NULL, '\0', POPT_ARG_INCLUDE_TABLE, helpOptionsTable, 0,
169 "Help options", NULL},
170 {NULL, '\0', 0, NULL, 0, NULL, NULL}
171 };
172
173 if ((program_name = strdup (argv[0])) == NULL)
174 {
175 fprintf (stderr, "%s: memory allocation error\n", argv[0]);
176 exit (1);
177 }
178 optCon = poptGetContext (NULL, argc, argv, opt, 0);
179 poptSetOtherOptionHelp (optCon, "A string to print...");
180
181 while ((rc = poptGetNextOpt (optCon)) > 0)
182 {
183 switch (rc)
184 {
185 case '?':
186 help (stdout, program_name);
187 goto exit;
188 case 'u':
189 usage (stdout, program_name);
190 goto exit;
191 case 'v':
192 version (stdout, program_name);
193 goto exit;
194 case 'l':
195 license (stdout, program_name);
196 goto exit;
197 case 'c': /* no additional action needed */
198 break;
199 case 'w': /* no additional action needed */
200 break;
201 }
202 }
203 if (rc < -1)
204 {
205 fprintf (stderr, "%s: bad argument %s: %s\n",
206 program_name, poptBadOption (optCon, POPT_BADOPTION_NOALIAS),
207 poptStrerror (rc));
208 ec = 2;
209 goto exit;
210 }
211 rest = poptGetArgs (optCon);
212
213 if (rest == NULL)
214 {
215 fprintf (stderr, "%s: not enough arguments\n", program_name);
216 usage (stderr, program_name);
217 }
218 else
219 {
220 initialize ();
221 while (*rest && !ec)
222 {
223 ec |= print_line (*rest);
224 rest++;
225 }
226 deinitialize ();
227 }
228
229 exit:
230 poptFreeContext (optCon);
231 free (program_name);
232 return (ec);
233 }
234
235 static const char *
236 getVersion ()
237 {
238 return versionID;
239 }
240
241 static void
242 printTopDescription (FILE * f, char *name)
243 {
244 fprintf (f, "%s is part of cygutils version %s\n", name, getVersion ());
245 fprintf (f, " Prints a string enlarged as a banner on the screen.\n\n");
246 }
247
248 static void
249 printBottomDescription (FILE * f, char *name)
250 {
251 fprintf (f,
252 "\nThis version works the same way as System V'S banner does:\n");
253 fprintf (f, "The banner text is displayed horizontally.\n");
254 }
255
256 static
257 printLicense (FILE * f, char *name)
258 {
259 fprintf (f,
260 "This program is free software: you can redistribute it and/or modify\n"
261 "it under the terms of the GNU General Public License as published by\n"
262 "the Free Software Foundation, either version 3 of the License, or\n"
263 "(at your option) any later version.\n\n"
264 "This program is distributed in the hope that it will be useful,\n"
265 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
266 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
267 "GNU General Public License for more details.\n\n"
268 "You should have received a copy of the GNU General Public License\n"
269 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n"
270 "See the COPYING file for full license information.\n");
271 }
272
273 static void
274 usage (FILE * f, char *name)
275 {
276 poptPrintUsage (optCon, f, 0);
277 }
278
279 static void
280 help (FILE * f, char *name)
281 {
282 printTopDescription (f, name);
283 poptPrintHelp (optCon, f, 0);
284 printBottomDescription (f, name);
285 }
286
287 static void
288 version (FILE * f, char *name)
289 {
290 printTopDescription (f, name);
291 }
292
293 static void
294 license (FILE * f, char *name)
295 {
296 printTopDescription (f, name);
297 printLicense (f, name);
298 }
This page took 0.047361 seconds and 5 git commands to generate.