Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/builtin/mime.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1992-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* *
20
***********************************************************************/
21
#pragma prototyped
22
23
/*
24
* Glenn Fowler
25
* AT&T Research
26
*
27
* mime capability interface
28
*/
29
30
static const char usage[] =
31
"[-?\n@(#)$Id: mime (AT&T Research) 1999-08-11 $\n]"
32
USAGE_LICENSE
33
"[+NAME?mime - list mime capabilities]"
34
"[+DESCRIPTION?\bmime\b matches and/or lists mime capability entries. With"
35
" no operands all mime entries are listed. With just a \atype\a operand"
36
" the mime entry for \atype\a is listed. Otherwise the expanded"
37
" \bsh\b(1) command for the \aview\a operation on the object \aname\a"
38
" with mime type \atype\a and optional \aattribute\a(s) is written to"
39
" the standard output. \aview\a may be \b-\b for the most common"
40
" action.]"
41
42
"[s:silent|quiet?Exit 0 if match found, 1 otherwise, with no diagnostics.]"
43
44
"\n\n"
45
"type\n"
46
"view name type [ attribute ... ]\n"
47
"\n"
48
49
"[+SEE ALSO?\bfile\b(1), \bmailx\b(1), \bmagic\b(3), \bmime\b(3)]"
50
;
51
52
#include <cmd.h>
53
#include <mime.h>
54
55
int
56
b_mime(int argc, char** argv, Shbltin_t* context)
57
{
58
Mime_t* mp;
59
char* s;
60
Sfio_t* sp;
61
int silent;
62
Mimedisc_t disc;
63
64
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
65
silent = 0;
66
for (;;)
67
{
68
switch (optget(argv, usage))
69
{
70
case 0:
71
break;
72
case 's':
73
silent = 1;
74
continue;
75
case ':':
76
error(2, "%s", opt_info.arg);
77
continue;
78
case '?':
79
error(ERROR_usage(2), "%s", opt_info.arg);
80
continue;
81
}
82
break;
83
}
84
argc -= opt_info.index;
85
argv += opt_info.index;
86
if (error_info.errors || argc > 1 && argc < 3)
87
error(ERROR_usage(2), "%s", optusage(NiL));
88
disc.version = MIME_VERSION;
89
disc.flags = 0;
90
disc.errorf = errorf;
91
if (!(mp = mimeopen(&disc)))
92
error(ERROR_exit(1), "mime library error");
93
if (mimeload(mp, NiL, 0))
94
{
95
mimeclose(mp);
96
error(ERROR_exit(1), "mime load error");
97
}
98
if (argc <= 1)
99
mimelist(mp, sfstdout, argv[0]);
100
else if (!(sp = sfstropen()))
101
error(ERROR_exit(1), "out of space");
102
else
103
{
104
for (argc = 3; s = argv[argc]; argc++)
105
sfputr(sp, s, ';');
106
if (!(s = sfstruse(sp)))
107
error(ERROR_exit(1), "out of space");
108
if (!(s = mimeview(mp, argv[0], argv[1], argv[2], s)))
109
{
110
if (silent)
111
exit(1);
112
error(ERROR_exit(1), "no %s view for mime type %s", argv[0], argv[2]);
113
}
114
else
115
sfputr(sfstdout, s, '\n');
116
sfclose(sp);
117
}
118
mimeclose(mp);
119
return 0;
120
}
121
122