1########################################################################
2##
3## Copyright (C) 2004-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} computer ()
28## @deftypefnx {} {@var{c} =} computer ()
29## @deftypefnx {} {[@var{c}, @var{maxsize}] =} computer ()
30## @deftypefnx {} {[@var{c}, @var{maxsize}, @var{endian}] =} computer ()
31## @deftypefnx {} {@var{arch} =} computer ("arch")
32## Print or return a string of the form @var{cpu}-@var{vendor}-@var{os} that
33## identifies the type of computer that Octave is running on.
34##
35## If invoked with an output argument, the value is returned instead of
36## printed.  For example:
37##
38## @example
39## @group
40## computer ()
41##    @print{} x86_64-pc-linux-gnu
42##
43## mycomp = computer ()
44##    @result{} mycomp = x86_64-pc-linux-gnu
45## @end group
46## @end example
47##
48## If two output arguments are requested, also return the maximum number of
49## elements for an array.  This will depend on whether Octave has been
50## compiled with 32-bit or 64-bit index vectors.
51##
52## If three output arguments are requested, also return the byte order of the
53## current system as a character (@qcode{"B"} for big-endian or @qcode{"L"}
54## for little-endian).
55##
56## If the argument @qcode{"arch"} is specified, return a string indicating the
57## architecture of the computer on which Octave is running.
58## @seealso{isunix, ismac, ispc}
59## @end deftypefn
60
61function [c, maxsize, endian] = computer (a)
62
63  if (nargin > 1)
64    print_usage ();
65  elseif (nargin == 1 && ! strcmpi (a, "arch"))
66    error ('computer: "arch" is only valid argument');
67  endif
68
69  if (nargin == 0)
70    msg = __octave_config_info__ ("canonical_host_type");
71
72    if (strcmp (msg, "unknown"))
73      msg = "Hi Dave, I'm a HAL-9000";
74    endif
75
76    if (nargout == 0)
77      disp (msg);
78    else
79      c = msg;
80      if (isargout (2))
81        if (__octave_config_info__ ("ENABLE_64"))
82          maxsize = 2^63-1;
83        else
84          maxsize = 2^31-1;
85        endif
86      endif
87      if (isargout (3))
88        if (__octave_config_info__ ("words_big_endian"))
89          endian = "B";
90        elseif (__octave_config_info__ ("words_little_endian"))
91          endian = "L";
92        else
93          endian = "?";
94        endif
95      endif
96    endif
97  else
98    ## "arch" argument asked for
99    tmp = ostrsplit (__octave_config_info__ ("canonical_host_type"), "-");
100    if (numel (tmp) == 4)
101      c = sprintf ("%s-%s-%s", tmp{4}, tmp{3}, tmp{1});
102    else
103      c = sprintf ("%s-%s", tmp{3}, tmp{1});
104    endif
105
106  endif
107
108endfunction
109
110
111%!assert (ischar (computer ()))
112%!assert (computer (), __octave_config_info__ ("canonical_host_type"))
113%!assert (ischar (computer ("arch")))
114
115%!error computer (1,2)
116%!error <"arch" is only valid argument> computer ("xyz")
117