annotate enc-text/main.c @ 2:a16b1b9728f6

enc-text: sms-encode-text program written
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 02:07:22 +0000
parents
children e56bb9f09ff1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This is the main module for sms-encode-text utility.
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <unistd.h>
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "defs.h"
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 int utf8_input, ucs2_mode;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 int concat_enable, concat_refno_set;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 char msgtext[MAX_MSG_CHARS*2+2];
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 u_char concat_refno;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 process_cmdline(argc, argv)
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char **argv;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 int c;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 extern int optind;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 extern char *optarg;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 while ((c = getopt(argc, argv, "cC:uU")) != EOF) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 switch (c) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 case 'c':
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 concat_enable = 1;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 continue;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 case 'C':
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 concat_enable = 1;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 concat_refno = strtoul(optarg, 0, 0);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 concat_refno_set = 1;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 continue;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 case 'u':
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 utf8_input = 1;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 continue;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 case 'U':
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 ucs2_mode = 1;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 continue;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 default:
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 /* error msg already printed */
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 exit(1);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (argc > optind + 1) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 fprintf(stderr, "usage: %s [options] [message]\n", argv[0]);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 exit(1);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (argc < optind + 1)
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 return(0);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (strlen(argv[optind]) > MAX_MSG_CHARS*2) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 fprintf(stderr, "error: message argument is too long\n");
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 exit(1);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 strcpy(msgtext, argv[optind]);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 return(1);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 read_msgtext_from_stdin()
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 unsigned pos, remain;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 int cc;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 pos = 0;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 remain = sizeof(msgtext);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 for (;;) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (!remain) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(stderr,
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 "error: message on stdin is too long\n");
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 exit(1);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 cc = read(0, msgtext + pos, remain);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (cc < 0) {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 fprintf(stderr, "error reading message from stdin\n");
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 exit(1);
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (cc == 0)
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 break;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 pos += cc;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 remain -= cc;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 msgtext[pos] = '\0';
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 trim_trailing_newlines()
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 char *cp;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 cp = index(msgtext, '\0');
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 while (cp > msgtext && cp[-1] == '\n')
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 cp--;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 *cp = '\0';
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 main(argc, argv)
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 char **argv;
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 {
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 if (!process_cmdline(argc, argv))
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 read_msgtext_from_stdin();
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 trim_trailing_newlines();
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (ucs2_mode)
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 ucs2_mode_main();
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 else
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 gsm7_mode_main();
a16b1b9728f6 enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }