Annotation of freem/src/cmd_write.c, revision 1.3
1.1 snw 1: /*
1.3 ! snw 2: * $Id$
1.1 snw 3: * Implementation of the WRITE command
4: *
5: *
1.2 snw 6: * Author: Serena Willis <snw@coherent-logic.com>
1.1 snw 7: * Copyright (C) 1998 MUG Deutschland
8: * Copyright (C) 2023 Coherent Logic Development LLC
9: *
10: *
11: * This file is part of FreeM.
12: *
13: * FreeM is free software: you can redistribute it and/or modify
14: * it under the terms of the GNU Affero Public License as published by
15: * the Free Software Foundation, either version 3 of the License, or
16: * (at your option) any later version.
17: *
18: * FreeM is distributed in the hope that it will be useful,
19: * but WITHOUT ANY WARRANTY; without even the implied warranty of
20: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: * GNU Affero Public License for more details.
22: *
23: * You should have received a copy of the GNU Affero Public License
24: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
25: *
1.3 ! snw 26: * $Log$
! 27: *
! 28: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
! 29: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 30: **/
31:
32: #include <string.h>
33: #include <stdlib.h>
34: #include "mpsdef.h"
35: #include "mcommand.h"
36: #if !defined(MSDOS)
37: # include "io_socket.h"
38: #endif
39:
40: MRESULT cmd_write(MACTION *ra, int *i)
41: {
42: char vn[255];
43: char *wr_terminator = (char *) malloc (STRLEN * sizeof (char));
44: char *sw_buf = (char *) malloc (STRLEN * sizeof (char));
45:
46: NULLPTRCHK(wr_terminator,"cmd_write");
47: NULLPTRCHK(sw_buf,"cmd_write");
48:
49: if (io > FIRSTSCK) msck_get_terminator (io, wr_terminator);
50:
51: if (io != HOME && devopen[io] == 'r' && io < FIRSTSCK) {
52: free (sw_buf);
53: return NOWRITE;
54: }
55:
56: if ((*i = (*codptr)) == SP || *i == EOL) {
57: return ARGLIST;
58: }
59:
60: writeproc:
61:
62: switch (*i)
63: {
64:
65: case '!':
66:
67: if (io < FIRSTSCK) {
68:
69: if (crlf[io]) {
70: write_m ("\012\201");
71: }
72: else {
73: write_m ("\015\012\201");
74: }
75:
76: }
77: else {
78: /* WRITE ! for sockets */
79: strcat (sw_buf, wr_terminator);
80: }
81:
82: break;
83:
84: case '#':
85:
86: if (io < FIRSTSCK) {
87: write_m ("\015\014\201");
88: }
89: else {
90: return SYNTERR;
91: }
92:
93: break;
94:
95: case '?':
96:
97: codptr++;
98:
99: expr (STRING);
100:
101: if (merr ()) return merr ();
102:
103: if (io < FIRSTSCK) {
104: write_t ((short) intexpr (argptr));
105: }
106: else {
107: return SYNTERR;
108: }
109:
110: break;
111:
112: case '/':
113: codptr++;
114:
115: expr (NAME);
116: if (merr ()) return merr ();
117:
118: if (io < FIRSTSCK) {
119: write_f (varnam);
120: }
121: else {
122: return SYNTERR;
123: }
124:
125: codptr++;
126:
127: break;
128:
129: case '*':
130: codptr++;
131:
132: expr (STRING);
133: if (merr ()) return merr ();
134:
135: argptr[0] = (char) UNSIGN (intexpr (argptr));
136: argptr[1] = EOL;
137:
138: /* special treatment for EOL as long as we don't have 8 bit */
139: if (argptr[0] == EOL) {
140:
141: mcmnd = '*';
142:
143: if (io < FIRSTSCK) {
144: m_output (argptr);
145: }
146: else {
147: /* EOL handling for socket devices */
148: }
149: mcmnd = WRITE;
150:
151: }
152: else {
153:
154: if (io < FIRSTSCK) {
155: write_m (argptr);
156: }
157: else {
158: /* non-EOL char-code handling for sockets */
159: }
160:
161: }
162:
163: break;
164:
165: default:
166: expr (STRING);
167: if (merr ()) return merr ();
168:
169: if (io < FIRSTSCK) {
170: write_m (argptr);
171: }
172: else {
173: /* regular output for socket devices */
174: stcpy (vn, argptr);
175: stcnv_m2c (vn);
176: strcat (sw_buf, vn);
177: }
178: }
179:
180: if ((*i == '!') || (*i == '#')) {
181: if ((*i = *++codptr) == '!' || *i == '#' || *i == '?') goto writeproc;
182: }
183:
184: if (io >= FIRSTSCK) {
185: msck_write (io, sw_buf, strlen (sw_buf));
186: }
187:
188: free (sw_buf);
189: free (wr_terminator);
190:
191: *ra = RA_CONTINUE;
192: return OK;
193: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>